Datei: UnitTestGenerator/TestGenerator/TestGenerator.cs
Last Commit (6d63e12)
1 | // |
2 | // Copyright (c) 2002-2016 Mirko Matytschak |
3 | // (www.netdataobjects.de) |
4 | // |
5 | // Author: Mirko Matytschak |
6 | // |
7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
11 | // conditions: |
12 | |
13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
14 | // of the Software. |
15 | // |
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. |
21 | |
22 | |
23 | using System; |
24 | using System.Collections; |
25 | using System.IO; |
26 | using CodeGenerator; |
27 | using System.Text; |
28 | using System.Collections.Generic; |
29 | |
30 | namespace TestGenerator |
31 | { |
32 | ····/// <summary> |
33 | ····/// Summary for TestGenerator. |
34 | ····/// </summary> |
35 | ····public class TestGenerator |
36 | ····{ |
37 | ········List<RelInfo> relInfos; |
38 | ········string fileName; |
39 | ········StreamWriter sw; |
40 | ········TestFixture fixture; |
41 | ········Test test; |
42 | ········readonly string nameSpace = "RelationUnitTests"; |
43 | |
44 | ········public TestGenerator( List<RelInfo> relInfos ) |
45 | ········{ |
46 | ············this.relInfos = relInfos; |
47 | ············fileName = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\..\UnitTests\UnitTests.cs" ); |
48 | ········} |
49 | |
50 | |
51 | ········void CreateTests( RelInfo ri ) |
52 | ········{ |
53 | ············CreateTestSaveReload( ri ); |
54 | ············CreateTestSaveReloadNull( ri ); |
55 | ············CreateTestSaveReloadRemove( ri ); |
56 | ············CreateTestChangeKeyHolderLeft( ri ); |
57 | ············CreateTestChangeKeyHolderRight( ri ); |
58 | ············CreateTestChangeKeyHolderLeftNoTouch( ri ); |
59 | ············CreateTestChangeKeyHolderRightNoTouch( ri ); |
60 | ············CreateTestUpdateOrder( ri ); |
61 | ············CreateTestRelationHash( ri ); |
62 | ············GenerateCreateObjects( ri ); |
63 | ············GenerateQueryOwn( ri ); |
64 | ············GenerateQueryOther( ri ); |
65 | ········} |
66 | |
67 | ········string AssertEquals( string text, object o2, object o3 ) |
68 | ········{ |
69 | ············return $"Assert.That({o2.ToString().ToLower()}, Is.EqualTo({o3}), \"{text}\");"; |
70 | ········} |
71 | ········string AssertNotNull( string text, object o ) |
72 | ········{ |
73 | ············return "Assert.That(" + o + ", Is.Not.Null, \"" + text + "\");"; |
74 | ········} |
75 | ········string AssertNull( string text, object o ) |
76 | ········{ |
77 | ············return "Assert.That(" + o + ", Is.Null, \"" + text + "\");"; |
78 | ········} |
79 | |
80 | ········string Assert( string text, object o ) |
81 | ········{ |
82 | ············return "Assert.That(" + o + ", \"" + text + "\");"; |
83 | ········} |
84 | |
85 | ········string QualifiedClassName( string className ) |
86 | ········{ |
87 | ············return nameSpace + "." + className; |
88 | ········} |
89 | |
90 | ········void CreateTestRelationHash( RelInfo ri ) |
91 | ········{ |
92 | ············if (!ri.IsBi) |
93 | ················return; |
94 | ············Function func = fixture.NewFunction( "void", "TestRelationHash" ); |
95 | ············func.Attributes.Add( "Test" ); |
96 | ············func.AccessModifier = "public"; |
97 | ············func.Statements.Add( "Class clbaseLeft = pm.NDOMapping.FindClass(typeof(" + test.OwnClass.Name + "));" ); |
98 | ············func.Statements.Add( "Relation relbaseLeft = clbaseLeft.FindRelation(\"relField\");" ); |
99 | ············func.Statements.Add( "Class clbaseRight = pm.NDOMapping.FindClass(typeof(" + test.OtherClass.Name + "));" ); |
100 | ············func.Statements.Add( "Relation relbaseRight = clbaseRight.FindRelation(\"relField\");" ); |
101 | ············func.Statements.Add( Assert( "Relation should be equal #1", "relbaseRight.Equals(relbaseLeft)" ) ); |
102 | ············func.Statements.Add( Assert( "Relation should be equal #2", "relbaseLeft.Equals(relbaseRight)" ) ); |
103 | ············if (ri.OwnPoly) |
104 | ············{ |
105 | ················func.Statements.Add( "Class clderLeft = pm.NDOMapping.FindClass(typeof(" + test.OwnDerivedClass.Name + "));" ); |
106 | ················func.Statements.Add( "Relation relderLeft = clderLeft.FindRelation(\"relField\");" ); |
107 | ················func.Statements.Add( Assert( "Relation should be equal #3", "relderLeft.Equals(relbaseRight)" ) ); |
108 | ················func.Statements.Add( Assert( "Relation should be equal #4", "relbaseRight.Equals(relderLeft)" ) ); |
109 | ············} |
110 | ············if (ri.OtherPoly) |
111 | ············{ |
112 | ················func.Statements.Add( "Class clderRight = pm.NDOMapping.FindClass(typeof(" + test.OtherDerivedClass.Name + "));" ); |
113 | ················func.Statements.Add( "Relation relderRight = clderRight.FindRelation(\"relField\");" ); |
114 | ················func.Statements.Add( Assert( "Relation should be equal #5", "relbaseLeft.Equals(relderRight)" ) ); |
115 | ················func.Statements.Add( Assert( "Relation should be equal #6", "relderRight.Equals(relbaseLeft)" ) ); |
116 | ················if (ri.OwnPoly) |
117 | ················{ |
118 | ····················func.Statements.Add( Assert( "Relation should be equal #7", "relderLeft.Equals(relderRight)" ) ); |
119 | ····················func.Statements.Add( Assert( "Relation should be equal #8", "relderRight.Equals(relderLeft)" ) ); |
120 | ················} |
121 | ············} |
122 | ········} |
123 | |
124 | ········bool IsForbiddenCase( RelInfo ri ) |
125 | ········{ |
126 | ············return ri.IsComposite && !ri.UseGuid && !ri.HasTable && !ri.IsList && ri.OtherPoly; |
127 | ········} |
128 | |
129 | ········void CreateTestSaveReload( RelInfo ri ) |
130 | ········{ |
131 | ············Function func = fixture.NewFunction( "void", "TestSaveReload" ); |
132 | ············func.Attributes.Add( "Test" ); |
133 | ············bool forbidden = IsForbiddenCase( ri ); |
134 | ············func.AccessModifier = "public"; |
135 | |
136 | ············if (forbidden) |
137 | ············{ |
138 | ················func.Statements.Add( "bool thrown = false;" ); |
139 | ················func.Statements.Add( "try" ); |
140 | ················func.Statements.Add( "{" ); |
141 | ············} |
142 | ············func.Statements.Add( "CreateObjects();" ); |
143 | ············func.Statements.Add( "QueryOwn();" ); |
144 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
145 | ············if (ri.IsList) |
146 | ················func.Statements.Add( AssertEquals( "Count wrong", 1, "ownVar.RelField.Count" ) ); |
147 | ············else |
148 | ················func.Statements.Add( AssertNotNull( "No related object", "ownVar.RelField" ) ); |
149 | ············if (forbidden) |
150 | ············{ |
151 | ················func.Statements.Add( "}" ); |
152 | ················func.Statements.Add( "catch (NDOException)" ); |
153 | ················func.Statements.Add( "{" ); |
154 | ················func.Statements.Add( "thrown = true;" ); |
155 | ················func.Statements.Add( "}" ); |
156 | ················func.Statements.Add( AssertEquals( "NDOException should have been thrown", true, "thrown" ) ); |
157 | ············} |
158 | ········} |
159 | |
160 | ········void CreateTestSaveReloadNull( RelInfo ri ) |
161 | ········{ |
162 | ············Function func = fixture.NewFunction( "void", "TestSaveReloadNull" ); |
163 | ············func.Attributes.Add( "Test" ); |
164 | ············bool forbidden = IsForbiddenCase( ri ); |
165 | ············func.AccessModifier = "public"; |
166 | |
167 | ············if (forbidden) |
168 | ············{ |
169 | ················func.Statements.Add( "bool thrown = false;" ); |
170 | ················func.Statements.Add( "try" ); |
171 | ················func.Statements.Add( "{" ); |
172 | ············} |
173 | ············func.Statements.Add( "CreateObjects();" ); |
174 | ············func.Statements.Add( "QueryOwn();" ); |
175 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
176 | |
177 | ············if (ri.IsList) |
178 | ················func.Statements.Add( AssertEquals( "Count wrong", 1, "ownVar.RelField.Count" ) ); |
179 | ············else |
180 | ················func.Statements.Add( AssertNotNull( "No related object", "ownVar.RelField" ) ); |
181 | |
182 | ············if (ri.IsList) |
183 | ················func.Statements.Add( "ownVar.RelField = new List<" + this.test.OtherClass.Name + ">();" ); |
184 | ············else |
185 | ················func.Statements.Add( "ownVar.RelField = null;" ); |
186 | ············func.Statements.Add( "pm.Save();" ); |
187 | ············func.Statements.Add( "pm.UnloadCache();" ); |
188 | ············func.Statements.Add( "QueryOwn();" ); |
189 | |
190 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
191 | |
192 | ············if (ri.IsList) |
193 | ················func.Statements.Add( AssertEquals( "Count wrong", 0, "ownVar.RelField.Count" ) ); |
194 | ············else |
195 | ················func.Statements.Add( AssertNull( "There should be no object", "ownVar.RelField" ) ); |
196 | |
197 | ············if (forbidden) |
198 | ············{ |
199 | ················func.Statements.Add( "}" ); |
200 | ················func.Statements.Add( "catch (NDOException)" ); |
201 | ················func.Statements.Add( "{" ); |
202 | ················func.Statements.Add( "thrown = true;" ); |
203 | ················func.Statements.Add( "}" ); |
204 | ················func.Statements.Add( AssertEquals( "NDOException should have been thrown", true, "thrown" ) ); |
205 | ············} |
206 | ········} |
207 | |
208 | ········void CreateTestSaveReloadRemove( RelInfo ri ) |
209 | ········{ |
210 | ············Function func = fixture.NewFunction( "void", "TestSaveReloadRemove" ); |
211 | ············func.AccessModifier = "public"; |
212 | ············func.Attributes.Add( "Test" ); |
213 | ············if (!ri.IsList) |
214 | ················return; |
215 | |
216 | ············bool forbidden = IsForbiddenCase( ri ); |
217 | ············if (forbidden) |
218 | ············{ |
219 | ················func.Statements.Add( "bool thrown = false;" ); |
220 | ················func.Statements.Add( "try" ); |
221 | ················func.Statements.Add( "{" ); |
222 | ············} |
223 | ············func.Statements.Add( "CreateObjects();" ); |
224 | ············func.Statements.Add( "QueryOwn();" ); |
225 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
226 | |
227 | ············if (ri.IsList) |
228 | ················func.Statements.Add( AssertEquals( "Count wrong", 1, "ownVar.RelField.Count" ) ); |
229 | ············else |
230 | ················func.Statements.Add( AssertNotNull( "No related object", "ownVar.RelField" ) ); |
231 | |
232 | ············func.Statements.Add( "ownVar.RemoveRelatedObject();" ); |
233 | ············func.Statements.Add( "pm.Save();" ); |
234 | ············func.Statements.Add( "pm.UnloadCache();" ); |
235 | ············func.Statements.Add( "QueryOwn();" ); |
236 | |
237 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
238 | |
239 | ············func.Statements.Add( AssertEquals( "Count wrong", 0, "ownVar.RelField.Count" ) ); |
240 | ············if (forbidden) |
241 | ············{ |
242 | ················func.Statements.Add( "}" ); |
243 | ················func.Statements.Add( "catch (NDOException)" ); |
244 | ················func.Statements.Add( "{" ); |
245 | ················func.Statements.Add( "thrown = true;" ); |
246 | ················func.Statements.Add( "}" ); |
247 | ················func.Statements.Add( AssertEquals( "NDOException should have been thrown", true, "thrown" ) ); |
248 | ············} |
249 | ········} |
250 | |
251 | ········void CreateTestChangeKeyHolderLeft( RelInfo ri ) |
252 | ········{ |
253 | ············if (IsForbiddenCase( ri )) |
254 | ················return; // These would throw exceptions |
255 | |
256 | ············// Check Keyholders only |
257 | ············if (ri.IsList) |
258 | ················return; |
259 | |
260 | ············Function func = fixture.NewFunction( "void", "TestChangeKeyHolderLeft" ); |
261 | ············func.Attributes.Add( "Test" ); |
262 | ············func.AccessModifier = "public"; |
263 | |
264 | ············func.Statements.Add( "CreateObjects();" ); |
265 | ············// 1:1 or n:1 - we check only the left side |
266 | ············func.Statements.Add( "QueryOwn();" ); |
267 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
268 | ············func.Statements.Add( AssertNotNull( "No related object", "ownVar.RelField" ) ); |
269 | ············// touch the related object |
270 | ············func.Statements.Add( "int x = ownVar.RelField.Dummy;" ); |
271 | ············// change our object |
272 | ············func.Statements.Add( "ownVar.Dummy = 4711;" ); |
273 | ············func.Statements.Add( "pm.Save();" ); |
274 | ············func.Statements.Add( "pm.UnloadCache();" ); |
275 | ············func.Statements.Add( "QueryOwn();" ); |
276 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
277 | ············func.Statements.Add( AssertNotNull( "Wrong value", "ownVar.Dummy == 4711" ) ); |
278 | ············func.Statements.Add( AssertNotNull( "No related object", "ownVar.RelField" ) ); |
279 | |
280 | ········} |
281 | |
282 | ········void CreateTestChangeKeyHolderRight( RelInfo ri ) |
283 | ········{ |
284 | ············if (IsForbiddenCase( ri )) |
285 | ················return; // These would throw exceptions |
286 | |
287 | ············// Check Keyholders only |
288 | ············if (ri.ForeignIsList || !ri.IsBi) |
289 | ················return; |
290 | |
291 | ············Function func = fixture.NewFunction( "void", "TestChangeKeyHolderRight" ); |
292 | ············func.Attributes.Add( "Test" ); |
293 | ············func.AccessModifier = "public"; |
294 | |
295 | ············func.Statements.Add( "CreateObjects();" ); |
296 | ············// 1:1 or n:1 - we check only the left side |
297 | ············func.Statements.Add( "QueryOther();" ); |
298 | ············func.Statements.Add( AssertNotNull( "No Query Result", "otherVar" ) ); |
299 | ············func.Statements.Add( AssertNotNull( "No related object", "otherVar.RelField" ) ); |
300 | ············// touch the related object |
301 | ············func.Statements.Add( "int x = otherVar.RelField.Dummy;" ); |
302 | ············// change our object |
303 | ············func.Statements.Add( "otherVar.Dummy = 4711;" ); |
304 | ············func.Statements.Add( "pm.Save();" ); |
305 | ············func.Statements.Add( "pm.UnloadCache();" ); |
306 | ············func.Statements.Add( "QueryOther();" ); |
307 | ············func.Statements.Add( AssertNotNull( "No Query Result", "otherVar" ) ); |
308 | ············func.Statements.Add( AssertNotNull( "Wrong value", "otherVar.Dummy == 4711" ) ); |
309 | ············func.Statements.Add( AssertNotNull( "No related object", "otherVar.RelField" ) ); |
310 | ········} |
311 | |
312 | ········void CreateTestChangeKeyHolderLeftNoTouch( RelInfo ri ) |
313 | ········{ |
314 | ············if (IsForbiddenCase( ri )) |
315 | ················return; // These would throw exceptions |
316 | |
317 | ············// Check Keyholders only |
318 | ············if (ri.IsList) |
319 | ················return; |
320 | |
321 | ············Function func = fixture.NewFunction( "void", "TestChangeKeyHolderLeftNoTouch" ); |
322 | ············func.Attributes.Add( "Test" ); |
323 | ············func.AccessModifier = "public"; |
324 | |
325 | ············func.Statements.Add( "CreateObjects();" ); |
326 | ············// 1:1 or n:1 - we check only the left side |
327 | ············func.Statements.Add( "QueryOwn();" ); |
328 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
329 | ············func.Statements.Add( AssertNotNull( "No related object", "ownVar.RelField" ) ); |
330 | ············// change our object |
331 | ············func.Statements.Add( "ownVar.Dummy = 4711;" ); |
332 | ············func.Statements.Add( "pm.Save();" ); |
333 | ············func.Statements.Add( "pm.UnloadCache();" ); |
334 | ············func.Statements.Add( "QueryOwn();" ); |
335 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
336 | ············func.Statements.Add( AssertNotNull( "Wrong value", "ownVar.Dummy == 4711" ) ); |
337 | ············func.Statements.Add( AssertNotNull( "No related object", "ownVar.RelField" ) ); |
338 | |
339 | ········} |
340 | |
341 | ········void CreateTestChangeKeyHolderRightNoTouch( RelInfo ri ) |
342 | ········{ |
343 | ············if (IsForbiddenCase( ri )) |
344 | ················return; // These would throw exceptions |
345 | |
346 | ············// Check Keyholders only |
347 | ············if (ri.ForeignIsList || !ri.IsBi) |
348 | ················return; |
349 | |
350 | ············Function func = fixture.NewFunction( "void", "TestChangeKeyHolderRightNoTouch" ); |
351 | ············func.Attributes.Add( "Test" ); |
352 | ············func.AccessModifier = "public"; |
353 | |
354 | ············func.Statements.Add( "CreateObjects();" ); |
355 | ············// 1:1 or n:1 - we check only the left side |
356 | ············func.Statements.Add( "QueryOther();" ); |
357 | ············func.Statements.Add( AssertNotNull( "No Query Result", "otherVar" ) ); |
358 | ············func.Statements.Add( AssertNotNull( "No related object", "otherVar.RelField" ) ); |
359 | ············// change our object |
360 | ············func.Statements.Add( "otherVar.Dummy = 4711;" ); |
361 | ············func.Statements.Add( "pm.Save();" ); |
362 | ············func.Statements.Add( "pm.UnloadCache();" ); |
363 | ············func.Statements.Add( "QueryOther();" ); |
364 | ············func.Statements.Add( AssertNotNull( "No Query Result", "otherVar" ) ); |
365 | ············func.Statements.Add( AssertNotNull( "Wrong value", "otherVar.Dummy == 4711" ) ); |
366 | ············func.Statements.Add( AssertNotNull( "No related object", "otherVar.RelField" ) ); |
367 | ········} |
368 | |
369 | ········void CreateTestUpdateOrder( RelInfo ri ) |
370 | ········{ |
371 | ············if (ri.HasTable || ri.UseGuid || IsForbiddenCase( ri )) |
372 | ················return; |
373 | ············if (!ri.IsList && ri.IsBi && !ri.ForeignIsList) |
374 | ················return; |
375 | |
376 | ············Function func = fixture.NewFunction( "void", "TestUpdateOrder" ); |
377 | ············func.Attributes.Add( "Test" ); |
378 | ············func.AccessModifier = "public"; |
379 | |
380 | ············func.Statements.Add( "NDO.Mapping.NDOMapping mapping = pm.NDOMapping;" ); |
381 | ············func.Statements.Add( "MethodInfo mi = mapping.GetType().GetMethod(\"GetUpdateOrder\");" ); |
382 | ············string br = null; |
383 | ············if (!ri.IsList) |
384 | ················br = ">"; |
385 | ············else |
386 | ················br = "<"; |
387 | ············if ((!ri.OwnPoly && !ri.OtherPoly) || !ri.IsAbstract) |
388 | ············{ |
389 | ················func.Statements.Add( Assert( "Wrong order #1", @"((int)mi.Invoke(mapping, new object[]{typeof(" + test.OwnClass.Name + @")})) |
390 | ················" + br + " ((int)mi.Invoke(mapping, new object[]{typeof(" + test.OtherClass.Name + ")}))" ) ); |
391 | ············} |
392 | ············if (ri.OwnPoly && !ri.OtherPoly) |
393 | ············{ |
394 | ················func.Statements.Add( Assert( "Wrong order #2", @"((int)mi.Invoke(mapping, new object[]{typeof(" + test.OwnDerivedClass.Name + @")})) |
395 | ················" + br + " ((int)mi.Invoke(mapping, new object[]{typeof(" + test.OtherClass.Name + ")}))" ) ); |
396 | ············} |
397 | ············if (!ri.OwnPoly && ri.OtherPoly) |
398 | ············{ |
399 | ················func.Statements.Add( Assert( "Wrong order #2", @"((int)mi.Invoke(mapping, new object[]{typeof(" + test.OwnClass.Name + @")})) |
400 | ················" + br + " ((int)mi.Invoke(mapping, new object[]{typeof(" + test.OtherDerivedClass.Name + ")}))" ) ); |
401 | ············} |
402 | ············if (ri.OwnPoly && ri.OtherPoly) |
403 | ············{ |
404 | ················func.Statements.Add( Assert( "Wrong order #2", @"((int)mi.Invoke(mapping, new object[]{typeof(" + test.OwnDerivedClass.Name + @")})) |
405 | ················" + br + " ((int)mi.Invoke(mapping, new object[]{typeof(" + test.OtherDerivedClass.Name + ")}))" ) ); |
406 | ············} |
407 | ············func.Statements.Add( "Debug.WriteLine(\"" + test.OwnClass.Name + "\");" ); |
408 | |
409 | ········} |
410 | |
411 | ········void GenerateTearDown( RelInfo ri ) |
412 | ········{ |
413 | ············Function func = fixture.TearDown; |
414 | ············func.Statements.Add( "try" ); |
415 | ············func.Statements.Add( "{" ); |
416 | ············func.Statements.Add( "pm.UnloadCache();" ); |
417 | ············func.Statements.Add( "var l = pm.Objects<" + test.OwnClass.Name + ">().ResultTable;" ); |
418 | ············func.Statements.Add( "pm.Delete(l);" ); |
419 | ············func.Statements.Add( "pm.Save();" ); |
420 | ············func.Statements.Add( "pm.UnloadCache();" ); |
421 | ············if (!ri.IsComposite) |
422 | ············{ |
423 | ················func.Statements.Add( "var m = pm.Objects<" + test.OtherClass.Name + ">().ResultTable;" ); |
424 | ················func.Statements.Add( "pm.Delete(m);" ); |
425 | ················func.Statements.Add( "pm.Save();" ); |
426 | ················func.Statements.Add( "pm.UnloadCache();" ); |
427 | ············} |
428 | ············func.Statements.Add( "decimal count;" ); |
429 | ············func.Statements.Add( "count = (decimal) new " + NDOQuery( test.OwnClass.Name ) + ".ExecuteAggregate(\"dummy\", AggregateType.Count);" ); |
430 | ············func.Statements.Add( "Assert.That(0, Is.EqualTo(count), \"Count wrong #1\");" ); |
431 | ············func.Statements.Add( "count = (decimal) new " + NDOQuery( test.OtherClass.Name ) + ".ExecuteAggregate(\"dummy\", AggregateType.Count);" ); |
432 | ············func.Statements.Add( "Assert.That(0, Is.EqualTo(count), \"Count wrong #2\");" ); |
433 | ············func.Statements.Add( "}" ); |
434 | ············func.Statements.Add( "catch (Exception)" ); |
435 | ············func.Statements.Add( "{" ); |
436 | ············func.Statements.Add( "var handler = pm.GetSqlPassThroughHandler( pm.NDOMapping.Connections.First() );" ); |
437 | ············ |
438 | ············func.Statements.Add( "handler.Execute($\"DELETE FROM {pm.NDOMapping.FindClass( ownVar.GetType() ).TableName}\");" ); |
439 | ············func.Statements.Add( "handler.Execute($\"DELETE FROM {pm.NDOMapping.FindClass( otherVar.GetType() ).TableName}\");" ); |
440 | ············if (ri.HasTable) |
441 | ············{ |
442 | ················func.Statements.Add( "handler.Execute( $\"DELETE FROM {pm.NDOMapping.FindClass( ownVar.GetType() ).Relations.First().MappingTable.TableName}\" );" ); |
443 | ············} |
444 | ············func.Statements.Add( "}" ); |
445 | |
446 | ········} |
447 | |
448 | ········void GenerateTestGroup( RelInfo ri ) |
449 | ········{ |
450 | ············fixture = new TestFixture( this.nameSpace, "Test" + ri.ToString() ); |
451 | ············test = new Test( ri, "RelationTestClasses" ); |
452 | ············Class ownClass = null; |
453 | ············Class otherClass = null; |
454 | ············if (ri.OwnPoly) |
455 | ············{ |
456 | ················ownClass = test.OwnDerivedClass; |
457 | ············} |
458 | ············else |
459 | ············{ |
460 | ················ownClass = test.OwnClass; |
461 | ············} |
462 | ············if (ri.OtherPoly) |
463 | ············{ |
464 | ················otherClass = test.OtherDerivedClass; |
465 | ············} |
466 | ············else |
467 | ············{ |
468 | ················otherClass = test.OtherClass; |
469 | ············} |
470 | |
471 | |
472 | ············fixture.Statements.Add( test.OwnClass.Name + " ownVar;" ); |
473 | ············fixture.Statements.Add( test.OtherClass.Name + " otherVar;" );··// always use the base class type |
474 | ············fixture.Statements.Add( "PersistenceManager pm;" ); |
475 | |
476 | ············fixture.SetUp.Statements.Add( "pm = PmFactory.NewPersistenceManager();" ); |
477 | ············fixture.SetUp.Statements.Add( "ownVar = new " + ownClass.Name + "();" ); |
478 | ············fixture.SetUp.Statements.Add( "otherVar = new " + otherClass.Name + "();" ); |
479 | |
480 | ············GenerateTearDown( ri ); |
481 | |
482 | ············CreateTests( ri ); |
483 | |
484 | ············this.sw.WriteLine( fixture.ToString() ); |
485 | ········} |
486 | |
487 | ········void GeneratePmFactory() |
488 | ········{ |
489 | ············Class cl = new Class( this.nameSpace, "PmFactory" ); |
490 | ············string path = AppDomain.CurrentDomain.BaseDirectory; |
491 | path = Path. Combine( path, @". . \. . \UnitTests\bin\Debug\NDOMapping. xml" ) ; |
492 | ············path = Path.GetFullPath( path ); |
493 | ············cl.Statements.Add( "static PersistenceManager pm;" ); |
494 | ············Function func = cl.NewFunction( "PersistenceManager", "NewPersistenceManager" ); |
495 | ············func.IsStatic = true; |
496 | ············func.AccessModifier = "public"; |
497 | |
498 | ············func.Statements.Add( "if (pm == null)" ); |
499 | ············func.Statements.Add( "{" ); |
500 | ············func.Statements.Add( "pm = new PersistenceManager(@\"" + path + "\");" ); |
501 | ············func.Statements.Add( "}" ); |
502 | ············func.Statements.Add( "else" ); |
503 | ············func.Statements.Add( "{" ); |
504 | ············func.Statements.Add( "pm.UnloadCache();" ); |
505 | ············func.Statements.Add( "}" ); |
506 | ············func.Statements.Add( "return pm;" ); |
507 | ············sw.WriteLine( cl.ToString() ); |
508 | ········} |
509 | |
510 | ········void GenerateCreateObjects( RelInfo ri ) |
511 | ········{ |
512 | ············Function func = fixture.NewFunction( "void", "CreateObjects" ); |
513 | ············func.Statements.Add( "pm.MakePersistent(ownVar);" ); |
514 | ············string secondMakePersistent = "pm.MakePersistent(otherVar);"; |
515 | ············string assignRelation = "ownVar.AssignRelation(otherVar);"; |
516 | ············if (ri.IsComposite) |
517 | ············{ |
518 | ················if (!ri.IsList && (ri.OtherPoly || ri.OwnPoly) && !ri.HasTable && !ri.UseGuid) |
519 | ····················func.Statements.Add( "pm.Save();" ); |
520 | ················func.Statements.Add( assignRelation ); |
521 | ············} |
522 | ············else |
523 | ············{ |
524 | ················if (!ri.IsList && ri.OtherPoly && !ri.HasTable && !ri.UseGuid) |
525 | ····················func.Statements.Add( "pm.Save();" ); |
526 | ················func.Statements.Add( secondMakePersistent ); |
527 | ················if (!ri.IsList && ri.OtherPoly && !ri.HasTable && !ri.UseGuid) |
528 | ····················func.Statements.Add( "pm.Save();" ); |
529 | ················if (ri.IsBi && !ri.ForeignIsList && ri.OwnPoly && !ri.UseGuid) |
530 | ····················func.Statements.Add( "pm.Save();" ); |
531 | ················func.Statements.Add( assignRelation ); |
532 | ············} |
533 | ············func.Statements.Add( "pm.Save();" ); |
534 | ············func.Statements.Add( "pm.UnloadCache();" ); |
535 | ········} |
536 | |
537 | ········string NDOQuery( string className, string condition = null ) |
538 | ········{ |
539 | ············StringBuilder sb = new StringBuilder( "NDOQuery<" ); |
540 | ············sb.Append( className ); |
541 | ············sb.Append( ">(pm" ); |
542 | ············if (condition != null) |
543 | ············{ |
544 | ················sb.Append( "," ); |
545 | ················sb.Append( condition ); |
546 | ············} |
547 | ············sb.Append( ")" ); |
548 | ············return sb.ToString(); |
549 | ········} |
550 | |
551 | ········void GenerateQueryOwn( RelInfo ri ) |
552 | ········{ |
553 | ············Function func = fixture.NewFunction( "void", "QueryOwn" ); |
554 | ············func.Statements.Add( "var q = new " + NDOQuery(test.OwnClass.Name) + ';' ); |
555 | ············func.Statements.Add( "ownVar = q.ExecuteSingle();" ); |
556 | ········} |
557 | |
558 | ········void GenerateQueryOther( RelInfo ri ) |
559 | ········{ |
560 | ············Function func = fixture.NewFunction( "void", "QueryOther" ); |
561 | ············func.Statements.Add( "var q = new " + NDOQuery( test.OtherClass.Name ) + ';' ); |
562 | ············func.Statements.Add( "otherVar = q.ExecuteSingle();" ); |
563 | ········} |
564 | |
565 | |
566 | ········public void Generate() |
567 | ········{ |
568 | ············sw = new StreamWriter( fileName ); |
569 | ············sw.WriteLine( @"// |
570 | // Copyright (c) 2002-2016 Mirko Matytschak |
571 | // (www.netdataobjects.de) |
572 | // |
573 | // Author: Mirko Matytschak |
574 | // |
575 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
576 | // documentation files (the ""Software""), to deal in the Software without restriction, including without limitation |
577 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
578 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
579 | // conditions: |
580 | |
581 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
582 | // of the Software. |
583 | // |
584 | // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
585 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
586 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
587 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
588 | // DEALINGS IN THE SOFTWARE. |
589 | |
590 | "); |
591 | ············sw.WriteLine( "using System;" ); |
592 | ············sw.WriteLine( "using System.Linq;" ); |
593 | ············sw.WriteLine( "using System.Reflection;" ); |
594 | ············sw.WriteLine( "using System.Diagnostics;" ); |
595 | ············sw.WriteLine( "using System.Collections;" ); |
596 | ············sw.WriteLine( "using System.Collections.Generic;" ); |
597 | ············sw.WriteLine( "using NDO;" ); |
598 | ············sw.WriteLine( "using NDO.Mapping;" ); |
599 | ············sw.WriteLine( "using NDO.Query;" ); |
600 | ············sw.WriteLine( "using NUnit.Framework;" ); |
601 | ············sw.WriteLine( "using RelationTestClasses;" ); |
602 | ············sw.WriteLine( "using NdoUnitTests;\n" ); |
603 | ············sw.WriteLine( "namespace " + nameSpace ); |
604 | ············sw.WriteLine( "{\n" ); |
605 | ············GeneratePmFactory(); |
606 | ············foreach (RelInfo ri in relInfos) |
607 | ················GenerateTestGroup( ri ); |
608 | ············sw.WriteLine( "\n}" ); |
609 | ············sw.Close(); |
610 | ········} |
611 | |
612 | ····} |
613 | } |
614 |
New Commit (36bb808)
1 | // |
2 | // Copyright (c) 2002-2016 Mirko Matytschak |
3 | // (www.netdataobjects.de) |
4 | // |
5 | // Author: Mirko Matytschak |
6 | // |
7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
11 | // conditions: |
12 | |
13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
14 | // of the Software. |
15 | // |
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. |
21 | |
22 | |
23 | using System; |
24 | using System.Collections; |
25 | using System.IO; |
26 | using CodeGenerator; |
27 | using System.Text; |
28 | using System.Collections.Generic; |
29 | |
30 | namespace TestGenerator |
31 | { |
32 | ····/// <summary> |
33 | ····/// Summary for TestGenerator. |
34 | ····/// </summary> |
35 | ····public class TestGenerator |
36 | ····{ |
37 | ········List<RelInfo> relInfos; |
38 | ········string fileName; |
39 | ········StreamWriter sw; |
40 | ········TestFixture fixture; |
41 | ········Test test; |
42 | ········readonly string nameSpace = "RelationUnitTests"; |
43 | |
44 | ········public TestGenerator( List<RelInfo> relInfos ) |
45 | ········{ |
46 | ············this.relInfos = relInfos; |
47 | ············fileName = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\..\UnitTests\UnitTests.cs" ); |
48 | ········} |
49 | |
50 | |
51 | ········void CreateTests( RelInfo ri ) |
52 | ········{ |
53 | ············CreateTestSaveReload( ri ); |
54 | ············CreateTestSaveReloadNull( ri ); |
55 | ············CreateTestSaveReloadRemove( ri ); |
56 | ············CreateTestChangeKeyHolderLeft( ri ); |
57 | ············CreateTestChangeKeyHolderRight( ri ); |
58 | ············CreateTestChangeKeyHolderLeftNoTouch( ri ); |
59 | ············CreateTestChangeKeyHolderRightNoTouch( ri ); |
60 | ············CreateTestUpdateOrder( ri ); |
61 | ············CreateTestRelationHash( ri ); |
62 | ············GenerateCreateObjects( ri ); |
63 | ············GenerateQueryOwn( ri ); |
64 | ············GenerateQueryOther( ri ); |
65 | ········} |
66 | |
67 | ········string AssertEquals( string text, object o2, object o3 ) |
68 | ········{ |
69 | ············return $"Assert.That({o2.ToString().ToLower()}, Is.EqualTo({o3}), \"{text}\");"; |
70 | ········} |
71 | ········string AssertNotNull( string text, object o ) |
72 | ········{ |
73 | ············return "Assert.That(" + o + ", Is.Not.Null, \"" + text + "\");"; |
74 | ········} |
75 | ········string AssertNull( string text, object o ) |
76 | ········{ |
77 | ············return "Assert.That(" + o + ", Is.Null, \"" + text + "\");"; |
78 | ········} |
79 | |
80 | ········string Assert( string text, object o ) |
81 | ········{ |
82 | ············return "Assert.That(" + o + ", \"" + text + "\");"; |
83 | ········} |
84 | |
85 | ········string QualifiedClassName( string className ) |
86 | ········{ |
87 | ············return nameSpace + "." + className; |
88 | ········} |
89 | |
90 | ········void CreateTestRelationHash( RelInfo ri ) |
91 | ········{ |
92 | ············if (!ri.IsBi) |
93 | ················return; |
94 | ············Function func = fixture.NewFunction( "void", "TestRelationHash" ); |
95 | ············func.Attributes.Add( "Test" ); |
96 | ············func.AccessModifier = "public"; |
97 | ············func.Statements.Add( "Class clbaseLeft = pm.NDOMapping.FindClass(typeof(" + test.OwnClass.Name + "));" ); |
98 | ············func.Statements.Add( "Relation relbaseLeft = clbaseLeft.FindRelation(\"relField\");" ); |
99 | ············func.Statements.Add( "Class clbaseRight = pm.NDOMapping.FindClass(typeof(" + test.OtherClass.Name + "));" ); |
100 | ············func.Statements.Add( "Relation relbaseRight = clbaseRight.FindRelation(\"relField\");" ); |
101 | ············func.Statements.Add( Assert( "Relation should be equal #1", "relbaseRight.Equals(relbaseLeft)" ) ); |
102 | ············func.Statements.Add( Assert( "Relation should be equal #2", "relbaseLeft.Equals(relbaseRight)" ) ); |
103 | ············if (ri.OwnPoly) |
104 | ············{ |
105 | ················func.Statements.Add( "Class clderLeft = pm.NDOMapping.FindClass(typeof(" + test.OwnDerivedClass.Name + "));" ); |
106 | ················func.Statements.Add( "Relation relderLeft = clderLeft.FindRelation(\"relField\");" ); |
107 | ················func.Statements.Add( Assert( "Relation should be equal #3", "relderLeft.Equals(relbaseRight)" ) ); |
108 | ················func.Statements.Add( Assert( "Relation should be equal #4", "relbaseRight.Equals(relderLeft)" ) ); |
109 | ············} |
110 | ············if (ri.OtherPoly) |
111 | ············{ |
112 | ················func.Statements.Add( "Class clderRight = pm.NDOMapping.FindClass(typeof(" + test.OtherDerivedClass.Name + "));" ); |
113 | ················func.Statements.Add( "Relation relderRight = clderRight.FindRelation(\"relField\");" ); |
114 | ················func.Statements.Add( Assert( "Relation should be equal #5", "relbaseLeft.Equals(relderRight)" ) ); |
115 | ················func.Statements.Add( Assert( "Relation should be equal #6", "relderRight.Equals(relbaseLeft)" ) ); |
116 | ················if (ri.OwnPoly) |
117 | ················{ |
118 | ····················func.Statements.Add( Assert( "Relation should be equal #7", "relderLeft.Equals(relderRight)" ) ); |
119 | ····················func.Statements.Add( Assert( "Relation should be equal #8", "relderRight.Equals(relderLeft)" ) ); |
120 | ················} |
121 | ············} |
122 | ········} |
123 | |
124 | ········bool IsForbiddenCase( RelInfo ri ) |
125 | ········{ |
126 | ············return ri.IsComposite && !ri.UseGuid && !ri.HasTable && !ri.IsList && ri.OtherPoly; |
127 | ········} |
128 | |
129 | ········void CreateTestSaveReload( RelInfo ri ) |
130 | ········{ |
131 | ············Function func = fixture.NewFunction( "void", "TestSaveReload" ); |
132 | ············func.Attributes.Add( "Test" ); |
133 | ············bool forbidden = IsForbiddenCase( ri ); |
134 | ············func.AccessModifier = "public"; |
135 | |
136 | ············if (forbidden) |
137 | ············{ |
138 | ················func.Statements.Add( "bool thrown = false;" ); |
139 | ················func.Statements.Add( "try" ); |
140 | ················func.Statements.Add( "{" ); |
141 | ············} |
142 | ············func.Statements.Add( "CreateObjects();" ); |
143 | ············func.Statements.Add( "QueryOwn();" ); |
144 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
145 | ············if (ri.IsList) |
146 | ················func.Statements.Add( AssertEquals( "Count wrong", 1, "ownVar.RelField.Count" ) ); |
147 | ············else |
148 | ················func.Statements.Add( AssertNotNull( "No related object", "ownVar.RelField" ) ); |
149 | ············if (forbidden) |
150 | ············{ |
151 | ················func.Statements.Add( "}" ); |
152 | ················func.Statements.Add( "catch (NDOException)" ); |
153 | ················func.Statements.Add( "{" ); |
154 | ················func.Statements.Add( "thrown = true;" ); |
155 | ················func.Statements.Add( "}" ); |
156 | ················func.Statements.Add( AssertEquals( "NDOException should have been thrown", true, "thrown" ) ); |
157 | ············} |
158 | ········} |
159 | |
160 | ········void CreateTestSaveReloadNull( RelInfo ri ) |
161 | ········{ |
162 | ············Function func = fixture.NewFunction( "void", "TestSaveReloadNull" ); |
163 | ············func.Attributes.Add( "Test" ); |
164 | ············bool forbidden = IsForbiddenCase( ri ); |
165 | ············func.AccessModifier = "public"; |
166 | |
167 | ············if (forbidden) |
168 | ············{ |
169 | ················func.Statements.Add( "bool thrown = false;" ); |
170 | ················func.Statements.Add( "try" ); |
171 | ················func.Statements.Add( "{" ); |
172 | ············} |
173 | ············func.Statements.Add( "CreateObjects();" ); |
174 | ············func.Statements.Add( "QueryOwn();" ); |
175 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
176 | |
177 | ············if (ri.IsList) |
178 | ················func.Statements.Add( AssertEquals( "Count wrong", 1, "ownVar.RelField.Count" ) ); |
179 | ············else |
180 | ················func.Statements.Add( AssertNotNull( "No related object", "ownVar.RelField" ) ); |
181 | |
182 | ············if (ri.IsList) |
183 | ················func.Statements.Add( "ownVar.RelField = new List<" + this.test.OtherClass.Name + ">();" ); |
184 | ············else |
185 | ················func.Statements.Add( "ownVar.RelField = null;" ); |
186 | ············func.Statements.Add( "pm.Save();" ); |
187 | ············func.Statements.Add( "pm.UnloadCache();" ); |
188 | ············func.Statements.Add( "QueryOwn();" ); |
189 | |
190 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
191 | |
192 | ············if (ri.IsList) |
193 | ················func.Statements.Add( AssertEquals( "Count wrong", 0, "ownVar.RelField.Count" ) ); |
194 | ············else |
195 | ················func.Statements.Add( AssertNull( "There should be no object", "ownVar.RelField" ) ); |
196 | |
197 | ············if (forbidden) |
198 | ············{ |
199 | ················func.Statements.Add( "}" ); |
200 | ················func.Statements.Add( "catch (NDOException)" ); |
201 | ················func.Statements.Add( "{" ); |
202 | ················func.Statements.Add( "thrown = true;" ); |
203 | ················func.Statements.Add( "}" ); |
204 | ················func.Statements.Add( AssertEquals( "NDOException should have been thrown", true, "thrown" ) ); |
205 | ············} |
206 | ········} |
207 | |
208 | ········void CreateTestSaveReloadRemove( RelInfo ri ) |
209 | ········{ |
210 | ············Function func = fixture.NewFunction( "void", "TestSaveReloadRemove" ); |
211 | ············func.AccessModifier = "public"; |
212 | ············func.Attributes.Add( "Test" ); |
213 | ············if (!ri.IsList) |
214 | ················return; |
215 | |
216 | ············bool forbidden = IsForbiddenCase( ri ); |
217 | ············if (forbidden) |
218 | ············{ |
219 | ················func.Statements.Add( "bool thrown = false;" ); |
220 | ················func.Statements.Add( "try" ); |
221 | ················func.Statements.Add( "{" ); |
222 | ············} |
223 | ············func.Statements.Add( "CreateObjects();" ); |
224 | ············func.Statements.Add( "QueryOwn();" ); |
225 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
226 | |
227 | ············if (ri.IsList) |
228 | ················func.Statements.Add( AssertEquals( "Count wrong", 1, "ownVar.RelField.Count" ) ); |
229 | ············else |
230 | ················func.Statements.Add( AssertNotNull( "No related object", "ownVar.RelField" ) ); |
231 | |
232 | ············func.Statements.Add( "ownVar.RemoveRelatedObject();" ); |
233 | ············func.Statements.Add( "pm.Save();" ); |
234 | ············func.Statements.Add( "pm.UnloadCache();" ); |
235 | ············func.Statements.Add( "QueryOwn();" ); |
236 | |
237 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
238 | |
239 | ············func.Statements.Add( AssertEquals( "Count wrong", 0, "ownVar.RelField.Count" ) ); |
240 | ············if (forbidden) |
241 | ············{ |
242 | ················func.Statements.Add( "}" ); |
243 | ················func.Statements.Add( "catch (NDOException)" ); |
244 | ················func.Statements.Add( "{" ); |
245 | ················func.Statements.Add( "thrown = true;" ); |
246 | ················func.Statements.Add( "}" ); |
247 | ················func.Statements.Add( AssertEquals( "NDOException should have been thrown", true, "thrown" ) ); |
248 | ············} |
249 | ········} |
250 | |
251 | ········void CreateTestChangeKeyHolderLeft( RelInfo ri ) |
252 | ········{ |
253 | ············if (IsForbiddenCase( ri )) |
254 | ················return; // These would throw exceptions |
255 | |
256 | ············// Check Keyholders only |
257 | ············if (ri.IsList) |
258 | ················return; |
259 | |
260 | ············Function func = fixture.NewFunction( "void", "TestChangeKeyHolderLeft" ); |
261 | ············func.Attributes.Add( "Test" ); |
262 | ············func.AccessModifier = "public"; |
263 | |
264 | ············func.Statements.Add( "CreateObjects();" ); |
265 | ············// 1:1 or n:1 - we check only the left side |
266 | ············func.Statements.Add( "QueryOwn();" ); |
267 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
268 | ············func.Statements.Add( AssertNotNull( "No related object", "ownVar.RelField" ) ); |
269 | ············// touch the related object |
270 | ············func.Statements.Add( "int x = ownVar.RelField.Dummy;" ); |
271 | ············// change our object |
272 | ············func.Statements.Add( "ownVar.Dummy = 4711;" ); |
273 | ············func.Statements.Add( "pm.Save();" ); |
274 | ············func.Statements.Add( "pm.UnloadCache();" ); |
275 | ············func.Statements.Add( "QueryOwn();" ); |
276 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
277 | ············func.Statements.Add( AssertNotNull( "Wrong value", "ownVar.Dummy == 4711" ) ); |
278 | ············func.Statements.Add( AssertNotNull( "No related object", "ownVar.RelField" ) ); |
279 | |
280 | ········} |
281 | |
282 | ········void CreateTestChangeKeyHolderRight( RelInfo ri ) |
283 | ········{ |
284 | ············if (IsForbiddenCase( ri )) |
285 | ················return; // These would throw exceptions |
286 | |
287 | ············// Check Keyholders only |
288 | ············if (ri.ForeignIsList || !ri.IsBi) |
289 | ················return; |
290 | |
291 | ············Function func = fixture.NewFunction( "void", "TestChangeKeyHolderRight" ); |
292 | ············func.Attributes.Add( "Test" ); |
293 | ············func.AccessModifier = "public"; |
294 | |
295 | ············func.Statements.Add( "CreateObjects();" ); |
296 | ············// 1:1 or n:1 - we check only the left side |
297 | ············func.Statements.Add( "QueryOther();" ); |
298 | ············func.Statements.Add( AssertNotNull( "No Query Result", "otherVar" ) ); |
299 | ············func.Statements.Add( AssertNotNull( "No related object", "otherVar.RelField" ) ); |
300 | ············// touch the related object |
301 | ············func.Statements.Add( "int x = otherVar.RelField.Dummy;" ); |
302 | ············// change our object |
303 | ············func.Statements.Add( "otherVar.Dummy = 4711;" ); |
304 | ············func.Statements.Add( "pm.Save();" ); |
305 | ············func.Statements.Add( "pm.UnloadCache();" ); |
306 | ············func.Statements.Add( "QueryOther();" ); |
307 | ············func.Statements.Add( AssertNotNull( "No Query Result", "otherVar" ) ); |
308 | ············func.Statements.Add( AssertNotNull( "Wrong value", "otherVar.Dummy == 4711" ) ); |
309 | ············func.Statements.Add( AssertNotNull( "No related object", "otherVar.RelField" ) ); |
310 | ········} |
311 | |
312 | ········void CreateTestChangeKeyHolderLeftNoTouch( RelInfo ri ) |
313 | ········{ |
314 | ············if (IsForbiddenCase( ri )) |
315 | ················return; // These would throw exceptions |
316 | |
317 | ············// Check Keyholders only |
318 | ············if (ri.IsList) |
319 | ················return; |
320 | |
321 | ············Function func = fixture.NewFunction( "void", "TestChangeKeyHolderLeftNoTouch" ); |
322 | ············func.Attributes.Add( "Test" ); |
323 | ············func.AccessModifier = "public"; |
324 | |
325 | ············func.Statements.Add( "CreateObjects();" ); |
326 | ············// 1:1 or n:1 - we check only the left side |
327 | ············func.Statements.Add( "QueryOwn();" ); |
328 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
329 | ············func.Statements.Add( AssertNotNull( "No related object", "ownVar.RelField" ) ); |
330 | ············// change our object |
331 | ············func.Statements.Add( "ownVar.Dummy = 4711;" ); |
332 | ············func.Statements.Add( "pm.Save();" ); |
333 | ············func.Statements.Add( "pm.UnloadCache();" ); |
334 | ············func.Statements.Add( "QueryOwn();" ); |
335 | ············func.Statements.Add( AssertNotNull( "No Query Result", "ownVar" ) ); |
336 | ············func.Statements.Add( AssertNotNull( "Wrong value", "ownVar.Dummy == 4711" ) ); |
337 | ············func.Statements.Add( AssertNotNull( "No related object", "ownVar.RelField" ) ); |
338 | |
339 | ········} |
340 | |
341 | ········void CreateTestChangeKeyHolderRightNoTouch( RelInfo ri ) |
342 | ········{ |
343 | ············if (IsForbiddenCase( ri )) |
344 | ················return; // These would throw exceptions |
345 | |
346 | ············// Check Keyholders only |
347 | ············if (ri.ForeignIsList || !ri.IsBi) |
348 | ················return; |
349 | |
350 | ············Function func = fixture.NewFunction( "void", "TestChangeKeyHolderRightNoTouch" ); |
351 | ············func.Attributes.Add( "Test" ); |
352 | ············func.AccessModifier = "public"; |
353 | |
354 | ············func.Statements.Add( "CreateObjects();" ); |
355 | ············// 1:1 or n:1 - we check only the left side |
356 | ············func.Statements.Add( "QueryOther();" ); |
357 | ············func.Statements.Add( AssertNotNull( "No Query Result", "otherVar" ) ); |
358 | ············func.Statements.Add( AssertNotNull( "No related object", "otherVar.RelField" ) ); |
359 | ············// change our object |
360 | ············func.Statements.Add( "otherVar.Dummy = 4711;" ); |
361 | ············func.Statements.Add( "pm.Save();" ); |
362 | ············func.Statements.Add( "pm.UnloadCache();" ); |
363 | ············func.Statements.Add( "QueryOther();" ); |
364 | ············func.Statements.Add( AssertNotNull( "No Query Result", "otherVar" ) ); |
365 | ············func.Statements.Add( AssertNotNull( "Wrong value", "otherVar.Dummy == 4711" ) ); |
366 | ············func.Statements.Add( AssertNotNull( "No related object", "otherVar.RelField" ) ); |
367 | ········} |
368 | |
369 | ········void CreateTestUpdateOrder( RelInfo ri ) |
370 | ········{ |
371 | ············if (ri.HasTable || ri.UseGuid || IsForbiddenCase( ri )) |
372 | ················return; |
373 | ············if (!ri.IsList && ri.IsBi && !ri.ForeignIsList) |
374 | ················return; |
375 | |
376 | ············Function func = fixture.NewFunction( "void", "TestUpdateOrder" ); |
377 | ············func.Attributes.Add( "Test" ); |
378 | ············func.AccessModifier = "public"; |
379 | |
380 | ············func.Statements.Add( "NDO.Mapping.NDOMapping mapping = pm.NDOMapping;" ); |
381 | ············func.Statements.Add( "MethodInfo mi = mapping.GetType().GetMethod(\"GetUpdateOrder\");" ); |
382 | ············string br = null; |
383 | ············if (!ri.IsList) |
384 | ················br = ">"; |
385 | ············else |
386 | ················br = "<"; |
387 | ············if ((!ri.OwnPoly && !ri.OtherPoly) || !ri.IsAbstract) |
388 | ············{ |
389 | ················func.Statements.Add( Assert( "Wrong order #1", @"((int)mi.Invoke(mapping, new object[]{typeof(" + test.OwnClass.Name + @")})) |
390 | ················" + br + " ((int)mi.Invoke(mapping, new object[]{typeof(" + test.OtherClass.Name + ")}))" ) ); |
391 | ············} |
392 | ············if (ri.OwnPoly && !ri.OtherPoly) |
393 | ············{ |
394 | ················func.Statements.Add( Assert( "Wrong order #2", @"((int)mi.Invoke(mapping, new object[]{typeof(" + test.OwnDerivedClass.Name + @")})) |
395 | ················" + br + " ((int)mi.Invoke(mapping, new object[]{typeof(" + test.OtherClass.Name + ")}))" ) ); |
396 | ············} |
397 | ············if (!ri.OwnPoly && ri.OtherPoly) |
398 | ············{ |
399 | ················func.Statements.Add( Assert( "Wrong order #2", @"((int)mi.Invoke(mapping, new object[]{typeof(" + test.OwnClass.Name + @")})) |
400 | ················" + br + " ((int)mi.Invoke(mapping, new object[]{typeof(" + test.OtherDerivedClass.Name + ")}))" ) ); |
401 | ············} |
402 | ············if (ri.OwnPoly && ri.OtherPoly) |
403 | ············{ |
404 | ················func.Statements.Add( Assert( "Wrong order #2", @"((int)mi.Invoke(mapping, new object[]{typeof(" + test.OwnDerivedClass.Name + @")})) |
405 | ················" + br + " ((int)mi.Invoke(mapping, new object[]{typeof(" + test.OtherDerivedClass.Name + ")}))" ) ); |
406 | ············} |
407 | ············func.Statements.Add( "Debug.WriteLine(\"" + test.OwnClass.Name + "\");" ); |
408 | |
409 | ········} |
410 | |
411 | ········void GenerateTearDown( RelInfo ri ) |
412 | ········{ |
413 | ············Function func = fixture.TearDown; |
414 | ············func.Statements.Add( "try" ); |
415 | ············func.Statements.Add( "{" ); |
416 | ············func.Statements.Add( "pm.UnloadCache();" ); |
417 | ············func.Statements.Add( "var l = pm.Objects<" + test.OwnClass.Name + ">().ResultTable;" ); |
418 | ············func.Statements.Add( "pm.Delete(l);" ); |
419 | ············func.Statements.Add( "pm.Save();" ); |
420 | ············func.Statements.Add( "pm.UnloadCache();" ); |
421 | ············if (!ri.IsComposite) |
422 | ············{ |
423 | ················func.Statements.Add( "var m = pm.Objects<" + test.OtherClass.Name + ">().ResultTable;" ); |
424 | ················func.Statements.Add( "pm.Delete(m);" ); |
425 | ················func.Statements.Add( "pm.Save();" ); |
426 | ················func.Statements.Add( "pm.UnloadCache();" ); |
427 | ············} |
428 | ············func.Statements.Add( "decimal count;" ); |
429 | ············func.Statements.Add( "count = (decimal) new " + NDOQuery( test.OwnClass.Name ) + ".ExecuteAggregate(\"dummy\", AggregateType.Count);" ); |
430 | ············func.Statements.Add( "Assert.That(0, Is.EqualTo(count), \"Count wrong #1\");" ); |
431 | ············func.Statements.Add( "count = (decimal) new " + NDOQuery( test.OtherClass.Name ) + ".ExecuteAggregate(\"dummy\", AggregateType.Count);" ); |
432 | ············func.Statements.Add( "Assert.That(0, Is.EqualTo(count), \"Count wrong #2\");" ); |
433 | ············func.Statements.Add( "}" ); |
434 | ············func.Statements.Add( "catch (Exception)" ); |
435 | ············func.Statements.Add( "{" ); |
436 | ············func.Statements.Add( "var handler = pm.GetSqlPassThroughHandler( pm.NDOMapping.Connections.First() );" ); |
437 | ············ |
438 | ············func.Statements.Add( "handler.Execute($\"DELETE FROM {pm.NDOMapping.FindClass( ownVar.GetType() ).TableName}\");" ); |
439 | ············func.Statements.Add( "handler.Execute($\"DELETE FROM {pm.NDOMapping.FindClass( otherVar.GetType() ).TableName}\");" ); |
440 | ············if (ri.HasTable) |
441 | ············{ |
442 | ················func.Statements.Add( "handler.Execute( $\"DELETE FROM {pm.NDOMapping.FindClass( ownVar.GetType() ).Relations.First().MappingTable.TableName}\" );" ); |
443 | ············} |
444 | ············func.Statements.Add( "}" ); |
445 | |
446 | ········} |
447 | |
448 | ········void GenerateTestGroup( RelInfo ri ) |
449 | ········{ |
450 | ············fixture = new TestFixture( this.nameSpace, "Test" + ri.ToString() ); |
451 | ············test = new Test( ri, "RelationTestClasses" ); |
452 | ············Class ownClass = null; |
453 | ············Class otherClass = null; |
454 | ············if (ri.OwnPoly) |
455 | ············{ |
456 | ················ownClass = test.OwnDerivedClass; |
457 | ············} |
458 | ············else |
459 | ············{ |
460 | ················ownClass = test.OwnClass; |
461 | ············} |
462 | ············if (ri.OtherPoly) |
463 | ············{ |
464 | ················otherClass = test.OtherDerivedClass; |
465 | ············} |
466 | ············else |
467 | ············{ |
468 | ················otherClass = test.OtherClass; |
469 | ············} |
470 | |
471 | |
472 | ············fixture.Statements.Add( test.OwnClass.Name + " ownVar;" ); |
473 | ············fixture.Statements.Add( test.OtherClass.Name + " otherVar;" );··// always use the base class type |
474 | ············fixture.Statements.Add( "PersistenceManager pm;" ); |
475 | |
476 | ············fixture.SetUp.Statements.Add( "pm = PmFactory.NewPersistenceManager();" ); |
477 | ············fixture.SetUp.Statements.Add( "ownVar = new " + ownClass.Name + "();" ); |
478 | ············fixture.SetUp.Statements.Add( "otherVar = new " + otherClass.Name + "();" ); |
479 | |
480 | ············GenerateTearDown( ri ); |
481 | |
482 | ············CreateTests( ri ); |
483 | |
484 | ············this.sw.WriteLine( fixture.ToString() ); |
485 | ········} |
486 | |
487 | ········void GeneratePmFactory() |
488 | ········{ |
489 | ············Class cl = new Class( this.nameSpace, "PmFactory" ); |
490 | ············string path = AppDomain.CurrentDomain.BaseDirectory; |
491 | path = Path. Combine( path, @". . \. . \. . \. . \UnitTests\bin\Debug\NDOMapping. xml" ) ; |
492 | ············path = Path.GetFullPath( path ); |
493 | ············cl.Statements.Add( "static PersistenceManager pm;" ); |
494 | ············Function func = cl.NewFunction( "PersistenceManager", "NewPersistenceManager" ); |
495 | ············func.IsStatic = true; |
496 | ············func.AccessModifier = "public"; |
497 | |
498 | ············func.Statements.Add( "if (pm == null)" ); |
499 | ············func.Statements.Add( "{" ); |
500 | ············func.Statements.Add( "pm = new PersistenceManager(@\"" + path + "\");" ); |
501 | ············func.Statements.Add( "}" ); |
502 | ············func.Statements.Add( "else" ); |
503 | ············func.Statements.Add( "{" ); |
504 | ············func.Statements.Add( "pm.UnloadCache();" ); |
505 | ············func.Statements.Add( "}" ); |
506 | ············func.Statements.Add( "return pm;" ); |
507 | ············sw.WriteLine( cl.ToString() ); |
508 | ········} |
509 | |
510 | ········void GenerateCreateObjects( RelInfo ri ) |
511 | ········{ |
512 | ············Function func = fixture.NewFunction( "void", "CreateObjects" ); |
513 | ············func.Statements.Add( "pm.MakePersistent(ownVar);" ); |
514 | ············string secondMakePersistent = "pm.MakePersistent(otherVar);"; |
515 | ············string assignRelation = "ownVar.AssignRelation(otherVar);"; |
516 | ············if (ri.IsComposite) |
517 | ············{ |
518 | ················if (!ri.IsList && (ri.OtherPoly || ri.OwnPoly) && !ri.HasTable && !ri.UseGuid) |
519 | ····················func.Statements.Add( "pm.Save();" ); |
520 | ················func.Statements.Add( assignRelation ); |
521 | ············} |
522 | ············else |
523 | ············{ |
524 | ················if (!ri.IsList && ri.OtherPoly && !ri.HasTable && !ri.UseGuid) |
525 | ····················func.Statements.Add( "pm.Save();" ); |
526 | ················func.Statements.Add( secondMakePersistent ); |
527 | ················if (!ri.IsList && ri.OtherPoly && !ri.HasTable && !ri.UseGuid) |
528 | ····················func.Statements.Add( "pm.Save();" ); |
529 | ················if (ri.IsBi && !ri.ForeignIsList && ri.OwnPoly && !ri.UseGuid) |
530 | ····················func.Statements.Add( "pm.Save();" ); |
531 | ················func.Statements.Add( assignRelation ); |
532 | ············} |
533 | ············func.Statements.Add( "pm.Save();" ); |
534 | ············func.Statements.Add( "pm.UnloadCache();" ); |
535 | ········} |
536 | |
537 | ········string NDOQuery( string className, string condition = null ) |
538 | ········{ |
539 | ············StringBuilder sb = new StringBuilder( "NDOQuery<" ); |
540 | ············sb.Append( className ); |
541 | ············sb.Append( ">(pm" ); |
542 | ············if (condition != null) |
543 | ············{ |
544 | ················sb.Append( "," ); |
545 | ················sb.Append( condition ); |
546 | ············} |
547 | ············sb.Append( ")" ); |
548 | ············return sb.ToString(); |
549 | ········} |
550 | |
551 | ········void GenerateQueryOwn( RelInfo ri ) |
552 | ········{ |
553 | ············Function func = fixture.NewFunction( "void", "QueryOwn" ); |
554 | ············func.Statements.Add( "var q = new " + NDOQuery(test.OwnClass.Name) + ';' ); |
555 | ············func.Statements.Add( "ownVar = q.ExecuteSingle();" ); |
556 | ········} |
557 | |
558 | ········void GenerateQueryOther( RelInfo ri ) |
559 | ········{ |
560 | ············Function func = fixture.NewFunction( "void", "QueryOther" ); |
561 | ············func.Statements.Add( "var q = new " + NDOQuery( test.OtherClass.Name ) + ';' ); |
562 | ············func.Statements.Add( "otherVar = q.ExecuteSingle();" ); |
563 | ········} |
564 | |
565 | |
566 | ········public void Generate() |
567 | ········{ |
568 | ············sw = new StreamWriter( fileName ); |
569 | ············sw.WriteLine( @"// |
570 | // Copyright (c) 2002-2016 Mirko Matytschak |
571 | // (www.netdataobjects.de) |
572 | // |
573 | // Author: Mirko Matytschak |
574 | // |
575 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
576 | // documentation files (the ""Software""), to deal in the Software without restriction, including without limitation |
577 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
578 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
579 | // conditions: |
580 | |
581 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
582 | // of the Software. |
583 | // |
584 | // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
585 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
586 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
587 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
588 | // DEALINGS IN THE SOFTWARE. |
589 | |
590 | "); |
591 | ············sw.WriteLine( "using System;" ); |
592 | ············sw.WriteLine( "using System.Linq;" ); |
593 | ············sw.WriteLine( "using System.Reflection;" ); |
594 | ············sw.WriteLine( "using System.Diagnostics;" ); |
595 | ············sw.WriteLine( "using System.Collections;" ); |
596 | ············sw.WriteLine( "using System.Collections.Generic;" ); |
597 | ············sw.WriteLine( "using NDO;" ); |
598 | ············sw.WriteLine( "using NDO.Mapping;" ); |
599 | ············sw.WriteLine( "using NDO.Query;" ); |
600 | ············sw.WriteLine( "using NUnit.Framework;" ); |
601 | ············sw.WriteLine( "using RelationTestClasses;" ); |
602 | ············sw.WriteLine( "using NdoUnitTests;\n" ); |
603 | ············sw.WriteLine( "namespace " + nameSpace ); |
604 | ············sw.WriteLine( "{\n" ); |
605 | ············GeneratePmFactory(); |
606 | ············foreach (RelInfo ri in relInfos) |
607 | ················GenerateTestGroup( ri ); |
608 | ············sw.WriteLine( "\n}" ); |
609 | ············sw.Close(); |
610 | ········} |
611 | |
612 | ····} |
613 | } |
614 |