PageRenderTime 36ms CodeModel.GetById 14ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Data.Linq/Test/EntitySetTest.cs

http://github.com/mono/mono
C# | 443 lines | 376 code | 59 blank | 8 comment | 1 complexity | e86fc477770eed9998729953b0313cfc 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, new ListChangedEventArgs(ListChangedType.ItemAdded, 0, -1));
190
191            events.Clear();
192            people.Clear();
193            AssertEqual(events,
194                new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1),
195                new ListChangedEventArgs(ListChangedType.Reset, 0, -1));
196
197            events.Clear();
198            people.AddRange(new[]{
199                new Person { FirstName = "1", LastName = "2" },
200                new Person { FirstName = "<", LastName = ">" },
201            });
202            AssertEqual(events,
203                new ListChangedEventArgs(ListChangedType.ItemAdded, 0, -1),
204                new ListChangedEventArgs(ListChangedType.ItemAdded, 1, -1));
205
206            events.Clear();
207            var p = new Person { FirstName = "{", LastName = "}" };
208            people.Insert(1, p);
209            AssertEqual(events, new ListChangedEventArgs(ListChangedType.ItemAdded, 1, -1));
210
211            events.Clear();
212            Assert.IsTrue(people.Remove(p));
213            AssertEqual(events, new ListChangedEventArgs(ListChangedType.ItemDeleted, 1, -1));
214
215            events.Clear();
216            people.RemoveAt(0);
217            AssertEqual(events, new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1));
218
219            events.Clear();
220            people[0] = p;
221            AssertEqual(events,
222                new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1),
223                new ListChangedEventArgs(ListChangedType.ItemAdded, 0, -1));
224        }
225
226        static void AssertEqual(List<ListChangedEventArgs> actual, params ListChangedEventArgs[] expected)
227        {
228            Assert.AreEqual(expected.Length, actual.Count);
229            for (int i = 0; i < expected.Length; ++i)
230            {
231                Assert.AreEqual(expected[i].ListChangedType, actual[i].ListChangedType, "ListChangedEventArgs.ListChangedType");
232                Assert.AreEqual(expected[i].NewIndex, actual[i].NewIndex, "ListChangedEventArgs.NewIndex");
233                Assert.AreEqual(expected[i].OldIndex, actual[i].OldIndex, "ListChangedEventArgs.OldIndex");
234            }
235        }
236
237        [Test]
238        public void ListChanged_WithSource()
239        {
240            // When is ListChanged emitted?
241            // It's not always when you think it would be.
242            var people = new EntitySet<Person>();
243            var events = new List<ListChangedEventArgs>();
244            people.ListChanged += (o, e) => events.Add(e);
245
246            // This is also true if Enumerable.Empty<Person>() is used here.
247            people.SetSource(new[]{
248                new Person { FirstName = "(", LastName = ")" },
249            });
250            AssertEqual(events);
251            Assert.IsTrue(people.IsDeferred);
252
253            // *Initial* Add()/AddRange() is ignored.
254            people.Add(new Person { FirstName = "A", LastName = "B" });
255            people.AddRange(new[]{
256                new Person { FirstName = "1", LastName = "2" },
257                new Person { FirstName = "<", LastName = ">" },
258            });
259            Assert.IsTrue(people.HasLoadedOrAssignedValues);
260            Assert.IsTrue(people.IsDeferred);
261            AssertEqual(events,
262                new ListChangedEventArgs(ListChangedType.ItemAdded, 0, -1),
263                new ListChangedEventArgs(ListChangedType.ItemAdded, 1, -1),
264                new ListChangedEventArgs(ListChangedType.ItemAdded, 2, -1));
265
266            events.Clear();
267            people.Clear();
268            AssertEqual(events, 
269                new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1),
270                new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1),
271                new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1),
272                new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1),
273                new ListChangedEventArgs(ListChangedType.Reset, 0, -1));
274            Assert.IsFalse(people.IsDeferred);
275
276            // Add()/AddRange() after a Clear has events.
277            events.Clear();
278            people.Add(new Person { FirstName = "A", LastName = "B" });
279            people.AddRange(new[]{
280                new Person { FirstName = "1", LastName = "2" },
281                new Person { FirstName = "<", LastName = ">" },
282            });
283            AssertEqual(events, 
284                new ListChangedEventArgs(ListChangedType.ItemAdded, 0, -1),
285                new ListChangedEventArgs(ListChangedType.ItemAdded, 1, -1),
286                new ListChangedEventArgs(ListChangedType.ItemAdded, 2, -1));
287
288            events.Clear();
289            var p = new Person { FirstName = "{", LastName = "}" };
290            people.Insert(1, p);
291            AssertEqual(events, new ListChangedEventArgs(ListChangedType.ItemAdded, 1, -1));
292
293            events.Clear();
294            Assert.IsTrue(people.Remove(p));
295            AssertEqual(events, new ListChangedEventArgs(ListChangedType.ItemDeleted, 1, -1));
296
297            events.Clear();
298            people.RemoveAt(0);
299            AssertEqual(events, new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1));
300
301            events.Clear();
302            people[0] = p;
303            AssertEqual(events,
304                new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1),
305                new ListChangedEventArgs(ListChangedType.ItemAdded, 0, -1));
306        }
307
308        [Test]
309        public void Remove()
310        {
311            var people = new EntitySet<Person>();
312            var events = new List<ListChangedEventArgs>();
313            people.ListChanged += (o, e) => events.Add(e);
314
315            people.SetSource(new[]{
316                new Person { FirstName = "(", LastName = ")" },
317            });
318            Assert.IsTrue(people.IsDeferred);
319            Assert.IsFalse(people.Remove(null));
320            AssertEqual(events);
321            events.Clear();
322            Assert.IsTrue(people.IsDeferred);
323
324            var p = people[0];
325            Assert.IsTrue(people.Remove(p));
326            Assert.IsFalse(people.IsDeferred);
327            Assert.AreEqual(0, people.Count);
328            AssertEqual(events, 
329                new ListChangedEventArgs(ListChangedType.ItemDeleted, 0, -1));
330        }
331
332        [Test]
333        public void SanityChecking()
334        {
335            var people = new EntitySet<Person>();
336            bool changed = false;
337            people.ListChanged += (o, e) => {
338                changed = true;
339            };
340
341            Assert.IsFalse(people.HasLoadedOrAssignedValues);
342            Assert.AreEqual(0, people.Count);
343            Assert.IsFalse(people.IsDeferred);
344
345            people.Add(new Person { FirstName = "A", LastName = "B" });
346            Assert.IsTrue(people.HasLoadedOrAssignedValues);
347            Assert.AreEqual(1, people.Count);
348            Assert.IsFalse(people.IsDeferred);
349            Assert.IsTrue(changed);
350
351            changed = false;
352            people.Add(new Person { FirstName = "1", LastName = "2" });
353            Assert.IsTrue(people.HasLoadedOrAssignedValues);
354            Assert.AreEqual(2, people.Count);
355            Assert.IsTrue(changed);
356
357
358            changed = false;
359            people.RemoveAt(0);
360            Assert.IsTrue(people.HasLoadedOrAssignedValues);
361            Assert.AreEqual(1, people.Count);
362            Assert.IsFalse(people.IsDeferred);
363            Assert.IsTrue(changed);
364        }
365
366        [Test]
367        public void SetSource_EntitySourceCanBeNull()
368        {
369            var entities = new EntitySet<Person>();
370            entities.SetSource(null);
371        }
372
373        [Test]
374        public void SetSource_HasLoadedOrAssignedValues_Is_False_Until_Enumeration()
375        {
376            var people = new EntitySet<Person>();
377
378            Assert.IsFalse(people.HasLoadedOrAssignedValues);
379
380            people.SetSource(new[]{
381                new Person { FirstName = "1", LastName = "2" }
382            });
383
384            Assert.IsTrue(people.IsDeferred);
385            Assert.IsFalse(people.HasLoadedOrAssignedValues);
386            Assert.AreEqual(1, people.Count());
387            Assert.IsTrue(people.HasLoadedOrAssignedValues);
388            Assert.IsFalse(people.IsDeferred);
389        }
390
391        [Test]
392        public void SetSource_HasLoadedOrAssignedValues_Is_False_Until_Count()
393        {
394            var people = new EntitySet<Person>();
395
396            people.SetSource(new[]{
397                new Person { FirstName = "1", LastName = "2" }
398            });
399
400            Assert.IsTrue(people.IsDeferred);
401            Assert.IsFalse(people.HasLoadedOrAssignedValues);
402            Assert.AreEqual(1, people.Count);
403            Assert.IsTrue(people.HasLoadedOrAssignedValues);
404            Assert.IsFalse(people.IsDeferred);
405        }
406
407        [Test]
408        public void SetSource_ThenAddIsFine()
409        {
410            var people = new EntitySet<Person>();
411
412            Assert.IsFalse(people.HasLoadedOrAssignedValues);
413            
414            people.SetSource(new[]{
415                new Person { FirstName = "1", LastName = "2" }
416            });
417            Assert.IsTrue(people.IsDeferred);
418            Assert.IsFalse(people.HasLoadedOrAssignedValues);
419            people.Add(new Person { FirstName = "A", LastName = "B" });
420            Assert.IsTrue(people.HasLoadedOrAssignedValues);
421            Assert.IsTrue(people.IsDeferred);
422            Assert.AreEqual(2, people.Count);
423        }
424
425        [Test]
426        public void SetSource_ThenSetSourceIsValid()
427        {
428            var people = new EntitySet<Person>();
429
430            people.SetSource(new[]{
431                new Person { FirstName = "1", LastName = "2" }
432            });
433            
434            Assert.IsTrue(people.IsDeferred);
435
436            people.SetSource(new[]{
437                new Person { FirstName = "A", LastName = "B" } 
438            });
439
440            Assert.IsTrue(people.IsDeferred);
441        }
442    }
443}