comparison UnitTests/CS/Data/ComplexMappingTest.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections.Generic;
3
4 using NUnit.Framework;
5
6 using BLToolkit.Data;
7 using BLToolkit.DataAccess;
8 using BLToolkit.EditableObjects;
9 using BLToolkit.Mapping;
10 using BLToolkit.Reflection.MetadataProvider;
11 using BLToolkit.Reflection;
12
13 namespace Data
14 {
15 [TestFixture]
16 public class ComplexMappingTest
17 {
18 private const string _parentChildquery = @"
19 SELECT 1 AS ParentId
20 UNION SELECT 2 AS ParentId
21
22 SELECT 1 AS ChildId, 1 AS ParentId
23 UNION SELECT 2 AS ChildId, 1 AS ParentId
24 UNION SELECT 3 AS ChildId, 2 AS ParentId
25 UNION SELECT 4 AS ChildId, 2 AS ParentId";
26
27 public abstract class Parent : EditableObject
28 {
29 [MapField("ParentId"), PrimaryKey]
30 public abstract int Id { get; set; }
31 [Relation(typeof(Child))]
32 public abstract List<Child> Children { get; set; }
33 }
34
35 [MapField("ParentId", "Parent.Id")]
36 public abstract class Child : EditableObject
37 {
38 [MapField("ChildId"), PrimaryKey]
39 public abstract int Id { get; set; }
40 [Relation]
41 public abstract Parent Parent { get; set; }
42 }
43
44 [Test]
45 public void Test1()
46 {
47 List<Parent> parents = new List<Parent>();
48 MapResultSet[] sets = new MapResultSet[2];
49
50 sets[0] = new MapResultSet(typeof(Parent), parents);
51 sets[1] = new MapResultSet(typeof(Child));
52
53 sets[0].AddRelation(sets[1], "ParentID", "ParentID", "Children");
54 sets[1].AddRelation(sets[0], "ParentID", "ParentID", "Parent");
55
56 using (DbManager db = new DbManager())
57 {
58 db
59 .SetCommand(_parentChildquery)
60 .ExecuteResultSet(sets);
61 }
62
63 foreach (Parent p in parents)
64 {
65 Assert.That(p.IsDirty == false);
66
67 foreach (Child c in p.Children)
68 Assert.That(c.IsDirty == false);
69 }
70 }
71
72 [Test]
73 public void RelationAttributeTest1()
74 {
75 bool isSet;
76 List<MapRelationBase> relations
77 = Map.DefaultSchema.MetadataProvider.GetRelations(Map.DefaultSchema,
78 Map.DefaultSchema.Extensions, typeof(Parent), typeof(Child), out isSet);
79
80 Assert.That(isSet);
81 Assert.That(relations.Count == 1);
82
83 Assert.That(relations[0].ContainerName == "Children");
84 Assert.That(relations[0].SlaveIndex.Fields[0].Name == "ParentId");
85
86 relations
87 = Map.DefaultSchema.MetadataProvider.GetRelations(Map.DefaultSchema,
88 Map.DefaultSchema.Extensions, typeof(Child), typeof(Parent), out isSet);
89
90 Assert.That(isSet);
91 Assert.That(relations.Count == 1);
92
93 Assert.That(relations[0].ContainerName == "Parent");
94 Assert.That(relations[0].SlaveIndex.Fields[0].Name == "ParentId");
95 }
96
97 public abstract class Master
98 {
99 [PrimaryKey, Nullable]
100 public abstract int MasterId {get; set;}
101 [Relation(typeof(Detail))]
102 public abstract List<Detail> Details {get; set;}
103 public abstract string Name {get; set;}
104 }
105
106 [MapField("MasterId", "Master.MasterId")]
107 public abstract class Detail
108 {
109 [PrimaryKey, MapField("Id"), Nullable]
110 public abstract int DetailId {get; set;}
111
112 [Relation]
113 public abstract Master Master {get; set;}
114
115 [Relation(typeof(SubDetail), "DetailId", "Id")]
116 public abstract List<SubDetail> SubDetails {get; set;}
117 }
118
119 [MapField("DetailId", "Master.DetailId")]
120 public abstract class SubDetail
121 {
122 [PrimaryKey]
123 public abstract int SubDetailId {get; set;}
124
125 [Relation("Id", "DetailId"), Nullable]
126 public abstract Detail Master {get; set;}
127 }
128
129 [Test]
130 public void RelationAttributeTest2()
131 {
132 MappingSchema ms = Map.DefaultSchema;
133 MetadataProviderBase mp = ms.MetadataProvider;
134 bool isSet;
135
136 List<MapRelationBase> relations = mp.GetRelations(ms, ms.Extensions, typeof(Master), typeof(Detail), out isSet);
137
138 //sets[0] = new MapResultSet(typeof(Master), masters);
139 //sets[1] = new MapResultSet(typeof(Detail), details);
140 //sets[2] = new MapResultSet(typeof(SubDetail), subdetails);
141
142 //sets[0].AddRelation(sets[1], "MasterId", "MasterId", "Details");
143 //sets[1].AddRelation(sets[0], "MasterId", "MasterId", "Master");
144 //sets[1].AddRelation(sets[2], "DetailId", "Id", "SubDetails");
145 //sets[2].AddRelation(sets[1], "Id", "DetailId", "Master");
146
147
148 Assert.That(isSet);
149 Assert.That(relations.Count == 1);
150 Assert.AreEqual("MasterId", relations[0].MasterIndex.Fields[0].Name);
151 Assert.AreEqual("MasterId", relations[0].SlaveIndex.Fields[0].Name);
152 Assert.AreEqual("Details", relations[0].ContainerName);
153
154 relations = mp.GetRelations(ms, ms.Extensions, typeof(Detail), typeof(Master), out isSet);
155
156 Assert.That(isSet);
157 Assert.That(relations.Count == 1);
158 Assert.AreEqual("MasterId", relations[0].MasterIndex.Fields[0].Name);
159 Assert.AreEqual("MasterId", relations[0].SlaveIndex.Fields[0].Name);
160 Assert.AreEqual("Master", relations[0].ContainerName);
161
162 relations = mp.GetRelations(ms, ms.Extensions, typeof(Detail), typeof(SubDetail), out isSet);
163
164 Assert.That(isSet);
165 Assert.That(relations.Count == 1);
166 Assert.AreEqual("Id", relations[0].MasterIndex.Fields[0].Name);
167 Assert.AreEqual("DetailId", relations[0].SlaveIndex.Fields[0].Name);
168 Assert.AreEqual("SubDetails", relations[0].ContainerName );
169
170 relations = mp.GetRelations(ms, ms.Extensions, typeof(SubDetail), typeof(Detail), out isSet);
171
172 Assert.That(isSet);
173 Assert.That(relations.Count == 1);
174 Assert.AreEqual("DetailId", relations[0].MasterIndex.Fields[0].Name);
175 Assert.AreEqual("Id", relations[0].SlaveIndex.Fields[0].Name);
176 Assert.AreEqual("Master", relations[0].ContainerName);
177 }
178
179 [Test]
180 public void RelationAttributeTest3()
181 {
182 MappingSchema ms = Map.DefaultSchema;
183 MetadataProviderBase mp = ms.MetadataProvider;
184 bool isSet;
185
186 List<MapRelationBase> relations = mp.GetRelations(ms, ms.Extensions, typeof(Detail), null, out isSet);
187
188 Assert.That(relations.Count == 2);
189
190 }
191
192 [Test]
193 public void NullKeyTest()
194 {
195 Master m = TypeAccessor.CreateInstance<Master>();
196 Detail d = TypeAccessor.CreateInstance<Detail>();
197
198 List<Master> masters = new List<Master>();
199 List<Detail> details = new List<Detail>();
200
201 masters.Add(m);
202 details.Add(d);
203
204 Map.ResultSets(new MapResultSet[] { new MapResultSet(typeof(Master), masters),
205 new MapResultSet(typeof(Detail), details) });
206
207 Assert.IsFalse (object.ReferenceEquals(d.Master, m));
208 Assert.AreEqual(0, m.Details.Count);
209
210 m.MasterId = 1;
211 d.DetailId = 1;
212 d.Master.MasterId = 1;
213
214 Map.ResultSets(new MapResultSet[] { new MapResultSet(typeof(Master), masters),
215 new MapResultSet(typeof(Detail), details) });
216
217
218 Assert.IsTrue (object.ReferenceEquals(d.Master, m));
219 Assert.AreEqual(1, m.Details.Count);
220 }
221 }
222 }