public void ShouldHaveColumnProperties() { var entities = EntityUtilities.GetEntities(); foreach (var entity in entities) { foreach (var property in entity.GetProperties().Where(EntityUtilities.IsColumnProperty)) { var columnAttribute = property.GetAttribute <ColumnAttribute>(); Assert.NotNull( columnAttribute, $"Property '{entity.Name}.{property.Name}' should be decorated with ColumnAttribute"); Assert.IsTrue( Notations.InPascal(property.Name), $"Property '{entity.Name}.{property.Name}' should be in pascal convention"); //var expectedPropertyName = columnAttribute.Name.ToPascal(); //Assert.AreEqual( // property.Name, // expectedPropertyName, // $"Property {entity.Name}.{property.Name} should have '{expectedPropertyName}' name"); Assert.IsTrue(property.HasPublicGetter(), $"{entity.Name}.{property.Name} getter should be public"); Assert.IsTrue(property.HasPublicSetter(), $"{entity.Name}.{property.Name} setter should be public"); } } }
public void ShouldHaveEntities() { var entities = EntityUtilities.GetEntities(); foreach (var entity in entities) { var tableAttribute = entity.GetAttribute <TableAttribute>(); Assert.NotNull(tableAttribute, $"Type '{entity.Name}' should have TableAttribute"); Assert.IsTrue(Notations.InPascal(entity.Name), $"Type '{entity.Name}' should be in pascal convention"); Assert.IsTrue(entity.IsPublic, $"Type {entity.Name} should be public"); Assert.IsFalse(entity.IsAbstract, $"Type {entity.Name} should not be abstract"); var tableName = tableAttribute.Name; var expectedEntityName = Notations.ToPascal(tableName); if (expectedEntityName == "ExtraTabs") { expectedEntityName = "ExtraTab"; } if (expectedEntityName == "ExtraTabsDescription") { expectedEntityName = "ExtraTabDescription"; } if (expectedEntityName == "ProductExtraTabs") { expectedEntityName = "ProductExtraTab"; } if (expectedEntityName == "ProductStickers") { expectedEntityName = "ProductSticker"; } if (expectedEntityName == "ProductStickersDescription") { expectedEntityName = "ProductStickerDescription"; } Assert.AreEqual( expectedEntityName, entity.Name, $"Entity mapped to table '{tableName}' should have name '{expectedEntityName}'"); } }
public void ShouldThrowException(string value) { Assert.Throws <ArgumentNullException>(() => Notations.InPascal(value)); }
public void ShouldDeterminePascalConvention(string value, bool inPascal) { Assert.AreEqual(Notations.InPascal(value), inPascal); }
public void ShouldHaveCollectionProperties() { var entities = EntityUtilities.GetEntities(); foreach (var entity in entities) { foreach (var property in entity.GetProperties().Where(EntityUtilities.IsForeignKeyProperty)) { var foreignKeyName = property.GetAttribute <ColumnAttribute>().Name; if (entity == typeof(CategoryPath) && foreignKeyName == "path_id") { continue; } if (entity == typeof(Category) && foreignKeyName == "parent_id") { foreignKeyName = "category_id"; } if (entity == typeof(Order) && property.Name.EndsWith("CountryId")) { foreignKeyName = "country_id"; } if (entity == typeof(Order) && property.Name.EndsWith("ZoneId")) { foreignKeyName = "zone_id"; } if (foreignKeyName == "extra_tab_id") { foreignKeyName = "extra_tabs_id"; } if (foreignKeyName == "product_sticker_id") { foreignKeyName = "product_stickers_id"; } if (entity == typeof(ProductImageByOption) && foreignKeyName == "product_value_id") { foreignKeyName = "option_value_id"; } var targetTableName = "oc_" + foreignKeyName.Replace("_id", string.Empty); var targetEntity = entities.FirstOrDefault(x => x.GetAttribute <TableAttribute>().Name == targetTableName); if (targetEntity == null) { throw new Exception($"There is no entity {targetTableName} related to {entity.Name}.{property.Name}"); } if (ShouldNotHaveCollectionProperty(entity, targetEntity)) { continue; } var collectionProperty = targetEntity.GetProperties().SingleOrDefault(x => EntityUtilities.IsCollectionProperty(x, entity)); if (collectionProperty == null) { throw new Exception($"Entity {targetEntity.Name} should have property ICollection<{entity.Name}>."); } Assert.IsTrue(Notations.InPascal(collectionProperty.Name), $"Property '{entity.Name}.{collectionProperty.Name}' should be in pascal convention"); var getter = collectionProperty.GetGetMethod(true); Assert.IsTrue(getter.IsPublic && getter.IsVirtual, $"{entity.Name}.{collectionProperty.Name} getter should be made public and virtual"); var setter = collectionProperty.GetSetMethod(true); Assert.IsTrue(setter.IsPublic && setter.IsVirtual, $"{entity.Name}.{collectionProperty.Name} setter should be made public and virtual"); var targetEntityInstance = Activator.CreateInstance(targetEntity); var collectionPropertyValue = collectionProperty.GetGetMethod().Invoke(targetEntityInstance, new object[0]); Assert.IsNotNull(collectionPropertyValue, $"Property '{targetEntity.Name}.{collectionProperty.Name}' should be initialized in constructor"); } } }