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