File: ReadTests_EntitySet.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (422 lines) | stat: -rw-r--r-- 13,402 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
´╗┐using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Test_NUnit;
using NUnit.Framework;
using Test_NUnit.Linq_101_Samples;
using System.Data.Linq;

#if MONO_STRICT
using DataLoadOptions = System.Data.Linq.DataLoadOptions;
#else
using DataLoadOptions = DbLinq.Data.Linq.DataLoadOptions;
#endif

using nwind;

// test ns 
#if MYSQL
    namespace Test_NUnit_MySql
#elif ORACLE && ODP
    namespace Test_NUnit_OracleODP
#elif ORACLE
    namespace Test_NUnit_Oracle
#elif POSTGRES
    namespace Test_NUnit_PostgreSql
#elif SQLITE
    namespace Test_NUnit_Sqlite
#elif INGRES
    namespace Test_NUnit_Ingres
#elif MSSQL && L2SQL
    namespace Test_NUnit_MsSql_Strict
#elif MSSQL
    namespace Test_NUnit_MsSql
#elif FIREBIRD
    namespace Test_NUnit_Firebird
#endif
{
    [TestFixture]
    public class EntitySet : TestBase
    {

        [Test]
        public void SimpleMemberAccess01()
        {
            var customer = new Customer();
            var orders = customer.Orders;
        }

        [Test]
        public void SimpleMemberAccess02()
        {
            var db = CreateDB();
            var customer = db.Customers.First();
            Assert.Greater(customer.Orders.Count, 0);
        }

#if !DEBUG && (SQLITE || (MSSQL && !L2SQL))
        [Explicit]
#endif
        [Test]
        public void EntitySetEnumerationProjection()
        {
            var db = CreateDB();
            var results = (from c in db.Customers select c.Orders).ToList();

            Assert.Greater(results.Count, 0);
        }

        [Test]
        public void HasLoadedOrAsignedValues01()
        {
            var db = CreateDB();
            var customer = db.Customers.First();
            Assert.IsFalse(customer.Orders.HasLoadedOrAssignedValues);

            customer.Orders.Add(new Order());
            Assert.IsTrue(customer.Orders.HasLoadedOrAssignedValues);
        }

        [Test]
        public void HasLoadedOrAsignedValues02()
        {
            var db = CreateDB();
            var customer = db.Customers.First();
            Assert.IsFalse(customer.Orders.HasLoadedOrAssignedValues);

            customer.Orders.Assign(System.Linq.Enumerable.Empty<Order>());
            Assert.IsTrue(customer.Orders.HasLoadedOrAssignedValues);
        }


        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void InvalidSourceChange()
        {
            var db = CreateDB();
            var customer = db.Customers.First();

            Assert.Greater(customer.Orders.Count, 0, "#1");
            Assert.IsTrue(customer.Orders.HasLoadedOrAssignedValues, "#2");
            customer.Orders.SetSource(System.Linq.Enumerable.Empty<Order>());
        }

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void InvalidSourceChange2()
        {
            var db = CreateDB();
            var customer = db.Customers.First();
            Assert.IsFalse(customer.Orders.HasLoadedOrAssignedValues, "#1");
            customer.Orders.Assign(new List<Order>());
            Assert.IsTrue(customer.Orders.HasLoadedOrAssignedValues, "#2");
            customer.Orders.SetSource(System.Linq.Enumerable.Empty<Order>());
        }

        [Test]
        [ExpectedException(typeof(InvalidOperationException))]
        public void InvalidSourceChange3()
        {
            var db = CreateDB();
            var customer = db.Customers.First();
            customer.Orders.SetSource(new List<Order>());
            Assert.IsFalse(customer.Orders.HasLoadedOrAssignedValues, "#1");
            customer.Orders.Load();
            Assert.IsTrue(customer.Orders.HasLoadedOrAssignedValues, "#2");
            customer.Orders.SetSource(System.Linq.Enumerable.Empty<Order>());
        }

        [Test]
        public void SourceChange()
        {
            var db = CreateDB();

            int ordersCount = (from cust in db.Customers
                               select cust.Orders.Count).First();

            Assert.Greater(ordersCount, 0);

            var customer2 = db.Customers.First();
            customer2.Orders.SetSource(System.Linq.Enumerable.Empty<Order>());
            Assert.AreEqual(customer2.Orders.Count, 0);
        }


#if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
        [Explicit]
#endif
        [Test]
        public void Refresh01()
        {
            var db = CreateDB();
            var c = db.Customers.First();

            int beforeCount = c.Orders.Count;
            Assert.Greater(beforeCount, 0);
            c.Orders.Clear();
            Assert.AreEqual(c.Orders.Count, 0);
            c.Orders.AddRange(db.Orders);
            Assert.Greater(c.Orders.Count, beforeCount);
            db.Refresh(RefreshMode.OverwriteCurrentValues, c.Orders);

            Assert.AreEqual(c.Orders.Count, beforeCount);
        }

#if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
        [Explicit]
#endif
        [Test]
        public void Refresh02()
        {
            var db = CreateDB();
            var c = db.Customers.First();

            int beforeCount = c.Orders.Count;
            Assert.Greater(beforeCount, 0);
            c.Orders.Clear();
            Assert.AreEqual(c.Orders.Count, 0);
            c.Orders.AddRange(db.Orders);

            int middleCount = c.Orders.Count;
            Assert.Greater(c.Orders.Count, beforeCount);

            db.Refresh(RefreshMode.KeepCurrentValues, c.Orders);
            Assert.AreEqual(c.Orders.Count, middleCount);

            db.Refresh(RefreshMode.KeepChanges, c.Orders);
            Assert.AreEqual(c.Orders.Count, middleCount);
        }


#if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
        [Explicit]
#endif
        [Test]
        public void Refresh03()
        {
            var db = CreateDB();
            var c = db.Customers.First();

            var order = c.Orders.First();
            string newcustomerId = "NEWCUSTOMERID";
            order.CustomerID = newcustomerId;

            db.Refresh(RefreshMode.OverwriteCurrentValues, c.Orders);
            Assert.AreNotEqual(order.CustomerID, newcustomerId);
        }

#if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
        [Explicit]
#endif
        [Test]
        public void Refresh04()
        {
            var db = CreateDB();
            var c = db.Customers.First();

            var order = c.Orders.First();
            string newcustomerId = "NEWCUSTOMERID";
            order.CustomerID = newcustomerId;

            db.Refresh(RefreshMode.KeepCurrentValues, c.Orders);
            Assert.AreEqual(order.CustomerID, newcustomerId);

            db.Refresh(RefreshMode.KeepChanges, c.Orders);
            Assert.AreEqual(order.CustomerID, newcustomerId);
        }


        [Test]
        public void ListChangedEvent()
        {
            var db = CreateDB();
            var customer = db.Customers.Where(c => c.Orders.Count > 0).First();
            Assert.Greater(customer.Orders.Count, 0);
            bool ok;
            System.ComponentModel.ListChangedEventArgs args = null;
            customer.Orders.ListChanged += delegate(object sender, System.ComponentModel.ListChangedEventArgs a) 
                { 
                    ok = true; 
                    args = a; 
                };

            ok = false;
            args = null;
            customer.Orders.Remove(customer.Orders.First());
            Assert.IsTrue(ok);
            Assert.AreEqual(System.ComponentModel.ListChangedType.ItemDeleted, args.ListChangedType);
            Assert.AreEqual(0, args.NewIndex);
            Assert.AreEqual(-1, args.OldIndex);

            ok = false;
            args = null;
            customer.Orders.Assign(Enumerable.Empty<Order>());
            Assert.IsTrue(ok);
            Assert.AreEqual(System.ComponentModel.ListChangedType.Reset, args.ListChangedType);
            Assert.AreEqual(0, args.NewIndex);
            Assert.AreEqual(-1, args.OldIndex);

            ok = false;
            args = null;
            customer.Orders.Add(db.Orders.First());
            Assert.IsTrue(ok);
            Assert.AreEqual(System.ComponentModel.ListChangedType.ItemAdded, args.ListChangedType);
            Assert.AreEqual(0, args.NewIndex);
            Assert.AreEqual(-1, args.OldIndex);

            ok = false;
            args = null;
            customer.Orders.Clear();
            Assert.IsTrue(ok);
            Assert.AreEqual(System.ComponentModel.ListChangedType.Reset, args.ListChangedType);
            Assert.AreEqual(0, args.NewIndex);
            Assert.AreEqual(-1, args.OldIndex);

            ok = false;
            args = null;
            customer.Orders.Insert(0, new Order());
            Assert.IsTrue(ok);
            Assert.AreEqual(System.ComponentModel.ListChangedType.ItemAdded, args.ListChangedType);
            Assert.AreEqual(0, args.NewIndex);
            Assert.AreEqual(-1, args.OldIndex);

            ok = false;
            args = null;
            customer.Orders.RemoveAt(0);
            Assert.IsTrue(ok);
            Assert.AreEqual(System.ComponentModel.ListChangedType.ItemDeleted, args.ListChangedType);
            Assert.AreEqual(args.NewIndex, 0);
            Assert.AreEqual(args.OldIndex, -1);
        }

        [Test]
        public void Load()
        {
            var db = CreateDB();
            var customer = db.Customers.First();
            var orders = customer.Orders;

            Assert.IsFalse(orders.HasLoadedOrAssignedValues);
            orders.Load();
            Assert.IsTrue(orders.HasLoadedOrAssignedValues);
        }

        [Test]
        public void DeferedExecution()
        {
            var db = CreateDB();
            var customer = db.Customers.First();
            Assert.IsTrue(customer.Orders.IsDeferred);

            customer.Orders.Load();
            Assert.IsFalse(customer.Orders.IsDeferred);
        }

#if !DEBUG && (SQLITE || POSTGRES || (MSSQL && !L2SQL))
        [Explicit]
#endif
        [Test]
        public void DeferedExecutionAndLoadWith()
        {
            var db = CreateDB();
            DataLoadOptions loadoptions = new DataLoadOptions();
            loadoptions.LoadWith<Customer>(c => c.Orders);
            db.LoadOptions = loadoptions;

            var customer = db.Customers.First();
            Assert.IsFalse(customer.Orders.IsDeferred, "#1");
            Assert.IsTrue(customer.Orders.HasLoadedOrAssignedValues, "#2");
        }

        [Test]
        public void Add()
        {
            var db = CreateDB();
            var customer = db.Customers.First();
            int beforeCount = customer.Orders.Count;
            var order = new Order();
            customer.Orders.Add(order);
            Assert.AreEqual(beforeCount + 1, customer.Orders.Count, "#3");
            customer.Orders.Add(order); // do not actually add
            Assert.AreEqual(beforeCount + 1, customer.Orders.Count, "#4");
        }

        [Test]
        [ExpectedException (typeof (ArgumentOutOfRangeException))]
        public void IList_Add()
        {
            var db = CreateDB();
            var customer = db.Customers.First();
            var order = new Order();
            ((IList)customer.Orders).Add(order);
            ((IList)customer.Orders).Add(order); // raises ArgumentOutOfRangeException for duplicate
        }

        [Test]
        public void Clear()
        {
            var db = CreateDB();
            var customer = db.Customers.First();

            if (customer.Orders.Count == 0)
                Assert.Ignore();

            customer.Orders.Clear();
            Assert.AreEqual(customer.Orders.Count, 0);
        }

        [Test]
        public void AddRange()
        {
            var db = CreateDB();
            var customer = db.Customers.First();
            int beforeCount = customer.Orders.Count;
            var order = new Order();
            customer.Orders.AddRange(new Order[] { order, new Order() });
            Assert.AreEqual(beforeCount + 2, customer.Orders.Count);
            customer.Orders.AddRange(new Order[] { new Order(), order }); // one is existing -> not added
            Assert.AreEqual(beforeCount + 3, customer.Orders.Count);
        }

        [Test]
        public void Remove()
        {
            var db = CreateDB();
            var customer = db.Customers.First();
            Assert.IsTrue(customer.Orders.IsDeferred);
            int beforeCount = customer.Orders.Count;
            Assert.IsFalse(customer.Orders.IsDeferred);

            if (beforeCount == 0)
                Assert.Ignore();

            Assert.IsFalse(customer.Orders.Remove(null));
            Assert.AreEqual(beforeCount, customer.Orders.Count);

            Assert.IsTrue(customer.Orders.Remove(customer.Orders.First()));
            Assert.AreEqual(customer.Orders.Count, beforeCount - 1);
        }

        [Test]
        public void RemoveAt()
        {
            var db = CreateDB();
            var customer = db.Customers.First();
            int beforeCount = customer.Orders.Count;

            if (beforeCount == 0)
                Assert.Ignore();

            customer.Orders.RemoveAt(0);
            Assert.AreEqual(customer.Orders.Count, beforeCount - 1);
        }

        [Test]
        public void RemoveAll()
        {
            Clear();
        }
    }
}