Skip to content

Commit cd7fe59

Browse files
committed
Added more unit tests
1 parent 7fa84e5 commit cd7fe59

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

src/NHibernate.Test/MappingByCode/ExplicitMappingTests/DynamicComponentMappingTests.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections;
3+
using System.Collections.Generic;
34
using System.Linq;
45
using NHibernate.Cfg.MappingSchema;
56
using NHibernate.Mapping.ByCode;
@@ -21,6 +22,17 @@ public IDictionary Info
2122
}
2223
}
2324

25+
private class PersonWithGenericInfo
26+
{
27+
public int Id { get; set; }
28+
private IDictionary<string, object> info;
29+
public IDictionary<string, object> Info
30+
{
31+
get { return info; }
32+
set { info = value; }
33+
}
34+
}
35+
2436
[Test]
2537
public void WhenMapDynCompoThenMapItAndItsProperties()
2638
{
@@ -38,6 +50,23 @@ public void WhenMapDynCompoThenMapItAndItsProperties()
3850
hbmDynamicComponent.Properties.Select(x=> x.Name).Should().Have.SameValuesAs("MyInt", "MyDate");
3951
}
4052

53+
[Test]
54+
public void WhenMapDynCompoThenMapItAndItsPropertiesGeneric()
55+
{
56+
var mapper = new ModelMapper();
57+
mapper.Class<PersonWithGenericInfo>(map =>
58+
{
59+
map.Id(x => x.Id, idmap => { });
60+
map.Component(x => x.Info, new { MyInt = 5, MyDate = DateTime.Now }, z => { });
61+
});
62+
63+
var hbmMapping = mapper.CompileMappingFor(new[] { typeof(PersonWithGenericInfo) });
64+
var hbmClass = hbmMapping.RootClasses[0];
65+
var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().SingleOrDefault();
66+
hbmDynamicComponent.Should().Not.Be.Null();
67+
hbmDynamicComponent.Properties.Select(x => x.Name).Should().Have.SameValuesAs("MyInt", "MyDate");
68+
}
69+
4170
[Test]
4271
public void WhenMapDynCompoPropertiesThenShouldAssignPropertyType()
4372
{
@@ -54,6 +83,22 @@ public void WhenMapDynCompoPropertiesThenShouldAssignPropertyType()
5483
hbmDynamicComponent.Properties.OfType<HbmProperty>().Select(x => x.type1).All(x=> x.Satisfy(value=> !string.IsNullOrEmpty(value)));
5584
}
5685

86+
[Test]
87+
public void WhenMapDynCompoPropertiesThenShouldAssignPropertyTypeGeneric()
88+
{
89+
var mapper = new ModelMapper();
90+
mapper.Class<PersonWithGenericInfo>(map =>
91+
{
92+
map.Id(x => x.Id, idmap => { });
93+
map.Component(x => x.Info, new { MyInt = 5, MyDate = DateTime.Now }, z => { });
94+
});
95+
96+
var hbmMapping = mapper.CompileMappingFor(new[] { typeof(PersonWithGenericInfo) });
97+
var hbmClass = hbmMapping.RootClasses[0];
98+
var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().Single();
99+
hbmDynamicComponent.Properties.OfType<HbmProperty>().Select(x => x.type1).All(x => x.Satisfy(value => !string.IsNullOrEmpty(value)));
100+
}
101+
57102
[Test]
58103
public void WhenMapDynCompoAttributesThenMapAttributes()
59104
{
@@ -80,5 +125,32 @@ public void WhenMapDynCompoAttributesThenMapAttributes()
80125
hbmDynamicComponent.optimisticlock.Should().Be.False();
81126
hbmDynamicComponent.unique.Should().Be.True();
82127
}
128+
129+
[Test]
130+
public void WhenMapDynCompoAttributesThenMapAttributesGeneric()
131+
{
132+
var mapper = new ModelMapper();
133+
mapper.Class<PersonWithGenericInfo>(map =>
134+
{
135+
map.Id(x => x.Id, idmap => { });
136+
map.Component(x => x.Info, new { MyInt = 5 }, z =>
137+
{
138+
z.Access(Accessor.Field);
139+
z.Insert(false);
140+
z.Update(false);
141+
z.Unique(true);
142+
z.OptimisticLock(false);
143+
});
144+
});
145+
146+
var hbmMapping = mapper.CompileMappingFor(new[] { typeof(PersonWithGenericInfo) });
147+
var hbmClass = hbmMapping.RootClasses[0];
148+
var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().SingleOrDefault();
149+
hbmDynamicComponent.access.Should().Contain("field");
150+
hbmDynamicComponent.insert.Should().Be.False();
151+
hbmDynamicComponent.update.Should().Be.False();
152+
hbmDynamicComponent.optimisticlock.Should().Be.False();
153+
hbmDynamicComponent.unique.Should().Be.True();
154+
}
83155
}
84156
}

