Datei: IntegrationTests/IntegrationTests/NDOObjectIdTests.cs
Last Commit (182cfd7)
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.Linq; |
25 | using System.Data; |
26 | using System.Reflection; |
27 | using System.Collections; |
28 | using NUnit.Framework; |
29 | using NDO; |
30 | using NDO.Mapping; |
31 | using NDOObjectIdTestClasses; |
32 | using NDOInterfaces; |
33 | using NDO.Query; |
34 | |
35 | namespace NdoUnitTests |
36 | { |
37 | ····[TestFixture] |
38 | ····public class NDOObjectIdTests : NDOTest |
39 | ····{ |
40 | ········PersistenceManager pm; |
41 | ········Guid guid; |
42 | |
43 | ········[SetUp] |
44 | ········public void Setup() |
45 | ········{ |
46 | ············pm = PmFactory.NewPersistenceManager(); |
47 | ············pm.IdGenerationEvent += new IdGenerationHandler(OnIdGeneration); |
48 | ············guid = new Guid(1,2,3,4,5,6,7,8,9,10,11); |
49 | ········} |
50 | |
51 | ········[TearDown] |
52 | ········public void TearDown() |
53 | ········{············ |
54 | ············IList l = pm.GetClassExtent(typeof(ObjectOwner)); |
55 | ············pm.Delete(l); |
56 | ············pm.Save(); |
57 | ············l = pm.GetClassExtent(typeof(NDOoidAndHandler)); |
58 | ············pm.Delete(l); |
59 | ············pm.Save(); |
60 | ············l = pm.GetClassExtent(typeof(HintOwner)); |
61 | ············pm.Delete(l); |
62 | ············pm.Save(); |
63 | ············l = pm.GetClassExtent(typeof(ClassWithHint)); |
64 | ············pm.Delete(l); |
65 | ············pm.Save(); |
66 | ············l = pm.GetClassExtent(typeof(DerivedGuid)); |
67 | ············pm.Delete(l); |
68 | ············pm.Save(); |
69 | ············pm.Dispose(); |
70 | ········} |
71 | |
72 | ········[Test] |
73 | ········public void OidTest() |
74 | ········{ |
75 | ············NDOoidAndHandler testobj = new NDOoidAndHandler(); |
76 | ············testobj.MyId = guid; |
77 | ············testobj.Text = "Test"; |
78 | ············pm.MakePersistent(testobj); |
79 | ············pm.Save(); |
80 | ············pm.UnloadCache(); |
81 | ············IQuery q = new NDOQuery<NDOoidAndHandler>(pm); |
82 | ············testobj = null; |
83 | ············testobj = (NDOoidAndHandler) q.ExecuteSingle(true); |
84 | ············Assert.That(guid ==··testobj.MyId, "Wrong guid"); |
85 | ············Assert.That("Test" ==··testobj.Text, "Wrong text"); |
86 | |
87 | ············testobj.Text = "Neuer Text"; |
88 | ············pm.Save(); |
89 | ············testobj = null; |
90 | ············testobj = (NDOoidAndHandler) q.ExecuteSingle(true); |
91 | ············Assert.That(guid ==··testobj.MyId, "Wrong guid"); |
92 | ············Assert.That("Neuer Text" ==··testobj.Text, "Wrong text"); |
93 | ············ |
94 | ············pm.Delete(testobj); |
95 | ············pm.Save(); |
96 | ········} |
97 | |
98 | ········[Test] |
99 | ········public void TestForeignKey() |
100 | ········{ |
101 | ············ObjectOwner owner = new ObjectOwner(); |
102 | ············pm.MakePersistent(owner); |
103 | ············owner.Element = new NDOoidAndHandler(); |
104 | ············owner.Element.Text = "Text"; |
105 | ············pm.Save(); |
106 | ············owner = null; |
107 | ············pm.UnloadCache(); |
108 | ············IQuery q = new NDOQuery<ObjectOwner>(pm); |
109 | ············owner = (ObjectOwner) q.ExecuteSingle(true); |
110 | ············Assert.That(owner.Element != null, "No element"); |
111 | ············Assert.That(guid ==··owner.Element.MyId, "Wrong guid"); |
112 | ············Assert.That("Text" ==··owner.Element.Text, "Wrong text"); |
113 | ········} |
114 | |
115 | |
116 | ········[Test] |
117 | ········public void TestHintForeignKey() |
118 | ········{ |
119 | ············HintOwner owner = new HintOwner(); |
120 | ············pm.MakePersistent(owner); |
121 | ············owner.Element = new ClassWithHint(); |
122 | ············owner.Element.Text = "Text"; |
123 | ············pm.Save(); |
124 | ············owner = null; |
125 | ············pm.UnloadCache(); |
126 | ············IQuery q = new NDOQuery<HintOwner>(pm); |
127 | ············owner = (HintOwner) q.ExecuteSingle(true); |
128 | ············Assert.That(owner.Element != null, "No element"); |
129 | ············Assert.That(guid == (Guid) owner.Element.NDOObjectId.Id[0], "Wrong guid"); |
130 | ············Assert.That("Text" == owner.Element.Text, "Wrong text"); |
131 | ········} |
132 | |
133 | |
134 | ········[Test] |
135 | ········public void HintEnhancerTest() |
136 | ········{ |
137 | ············Class cl = pm.NDOMapping.FindClass(typeof(ClassWithHint)); |
138 | ············IProvider provider = NDOProviderFactory.Instance[((Connection)pm.NDOMapping.Connections.First()).Type]; |
139 | ············Assert.That(cl != null, "Class not found"); |
140 | ············Assert.That(typeof(Guid) ==··((OidColumn)cl.Oid.OidColumns[0]).SystemType, "Wrong type"); |
141 | ············Type t = pm.GetType(); |
142 | ············FieldInfo fi = t.GetField("ds", BindingFlags.NonPublic | BindingFlags.Instance); |
143 | ············Assert.That(fi != null, "Field 'ds' not found"); |
144 | ············DataSet ds = (DataSet) fi.GetValue(pm); |
145 | ············Assert.That(ds != null, "DataSet is null"); |
146 | ············DataTable dt = ds.Tables[cl.TableName]; |
147 | ············Assert.That(dt != null, "DataTable is null"); |
148 | ············OidColumn oidColumn = (OidColumn)cl.Oid.OidColumns[0]; |
149 | ············DataColumn dc = dt.Columns[oidColumn.Name]; |
150 | ············Assert.That(dc != null, "DataColumn is null"); |
151 | ············Assert.That(provider != null, "Provider is null"); |
152 | ············if (provider.SupportsNativeGuidType) |
153 | ················Assert.That(typeof(Guid) ==··dc.DataType, "Wrong column type"); |
154 | ············else |
155 | ················Assert.That(typeof(string) ==··dc.DataType, "Wrong column type"); |
156 | ········} |
157 | |
158 | |
159 | ········[Test] |
160 | ········public void HintTest() |
161 | ········{ |
162 | ············ClassWithHint testobj = new ClassWithHint(); |
163 | ············testobj.Text = "Test"; |
164 | ············pm.MakePersistent(testobj); |
165 | ············pm.Save(); |
166 | ············pm.UnloadCache(); |
167 | ············IQuery q = new NDOQuery<ClassWithHint>(pm); |
168 | ············testobj = null; |
169 | ············testobj = (ClassWithHint) q.ExecuteSingle(true); |
170 | ············Assert.That(guid == (Guid) testobj.NDOObjectId.Id[0], "Wrong guid #1"); |
171 | ············Assert.That("Test" ==··testobj.Text, "Wrong text #1"); |
172 | |
173 | ············testobj.Text = "Neuer Text"; |
174 | ············pm.Save(); |
175 | ············testobj = null; |
176 | ············testobj = (ClassWithHint) q.ExecuteSingle(true); |
177 | ············Assert.That(guid == (Guid) testobj.NDOObjectId.Id[0], "Wrong guid #2"); |
178 | ············Assert.That("Neuer Text" ==··testobj.Text, "Wrong text #2"); |
179 | ············ |
180 | ············pm.Delete(testobj); |
181 | ············pm.Save(); |
182 | ········} |
183 | |
184 | ········[Test] |
185 | ········public void DerivedGuidTest() |
186 | ········{ |
187 | ············PersistenceManager pm = PmFactory.NewPersistenceManager(); |
188 | ············DerivedGuid dg = new DerivedGuid(); |
189 | ············dg.Guid = new Guid(5,5,5,5,5,5,5,5,5,5,5); |
190 | ············dg.Myfield = "Test"; |
191 | ············pm.MakePersistent(dg); |
192 | ············pm.Save(); |
193 | ············dg = null; |
194 | ············pm.UnloadCache(); |
195 | ············IQuery q = new NDOQuery<DerivedGuid>(pm); |
196 | ············dg = (DerivedGuid) q.ExecuteSingle(true); |
197 | ············Assert.That(new Guid(5,5,5,5,5,5,5,5,5,5,5) == dg.Guid, "Guid wrong"); |
198 | ············Assert.That(new Guid(5,5,5,5,5,5,5,5,5,5,5) == (Guid) dg.NDOObjectId.Id[0], "Oid wrong"); |
199 | ············Assert.That("Test" ==··dg.Myfield, "Text wrong"); |
200 | ············pm.Delete(dg); |
201 | ············pm.Save(); |
202 | ········} |
203 | |
204 | ········private void OnIdGeneration(Type t, ObjectId oid) |
205 | ········{ |
206 | ············if (!t.FullName.StartsWith ("NDOObjectIdTestClasses.")) |
207 | ················return;··// Use the other handler |
208 | ············Class cl = pm.NDOMapping.FindClass(t); |
209 | ············Assert.That(cl != null); |
210 | ············OidColumn oidColumn = (OidColumn)cl.Oid.OidColumns[0]; |
211 | ············if (t == typeof(NDOoidAndHandler) || t == typeof(ClassWithHint)) |
212 | ············{ |
213 | ················if (oidColumn.SystemType == typeof(string)) |
214 | ····················oid.Id[0] = this.guid.ToString(); |
215 | ················else |
216 | ····················oid.Id[0] = this.guid; |
217 | ············} |
218 | ············else |
219 | ············{ |
220 | ················oid.Id[0] = 1; |
221 | ············} |
222 | ········} |
223 | ····} |
224 | } |
225 |
New Commit (a04c7be)
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.Linq; |
25 | using System.Data; |
26 | using System.Reflection; |
27 | using System.Collections; |
28 | using NUnit.Framework; |
29 | using NDO; |
30 | using NDO.Mapping; |
31 | using NDOObjectIdTestClasses; |
32 | using NDOInterfaces; |
33 | using NDO.Query; |
34 | using NDO.ProviderFactory; |
35 | |
36 | namespace NdoUnitTests |
37 | { |
38 | ····[TestFixture] |
39 | ····public class NDOObjectIdTests : NDOTest |
40 | ····{ |
41 | ········PersistenceManager pm; |
42 | ········Guid guid; |
43 | |
44 | ········[SetUp] |
45 | ········public void Setup() |
46 | ········{ |
47 | ············pm = PmFactory.NewPersistenceManager(); |
48 | ············pm.IdGenerationEvent += new IdGenerationHandler(OnIdGeneration); |
49 | ············guid = new Guid(1,2,3,4,5,6,7,8,9,10,11); |
50 | ········} |
51 | |
52 | ········[TearDown] |
53 | ········public void TearDown() |
54 | ········{············ |
55 | ············IList l = pm.GetClassExtent(typeof(ObjectOwner)); |
56 | ············pm.Delete(l); |
57 | ············pm.Save(); |
58 | ············l = pm.GetClassExtent(typeof(NDOoidAndHandler)); |
59 | ············pm.Delete(l); |
60 | ············pm.Save(); |
61 | ············l = pm.GetClassExtent(typeof(HintOwner)); |
62 | ············pm.Delete(l); |
63 | ············pm.Save(); |
64 | ············l = pm.GetClassExtent(typeof(ClassWithHint)); |
65 | ············pm.Delete(l); |
66 | ············pm.Save(); |
67 | ············l = pm.GetClassExtent(typeof(DerivedGuid)); |
68 | ············pm.Delete(l); |
69 | ············pm.Save(); |
70 | ············pm.Dispose(); |
71 | ········} |
72 | |
73 | ········[Test] |
74 | ········public void OidTest() |
75 | ········{ |
76 | ············NDOoidAndHandler testobj = new NDOoidAndHandler(); |
77 | ············testobj.MyId = guid; |
78 | ············testobj.Text = "Test"; |
79 | ············pm.MakePersistent(testobj); |
80 | ············pm.Save(); |
81 | ············pm.UnloadCache(); |
82 | ············IQuery q = new NDOQuery<NDOoidAndHandler>(pm); |
83 | ············testobj = null; |
84 | ············testobj = (NDOoidAndHandler) q.ExecuteSingle(true); |
85 | ············Assert.That(guid ==··testobj.MyId, "Wrong guid"); |
86 | ············Assert.That("Test" ==··testobj.Text, "Wrong text"); |
87 | |
88 | ············testobj.Text = "Neuer Text"; |
89 | ············pm.Save(); |
90 | ············testobj = null; |
91 | ············testobj = (NDOoidAndHandler) q.ExecuteSingle(true); |
92 | ············Assert.That(guid ==··testobj.MyId, "Wrong guid"); |
93 | ············Assert.That("Neuer Text" ==··testobj.Text, "Wrong text"); |
94 | ············ |
95 | ············pm.Delete(testobj); |
96 | ············pm.Save(); |
97 | ········} |
98 | |
99 | ········[Test] |
100 | ········public void TestForeignKey() |
101 | ········{ |
102 | ············ObjectOwner owner = new ObjectOwner(); |
103 | ············pm.MakePersistent(owner); |
104 | ············owner.Element = new NDOoidAndHandler(); |
105 | ············owner.Element.Text = "Text"; |
106 | ············pm.Save(); |
107 | ············owner = null; |
108 | ············pm.UnloadCache(); |
109 | ············IQuery q = new NDOQuery<ObjectOwner>(pm); |
110 | ············owner = (ObjectOwner) q.ExecuteSingle(true); |
111 | ············Assert.That(owner.Element != null, "No element"); |
112 | ············Assert.That(guid ==··owner.Element.MyId, "Wrong guid"); |
113 | ············Assert.That("Text" ==··owner.Element.Text, "Wrong text"); |
114 | ········} |
115 | |
116 | |
117 | ········[Test] |
118 | ········public void TestHintForeignKey() |
119 | ········{ |
120 | ············HintOwner owner = new HintOwner(); |
121 | ············pm.MakePersistent(owner); |
122 | ············owner.Element = new ClassWithHint(); |
123 | ············owner.Element.Text = "Text"; |
124 | ············pm.Save(); |
125 | ············owner = null; |
126 | ············pm.UnloadCache(); |
127 | ············IQuery q = new NDOQuery<HintOwner>(pm); |
128 | ············owner = (HintOwner) q.ExecuteSingle(true); |
129 | ············Assert.That(owner.Element != null, "No element"); |
130 | ············Assert.That(guid == (Guid) owner.Element.NDOObjectId.Id[0], "Wrong guid"); |
131 | ············Assert.That("Text" == owner.Element.Text, "Wrong text"); |
132 | ········} |
133 | |
134 | |
135 | ········[Test] |
136 | ········public void HintEnhancerTest() |
137 | ········{ |
138 | ············Class cl = pm.NDOMapping.FindClass(typeof(ClassWithHint)); |
139 | ············IProvider provider = NDOProviderFactory.Instance[((Connection)pm.NDOMapping.Connections.First()).Type]; |
140 | ············Assert.That(cl != null, "Class not found"); |
141 | ············Assert.That(typeof(Guid) ==··((OidColumn)cl.Oid.OidColumns[0]).SystemType, "Wrong type"); |
142 | ············Type t = pm.GetType(); |
143 | ············FieldInfo fi = t.GetField("ds", BindingFlags.NonPublic | BindingFlags.Instance); |
144 | ············Assert.That(fi != null, "Field 'ds' not found"); |
145 | ············DataSet ds = (DataSet) fi.GetValue(pm); |
146 | ············Assert.That(ds != null, "DataSet is null"); |
147 | ············DataTable dt = ds.Tables[cl.TableName]; |
148 | ············Assert.That(dt != null, "DataTable is null"); |
149 | ············OidColumn oidColumn = (OidColumn)cl.Oid.OidColumns[0]; |
150 | ············DataColumn dc = dt.Columns[oidColumn.Name]; |
151 | ············Assert.That(dc != null, "DataColumn is null"); |
152 | ············Assert.That(provider != null, "Provider is null"); |
153 | ············if (provider.SupportsNativeGuidType) |
154 | ················Assert.That(typeof(Guid) ==··dc.DataType, "Wrong column type"); |
155 | ············else |
156 | ················Assert.That(typeof(string) ==··dc.DataType, "Wrong column type"); |
157 | ········} |
158 | |
159 | |
160 | ········[Test] |
161 | ········public void HintTest() |
162 | ········{ |
163 | ············ClassWithHint testobj = new ClassWithHint(); |
164 | ············testobj.Text = "Test"; |
165 | ············pm.MakePersistent(testobj); |
166 | ············pm.Save(); |
167 | ············pm.UnloadCache(); |
168 | ············IQuery q = new NDOQuery<ClassWithHint>(pm); |
169 | ············testobj = null; |
170 | ············testobj = (ClassWithHint) q.ExecuteSingle(true); |
171 | ············Assert.That(guid == (Guid) testobj.NDOObjectId.Id[0], "Wrong guid #1"); |
172 | ············Assert.That("Test" ==··testobj.Text, "Wrong text #1"); |
173 | |
174 | ············testobj.Text = "Neuer Text"; |
175 | ············pm.Save(); |
176 | ············testobj = null; |
177 | ············testobj = (ClassWithHint) q.ExecuteSingle(true); |
178 | ············Assert.That(guid == (Guid) testobj.NDOObjectId.Id[0], "Wrong guid #2"); |
179 | ············Assert.That("Neuer Text" ==··testobj.Text, "Wrong text #2"); |
180 | ············ |
181 | ············pm.Delete(testobj); |
182 | ············pm.Save(); |
183 | ········} |
184 | |
185 | ········[Test] |
186 | ········public void DerivedGuidTest() |
187 | ········{ |
188 | ············PersistenceManager pm = PmFactory.NewPersistenceManager(); |
189 | ············DerivedGuid dg = new DerivedGuid(); |
190 | ············dg.Guid = new Guid(5,5,5,5,5,5,5,5,5,5,5); |
191 | ············dg.Myfield = "Test"; |
192 | ············pm.MakePersistent(dg); |
193 | ············pm.Save(); |
194 | ············dg = null; |
195 | ············pm.UnloadCache(); |
196 | ············IQuery q = new NDOQuery<DerivedGuid>(pm); |
197 | ············dg = (DerivedGuid) q.ExecuteSingle(true); |
198 | ············Assert.That(new Guid(5,5,5,5,5,5,5,5,5,5,5) == dg.Guid, "Guid wrong"); |
199 | ············Assert.That(new Guid(5,5,5,5,5,5,5,5,5,5,5) == (Guid) dg.NDOObjectId.Id[0], "Oid wrong"); |
200 | ············Assert.That("Test" ==··dg.Myfield, "Text wrong"); |
201 | ············pm.Delete(dg); |
202 | ············pm.Save(); |
203 | ········} |
204 | |
205 | ········private void OnIdGeneration(Type t, ObjectId oid) |
206 | ········{ |
207 | ············if (!t.FullName.StartsWith ("NDOObjectIdTestClasses.")) |
208 | ················return;··// Use the other handler |
209 | ············Class cl = pm.NDOMapping.FindClass(t); |
210 | ············Assert.That(cl != null); |
211 | ············OidColumn oidColumn = (OidColumn)cl.Oid.OidColumns[0]; |
212 | ············if (t == typeof(NDOoidAndHandler) || t == typeof(ClassWithHint)) |
213 | ············{ |
214 | ················if (oidColumn.SystemType == typeof(string)) |
215 | ····················oid.Id[0] = this.guid.ToString(); |
216 | ················else |
217 | ····················oid.Id[0] = this.guid; |
218 | ············} |
219 | ············else |
220 | ············{ |
221 | ················oid.Id[0] = 1; |
222 | ············} |
223 | ········} |
224 | ····} |
225 | } |
226 |