Datei: IntegrationTests/IntegrationTests/NDOObjectIdTests.cs

Last Commit (9d1552b)
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
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 (60cb179)
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