src/NHibernate.Test/MappingByCode/MappersTests/AbstractPropertyContainerMapperTest.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ private class MyClassWithDynamic
3737
{
3838
public IDictionary DynCompo { get; set; }
3939
}
40+
private class MyClassWithDynamicGeneric
41+
{
42+
public IDictionary<string, object> DynCompo { get; set; }
43+
}
4044

4145
[Test]
4246
public void CantCreateWithoutHbmMapping()
@@ -126,6 +130,15 @@ public void AddDynamicComponentProperty()
126130
properties.Single().Should().Be.OfType<HbmDynamicComponent>().And.ValueOf.Name.Should().Be.EqualTo("DynCompo");
127131
}
128132

133+
[Test]
134+
public void AddDynamicComponentPropertyGeneric()
135+
{
136+
var properties = new List<object>();
137+
var map = new StubPropertyContainerMapper<MyClassWithDynamicGeneric>(properties);
138+
map.Component(For<MyClassWithDynamicGeneric>.Property(x => x.DynCompo), (IDynamicComponentMapper cp) => { });
139+
properties.Single().Should().Be.OfType<HbmDynamicComponent>().And.ValueOf.Name.Should().Be.EqualTo("DynCompo");
140+
}
141+
129142
[Test]
130143
public void CallDynamicComponentMapper()
131144
{
@@ -136,6 +149,16 @@ public void CallDynamicComponentMapper()
136149
called.Should().Be.True();
137150
}
138151

152+
[Test]
153+
public void CallDynamicComponentMapperGeneric()
154+
{
155+
var properties = new List<object>();
156+
var map = new StubPropertyContainerMapper<MyClassWithDynamicGeneric>(properties);
157+
var called = false;
158+
map.Component(For<MyClassWithDynamicGeneric>.Property(x => x.DynCompo), (IDynamicComponentMapper cp) => called = true);
159+
called.Should().Be.True();
160+
}
161+
139162
private class HackPropertyContainerMapper : AbstractPropertyContainerMapper
140163
{
141164
public HackPropertyContainerMapper(System.Type container, HbmMapping mapDoc) : base(container, mapDoc) {}

src/NHibernate.Test/MappingByCode/MappersTests/DynamicComponentMapperTests/ComponentPropertyOnDynamicCompoTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using NHibernate.Cfg.MappingSchema;
45
using NHibernate.Mapping.ByCode;
@@ -20,6 +21,16 @@ public IDictionary Info
2021
}
2122
}
2223

24+
private class PersonWithGenericInfo
25+
{
26+
public int Id { get; set; }
27+
private IDictionary<string, object> info;
28+
public IDictionary<string, object> Info
29+
{
30+
get { return info; }
31+
}
32+
}
33+
2334
private class MyClass
2435
{
2536
public int Something { get; set; }
@@ -37,6 +48,19 @@ public void WhenAddThenHas()
3748
component.Properties.Select(x => x.Name).Should().Have.SameSequenceAs("A");
3849
}
3950

51+
[Test]
52+
public void WhenAddThenHasGeneric()
53+
{
54+
var mapdoc = new HbmMapping();
55+
var component = new HbmDynamicComponent();
56+
var mapper = new DynamicComponentMapper(component, For<PersonWithGenericInfo>.Property(p => p.Info), mapdoc);
57+
var propertyInfo = (new { A = (MyClass)null }).GetType().GetProperty("A");
58+
59+
mapper.Component(propertyInfo, (IComponentMapper x) => { });
60+
61+
component.Properties.Select(x => x.Name).Should().Have.SameSequenceAs("A");
62+
}
63+
4064
[Test]
4165
public void WhenCustomizeThenCallCustomizer()
4266
{
@@ -51,6 +75,20 @@ public void WhenCustomizeThenCallCustomizer()
5175
called.Should().Be.True();
5276
}
5377

