fluent-nhibernate /src/FluentNHibernate.Testing/AutoMapping/AutoMapOneToManyTester.cs

Language C# Lines 113
MD5 Hash eb8d13a1b01f286205e1d29aef62487a
Repository https://github.com/dotnetchris/fluent-nhibernate.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using FluentNHibernate.Automapping;
using FluentNHibernate.MappingModel.ClassBased;
using FluentNHibernate.MappingModel.Collections;
using FluentNHibernate.Utils;
using Iesi.Collections.Generic;
using NUnit.Framework;

namespace FluentNHibernate.Testing.Automapping
{
    [TestFixture]
    public class AutoMapOneToManyTester
    {
        private AutoMapOneToMany mapper;

        [SetUp]
        public void CreateMapper()
        {
            mapper = new AutoMapOneToMany(new AutoMappingExpressions());
        }

        [Test]
        public void ShouldMapSets()
        {
            ShouldMap(x => x.Set);
        }

        [Test]
        public void ShouldMapLists()
        {
            ShouldMap(x => x.List);
        }

        [Test]
        public void ShouldntMapValueTypes()
        {
            ShouldntMap(x => x.Int);
            ShouldntMap(x => x.String);
            ShouldntMap(x => x.DateTime);
        }

        [Test]
        public void ShouldntMapEntities()
        {
            ShouldntMap(x => x.Entity);
        }

        [Test]
        public void ShouldMapListAsBag()
        {
            var classMapping = new ClassMapping()
            {
                Type = typeof(PropertyTarget)
            };

            mapper.Map(classMapping, typeof(PropertyTarget).GetProperty("List").ToMember());

            classMapping.Collections
                .First().ShouldBeOfType(typeof(BagMapping));
        }

        [Test]
        public void ShouldMapSetAsSet()
        {
            var classMapping = new ClassMapping()
            {
                Type = typeof(PropertyTarget)
            };

            mapper.Map(classMapping, typeof(PropertyTarget).GetProperty("Set").ToMember());

            classMapping.Collections
                .First().ShouldBeOfType(typeof(SetMapping));
        }

        [Test]
        public void ShouldMapHashSetAsSet()
        {
            var classMapping = new ClassMapping()
            {
                Type = typeof(PropertyTarget)
            };

            mapper.Map(classMapping, typeof(PropertyTarget).GetProperty("HashSet").ToMember());

            classMapping.Collections
                .First().ShouldBeOfType(typeof(SetMapping));
        }

        protected void ShouldMap(Expression<System.Func<PropertyTarget, object>> property)
        {
            mapper.MapsProperty(ReflectionHelper.GetProperty(property).ToMember()).ShouldBeTrue();
        }

        protected void ShouldntMap(Expression<System.Func<PropertyTarget, object>> property)
        {
            mapper.MapsProperty(ReflectionHelper.GetProperty(property).ToMember()).ShouldBeFalse();
        }

        protected class PropertyTarget
        {
            public ISet<PropertyTarget> Set { get; set; }
            public HashSet<PropertyTarget> HashSet { get; set; }
            public IList<PropertyTarget> List { get; set; }
            public int Int { get; set; }
            public string String { get; set; }
            public System.DateTime DateTime { get; set; }
            public PropertyTarget Entity { get; set; }
        }
    }
}
Back to Top