/src/FluentNHibernate.Testing/AutoMapping/AutoMapOneToManyTester.cs

https://github.com/dotnetchris/fluent-nhibernate · C# · 113 lines · 95 code · 18 blank · 0 comment · 0 complexity · eb8d13a1b01f286205e1d29aef62487a MD5 · raw file

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using FluentNHibernate.Automapping;
  5. using FluentNHibernate.MappingModel.ClassBased;
  6. using FluentNHibernate.MappingModel.Collections;
  7. using FluentNHibernate.Utils;
  8. using Iesi.Collections.Generic;
  9. using NUnit.Framework;
  10. namespace FluentNHibernate.Testing.Automapping
  11. {
  12. [TestFixture]
  13. public class AutoMapOneToManyTester
  14. {
  15. private AutoMapOneToMany mapper;
  16. [SetUp]
  17. public void CreateMapper()
  18. {
  19. mapper = new AutoMapOneToMany(new AutoMappingExpressions());
  20. }
  21. [Test]
  22. public void ShouldMapSets()
  23. {
  24. ShouldMap(x => x.Set);
  25. }
  26. [Test]
  27. public void ShouldMapLists()
  28. {
  29. ShouldMap(x => x.List);
  30. }
  31. [Test]
  32. public void ShouldntMapValueTypes()
  33. {
  34. ShouldntMap(x => x.Int);
  35. ShouldntMap(x => x.String);
  36. ShouldntMap(x => x.DateTime);
  37. }
  38. [Test]
  39. public void ShouldntMapEntities()
  40. {
  41. ShouldntMap(x => x.Entity);
  42. }
  43. [Test]
  44. public void ShouldMapListAsBag()
  45. {
  46. var classMapping = new ClassMapping()
  47. {
  48. Type = typeof(PropertyTarget)
  49. };
  50. mapper.Map(classMapping, typeof(PropertyTarget).GetProperty("List").ToMember());
  51. classMapping.Collections
  52. .First().ShouldBeOfType(typeof(BagMapping));
  53. }
  54. [Test]
  55. public void ShouldMapSetAsSet()
  56. {
  57. var classMapping = new ClassMapping()
  58. {
  59. Type = typeof(PropertyTarget)
  60. };
  61. mapper.Map(classMapping, typeof(PropertyTarget).GetProperty("Set").ToMember());
  62. classMapping.Collections
  63. .First().ShouldBeOfType(typeof(SetMapping));
  64. }
  65. [Test]
  66. public void ShouldMapHashSetAsSet()
  67. {
  68. var classMapping = new ClassMapping()
  69. {
  70. Type = typeof(PropertyTarget)
  71. };
  72. mapper.Map(classMapping, typeof(PropertyTarget).GetProperty("HashSet").ToMember());
  73. classMapping.Collections
  74. .First().ShouldBeOfType(typeof(SetMapping));
  75. }
  76. protected void ShouldMap(Expression<System.Func<PropertyTarget, object>> property)
  77. {
  78. mapper.MapsProperty(ReflectionHelper.GetProperty(property).ToMember()).ShouldBeTrue();
  79. }
  80. protected void ShouldntMap(Expression<System.Func<PropertyTarget, object>> property)
  81. {
  82. mapper.MapsProperty(ReflectionHelper.GetProperty(property).ToMember()).ShouldBeFalse();
  83. }
  84. protected class PropertyTarget
  85. {
  86. public ISet<PropertyTarget> Set { get; set; }
  87. public HashSet<PropertyTarget> HashSet { get; set; }
  88. public IList<PropertyTarget> List { get; set; }
  89. public int Int { get; set; }
  90. public string String { get; set; }
  91. public System.DateTime DateTime { get; set; }
  92. public PropertyTarget Entity { get; set; }
  93. }
  94. }
  95. }