PageRenderTime 131ms CodeModel.GetById 36ms app.highlight 56ms RepoModel.GetById 26ms app.codeStats 0ms

/mcs/tools/sqlmetal/src/DbLinq/Test/EntitySetTest.cs

http://github.com/mono/mono
C# | 438 lines | 369 code | 59 blank | 10 comment | 1 complexity | 66fbdb4b385d7ae9a53399975ae8c199 MD5 | raw file
  1using System;
  2using System.Collections;
  3using System.Collections.Generic;
  4using System.ComponentModel;
  5using System.Linq;
  6
  7#if MONO_STRICT
  8using System.Data.Linq;
  9#else
 10using DbLinq.Data.Linq;
 11#endif
 12
 13using NUnit.Framework;
 14
 15namespace DbLinqTest
 16{
 17    [TestFixture]
 18    public class EntitySetTest
 19    {
 20        [Test]
 21        public void Ctor_OnAddAndOnRemoveCanBeNull()
 22        {
 23            new EntitySet<Person>(null, null);
 24        }
 25
 26        [Test, ExpectedException(typeof(ArgumentNullException))]
 27        public void Add_EntityNull()
 28        {
 29            var people = new EntitySet<Person>();
 30            people.Add(null);
 31        }
 32
 33        [Test]
 34        public void Add_IgnoreRepeats()
 35        {
 36            var people = new EntitySet<Person>();
 37            var p = new Person { FirstName = "A", LastName = "B" };
 38            people.Add(p);
 39            people.Add(p);
 40            Assert.AreEqual(1, people.Count);
 41        }
 42
 43        [Test, ExpectedException(typeof(InvalidOperationException))]
 44        public void Add_ThenSetSourceIsInvalid()
 45        {
 46            var people = new EntitySet<Person>();
 47            Assert.IsFalse(people.HasLoadedOrAssignedValues);
 48            people.Add(new Person { FirstName = "A", LastName = "B" });
 49            Assert.IsTrue(people.HasLoadedOrAssignedValues);
 50            people.SetSource(new[]{
 51                new Person { FirstName = "1", LastName = "2" }
 52            });
 53        }
 54
 55        [Test]
 56        public void Assign()
 57        {
 58            var people = new EntitySet<Person>();
 59            people.SetSource(new[]{
 60                new Person { FirstName = "A", LastName = "B" },
 61            });
 62            Assert.IsTrue(people.IsDeferred);
 63            people.Load();
 64            Assert.IsTrue(people.HasLoadedOrAssignedValues);
 65            Assert.IsFalse(people.IsDeferred);
 66            people.Assign(new[]{
 67                new Person { FirstName = "1", LastName = "2" },
 68            });
 69            Assert.IsTrue(people.HasLoadedOrAssignedValues);
 70            Assert.AreEqual(1, people.Count);
 71            Assert.IsFalse(people.IsDeferred);
 72        }
 73
 74        [Test, ExpectedException(typeof(InvalidOperationException))]
 75        public void Clear_DoesNotResetSource()
 76        {
 77            var people = new EntitySet<Person>();
 78            Assert.IsFalse(people.HasLoadedOrAssignedValues);
 79            people.Add(new Person { FirstName = "A", LastName = "B" });
 80            Assert.IsTrue(people.HasLoadedOrAssignedValues);
 81            people.Clear();
 82            Assert.IsTrue(people.HasLoadedOrAssignedValues);
 83            people.SetSource(new[]{
 84                new Person { FirstName = "1", LastName = "2" },
 85            });
 86        }
 87
 88        [Test]
 89        public void Contains_KillsDeferred()
 90        {
 91            var people = new EntitySet<Person>();
 92            var p = new Person { FirstName = "A", LastName = "B" };
 93            people.SetSource(new[]{
 94                p
 95            });
 96            Assert.IsTrue(people.IsDeferred);
 97            Assert.IsTrue(people.Contains(p));
 98            Assert.IsFalse(people.IsDeferred);
 99        }
100
101        [Test]
102        public void HasLoadedOrAssignedValues()
103        {
104            var people = new EntitySet<Person>();
105            Assert.IsFalse(people.HasLoadedOrAssignedValues);
106            people.SetSource(new[]{
107                new Person { FirstName = "A", LastName = "B" },
108            });
109            Assert.IsFalse(people.HasLoadedOrAssignedValues);
110            Assert.IsTrue(people.IsDeferred);
111            people.Load();
112            Assert.IsTrue(people.HasLoadedOrAssignedValues);
113            Assert.IsFalse(people.IsDeferred);
114        }
115
116        [Test, ExpectedException(typeof(ArgumentOutOfRangeException))]
117        public void IList_Add_WrongType()
118        {
119            var people = new EntitySet<Person>();
120            System.Collections.IList list = people;
121            list.Add("WrongType");
122        }
123
124        [Test, ExpectedException(typeof(ArgumentOutOfRangeException))]
125        public void IList_Add_DuplicateItem()
126        {
127            var people = new EntitySet<Person>();
128            var p = new Person { FirstName = "A", LastName = "B" };
129            people.Add(p);
130            System.Collections.IList list = people;
131            list.Add(p);
132        }
133
134        [Test]
135        public void IList_Remove_WrongTypeIsIgnored()
136        {
137            var people = new EntitySet<Person>();
138            System.Collections.IList list = people;
139            list.Remove("DoesNotExist");
140        }
141
142        [Test]
143        public void IndexOf_KillsDeferred()
144        {
145            var people = new EntitySet<Person>();
146            var p = new Person { FirstName = "A", LastName = "B" };
147            people.SetSource(new[]{
148                p
149            });
150            Assert.IsTrue(people.IsDeferred);
151            Assert.AreEqual(0, people.IndexOf(p));
152            Assert.IsFalse(people.IsDeferred);
153        }
154
155        [Test, ExpectedException(typeof(ArgumentOutOfRangeException))]
156        public void Insert_RepeatValue()
157        {
158            var people = new EntitySet<Person>();
159            var p = new Person { FirstName = "A", LastName = "B" };
160            people.Add(p);
161            people.Insert(0, p);
162        }
163
164        [Test]
165        public void Item_IsDeferredSourceLoaded()
166        {
167            var people = new EntitySet<Person>();
168            people.SetSource(new[]{
169                new Person { FirstName = "A", LastName = "B" },
170            });
171            Assert.IsFalse(people.HasLoadedOrAssignedValues);
172            Assert.IsTrue(people.IsDeferred);
173            var p = people[0];
174            Assert.IsTrue(people.HasLoadedOrAssignedValues);
175            Assert.IsFalse(people.IsDeferred);
176        }
177
178        [Test]
179        public void ListChanged_NoSource()
180        {
181            // When is ListChanged emitted?
182            // It's not always when you think it would be.
183            // It depends on whether there's a Source present.
184            var people = new EntitySet<Person>();
185            var events = new List<ListChangedEventArgs> ();
186            people.ListChanged += (o, e) => events.Add(e);
187
188            people.Add(new Person { FirstName = "A", LastName = "B" });
189            AssertEqual(events);
190
191            events.Clear();
192            people.Clear();
193            AssertEqual(events, new ListChangedEventArgs(ListChangedType.Reset, 0, -1));
194
195            events.Clear();
196            people.AddRange(new[]{
197                new Person { FirstName = "1", LastName = "2" },
198                new Person { FirstName = "<", LastName = ">" },
199            });
200            AssertEqual(events);
201
202            events.Clear();
203            var p = new Person { FirstName = "{", LastName = "}" };
204            people.Insert(1, p);
205            AssertEqual(events, new ListChangedEventArgs(ListChangedType.ItemAdded, 1, -1));
206
207            events.Clear();
208            Assert.IsTrue(people.Remove(p));
209            AssertEqual(events);
210
211            events.Clear();
212            people.RemoveAt(0);
213            AssertEqual(events, new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1));
214
215            events.Clear();
216            people[0] = p;
217            AssertEqual(events,
218                new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1),
219                new ListChangedEventArgs(ListChangedType.ItemAdded, 0, -1));
220        }
221
222        static void AssertEqual(List<ListChangedEventArgs> actual, params ListChangedEventArgs[] expected)
223        {
224            Assert.AreEqual(expected.Length, actual.Count);
225            for (int i = 0; i < expected.Length; ++i)
226            {
227                Assert.AreEqual(expected[i].ListChangedType, actual[i].ListChangedType, "ListChangedEventArgs.ListChangedType");
228                Assert.AreEqual(expected[i].NewIndex, actual[i].NewIndex, "ListChangedEventArgs.NewIndex");
229                Assert.AreEqual(expected[i].OldIndex, actual[i].OldIndex, "ListChangedEventArgs.OldIndex");
230            }
231        }
232
233        [Test]
234        public void ListChanged_WithSource()
235        {
236            // When is ListChanged emitted?
237            // It's not always when you think it would be.
238            var people = new EntitySet<Person>();
239            var events = new List<ListChangedEventArgs>();
240            people.ListChanged += (o, e) => events.Add(e);
241
242            // This is also true if Enumerable.Empty<Person>() is used here.
243            people.SetSource(new[]{
244                new Person { FirstName = "(", LastName = ")" },
245            });
246            AssertEqual(events);
247            Assert.IsTrue(people.IsDeferred);
248
249            // *Initial* Add()/AddRange() is ignored.
250            people.Add(new Person { FirstName = "A", LastName = "B" });
251            people.AddRange(new[]{
252                new Person { FirstName = "1", LastName = "2" },
253                new Person { FirstName = "<", LastName = ">" },
254            });
255            Assert.IsTrue(people.HasLoadedOrAssignedValues);
256            Assert.IsTrue(people.IsDeferred);
257            AssertEqual(events);
258
259            events.Clear();
260            people.Clear();
261            AssertEqual(events, 
262                new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1),
263                new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1),
264                new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1),
265                new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1),
266                new ListChangedEventArgs(ListChangedType.Reset, 0, -1));
267            Assert.IsFalse(people.IsDeferred);
268
269            // Add()/AddRange() after a Clear has events.
270            events.Clear();
271            people.Add(new Person { FirstName = "A", LastName = "B" });
272            people.AddRange(new[]{
273                new Person { FirstName = "1", LastName = "2" },
274                new Person { FirstName = "<", LastName = ">" },
275            });
276            AssertEqual(events, 
277                new ListChangedEventArgs(ListChangedType.ItemAdded, 0, -1),
278                new ListChangedEventArgs(ListChangedType.ItemAdded, 1, -1),
279                new ListChangedEventArgs(ListChangedType.ItemAdded, 2, -1));
280
281            events.Clear();
282            var p = new Person { FirstName = "{", LastName = "}" };
283            people.Insert(1, p);
284            AssertEqual(events, new ListChangedEventArgs(ListChangedType.ItemAdded, 1, -1));
285
286            events.Clear();
287            Assert.IsTrue(people.Remove(p));
288            AssertEqual(events, new ListChangedEventArgs(ListChangedType.ItemDeleted, 1, -1));
289
290            events.Clear();
291            people.RemoveAt(0);
292            AssertEqual(events, new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1));
293
294            events.Clear();
295            people[0] = p;
296            AssertEqual(events,
297                new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1),
298                new ListChangedEventArgs(ListChangedType.ItemAdded, 0, -1));
299        }
300
301        [Test]
302        public void Remove()
303        {
304            var people = new EntitySet<Person>();
305            var events = new List<ListChangedEventArgs>();
306            people.ListChanged += (o, e) => events.Add(e);
307
308            people.SetSource(new[]{
309                new Person { FirstName = "(", LastName = ")" },
310            });
311            Assert.IsTrue(people.IsDeferred);
312            Assert.IsFalse(people.Remove(null));
313            AssertEqual(events);
314            events.Clear();
315            Assert.IsTrue(people.IsDeferred);
316
317            var p = people[0];
318            Assert.IsTrue(people.Remove(p));
319            Assert.IsFalse(people.IsDeferred);
320            Assert.AreEqual(0, people.Count);
321            AssertEqual(events, 
322                new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1));
323        }
324
325        [Test]
326        public void SanityChecking()
327        {
328            var people = new EntitySet<Person>();
329            bool changed = false;
330            people.ListChanged += (o, e) => {
331                changed = true;
332            };
333
334            Assert.IsFalse(people.HasLoadedOrAssignedValues);
335            Assert.AreEqual(0, people.Count);
336            Assert.IsFalse(people.IsDeferred);
337
338            people.Add(new Person { FirstName = "A", LastName = "B" });
339            Assert.IsTrue(people.HasLoadedOrAssignedValues);
340            Assert.AreEqual(1, people.Count);
341            Assert.IsFalse(people.IsDeferred);
342            // WTF?!
343            Assert.IsFalse(changed);
344
345            changed = false;
346            people.Add(new Person { FirstName = "1", LastName = "2" });
347            Assert.IsTrue(people.HasLoadedOrAssignedValues);
348            Assert.AreEqual(2, people.Count);
349            // WTF?!
350            Assert.IsFalse(changed);
351
352
353            changed = false;
354            people.RemoveAt(0);
355            Assert.IsTrue(people.HasLoadedOrAssignedValues);
356            Assert.AreEqual(1, people.Count);
357            Assert.IsFalse(people.IsDeferred);
358            Assert.IsTrue(changed);
359        }
360
361        [Test]
362        public void SetSource_EntitySourceCanBeNull()
363        {
364            var entities = new EntitySet<Person>();
365            entities.SetSource(null);
366        }
367
368        [Test]
369        public void SetSource_HasLoadedOrAssignedValues_Is_False_Until_Enumeration()
370        {
371            var people = new EntitySet<Person>();
372
373            Assert.IsFalse(people.HasLoadedOrAssignedValues);
374
375            people.SetSource(new[]{
376                new Person { FirstName = "1", LastName = "2" }
377            });
378
379            Assert.IsTrue(people.IsDeferred);
380            Assert.IsFalse(people.HasLoadedOrAssignedValues);
381            Assert.AreEqual(1, people.Count());
382            Assert.IsTrue(people.HasLoadedOrAssignedValues);
383            Assert.IsFalse(people.IsDeferred);
384        }
385
386        [Test]
387        public void SetSource_HasLoadedOrAssignedValues_Is_False_Until_Count()
388        {
389            var people = new EntitySet<Person>();
390
391            people.SetSource(new[]{
392                new Person { FirstName = "1", LastName = "2" }
393            });
394
395            Assert.IsTrue(people.IsDeferred);
396            Assert.IsFalse(people.HasLoadedOrAssignedValues);
397            Assert.AreEqual(1, people.Count);
398            Assert.IsTrue(people.HasLoadedOrAssignedValues);
399            Assert.IsFalse(people.IsDeferred);
400        }
401
402        [Test]
403        public void SetSource_ThenAddIsFine()
404        {
405            var people = new EntitySet<Person>();
406
407            Assert.IsFalse(people.HasLoadedOrAssignedValues);
408            
409            people.SetSource(new[]{
410                new Person { FirstName = "1", LastName = "2" }
411            });
412            Assert.IsTrue(people.IsDeferred);
413            Assert.IsFalse(people.HasLoadedOrAssignedValues);
414            people.Add(new Person { FirstName = "A", LastName = "B" });
415            Assert.IsTrue(people.HasLoadedOrAssignedValues);
416            Assert.IsTrue(people.IsDeferred);
417            Assert.AreEqual(2, people.Count);
418        }
419
420        [Test]
421        public void SetSource_ThenSetSourceIsValid()
422        {
423            var people = new EntitySet<Person>();
424
425            people.SetSource(new[]{
426                new Person { FirstName = "1", LastName = "2" }
427            });
428            
429            Assert.IsTrue(people.IsDeferred);
430
431            people.SetSource(new[]{
432                new Person { FirstName = "A", LastName = "B" } 
433            });
434
435            Assert.IsTrue(people.IsDeferred);
436        }
437    }
438}