Datei: UnitTestGenerator/TestGenerator/MappingTestGenerator.cs
Last Commit (6d63e12)
| 1 | // |
| 2 | // Copyright (c) 2002-2016 Mirko Matytschak |
| 3 | // (www.netdataobjects.de) |
| 4 | // |
| 5 | // Author: Mirko Matytschak |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 11 | // conditions: |
| 12 | |
| 13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 14 | // of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | // DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | |
| 23 | using System; |
| 24 | using System.Collections; |
| 25 | using System.IO; |
| 26 | using CodeGenerator; |
| 27 | using System.Text; |
| 28 | using System.Collections.Generic; |
| 29 | |
| 30 | namespace TestGenerator |
| 31 | { |
| 32 | ····/// <summary> |
| 33 | ····/// Summary for TestGenerator. |
| 34 | ····/// </summary> |
| 35 | ····public class MappingTestGenerator |
| 36 | ····{ |
| 37 | ········List<RelInfo> relInfos; |
| 38 | ········string fileName; |
| 39 | ········StreamWriter sw; |
| 40 | ········TestFixture fixture; |
| 41 | ········Test test; |
| 42 | ········readonly string nameSpace = "MappingUnitTests"; |
| 43 | |
| 44 | ········public MappingTestGenerator( List<RelInfo> relInfos ) |
| 45 | ········{ |
| 46 | ············this.relInfos = relInfos; |
| 47 | ············fileName = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\..\UnitTests\MappingUnitTests.cs" ); |
| 48 | ········} |
| 49 | |
| 50 | |
| 51 | ········void CreateTests( RelInfo ri ) |
| 52 | ········{ |
| 53 | ············CreateTestHasMappingTable( ri ); |
| 54 | ············CreateTestHasTypeColumn( ri ); |
| 55 | ············CreateTestHasTypeCode( ri ); |
| 56 | ········} |
| 57 | |
| 58 | |
| 59 | ········string AssertEquals( string text, object o2, object o3 ) |
| 60 | ········{ |
| 61 | ············return $"Assert.That({o2.ToString().ToLower()}, Is.EqualTo({o3}), \"{text}\");"; |
| 62 | ········} |
| 63 | ········string AssertNotNull( string text, object o ) |
| 64 | ········{ |
| 65 | ············return "Assert.That(" + o + ", Is.Not.Null, \"" + text + "\");"; |
| 66 | ········} |
| 67 | ········string AssertNull( string text, object o ) |
| 68 | ········{ |
| 69 | ············return "Assert.That(" + o + ", Is.Null, \"" + text + "\");"; |
| 70 | ········} |
| 71 | |
| 72 | ········string Assert( string text, object o ) |
| 73 | ········{ |
| 74 | ············return "Assert.That(" + o + ", \"" + text + "\");"; |
| 75 | ········} |
| 76 | |
| 77 | ········string QualifiedClassName( string className ) |
| 78 | ········{ |
| 79 | ············return nameSpace + "." + className; |
| 80 | ········} |
| 81 | |
| 82 | ········void CreateTestHasTypeCode( RelInfo ri ) |
| 83 | ········{ |
| 84 | ············Function func = fixture.NewFunction( "void", "HasTypeCode" ); |
| 85 | ············func.Attributes.Add( "Test" ); |
| 86 | ············func.AccessModifier = "public"; |
| 87 | ············if (!(ri.IsAbstract && ri.OwnPoly)) // abstract classes don't need a type code |
| 88 | ················func.Statements.Add( Assert("Class should have a Type Code #1", "this.ownClass.TypeCode != 0" ) ); |
| 89 | ············if (!(ri.IsAbstract && ri.OtherPoly)) |
| 90 | ················func.Statements.Add( Assert( "Class should have a Type Code #2", "this.otherClass.TypeCode != 0" ) ); |
| 91 | ············if (ri.OwnPoly) |
| 92 | ················func.Statements.Add( Assert( "Class should have a Type Code #3", "this.ownDerivedClass.TypeCode != 0" ) ); |
| 93 | ············if (ri.OtherPoly) |
| 94 | ················func.Statements.Add( Assert( "Class should have a Type Code #4", "this.otherDerivedClass.TypeCode != 0" ) ); |
| 95 | ········} |
| 96 | |
| 97 | ········void CreateTestHasMappingTable( RelInfo ri ) |
| 98 | ········{ |
| 99 | ············Function func = fixture.NewFunction( "void", "HasMappingTable" ); |
| 100 | ············func.Attributes.Add( "Test" ); |
| 101 | ············func.AccessModifier = "public"; |
| 102 | ············if (ri.HasTable) |
| 103 | ············{ |
| 104 | ················func.Statements.Add( AssertNotNull( "Relation should have a MappingTable #1", "ownClass.Relations.First().MappingTable" ) ); |
| 105 | ················if (ri.OwnPoly) |
| 106 | ····················func.Statements.Add( AssertNotNull( "Relation should have a MappingTable #2", "ownDerivedClass.Relations.First().MappingTable" ) ); |
| 107 | ················if (ri.IsBi) |
| 108 | ················{ |
| 109 | ····················func.Statements.Add( AssertNotNull( "Relation should have a MappingTable #3", "otherClass.Relations.First().MappingTable" ) ); |
| 110 | ····················if (ri.OtherPoly) |
| 111 | ························func.Statements.Add( AssertNotNull( "Relation should have a MappingTable #4", "otherDerivedClass.Relations.First().MappingTable" ) ); |
| 112 | ················} |
| 113 | ············} |
| 114 | ············else |
| 115 | ············{ |
| 116 | ················func.Statements.Add( AssertNull( "Relation shouldn't have a MappingTable #5", "ownClass.Relations.First().MappingTable" ) ); |
| 117 | ················if (ri.OwnPoly) |
| 118 | ····················func.Statements.Add( AssertNull( "Relation shouldn't have a MappingTable #6", "ownDerivedClass.Relations.First().MappingTable" ) ); |
| 119 | ················if (ri.IsBi) |
| 120 | ················{ |
| 121 | ····················func.Statements.Add( AssertNull( "Relation shouldn't have a MappingTable #7", "otherClass.Relations.First().MappingTable" ) ); |
| 122 | ····················if (ri.OtherPoly) |
| 123 | ························func.Statements.Add( AssertNull( "Relation shouldn't have a MappingTable #8", "otherDerivedClass.Relations.First().MappingTable" ) ); |
| 124 | ················} |
| 125 | ············} |
| 126 | ········} |
| 127 | |
| 128 | ········void CreateTestHasTypeColumn( RelInfo ri ) |
| 129 | ········{ |
| 130 | ············Function func = fixture.NewFunction( "void", "HasTypeColumn" ); |
| 131 | ············func.Attributes.Add( "Test" ); |
| 132 | ············func.AccessModifier = "public"; |
| 133 | ············if (ri.HasTable) |
| 134 | ············{ |
| 135 | ················if (ri.OwnPoly) |
| 136 | ················{ |
| 137 | ····················func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #1", "ownClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 138 | ····················func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #2", "ownDerivedClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 139 | ················} |
| 140 | ················if (ri.OtherPoly) |
| 141 | ················{ |
| 142 | ····················func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #3", "ownClass.Relations.First().MappingTable.ChildForeignKeyTypeColumnName" ) ); |
| 143 | ····················if (ri.OwnPoly) |
| 144 | ························func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #4", "ownDerivedClass.Relations.First().MappingTable.ChildForeignKeyTypeColumnName" ) ); |
| 145 | ················} |
| 146 | ················if (ri.IsBi) |
| 147 | ················{ |
| 148 | ····················if (ri.OtherPoly) |
| 149 | ····················{ |
| 150 | ························func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #5", "otherClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 151 | ························func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #6", "otherDerivedClass.Relations.First().ForeignKeyTypeColumnName" ) );························ |
| 152 | ····················} |
| 153 | ····················if (ri.OwnPoly) |
| 154 | ····················{ |
| 155 | ························func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #7", "otherClass.Relations.First().MappingTable.ChildForeignKeyTypeColumnName" ) ); |
| 156 | ························if (ri.OtherPoly) |
| 157 | ····························func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #8", "otherDerivedClass.Relations.First().MappingTable.ChildForeignKeyTypeColumnName" ) ); |
| 158 | ····················} |
| 159 | ················} |
| 160 | ················if (!ri.OwnPoly) |
| 161 | ················{ |
| 162 | ····················func.Statements.Add( AssertNull( "Relation shouldn't have a TypeColumn #1", "ownClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 163 | ····················if (ri.IsBi) |
| 164 | ····················{ |
| 165 | ························func.Statements.Add( AssertNull( "Relation shouldn't have a TypeColumn #2", "otherClass.Relations.First().MappingTable.ChildForeignKeyTypeColumnName" ) ); |
| 166 | ····················} |
| 167 | ················} |
| 168 | ················if (!ri.OtherPoly) |
| 169 | ················{ |
| 170 | ····················func.Statements.Add( AssertNull( "Relation shouldn't have a TypeColumn #3", "ownClass.Relations.First().MappingTable.ChildForeignKeyTypeColumnName" ) ); |
| 171 | ····················if (ri.IsBi) |
| 172 | ····················{ |
| 173 | ························func.Statements.Add( AssertNull( "Relation shouldn't have a TypeColumn #4", "otherClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 174 | ····················} |
| 175 | ················} |
| 176 | ············} |
| 177 | ············else··// No Mapping Table |
| 178 | ············{ |
| 179 | ················if (!ri.OtherPoly) |
| 180 | ····················func.Statements.Add( AssertNull( "Relation shouldn't have a TypeColumn #1", "ownClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 181 | |
| 182 | ················// Polymorphic 1:n relations always have a mapping table, |
| 183 | ················// so we check only the 1:1 relations. |
| 184 | ················if (!ri.IsList) |
| 185 | ················{ |
| 186 | ····················if (ri.OtherPoly) |
| 187 | ························func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #9", "ownClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 188 | ····················if (ri.IsBi && ri.OwnPoly) |
| 189 | ························func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #10", "otherClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 190 | ················} |
| 191 | ············} |
| 192 | ········} |
| 193 | |
| 194 | ········bool IsForbiddenCase( RelInfo ri ) |
| 195 | ········{ |
| 196 | ············return ri.IsComposite && !ri.UseGuid && !ri.HasTable && !ri.IsList && ri.OtherPoly; |
| 197 | ········} |
| 198 | |
| 199 | |
| 200 | ········public void Generate() |
| 201 | ········{ |
| 202 | ············sw = new StreamWriter( fileName ); |
| 203 | ············sw.WriteLine( @"// |
| 204 | // Copyright (c) 2002-2019 Mirko Matytschak |
| 205 | // (www.netdataobjects.de) |
| 206 | // |
| 207 | // Author: Mirko Matytschak |
| 208 | // |
| 209 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 210 | // documentation files (the ""Software""), to deal in the Software without restriction, including without limitation |
| 211 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 212 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 213 | // conditions: |
| 214 | |
| 215 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 216 | // of the Software. |
| 217 | // |
| 218 | // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 219 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 220 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 221 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 222 | // DEALINGS IN THE SOFTWARE. |
| 223 | |
| 224 | "); |
| 225 | ············sw.WriteLine( "using System;" ); |
| 226 | ············sw.WriteLine( "using System.Linq;" ); |
| 227 | ············sw.WriteLine( "using System.Diagnostics;" ); |
| 228 | ············sw.WriteLine( "using System.Collections;" ); |
| 229 | ············sw.WriteLine( "using System.Collections.Generic;" ); |
| 230 | ············sw.WriteLine( "using NDO;" ); |
| 231 | ············sw.WriteLine( "using NDO.Mapping;" ); |
| 232 | ············sw.WriteLine( "using NUnit.Framework;" ); |
| 233 | ············sw.WriteLine( "using NdoUnitTests;\n" ); |
| 234 | |
| 235 | ············sw.WriteLine( "namespace " + nameSpace ); |
| 236 | ············sw.WriteLine( "{\n" ); |
| 237 | ············GeneratePmFactory(); |
| 238 | ············foreach (RelInfo ri in relInfos) |
| 239 | ············{ |
| 240 | ················if (IsForbiddenCase( ri )) |
| 241 | ····················continue; |
| 242 | |
| 243 | ················GenerateTestGroup( ri ); |
| 244 | ············} |
| 245 | ············sw.WriteLine( "\n}" ); |
| 246 | ············sw.Close(); |
| 247 | ········} |
| 248 | |
| 249 | ········void GenerateTestGroup( RelInfo ri ) |
| 250 | ········{ |
| 251 | ············fixture = new TestFixture( this.nameSpace, "MappingTest" + ri.ToString() ); |
| 252 | ············test = new Test( ri, "RelationTestClasses" ); |
| 253 | ············fixture.AddField( "PersistenceManager", "pm" ); |
| 254 | ············fixture.AddField( "NDOMapping", "mapping" ); |
| 255 | ············fixture.AddField( "Class", "ownClass" ); |
| 256 | ············fixture.AddField( "Class", "otherClass" ); |
| 257 | ············if (ri.OwnPoly) |
| 258 | ················fixture.AddField( "Class", "ownDerivedClass" ); |
| 259 | ············if (ri.OtherPoly) |
| 260 | ················fixture.AddField( "Class", "otherDerivedClass" ); |
| 261 | |
| 262 | ············fixture.SetUp.Statements.Add( "this.pm = PmFactory.NewPersistenceManager();" ); |
| 263 | ············fixture.SetUp.Statements.Add( "this.mapping = pm.NDOMapping;" ); |
| 264 | ············fixture.SetUp.Statements.Add( $"this.ownClass = this.mapping.FindClass( \"{test.OwnClass.FullName}\" );" ); |
| 265 | ············fixture.SetUp.Statements.Add( $"this.otherClass = this.mapping.FindClass( \"{test.OtherClass.FullName}\" );" ); |
| 266 | ············if (ri.OwnPoly) |
| 267 | ············fixture.SetUp.Statements.Add( $"this.ownDerivedClass = this.mapping.FindClass( \"{test.OwnDerivedClass.FullName}\" );" ); |
| 268 | ············if (ri.OtherPoly) |
| 269 | ············fixture.SetUp.Statements.Add( $"this.otherDerivedClass = this.mapping.FindClass( \"{test.OtherDerivedClass.FullName}\" );" ); |
| 270 | |
| 271 | ············//GenerateTearDown( ri ); |
| 272 | |
| 273 | ············CreateTests( ri ); |
| 274 | |
| 275 | ············this.sw.WriteLine( fixture.ToString() ); |
| 276 | ········} |
| 277 | |
| 278 | ········void GeneratePmFactory() |
| 279 | ········{ |
| 280 | ············Class cl = new Class( this.nameSpace, "PmFactory" ); |
| 281 | ············string path = AppDomain.CurrentDomain.BaseDirectory; |
| 282 | path = Path. Combine( path, @". . \. . \UnitTests\bin\Debug\NDOMapping. xml" ) ; |
| 283 | ············path = Path.GetFullPath( path ); |
| 284 | ············cl.Statements.Add( "static PersistenceManager pm;" ); |
| 285 | ············Function func = cl.NewFunction( "PersistenceManager", "NewPersistenceManager" ); |
| 286 | ············func.IsStatic = true; |
| 287 | ············func.AccessModifier = "public"; |
| 288 | |
| 289 | ············func.Statements.Add( "if (pm == null)" ); |
| 290 | ············func.Statements.Add( "{" ); |
| 291 | ············func.Statements.Add( "pm = new PersistenceManager(@\"" + path + "\");" ); |
| 292 | ············path = Path.GetFullPath( Path.Combine( path, @"..\..\.." ) ); |
| 293 | ············func.Statements.Add( "}" ); |
| 294 | ············func.Statements.Add( "else" ); |
| 295 | ············func.Statements.Add( "{" ); |
| 296 | ············func.Statements.Add( "pm.UnloadCache();" ); |
| 297 | ············func.Statements.Add( "}" ); |
| 298 | ············func.Statements.Add( "return pm;" ); |
| 299 | ············sw.WriteLine( cl.ToString() ); |
| 300 | ········} |
| 301 | |
| 302 | ····} |
| 303 | } |
| 304 |
New Commit (36bb808)
| 1 | // |
| 2 | // Copyright (c) 2002-2016 Mirko Matytschak |
| 3 | // (www.netdataobjects.de) |
| 4 | // |
| 5 | // Author: Mirko Matytschak |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 11 | // conditions: |
| 12 | |
| 13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 14 | // of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | // DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | |
| 23 | using System; |
| 24 | using System.Collections; |
| 25 | using System.IO; |
| 26 | using CodeGenerator; |
| 27 | using System.Text; |
| 28 | using System.Collections.Generic; |
| 29 | |
| 30 | namespace TestGenerator |
| 31 | { |
| 32 | ····/// <summary> |
| 33 | ····/// Summary for TestGenerator. |
| 34 | ····/// </summary> |
| 35 | ····public class MappingTestGenerator |
| 36 | ····{ |
| 37 | ········List<RelInfo> relInfos; |
| 38 | ········string fileName; |
| 39 | ········StreamWriter sw; |
| 40 | ········TestFixture fixture; |
| 41 | ········Test test; |
| 42 | ········readonly string nameSpace = "MappingUnitTests"; |
| 43 | |
| 44 | ········public MappingTestGenerator( List<RelInfo> relInfos ) |
| 45 | ········{ |
| 46 | ············this.relInfos = relInfos; |
| 47 | ············fileName = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, @"..\..\..\..\UnitTests\MappingUnitTests.cs" ); |
| 48 | ········} |
| 49 | |
| 50 | |
| 51 | ········void CreateTests( RelInfo ri ) |
| 52 | ········{ |
| 53 | ············CreateTestHasMappingTable( ri ); |
| 54 | ············CreateTestHasTypeColumn( ri ); |
| 55 | ············CreateTestHasTypeCode( ri ); |
| 56 | ········} |
| 57 | |
| 58 | |
| 59 | ········string AssertEquals( string text, object o2, object o3 ) |
| 60 | ········{ |
| 61 | ············return $"Assert.That({o2.ToString().ToLower()}, Is.EqualTo({o3}), \"{text}\");"; |
| 62 | ········} |
| 63 | ········string AssertNotNull( string text, object o ) |
| 64 | ········{ |
| 65 | ············return "Assert.That(" + o + ", Is.Not.Null, \"" + text + "\");"; |
| 66 | ········} |
| 67 | ········string AssertNull( string text, object o ) |
| 68 | ········{ |
| 69 | ············return "Assert.That(" + o + ", Is.Null, \"" + text + "\");"; |
| 70 | ········} |
| 71 | |
| 72 | ········string Assert( string text, object o ) |
| 73 | ········{ |
| 74 | ············return "Assert.That(" + o + ", \"" + text + "\");"; |
| 75 | ········} |
| 76 | |
| 77 | ········string QualifiedClassName( string className ) |
| 78 | ········{ |
| 79 | ············return nameSpace + "." + className; |
| 80 | ········} |
| 81 | |
| 82 | ········void CreateTestHasTypeCode( RelInfo ri ) |
| 83 | ········{ |
| 84 | ············Function func = fixture.NewFunction( "void", "HasTypeCode" ); |
| 85 | ············func.Attributes.Add( "Test" ); |
| 86 | ············func.AccessModifier = "public"; |
| 87 | ············if (!(ri.IsAbstract && ri.OwnPoly)) // abstract classes don't need a type code |
| 88 | ················func.Statements.Add( Assert("Class should have a Type Code #1", "this.ownClass.TypeCode != 0" ) ); |
| 89 | ············if (!(ri.IsAbstract && ri.OtherPoly)) |
| 90 | ················func.Statements.Add( Assert( "Class should have a Type Code #2", "this.otherClass.TypeCode != 0" ) ); |
| 91 | ············if (ri.OwnPoly) |
| 92 | ················func.Statements.Add( Assert( "Class should have a Type Code #3", "this.ownDerivedClass.TypeCode != 0" ) ); |
| 93 | ············if (ri.OtherPoly) |
| 94 | ················func.Statements.Add( Assert( "Class should have a Type Code #4", "this.otherDerivedClass.TypeCode != 0" ) ); |
| 95 | ········} |
| 96 | |
| 97 | ········void CreateTestHasMappingTable( RelInfo ri ) |
| 98 | ········{ |
| 99 | ············Function func = fixture.NewFunction( "void", "HasMappingTable" ); |
| 100 | ············func.Attributes.Add( "Test" ); |
| 101 | ············func.AccessModifier = "public"; |
| 102 | ············if (ri.HasTable) |
| 103 | ············{ |
| 104 | ················func.Statements.Add( AssertNotNull( "Relation should have a MappingTable #1", "ownClass.Relations.First().MappingTable" ) ); |
| 105 | ················if (ri.OwnPoly) |
| 106 | ····················func.Statements.Add( AssertNotNull( "Relation should have a MappingTable #2", "ownDerivedClass.Relations.First().MappingTable" ) ); |
| 107 | ················if (ri.IsBi) |
| 108 | ················{ |
| 109 | ····················func.Statements.Add( AssertNotNull( "Relation should have a MappingTable #3", "otherClass.Relations.First().MappingTable" ) ); |
| 110 | ····················if (ri.OtherPoly) |
| 111 | ························func.Statements.Add( AssertNotNull( "Relation should have a MappingTable #4", "otherDerivedClass.Relations.First().MappingTable" ) ); |
| 112 | ················} |
| 113 | ············} |
| 114 | ············else |
| 115 | ············{ |
| 116 | ················func.Statements.Add( AssertNull( "Relation shouldn't have a MappingTable #5", "ownClass.Relations.First().MappingTable" ) ); |
| 117 | ················if (ri.OwnPoly) |
| 118 | ····················func.Statements.Add( AssertNull( "Relation shouldn't have a MappingTable #6", "ownDerivedClass.Relations.First().MappingTable" ) ); |
| 119 | ················if (ri.IsBi) |
| 120 | ················{ |
| 121 | ····················func.Statements.Add( AssertNull( "Relation shouldn't have a MappingTable #7", "otherClass.Relations.First().MappingTable" ) ); |
| 122 | ····················if (ri.OtherPoly) |
| 123 | ························func.Statements.Add( AssertNull( "Relation shouldn't have a MappingTable #8", "otherDerivedClass.Relations.First().MappingTable" ) ); |
| 124 | ················} |
| 125 | ············} |
| 126 | ········} |
| 127 | |
| 128 | ········void CreateTestHasTypeColumn( RelInfo ri ) |
| 129 | ········{ |
| 130 | ············Function func = fixture.NewFunction( "void", "HasTypeColumn" ); |
| 131 | ············func.Attributes.Add( "Test" ); |
| 132 | ············func.AccessModifier = "public"; |
| 133 | ············if (ri.HasTable) |
| 134 | ············{ |
| 135 | ················if (ri.OwnPoly) |
| 136 | ················{ |
| 137 | ····················func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #1", "ownClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 138 | ····················func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #2", "ownDerivedClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 139 | ················} |
| 140 | ················if (ri.OtherPoly) |
| 141 | ················{ |
| 142 | ····················func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #3", "ownClass.Relations.First().MappingTable.ChildForeignKeyTypeColumnName" ) ); |
| 143 | ····················if (ri.OwnPoly) |
| 144 | ························func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #4", "ownDerivedClass.Relations.First().MappingTable.ChildForeignKeyTypeColumnName" ) ); |
| 145 | ················} |
| 146 | ················if (ri.IsBi) |
| 147 | ················{ |
| 148 | ····················if (ri.OtherPoly) |
| 149 | ····················{ |
| 150 | ························func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #5", "otherClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 151 | ························func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #6", "otherDerivedClass.Relations.First().ForeignKeyTypeColumnName" ) );························ |
| 152 | ····················} |
| 153 | ····················if (ri.OwnPoly) |
| 154 | ····················{ |
| 155 | ························func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #7", "otherClass.Relations.First().MappingTable.ChildForeignKeyTypeColumnName" ) ); |
| 156 | ························if (ri.OtherPoly) |
| 157 | ····························func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #8", "otherDerivedClass.Relations.First().MappingTable.ChildForeignKeyTypeColumnName" ) ); |
| 158 | ····················} |
| 159 | ················} |
| 160 | ················if (!ri.OwnPoly) |
| 161 | ················{ |
| 162 | ····················func.Statements.Add( AssertNull( "Relation shouldn't have a TypeColumn #1", "ownClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 163 | ····················if (ri.IsBi) |
| 164 | ····················{ |
| 165 | ························func.Statements.Add( AssertNull( "Relation shouldn't have a TypeColumn #2", "otherClass.Relations.First().MappingTable.ChildForeignKeyTypeColumnName" ) ); |
| 166 | ····················} |
| 167 | ················} |
| 168 | ················if (!ri.OtherPoly) |
| 169 | ················{ |
| 170 | ····················func.Statements.Add( AssertNull( "Relation shouldn't have a TypeColumn #3", "ownClass.Relations.First().MappingTable.ChildForeignKeyTypeColumnName" ) ); |
| 171 | ····················if (ri.IsBi) |
| 172 | ····················{ |
| 173 | ························func.Statements.Add( AssertNull( "Relation shouldn't have a TypeColumn #4", "otherClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 174 | ····················} |
| 175 | ················} |
| 176 | ············} |
| 177 | ············else··// No Mapping Table |
| 178 | ············{ |
| 179 | ················if (!ri.OtherPoly) |
| 180 | ····················func.Statements.Add( AssertNull( "Relation shouldn't have a TypeColumn #1", "ownClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 181 | |
| 182 | ················// Polymorphic 1:n relations always have a mapping table, |
| 183 | ················// so we check only the 1:1 relations. |
| 184 | ················if (!ri.IsList) |
| 185 | ················{ |
| 186 | ····················if (ri.OtherPoly) |
| 187 | ························func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #9", "ownClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 188 | ····················if (ri.IsBi && ri.OwnPoly) |
| 189 | ························func.Statements.Add( AssertNotNull( "Relation should have a TypeColumn #10", "otherClass.Relations.First().ForeignKeyTypeColumnName" ) ); |
| 190 | ················} |
| 191 | ············} |
| 192 | ········} |
| 193 | |
| 194 | ········bool IsForbiddenCase( RelInfo ri ) |
| 195 | ········{ |
| 196 | ············return ri.IsComposite && !ri.UseGuid && !ri.HasTable && !ri.IsList && ri.OtherPoly; |
| 197 | ········} |
| 198 | |
| 199 | |
| 200 | ········public void Generate() |
| 201 | ········{ |
| 202 | ············sw = new StreamWriter( fileName ); |
| 203 | ············sw.WriteLine( @"// |
| 204 | // Copyright (c) 2002-2019 Mirko Matytschak |
| 205 | // (www.netdataobjects.de) |
| 206 | // |
| 207 | // Author: Mirko Matytschak |
| 208 | // |
| 209 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 210 | // documentation files (the ""Software""), to deal in the Software without restriction, including without limitation |
| 211 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 212 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 213 | // conditions: |
| 214 | |
| 215 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 216 | // of the Software. |
| 217 | // |
| 218 | // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 219 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 220 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 221 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 222 | // DEALINGS IN THE SOFTWARE. |
| 223 | |
| 224 | "); |
| 225 | ············sw.WriteLine( "using System;" ); |
| 226 | ············sw.WriteLine( "using System.Linq;" ); |
| 227 | ············sw.WriteLine( "using System.Diagnostics;" ); |
| 228 | ············sw.WriteLine( "using System.Collections;" ); |
| 229 | ············sw.WriteLine( "using System.Collections.Generic;" ); |
| 230 | ············sw.WriteLine( "using NDO;" ); |
| 231 | ············sw.WriteLine( "using NDO.Mapping;" ); |
| 232 | ············sw.WriteLine( "using NUnit.Framework;" ); |
| 233 | ············sw.WriteLine( "using NdoUnitTests;\n" ); |
| 234 | |
| 235 | ············sw.WriteLine( "namespace " + nameSpace ); |
| 236 | ············sw.WriteLine( "{\n" ); |
| 237 | ············GeneratePmFactory(); |
| 238 | ············foreach (RelInfo ri in relInfos) |
| 239 | ············{ |
| 240 | ················if (IsForbiddenCase( ri )) |
| 241 | ····················continue; |
| 242 | |
| 243 | ················GenerateTestGroup( ri ); |
| 244 | ············} |
| 245 | ············sw.WriteLine( "\n}" ); |
| 246 | ············sw.Close(); |
| 247 | ········} |
| 248 | |
| 249 | ········void GenerateTestGroup( RelInfo ri ) |
| 250 | ········{ |
| 251 | ············fixture = new TestFixture( this.nameSpace, "MappingTest" + ri.ToString() ); |
| 252 | ············test = new Test( ri, "RelationTestClasses" ); |
| 253 | ············fixture.AddField( "PersistenceManager", "pm" ); |
| 254 | ············fixture.AddField( "NDOMapping", "mapping" ); |
| 255 | ············fixture.AddField( "Class", "ownClass" ); |
| 256 | ············fixture.AddField( "Class", "otherClass" ); |
| 257 | ············if (ri.OwnPoly) |
| 258 | ················fixture.AddField( "Class", "ownDerivedClass" ); |
| 259 | ············if (ri.OtherPoly) |
| 260 | ················fixture.AddField( "Class", "otherDerivedClass" ); |
| 261 | |
| 262 | ············fixture.SetUp.Statements.Add( "this.pm = PmFactory.NewPersistenceManager();" ); |
| 263 | ············fixture.SetUp.Statements.Add( "this.mapping = pm.NDOMapping;" ); |
| 264 | ············fixture.SetUp.Statements.Add( $"this.ownClass = this.mapping.FindClass( \"{test.OwnClass.FullName}\" );" ); |
| 265 | ············fixture.SetUp.Statements.Add( $"this.otherClass = this.mapping.FindClass( \"{test.OtherClass.FullName}\" );" ); |
| 266 | ············if (ri.OwnPoly) |
| 267 | ············fixture.SetUp.Statements.Add( $"this.ownDerivedClass = this.mapping.FindClass( \"{test.OwnDerivedClass.FullName}\" );" ); |
| 268 | ············if (ri.OtherPoly) |
| 269 | ············fixture.SetUp.Statements.Add( $"this.otherDerivedClass = this.mapping.FindClass( \"{test.OtherDerivedClass.FullName}\" );" ); |
| 270 | |
| 271 | ············//GenerateTearDown( ri ); |
| 272 | |
| 273 | ············CreateTests( ri ); |
| 274 | |
| 275 | ············this.sw.WriteLine( fixture.ToString() ); |
| 276 | ········} |
| 277 | |
| 278 | ········void GeneratePmFactory() |
| 279 | ········{ |
| 280 | ············Class cl = new Class( this.nameSpace, "PmFactory" ); |
| 281 | ············string path = AppDomain.CurrentDomain.BaseDirectory; |
| 282 | path = Path. Combine( path, @". . \. . \. . \. . \UnitTests\bin\Debug\NDOMapping. xml" ) ; |
| 283 | ············path = Path.GetFullPath( path ); |
| 284 | ············cl.Statements.Add( "static PersistenceManager pm;" ); |
| 285 | ············Function func = cl.NewFunction( "PersistenceManager", "NewPersistenceManager" ); |
| 286 | ············func.IsStatic = true; |
| 287 | ············func.AccessModifier = "public"; |
| 288 | |
| 289 | ············func.Statements.Add( "if (pm == null)" ); |
| 290 | ············func.Statements.Add( "{" ); |
| 291 | ············func.Statements.Add( "pm = new PersistenceManager(@\"" + path + "\");" ); |
| 292 | ············path = Path.GetFullPath( Path.Combine( path, @"..\..\.." ) ); |
| 293 | ············func.Statements.Add( "}" ); |
| 294 | ············func.Statements.Add( "else" ); |
| 295 | ············func.Statements.Add( "{" ); |
| 296 | ············func.Statements.Add( "pm.UnloadCache();" ); |
| 297 | ············func.Statements.Add( "}" ); |
| 298 | ············func.Statements.Add( "return pm;" ); |
| 299 | ············sw.WriteLine( cl.ToString() ); |
| 300 | ········} |
| 301 | |
| 302 | ····} |
| 303 | } |
| 304 |