78+
[Test]
79+
public void WhenCustomizeThenCallCustomizerGeneric()
80+
{
81+
var mapdoc = new HbmMapping();
82+
var component = new HbmDynamicComponent();
83+
var mapper = new DynamicComponentMapper(component, For<PersonWithGenericInfo>.Property(p => p.Info), mapdoc);
84+
var propertyInfo = (new { A = (MyClass)null }).GetType().GetProperty("A");
85+
86+
var called = false;
87+
mapper.Component(propertyInfo, (IComponentMapper x) => called = true);
88+
89+
called.Should().Be.True();
90+
}
91+
5492
[Test]
5593
public void WhenCustomizeAccessorThenIgnore()
5694
{
@@ -63,5 +101,18 @@ public void WhenCustomizeAccessorThenIgnore()
63101

64102
component.Properties.OfType<HbmComponent>().Single().Access.Should().Be.NullOrEmpty();
65103
}
104+
105+
[Test]
106+
public void WhenCustomizeAccessorThenIgnoreGeneric()
107+
{
108+
var mapdoc = new HbmMapping();
109+
var component = new HbmDynamicComponent();
110+
var mapper = new DynamicComponentMapper(component, For<PersonWithGenericInfo>.Property(p => p.Info), mapdoc);
111+
var propertyInfo = (new { A = (MyClass)null }).GetType().GetProperty("A");
112+
113+
mapper.Component(propertyInfo, (IComponentMapper x) => x.Access(Accessor.Field));
114+
115+
component.Properties.OfType<HbmComponent>().Single().Access.Should().Be.NullOrEmpty();
116+
}
66117
}
67118
}

src/NHibernate.Test/MappingByCode/MappersTests/DynamicComponentMapperTests/DynComponentPropertyOnDynamicCompoTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using NHibernate.Cfg.MappingSchema;
45
using NHibernate.Mapping.ByCode;
@@ -20,6 +21,17 @@ public IDictionary Info
2021
}
2122
}
2223

24+
private class PersonWithGenericInfo
25+
{
26+
public int Id { get; set; }
27+
private IDictionary<string, object> info;
28+
public IDictionary<string, object> Info
29+
{
30+
get { return info; }
31+
}
32+
}
33+
34+
2335
[Test]
2436
public void WhenAddThenHas()
2537
{
@@ -33,6 +45,19 @@ public void WhenAddThenHas()
3345
component.Properties.Select(x => x.Name).Should().Have.SameSequenceAs("Info");
3446
}
3547

48+
[Test]
49+
public void WhenAddThenHasGeneric()
50+
{
51+
var mapdoc = new HbmMapping();
52+
var component = new HbmDynamicComponent();
53+
var mapper = new DynamicComponentMapper(component, For<PersonWithGenericInfo>.Property(p => p.Info), mapdoc);
54+
var propertyInfo = For<PersonWithGenericInfo>.Property(p => p.Info);//just as another dyn-compo
55+
56+
mapper.Component(propertyInfo, (IDynamicComponentMapper x) => { });
57+
58+
component.Properties.Select(x => x.Name).Should().Have.SameSequenceAs("Info");
59+
}
60+
3661
[Test]
3762
public void WhenCustomizeThenCallCustomizer()
3863
{
@@ -47,6 +72,20 @@ public void WhenCustomizeThenCallCustomizer()
4772
called.Should().Be.True();
4873
}
4974

75+
[Test]
76+
public void WhenCustomizeThenCallCustomizerGeneric()
77+
{
78+
var mapdoc = new HbmMapping();
79+
var component = new HbmDynamicComponent();
80+
var mapper = new DynamicComponentMapper(component, For<PersonWithGenericInfo>.Property(p => p.Info), mapdoc);
81+
var propertyInfo = For<PersonWithGenericInfo>.Property(p => p.Info);//just as another dyn-compo
82+
83+
var called = false;
84+
mapper.Component(propertyInfo, (IDynamicComponentMapper x) => called = true);
85+
86+
called.Should().Be.True();
87+
}
88+
5089
[Test]
5190
public void WhenCustomizeAccessorThenIgnore()
5291
{
@@ -59,5 +98,18 @@ public void WhenCustomizeAccessorThenIgnore()
5998

6099
component.Properties.OfType<HbmDynamicComponent>().Single().Access.Should().Be.NullOrEmpty();
61100
}
101+
102+
[Test]
103+
public void WhenCustomizeAccessorThenIgnoreGeneric()
104+
{
105+
var mapdoc = new HbmMapping();
106+
var component = new HbmDynamicComponent();
107+
var mapper = new DynamicComponentMapper(component, For<PersonWithGenericInfo>.Property(p => p.Info), mapdoc);
108+
var propertyInfo = For<PersonWithGenericInfo>.Property(p => p.Info);//just as another dyn-compo
109+
110+
mapper.Component(propertyInfo, (IDynamicComponentMapper x) => x.Access(Accessor.Field));
111+
112+
component.Properties.OfType<HbmDynamicComponent>().Single().Access.Should().Be.NullOrEmpty();
113+
}
62114
}
63115
}

0 commit comments

Comments
 (0)