/Xpand/Xpand.Tests/Xpand.Tests/Xpand.WorldCreator/DbMapper/AttributeMapperSpecs.cs

https://bitbucket.org/expand/expand · C# · 216 lines · 165 code · 51 blank · 0 comment · 0 complexity · 390dc32773c8c818c379581d42c26e8f MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Machine.Specifications;
  5. using Microsoft.SqlServer.Management.Smo;
  6. using TypeMock.ArrangeActAssert;
  7. using Xpand.ExpressApp.WorldCreator.SqlDBMapper;
  8. using Xpand.Persistent.Base.PersistentMetaData;
  9. using Xpand.Persistent.Base.PersistentMetaData.PersistentAttributeInfos;
  10. using Xpand.Persistent.BaseImpl.PersistentMetaData.PersistentAttributeInfos;
  11. namespace Xpand.Tests.Xpand.WorldCreator.DbMapper {
  12. public class When_a_column_is_nullable : With_Column {
  13. static List<IPersistentAttributeInfo> _persistentAttributeInfos;
  14. Establish context = () => {
  15. _column.Nullable = false;
  16. };
  17. Because of = () => {
  18. _persistentAttributeInfos = new AttributeMapper(XPObjectSpace).Create(_column, Isolate.Fake.Instance<IPersistentMemberInfo>(), new DataTypeMapper());
  19. };
  20. It should_create_a_rule_required_attribute =
  21. () => _persistentAttributeInfos.OfType<PersistentRuleRequiredFieldAttribute>().FirstOrDefault().ShouldNotBeNull();
  22. }
  23. public class When_column_size_is_100 : With_Column {
  24. static IEnumerable<IPersistentAttributeInfo> _persistentAttributeInfos;
  25. Establish context = () => {
  26. _column.DataType.MaximumLength = 100;
  27. _column.DataType.SqlDataType = SqlDataType.Text;
  28. };
  29. Because of = () => {
  30. _persistentAttributeInfos = new AttributeMapper(XPObjectSpace).Create(_column, Isolate.Fake.Instance<IPersistentMemberInfo>(), new DataTypeMapper());
  31. };
  32. It should_create_a_size_attribute_lenght_100 =
  33. () => _persistentAttributeInfos.OfType<PersistentSizeAttribute>().Single().Size.ShouldEqual(100);
  34. }
  35. public class When_column_is_primary_key : With_Column {
  36. static IEnumerable<IPersistentAttributeInfo> _persistentAttributeInfos;
  37. Establish context = () => Isolate.WhenCalled(() => _column.InPrimaryKey).WillReturn(true);
  38. Because of = () => {
  39. _persistentAttributeInfos = new AttributeMapper(XPObjectSpace).Create(_column, Isolate.Fake.Instance<IPersistentMemberInfo>(), new DataTypeMapper());
  40. };
  41. It should_create_persistent_key_attribute =
  42. () => _persistentAttributeInfos.OfType<PersistentKeyAttribute>().FirstOrDefault().ShouldNotBeNull();
  43. }
  44. public class When_column_is_primary_key_and_autogenerated : With_Column {
  45. static IEnumerable<IPersistentAttributeInfo> _persistentAttributeInfos;
  46. Establish context = () => {
  47. Isolate.WhenCalled(() => _column.InPrimaryKey).WillReturn(true);
  48. Isolate.WhenCalled(() => _column.Identity).WillReturn(true);
  49. };
  50. Because of = () => {
  51. _persistentAttributeInfos = new AttributeMapper(XPObjectSpace).Create(_column, Isolate.Fake.Instance<IPersistentMemberInfo>(), new DataTypeMapper());
  52. };
  53. It should_create_an_autogenerated_persistentkey_attribute =
  54. () => _persistentAttributeInfos.OfType<PersistentKeyAttribute>().Single().AutoGenerated.ShouldEqual(true);
  55. }
  56. public class When_column_is_foreign : With_ForeignKey_Column {
  57. static PersistentAssociationAttribute _persistentAssociationAttribute;
  58. static IEnumerable<IPersistentAttributeInfo> _persistentAttributeInfos;
  59. Because of = () => {
  60. _persistentAttributeInfos = new AttributeMapper(XPObjectSpace).Create(_column, Isolate.Fake.Instance<IPersistentMemberInfo>(), new DataTypeMapper()).ToList();
  61. };
  62. It should_create_an_association_attribute = () => {
  63. _persistentAssociationAttribute = _persistentAttributeInfos.OfType<PersistentAssociationAttribute>().FirstOrDefault();
  64. _persistentAssociationAttribute.ShouldNotBeNull();
  65. };
  66. It should_have_as_association_the_name_of_the_foreignKey_that_this_column_belongs = () => _persistentAssociationAttribute.AssociationName.ShouldEqual(FK_Name);
  67. }
  68. public class When_column_is_primarykey_and_table_has_more_privae_keys : With_Column {
  69. static List<IPersistentAttributeInfo> _persistentAttributeInfos;
  70. Establish context = () => {
  71. Isolate.WhenCalled(() => _column.InPrimaryKey).WillReturn(true);
  72. _table.Columns.Add(_column);
  73. };
  74. Because of = () => {
  75. _persistentAttributeInfos = new AttributeMapper(XPObjectSpace).Create(_column, Isolate.Fake.Instance<IPersistentMemberInfo>(), new DataTypeMapper());
  76. };
  77. It should_return_a_keyAttribute =
  78. () => _persistentAttributeInfos.OfType<PersistentKeyAttribute>().FirstOrDefault().ShouldNotBeNull();
  79. It should_return_a_persistent_attribute =
  80. () => _persistentAttributeInfos.OfType<PersistentPersistentAttribute>();
  81. }
  82. public class When_column_is_foreign_and_primary_key_and_table_has_combound_keys : With_ForeignKey_Column {
  83. static List<IPersistentAttributeInfo> _persistentAttributeInfos;
  84. static IPersistentMemberInfo _persistentMemberInfo;
  85. Establish context = () => {
  86. Isolate.WhenCalled(() => _column.InPrimaryKey).WillReturn(true);
  87. _persistentMemberInfo = Isolate.Fake.Instance<IPersistentMemberInfo>();
  88. _persistentMemberInfo.Owner.CodeTemplateInfo.CodeTemplate.TemplateType = TemplateType.Struct;
  89. };
  90. Because of = () => {
  91. _persistentAttributeInfos = new AttributeMapper(XPObjectSpace).Create(_column, _persistentMemberInfo, new DataTypeMapper());
  92. };
  93. It should_create_an_assocation_attribute =
  94. () => _persistentAttributeInfos.OfType<IPersistentAssociationAttribute>().FirstOrDefault().ShouldNotBeNull();
  95. It should_not_create_a_key_attribute =
  96. () => _persistentAttributeInfos.OfType<IPersistentKeyAttribute>().FirstOrDefault().ShouldBeNull();
  97. }
  98. public class When_column_is_combound_foreign_key : With_ForeignKey_Column {
  99. static IPersistentPersistentAttribute _persistentPersistentAttribute;
  100. static List<IPersistentAttributeInfo> _persistentAttributeInfos;
  101. Establish context = () => {
  102. var foreignKeyColumn = Isolate.Fake.Instance<ForeignKeyColumn>();
  103. _foreignKey.Columns.Add(foreignKeyColumn);
  104. };
  105. Because of = () => {
  106. _persistentAttributeInfos = new AttributeMapper(XPObjectSpace).Create(_column, Isolate.Fake.Instance<IPersistentMemberInfo>(), new DataTypeMapper());
  107. };
  108. It should_create_a_persitent_attrbiute = () => {
  109. _persistentPersistentAttribute =
  110. _persistentAttributeInfos.OfType<IPersistentPersistentAttribute>().FirstOrDefault();
  111. _persistentPersistentAttribute.ShouldNotBeNull();
  112. };
  113. It should_map_it_to_an_empty_string = () => _persistentPersistentAttribute.MapTo.ShouldEqual(String.Empty);
  114. }
  115. public class When_owner_is_one_to_one : With_ForeignKey_Column {
  116. static List<IPersistentAttributeInfo> _persistentAttributeInfos;
  117. static IPersistentMemberInfo _persistentMemberInfo;
  118. Establish context = () => {
  119. _persistentMemberInfo = Isolate.Fake.Instance<IPersistentMemberInfo>();
  120. _persistentMemberInfo.CodeTemplateInfo.CodeTemplate.TemplateType = TemplateType.XPOneToOnePropertyMember;
  121. };
  122. Because of = () => {
  123. _persistentAttributeInfos = new AttributeMapper(XPObjectSpace).Create(_column, _persistentMemberInfo, new DataTypeMapper());
  124. };
  125. It should_not_create_any_attribute = () => _persistentAttributeInfos.Count.ShouldEqual(0);
  126. }
  127. public class When_column_is_mapped : With_Column {
  128. static IPersistentPersistentAttribute _persistentPersistentAttribute;
  129. static List<IPersistentAttributeInfo> _persistentAttributeInfos;
  130. Because of = () => {
  131. _persistentAttributeInfos = new AttributeMapper(XPObjectSpace).Create(_column, Isolate.Fake.Instance<IPersistentMemberInfo>(), new DataTypeMapper());
  132. };
  133. It should_create_a_persistent_attribute = () => {
  134. _persistentPersistentAttribute =
  135. _persistentAttributeInfos.OfType<IPersistentPersistentAttribute>().FirstOrDefault();
  136. _persistentPersistentAttribute.ShouldNotBeNull();
  137. };
  138. It should_map_it_to_column_name = () => _persistentPersistentAttribute.MapTo.ShouldEqual(_column.Name);
  139. }
  140. public class When_table_is_mapped : With_table {
  141. static IPersistentPersistentAttribute _persistentPersistentAttribute;
  142. static List<IPersistentAttributeInfo> _persistentAttributeInfos;
  143. Because of = () => {
  144. _persistentAttributeInfos = new AttributeMapper(XPObjectSpace).Create(_table, Isolate.Fake.Instance<IPersistentClassInfo>(), Isolate.Fake.Instance<IMapperInfo>());
  145. };
  146. It should_create_a_persistent_attribute = () => {
  147. _persistentPersistentAttribute =
  148. _persistentAttributeInfos.OfType<IPersistentPersistentAttribute>().FirstOrDefault();
  149. _persistentPersistentAttribute.ShouldNotBeNull();
  150. };
  151. It should_map_it_to_column_name = () => _persistentPersistentAttribute.MapTo.ShouldEqual(_table.Name);
  152. }
  153. public class When_table_owner_has_a_persiste_attribute : With_table {
  154. static IPersistentClassInfo _persistentClassInfo;
  155. static List<IPersistentAttributeInfo> _persistentAttributeInfos;
  156. Establish context = () => {
  157. _persistentClassInfo = Isolate.Fake.Instance<IPersistentClassInfo>();
  158. Isolate.WhenCalled(() => _persistentClassInfo.TypeAttributes).WillReturn(new List<IPersistentAttributeInfo> { Isolate.Fake.Instance<IPersistentPersistentAttribute>() });
  159. };
  160. Because of = () => {
  161. _persistentAttributeInfos = new AttributeMapper(XPObjectSpace).Create(_table, _persistentClassInfo, Isolate.Fake.Instance<IMapperInfo>());
  162. };
  163. It should_not_return_any_attribute = () => _persistentAttributeInfos.OfType<IPersistentPersistentAttribute>().Count().ShouldEqual(0);
  164. }
  165. }