CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
LinqObservableCollectionTest.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="LinqObservableCollectionTest.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>no summary</summary>
7//-----------------------------------------------------------------------
8using Csla;
9using System;
10using System.Collections.ObjectModel;
11using System.Collections.Generic;
12using System.Collections;
13using System.Linq;
15using UnitDriven;
16using Csla.TestHelpers;
17
18#if NUNIT
19using NUnit.Framework;
20using TestClass = NUnit.Framework.TestFixtureAttribute;
21using TestInitialize = NUnit.Framework.SetUpAttribute;
22using TestCleanup = NUnit.Framework.TearDownAttribute;
23using TestMethod = NUnit.Framework.TestAttribute;
24using TestSetup = NUnit.Framework.SetUpAttribute;
25#elif MSTEST
26using Microsoft.VisualStudio.TestTools.UnitTesting;
27#endif
28
30{
35 [TestClass]
37 {
38 private static TestDIContext _testDIContext;
39
41 public static void ClassInitialize(TestContext context)
42 {
43 _testDIContext = TestDIContextFactory.CreateDefaultContext();
44 }
45
46#if !WINDOWS_PHONE
47 [TestMethod]
48 public void Blb2Loc_WhereOnly()
49 {
50 IDataPortal<TestList> dataPortal = _testDIContext.CreateDataPortal<TestList>();
51
52 var source = dataPortal.Create();
53 var synced = source.ToSyncList(c => c.Id > 100);
54 Assert.AreEqual(3, synced.Count);
55 }
56#endif
57
58 [TestMethod]
59 public void Blb2Loc()
60 {
61 IDataPortal<TestList> dataPortal = _testDIContext.CreateDataPortal<TestList>();
62
63 var source = dataPortal.Create();
64 var synced = source.ToSyncList(from r in source
65 where r.Id > 100
66 select r);
67 Assert.AreEqual(3, synced.Count);
68 }
69
70 [TestMethod]
71 public void Blb2Loc_Ordered()
72 {
73 IDataPortal<TestList> dataPortal = _testDIContext.CreateDataPortal<TestList>();
74
75 var source = dataPortal.Create();
76 var synced = source.ToSyncList(from r in source
77 orderby r.Name
78 select r);
79 Assert.AreEqual(453, synced[0].Id);
80 Assert.AreEqual(123, synced[3].Id);
81 }
82
83 [TestMethod]
85 {
86 IDataPortal<TestList> dataPortal = _testDIContext.CreateDataPortal<TestList>();
87
88 var source = dataPortal.Create();
89 var synced = (from r in source
90 where r.Id > 100
91 select r).ToSyncList(source);
92 Assert.AreEqual(3, synced.Count);
93 }
94
95 [TestMethod]
96 public void Blb2Loc_Add()
97 {
98 IDataPortal<TestList> dataPortal = _testDIContext.CreateDataPortal<TestList>();
99 IChildDataPortal<TestItem> childDataPortal = _testDIContext.CreateChildDataPortal<TestItem>();
100
101 var source = dataPortal.Create();
102 var query = from r in source
103 where r.Id > 100
104 select r;
105 var synced = source.ToSyncList(query);
106
107 var newItem = childDataPortal.FetchChild(432, "New item");
108 synced.Add(newItem);
109
110 Assert.AreEqual(4, synced.Count, "synced should have item");
111 Assert.AreEqual(5, source.Count, "source should have item");
112 Assert.IsTrue(synced.Contains(newItem), "synced should contain new item");
113 Assert.IsTrue(source.Contains(newItem), "source should contain new item");
114 }
115
116 [TestMethod]
117 public void Blb2Loc_Remove()
118 {
119 IDataPortal<TestList> dataPortal = _testDIContext.CreateDataPortal<TestList>();
120
121 var source = dataPortal.Create();
122 var query = from r in source
123 where r.Id > 100
124 select r;
125 var synced = source.ToSyncList(query);
126
127 var oldItem = synced[0];
128 synced.RemoveAt(0);
129
130 Assert.AreEqual(2, synced.Count, "synced count wrong");
131 Assert.AreEqual(3, source.Count, "source count wrong");
132 Assert.IsFalse(synced.Contains(oldItem), "synced should not contain item");
133 Assert.IsFalse(source.Contains(oldItem), "source should not contain item");
134 }
135
136 [TestMethod]
138 {
139 IDataPortal<TestList> dataPortal = _testDIContext.CreateDataPortal<TestList>();
140
141 var source = dataPortal.Create();
142 var query = from r in source
143 where r.Id > 100
144 select r;
145 var synced = source.ToSyncList(query);
146 int count = 0;
147 foreach (var item in synced)
148 count++;
149 Assert.AreEqual(3, count, "Calculated count wrong (br)");
150 Assert.AreEqual(3, synced.Count, "Synced count wrong (br)");
151 Assert.AreEqual(4, source.Count, "source count wrong (br)");
152 Assert.AreEqual(0, synced.Where(_ => _.Id == 12).Count(), "synced contains 12");
153
154 source.RemoveAt(3);
155
156 count = 0;
157 foreach (var item in synced)
158 count++;
159
160 Assert.AreEqual(3, count, "Calculated count wrong");
161 Assert.AreEqual(3, synced.Count, "Synced count wrong");
162 Assert.AreEqual(3, source.Count, "source count wrong");
163 Assert.AreEqual(0, synced.Where(_ => _.Id == 12).Count(), "synced contains 12");
164 }
165
166 [TestMethod]
167 public void Create()
168 {
169 var source = new ObservableCollection<string>() { "a", "b", "c" };
170 var query = from r in source
171 where r == "b"
172 select r;
173 var obj = new LinqObservableCollection<string>(source, query);
174 Assert.IsNotNull(obj);
175 Assert.AreEqual(1, obj.Count);
176 Assert.AreEqual("b", obj[0]);
177 }
178
179 [TestMethod]
180 public void AddItem()
181 {
182 var source = new ObservableCollection<string>() { "a", "b", "c" };
183 var query = from r in source
184 where r == "b"
185 select r;
186 var obj = new LinqObservableCollection<string>(source, query);
187
188 bool sourceChanged = false;
189 bool objChanged = false;
190 source.CollectionChanged += (o, e) =>
191 {
192 sourceChanged = true;
193 };
194 obj.CollectionChanged += (o, e) =>
195 {
196 objChanged = true;
197 };
198 source.Add("z");
199 Assert.IsTrue(sourceChanged, "SourceChanged");
200 Assert.IsTrue(objChanged, "objChanged");
201 Assert.AreEqual(4, source.Count);
202 Assert.AreEqual(2, obj.Count);
203 Assert.AreEqual("z", source[3]);
204 Assert.AreEqual("z", obj[1]);
205
206 sourceChanged = false;
207 objChanged = false;
208 obj.Add("x");
209 Assert.IsTrue(sourceChanged, "SourceChanged");
210 Assert.IsTrue(objChanged, "objChanged");
211 Assert.AreEqual(5, source.Count);
212 Assert.AreEqual(3, obj.Count);
213 Assert.AreEqual("x", source[4]);
214 Assert.AreEqual("x", obj[2]);
215 }
216
217 [TestMethod]
218 public void InsertItem()
219 {
220 var source = new ObservableCollection<string>() { "a", "b", "c" };
221 var query = from r in source
222 where r == "b"
223 select r;
224 var obj = new LinqObservableCollection<string>(source, query);
225
226 bool sourceChanged = false;
227 bool objChanged = false;
228 source.CollectionChanged += (o, e) =>
229 {
230 sourceChanged = true;
231 };
232 obj.CollectionChanged += (o, e) =>
233 {
234 objChanged = true;
235 };
236 source.Insert(1, "z");
237 Assert.IsTrue(sourceChanged, "SourceChanged");
238 Assert.IsTrue(objChanged, "objChanged");
239 Assert.AreEqual(4, source.Count);
240 Assert.AreEqual(2, obj.Count);
241 Assert.AreEqual("z", source[1]);
242 Assert.AreEqual("z", obj[1]);
243
244 sourceChanged = false;
245 objChanged = false;
246 obj.Insert(1, "x");
247 Assert.IsTrue(sourceChanged, "SourceChanged");
248 Assert.IsTrue(objChanged, "objChanged");
249 Assert.AreEqual(5, source.Count);
250 Assert.AreEqual(3, obj.Count);
251 Assert.AreEqual("x", source[1]);
252 Assert.AreEqual("x", obj[1]);
253 }
254
255 [TestMethod]
256 public void RemoveAt()
257 {
258 var source = new ObservableCollection<string>() { "a", "b", "c", "bb" };
259 var query = from r in source
260 where r[0] == 'b'
261 select r;
262 var obj = new LinqObservableCollection<string>(source, query);
263
264 bool sourceChanged = false;
265 bool objChanged = false;
266 source.CollectionChanged += (o, e) =>
267 {
268 sourceChanged = true;
269 };
270 obj.CollectionChanged += (o, e) =>
271 {
272 objChanged = true;
273 };
274 source.RemoveAt(1);
275 Assert.IsTrue(sourceChanged, "SourceChanged");
276 Assert.IsTrue(objChanged, "objChanged");
277 Assert.AreEqual(3, source.Count);
278 Assert.AreEqual(1, obj.Count);
279 Assert.AreEqual("c", source[1]);
280 Assert.AreEqual("bb", obj[0]);
281
282 sourceChanged = false;
283 objChanged = false;
284 obj.RemoveAt(0);
285 Assert.IsTrue(sourceChanged, "SourceChanged");
286 Assert.IsTrue(objChanged, "objChanged");
287 Assert.AreEqual(2, source.Count);
288 Assert.AreEqual(0, obj.Count);
289 Assert.AreEqual("a", source[0]);
290 Assert.AreEqual("c", source[1]);
291 }
292
293 [TestMethod]
294 public void RemoveItem()
295 {
296 var source = new ObservableCollection<string>() { "a", "b", "c", "bb" };
297 var query = from r in source
298 where r[0] == 'b'
299 select r;
300 var obj = new LinqObservableCollection<string>(source, query);
301
302 bool sourceChanged = false;
303 bool objChanged = false;
304 source.CollectionChanged += (o, e) =>
305 {
306 sourceChanged = true;
307 };
308 obj.CollectionChanged += (o, e) =>
309 {
310 objChanged = true;
311 };
312 source.Remove("b");
313 Assert.IsTrue(sourceChanged, "SourceChanged");
314 Assert.IsTrue(objChanged, "objChanged");
315 Assert.AreEqual(3, source.Count);
316 Assert.AreEqual(1, obj.Count);
317 Assert.AreEqual("c", source[1]);
318 Assert.AreEqual("bb", obj[0]);
319
320 sourceChanged = false;
321 objChanged = false;
322 obj.Remove("bb");
323 Assert.IsTrue(sourceChanged, "SourceChanged");
324 Assert.IsTrue(objChanged, "objChanged");
325 Assert.AreEqual(2, source.Count);
326 Assert.AreEqual(0, obj.Count);
327 Assert.AreEqual("a", source[0]);
328 Assert.AreEqual("c", source[1]);
329 }
330
331 [TestMethod]
332 public void ReplaceItem()
333 {
334 var source = new ObservableCollection<string>() { "a", "b", "c" };
335 var query = from r in source
336 where r == "b"
337 select r;
338 var obj = new LinqObservableCollection<string>(source, query);
339
340 bool sourceChanged = false;
341 bool objChanged = false;
342 source.CollectionChanged += (o, e) =>
343 {
344 sourceChanged = true;
345 };
346 obj.CollectionChanged += (o, e) =>
347 {
348 objChanged = true;
349 };
350 source[0] = "z";
351 Assert.IsTrue(sourceChanged, "SourceChanged");
352 Assert.IsFalse(objChanged, "objChanged");
353 Assert.AreEqual(3, source.Count);
354 Assert.AreEqual(1, obj.Count);
355 Assert.AreEqual("z", source[0]);
356
357 source[1] = "x";
358 Assert.IsTrue(sourceChanged, "SourceChanged");
359 Assert.IsTrue(objChanged, "objChanged");
360 Assert.AreEqual(3, source.Count);
361 Assert.AreEqual(1, obj.Count);
362 Assert.AreEqual("x", source[1]);
363 Assert.AreEqual("x", obj[0]);
364
365 obj[0] = "r";
366 Assert.IsTrue(sourceChanged, "SourceChanged");
367 Assert.IsTrue(objChanged, "objChanged");
368 Assert.AreEqual(3, source.Count);
369 Assert.AreEqual(1, obj.Count);
370 Assert.AreEqual("r", source[1]);
371 Assert.AreEqual("r", obj[0]);
372 }
373
374 [TestMethod]
375 public void MoveItem()
376 {
377 var source = new ObservableCollection<string>() { "a", "b", "c" };
378 var query = from r in source
379 where r == "b"
380 select r;
381 var obj = new LinqObservableCollection<string>(source, query);
382
383 bool sourceChanged = false;
384 bool objChanged = false;
385 source.CollectionChanged += (o, e) =>
386 {
387 sourceChanged = true;
388 };
389 obj.CollectionChanged += (o, e) =>
390 {
391 objChanged = true;
392 };
393 source.Move(1, 2);
394 Assert.IsTrue(sourceChanged, "SourceChanged");
395 Assert.IsFalse(objChanged, "objChanged");
396 Assert.AreEqual(3, source.Count);
397 Assert.AreEqual(1, obj.Count);
398 Assert.AreEqual("b", obj[0]);
399 Assert.AreEqual("b", source[2]);
400 Assert.AreEqual("c", source[1]);
401 }
402
403 [TestMethod]
404 public void ClearSource()
405 {
406 var source = new ObservableCollection<string>() { "a", "b", "c" };
407 var query = from r in source
408 where r == "b"
409 select r;
410 var obj = new LinqObservableCollection<string>(source, query);
411
412 bool sourceChanged = false;
413 bool objChanged = false;
414 source.CollectionChanged += (o, e) =>
415 {
416 sourceChanged = true;
417 };
418 obj.CollectionChanged += (o, e) =>
419 {
420 objChanged = true;
421 };
422 source.Clear();
423 Assert.IsTrue(sourceChanged, "SourceChanged");
424 Assert.IsTrue(objChanged, "objChanged");
425 Assert.AreEqual(0, source.Count);
426 Assert.AreEqual(0, obj.Count);
427 }
428
429 [TestMethod]
430 public void ClearView()
431 {
432 var source = new ObservableCollection<string>() { "a", "b", "c" };
433 var query = from r in source
434 where r == "b"
435 select r;
436 var obj = new LinqObservableCollection<string>(source, query);
437
438 bool sourceChanged = false;
439 bool objChanged = false;
440 source.CollectionChanged += (o, e) =>
441 {
442 sourceChanged = true;
443 };
444 obj.CollectionChanged += (o, e) =>
445 {
446 objChanged = true;
447 };
448 obj.Clear();
449 Assert.IsTrue(sourceChanged, "SourceChanged");
450 Assert.IsTrue(objChanged, "objChanged");
451 Assert.AreEqual(2, source.Count);
452 Assert.AreEqual(0, obj.Count);
453 Assert.AreEqual("a", source[0]);
454 Assert.AreEqual("c", source[1]);
455 }
456 }
457
459 public class TestList : BusinessListBase<TestList, TestItem>
460 {
461 public TestList()
462 {
463 }
464
466 {
467 return dataPortal.Create();
468 }
469
470 [Create]
471 private void Create([Inject] IChildDataPortal<TestItem> childDataPortal)
472 {
473 Add(childDataPortal.FetchChild(123, "Zebra has stripes"));
474 Add(childDataPortal.FetchChild(2233, "Software is neat"));
475 Add(childDataPortal.FetchChild(453, "Run, the sky is falling"));
476 Add(childDataPortal.FetchChild(12, "What is new?"));
477 }
478 }
479
481 public class TestItem : BusinessBase<TestItem>
482 {
483 public static PropertyInfo<int> IdProperty = RegisterProperty<int>(c => c.Id);
484 public int Id
485 {
486 get { return GetProperty(IdProperty); }
487 set { SetProperty(IdProperty, value); }
488 }
489
490 public static PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
491 public string Name
492 {
493 get { return GetProperty(NameProperty); }
494 set { SetProperty(NameProperty, value); }
495 }
496
497 private void Child_Fetch(int id, string name)
498 {
499 using (BypassPropertyChecks)
500 {
501 Id = id;
502 Name = name;
503 }
504 }
505 }
506}
This is the base class from which most business objects will be derived.
Definition: BusinessBase.cs:38
This is the base class from which most business collections or lists will be derived.
Synchronized view over a source list, filtered, sorted and ordered based on a query result.
Maintains metadata about a property.
This is a test class for LinqObservableCollectionTest and is intended to contain all LinqObservableCo...
static PropertyInfo< int > IdProperty
static PropertyInfo< string > NameProperty
static TestList NewTestList(IDataPortal< TestList > dataPortal)
Type to carry context information for DI in unit tests
Interface defining the members of the child data portal type.
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
object Create(params object[] criteria)
Called by a factory method in a business class to create a new object, which is loaded with default v...
@ Serializable
Prevents updating or inserting until the transaction is complete.