PageRenderTime 23ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/src/FluentNHibernate.Testing/ConventionsTests/OverridingFluentInterface/HasManyConventionTests.cs

http://github.com/jagregory/fluent-nhibernate
C# | 293 lines | 213 code | 80 blank | 0 comment | 2 complexity | 265ec8829506ac0a741faebbbacd17ef MD5 | raw file
Possible License(s): BSD-3-Clause, CC-BY-SA-3.0, Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using FluentNHibernate.Automapping.TestFixtures;
  6. using FluentNHibernate.Conventions.Helpers.Builders;
  7. using FluentNHibernate.Conventions.Instances;
  8. using FluentNHibernate.Mapping;
  9. using FluentNHibernate.MappingModel;
  10. using FluentNHibernate.MappingModel.Collections;
  11. using FluentNHibernate.Testing.FluentInterfaceTests;
  12. using NUnit.Framework;
  13. namespace FluentNHibernate.Testing.ConventionsTests.OverridingFluentInterface
  14. {
  15. [TestFixture]
  16. public class HasManyConventionTests
  17. {
  18. private PersistenceModel model;
  19. private IMappingProvider mapping;
  20. private Type mappingType;
  21. [SetUp]
  22. public void CreatePersistenceModel()
  23. {
  24. model = new PersistenceModel();
  25. }
  26. [Test]
  27. public void AccessShouldntBeOverwritten()
  28. {
  29. Mapping(x => x.Children, x => x.Access.Field());
  30. Convention(x => x.Access.Property());
  31. VerifyModel(x => SpecificationExtensions.ShouldEqual(x.Access, "field"));
  32. }
  33. [Test]
  34. public void BatchSizeShouldntBeOverwritten()
  35. {
  36. Mapping(x => x.Children, x => x.BatchSize(10));
  37. Convention(x => x.BatchSize(100));
  38. VerifyModel(x => x.BatchSize.ShouldEqual(10));
  39. }
  40. [Test]
  41. public void CacheShouldntBeOverwritten()
  42. {
  43. Mapping(x => x.Children, x => x.Cache.ReadOnly());
  44. Convention(x => x.Cache.ReadWrite());
  45. VerifyModel(x => x.Cache.Usage.ShouldEqual("read-only"));
  46. }
  47. [Test]
  48. public void CascadeShouldntBeOverwritten()
  49. {
  50. Mapping(x => x.Children, x => x.Cascade.All());
  51. Convention(x => x.Cascade.None());
  52. VerifyModel(x => x.Cascade.ShouldEqual("all"));
  53. }
  54. [Test]
  55. public void CheckConstraintShouldntBeOverwritten()
  56. {
  57. Mapping(x => x.Children, x => x.Check("constraint = 1"));
  58. Convention(x => x.Check("constraint = 0"));
  59. VerifyModel(x => x.Check.ShouldEqual("constraint = 1"));
  60. }
  61. [Test]
  62. public void CollectionTypeShouldntBeOverwritten()
  63. {
  64. Mapping(x => x.Children, x => x.CollectionType<int>());
  65. Convention(x => x.CollectionType<string>());
  66. VerifyModel(x => x.CollectionType.GetUnderlyingSystemType().ShouldEqual(typeof(int)));
  67. }
  68. [Test]
  69. public void FetchShouldntBeOverwritten()
  70. {
  71. Mapping(x => x.Children, x => x.Fetch.Join());
  72. Convention(x => x.Fetch.Select());
  73. VerifyModel(x => x.Fetch.ShouldEqual("join"));
  74. }
  75. [Test]
  76. public void GenericShouldntBeOverwritten()
  77. {
  78. Mapping(x => x.Children, x => x.Generic());
  79. Convention(x => x.Not.Generic());
  80. VerifyModel(x => x.Generic.ShouldBeTrue());
  81. }
  82. [Test]
  83. public void InverseShouldntBeOverwritten()
  84. {
  85. Mapping(x => x.Children, x => x.Inverse());
  86. Convention(x => x.Not.Inverse());
  87. VerifyModel(x => x.Inverse.ShouldBeTrue());
  88. }
  89. [Test]
  90. public void KeyColumnNameShouldntBeOverwritten()
  91. {
  92. Mapping(x => x.Children, x => x.KeyColumn("name"));
  93. Convention(x => x.Key.Column("xxx"));
  94. VerifyModel(x => x.Key.Columns.First().Name.ShouldEqual("name"));
  95. }
  96. [Test]
  97. public void ElementColumnNameShouldntBeOverwritten()
  98. {
  99. Mapping(x => x.Children, x => x.Element("name"));
  100. Convention(x => x.Element.Column("xxx"));
  101. VerifyModel(x => x.Element.Columns.First().Name.ShouldEqual("name"));
  102. }
  103. [Test]
  104. public void ElementTypeShouldntBeOverwrittenUsingGeneric()
  105. {
  106. Mapping(x => x.Children, x => x.Element("xxx", e => e.Type<string>()));
  107. Convention(x => x.Element.Type<int>());
  108. VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
  109. }
  110. [Test]
  111. public void ElementTypeShouldntBeOverwrittenUsingTypeOf()
  112. {
  113. Mapping(x => x.Children, x => x.Element("xxx", e => e.Type<string>()));
  114. Convention(x => x.Element.Type(typeof(int)));
  115. VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
  116. }
  117. [Test]
  118. public void ElementTypeShouldntBeOverwrittenUsingString()
  119. {
  120. Mapping(x => x.Children, x => x.Element("xxx", e => e.Type<string>()));
  121. Convention(x => x.Element.Type(typeof(int).AssemblyQualifiedName));
  122. VerifyModel(x => x.Element.Type.GetUnderlyingSystemType().ShouldEqual(typeof(string)));
  123. }
  124. [Test]
  125. public void LazyShouldntBeOverwritten()
  126. {
  127. Mapping(x => x.Children, x => x.LazyLoad());
  128. Convention(x => x.Not.LazyLoad());
  129. VerifyModel(x => x.Lazy.ShouldEqual(Lazy.True));
  130. }
  131. [Test]
  132. public void OptimisticLockShouldntBeOverwritten()
  133. {
  134. Mapping(x => x.Children, x => x.OptimisticLock());
  135. Convention(x => x.Not.OptimisticLock());
  136. VerifyModel(x => x.OptimisticLock.ShouldEqual(true));
  137. }
  138. [Test]
  139. public void PersisterShouldntBeOverwritten()
  140. {
  141. Mapping(x => x.Children, x => x.Persister<CustomPersister>());
  142. Convention(x => x.Persister<SecondCustomPersister>());
  143. VerifyModel(x => x.Persister.GetUnderlyingSystemType().ShouldEqual(typeof(CustomPersister)));
  144. }
  145. [Test]
  146. public void SchemaShouldntBeOverwritten()
  147. {
  148. Mapping(x => x.Children, x => x.Schema("dbo"));
  149. Convention(x => x.Schema("test"));
  150. VerifyModel(x => x.Schema.ShouldEqual("dbo"));
  151. }
  152. [Test]
  153. public void WhereShouldntBeOverwritten()
  154. {
  155. Mapping(x => x.Children, x => x.Where("x = 1"));
  156. Convention(x => x.Where("y = 2"));
  157. VerifyModel(x => x.Where.ShouldEqual("x = 1"));
  158. }
  159. [Test]
  160. public void ForeignKeyShouldntBeOverwritten()
  161. {
  162. Mapping(x => x.Children, x => x.ForeignKeyConstraintName("key"));
  163. Convention(x => x.Key.ForeignKey("xxx"));
  164. VerifyModel(x => x.Key.ForeignKey.ShouldEqual("key"));
  165. }
  166. [Test]
  167. public void PropertyRefShouldntBeOverwritten()
  168. {
  169. Mapping(x => x.Children, x => x.PropertyRef("ref"));
  170. Convention(x => x.Key.PropertyRef("xxx"));
  171. VerifyModel(x => x.Key.PropertyRef.ShouldEqual("ref"));
  172. }
  173. [Test]
  174. public void TableNameShouldntBeOverwritten()
  175. {
  176. Mapping(x => x.Children, x => x.Table("table"));
  177. Convention(x => x.Table("xxx"));
  178. VerifyModel(x => x.TableName.ShouldEqual("table"));
  179. }
  180. [Test]
  181. public void NotFoundShouldntBeOverwritten()
  182. {
  183. Mapping(x => x.Children, x => x.NotFound.Exception());
  184. Convention(x => x.Relationship.NotFound.Ignore());
  185. VerifyModel(x => x.Relationship.NotFound.ShouldEqual("exception"));
  186. }
  187. #region Helpers
  188. private void Convention(Action<IOneToManyCollectionInstance> convention)
  189. {
  190. model.Conventions.Add(new OneToManyCollectionConventionBuilder().Always(convention));
  191. }
  192. private void Mapping<TChild>(Expression<Func<ExampleInheritedClass, IEnumerable<TChild>>> property, Action<OneToManyPart<TChild>> mappingDefinition)
  193. {
  194. var classMap = new ClassMap<ExampleInheritedClass>();
  195. classMap.Id(x => x.Id);
  196. var map = classMap.HasMany(property);
  197. mappingDefinition(map);
  198. mapping = classMap;
  199. mappingType = typeof(ExampleInheritedClass);
  200. }
  201. private void VerifyModel(Action<CollectionMapping> modelVerification)
  202. {
  203. model.Add(mapping);
  204. var generatedModels = model.BuildMappings();
  205. var modelInstance = generatedModels
  206. .First(x => x.Classes.FirstOrDefault(c => c.Type == mappingType) != null)
  207. .Classes.First()
  208. .Collections.First();
  209. modelVerification(modelInstance);
  210. }
  211. #endregion
  212. }
  213. }