Datei: UnitTestGenerator/TestGenerator/TestGenerator.cs

Last Commit (72f6226)
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 (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