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.
csla.netcore.test/Basic/BasicTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="BasicTests.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 System;
9using System.Collections.Generic;
10using System.Text;
11using System.Threading.Tasks;
12using Csla.TestHelpers;
13
14#if !NUNIT
15using Microsoft.VisualStudio.TestTools.UnitTesting;
16#else
17using NUnit.Framework;
18using TestClass = NUnit.Framework.TestFixtureAttribute;
19using TestInitialize = NUnit.Framework.SetUpAttribute;
20using TestCleanup = NUnit.Framework.TearDownAttribute;
21using TestMethod = NUnit.Framework.TestAttribute;
22#endif
23
25{
26 [TestClass]
27 public class BasicTests
28 {
29 private static TestDIContext _testDIContext;
30
32 public static void ClassInitialize(TestContext context)
33 {
34 _testDIContext = TestDIContextFactory.CreateDefaultContext();
35 }
36
37 [TestMethod]
39 {
41
42 Csla.Test.DataBinding.ParentEntity p = CreateParentEntityInstance();
43
44 p.NotUndoable = "something";
45 p.Data = "data";
46 p.BeginEdit();
47 p.NotUndoable = "something else";
48 p.Data = "new data";
49 p.CancelEdit();
50 //NotUndoable property points to a private field marked with [NotUndoable()]
51 //so its state is never copied when BeginEdit() is called
52 Assert.AreEqual("something else", p.NotUndoable);
53 //the Data property points to a field that is undoable, so it reverts
54 Assert.AreEqual("data", p.Data);
55 }
56
57 [TestMethod]
58 public void TestReadOnlyList()
59 {
61
62 //ReadOnlyList list = ReadOnlyList.GetReadOnlyList();
63 //Assert.AreEqual("Fetched", TestResults.GetResult("ReadOnlyList"));
64 }
65
66 [TestMethod]
67 public void TestNameValueList()
68 {
70
71 NameValueListObj nvList = GetNameValueListObjInstance();
72 Assert.AreEqual("Fetched", TestResults.GetResult("NameValueListObj"));
73
74 Assert.AreEqual("element_1", nvList[1].Value);
75
76 //won't work, because IsReadOnly is set to true after object is populated in the for
77 //loop in DataPortal_Fetch
78 //NameValueListObj.NameValuePair newPair = new NameValueListObj.NameValuePair(45, "something");
79
80 //nvList.Add(newPair);
81
82 //Assert.AreEqual("something", nvList[45].Value);
83 }
84
85 [TestMethod]
86 public void TestCommandBase()
87 {
89
90 IDataPortal<CommandObject> dataPortal = _testDIContext.CreateDataPortal<CommandObject>();
91 CommandObject obj = dataPortal.Create();
92 obj = dataPortal.Execute(obj);
93 Assert.AreEqual("Executed", obj.AProperty);
94 }
95
96 [TestMethod]
97 public void CreateGenRoot()
98 {
100
101 GenRoot root;
102 root = CreateGenRootInstance();
103 Assert.IsNotNull(root);
104 Assert.AreEqual("<new>", root.Data);
105 Assert.AreEqual("Created", TestResults.GetResult("GenRoot"));
106 Assert.AreEqual(true, root.IsNew);
107 Assert.AreEqual(false, root.IsDeleted);
108 Assert.AreEqual(true, root.IsDirty);
109 }
110
111 [TestMethod]
112 public void InheritanceUndo()
113 {
115
116 GenRoot root;
117 root = CreateGenRootInstance();
118 root.BeginEdit();
119 root.Data = "abc";
120 root.CancelEdit();
121
123 root = CreateGenRootInstance();
124 root.BeginEdit();
125 root.Data = "abc";
126 root.ApplyEdit();
127 }
128
129 [TestMethod]
130 public void CreateRoot()
131 {
133
134 Root root;
135 root = CreateRootInstance();
136 Assert.IsNotNull(root);
137 Assert.AreEqual("<new>", root.Data);
138 Assert.AreEqual("Created", TestResults.GetResult("Root"));
139 Assert.AreEqual(true, root.IsNew);
140 Assert.AreEqual(false, root.IsDeleted);
141 Assert.AreEqual(true, root.IsDirty);
142 }
143
144 [TestMethod]
145 public void AddChild()
146 {
148
149 Root root = CreateRootInstance();
150 root.Children.Add("1");
151 Assert.AreEqual(1, root.Children.Count);
152 Assert.AreEqual("1", root.Children[0].Data);
153 }
154
155 [TestMethod]
156 public void AddRemoveChild()
157 {
159
160 Root root = CreateRootInstance();
161 root.Children.Add("1");
162 root.Children.Remove(root.Children[0]);
163 Assert.AreEqual(0, root.Children.Count);
164 }
165
166 [TestMethod]
167 public void AddRemoveAddChild()
168 {
170
171 Root root = CreateRootInstance();
172 root.Children.Add("1");
173 root.BeginEdit();
174 root.Children.Remove(root.Children[0]);
175
176 root.Children.Add("2");
177 root.CancelEdit();
178
179 Assert.AreEqual(1, root.Children.Count);
180 Assert.AreEqual("1", root.Children[0].Data);
181 }
182
183 [TestMethod]
184 public void AddGrandChild()
185 {
187
188 Root root = CreateRootInstance();
189 root.Children.Add("1");
190 Child child = root.Children[0];
191 child.GrandChildren.Add("1");
192 Assert.AreEqual(1, child.GrandChildren.Count);
193 Assert.AreEqual("1", child.GrandChildren[0].Data);
194 }
195
196 [TestMethod]
198 {
200
201 Root root = CreateRootInstance();
202 root.Children.Add("1");
203 Child child = root.Children[0];
204 child.GrandChildren.Add("1");
205 child.GrandChildren.Remove(child.GrandChildren[0]);
206 Assert.AreEqual(0, child.GrandChildren.Count);
207 }
208
209 [TestMethod]
210 public void ClearChildList()
211 {
213
214 Root root = CreateRootInstance();
215 root.Children.Add("A");
216 root.Children.Add("B");
217 root.Children.Add("C");
218 root.Children.Clear();
219 Assert.AreEqual(0, root.Children.Count, "Count should be 0");
220 Assert.AreEqual(3, root.Children.DeletedCount, "Deleted count should be 3");
221 }
222
223 [TestMethod]
225 {
227
228 Root root = CreateRootInstance();
229 root.BeginEdit();
230 root.Children.Add("A");
231 root.BeginEdit();
232 root.Children.Add("B");
233 root.BeginEdit();
234 root.Children.Add("C");
235 root.ApplyEdit();
236 root.ApplyEdit();
237 root.ApplyEdit();
238 Assert.AreEqual(3, root.Children.Count);
239 }
240
241 [TestMethod]
243 {
245
246 Root root = CreateRootInstance();
247 root.BeginEdit();
248 root.Children.Add("A");
249 root.BeginEdit();
250 root.Children.Add("B");
251 root.BeginEdit();
252 root.Children.Add("C");
253 Child childC = root.Children[2];
254 Assert.AreEqual(true, root.Children.Contains(childC), "Child should be in collection");
255 root.Children.Remove(root.Children[0]);
256 root.Children.Remove(root.Children[0]);
257 root.Children.Remove(root.Children[0]);
258 Assert.AreEqual(false, root.Children.Contains(childC), "Child should not be in collection");
259 Assert.AreEqual(true, root.Children.ContainsDeleted(childC), "Deleted child should be in deleted collection");
260 root.ApplyEdit();
261 Assert.AreEqual(false, root.Children.ContainsDeleted(childC), "Deleted child should not be in deleted collection after first applyedit");
262 root.ApplyEdit();
263 Assert.AreEqual(false, root.Children.ContainsDeleted(childC), "Deleted child should not be in deleted collection after ApplyEdit");
264 root.ApplyEdit();
265 Assert.AreEqual(0, root.Children.Count, "No children should remain");
266 Assert.AreEqual(false, root.Children.ContainsDeleted(childC), "Deleted child should not be in deleted collection after third applyedit");
267 }
268
269 [TestMethod]
270 public void BasicEquality()
271 {
273
274 Root r1 = CreateRootInstance();
275 r1.Data = "abc";
276 Assert.AreEqual(true, r1.Equals(r1), "objects should be equal on instance compare");
277 Assert.AreEqual(true, Equals(r1, r1), "objects should be equal on static compare");
278
280 Root r2 = CreateRootInstance();
281 r2.Data = "xyz";
282 Assert.AreEqual(false, r1.Equals(r2), "objects should not be equal");
283 Assert.AreEqual(false, Equals(r1, r2), "objects should not be equal");
284
285 Assert.AreEqual(false, r1.Equals(null), "Objects should not be equal");
286 Assert.AreEqual(false, Equals(r1, null), "Objects should not be equal");
287 Assert.AreEqual(false, Equals(null, r2), "Objects should not be equal");
288 }
289
290 [TestMethod]
291 public void ChildEquality()
292 {
294
295 Root root = CreateRootInstance();
296 root.Children.Add("abc");
297 root.Children.Add("xyz");
298 root.Children.Add("123");
299 Child c1 = root.Children[0];
300 Child c2 = root.Children[1];
301 Child c3 = root.Children[2];
302 root.Children.Remove(c3);
303
304 Assert.AreEqual(true, c1.Equals(c1), "objects should be equal");
305 Assert.AreEqual(true, Equals(c1, c1), "objects should be equal");
306
307 Assert.AreEqual(false, c1.Equals(c2), "objects should not be equal");
308 Assert.AreEqual(false, Equals(c1, c2), "objects should not be equal");
309
310 Assert.AreEqual(false, c1.Equals(null), "objects should not be equal");
311 Assert.AreEqual(false, Equals(c1, null), "objects should not be equal");
312 Assert.AreEqual(false, Equals(null, c2), "objects should not be equal");
313
314 Assert.AreEqual(true, root.Children.Contains(c1), "Collection should contain c1");
315 Assert.AreEqual(true, root.Children.Contains(c2), "collection should contain c2");
316 Assert.AreEqual(false, root.Children.Contains(c3), "collection should not contain c3");
317 Assert.AreEqual(true, root.Children.ContainsDeleted(c3), "Deleted collection should contain c3");
318 }
319
320 [TestMethod]
321 public void DeletedListTest()
322 {
324
325 Root root = CreateRootInstance();
326 root.Children.Add("1");
327 root.Children.Add("2");
328 root.Children.Add("3");
329 root.BeginEdit();
330 root.Children.Remove(root.Children[0]);
331 root.Children.Remove(root.Children[0]);
332 root.ApplyEdit();
333
334 Root copy = root.Clone();
335
336 var deleted = copy.Children.GetDeletedList();
337
338 Assert.AreEqual(2, deleted.Count);
339 Assert.AreEqual("1", deleted[0].Data);
340 Assert.AreEqual("2", deleted[1].Data);
341 Assert.AreEqual(1, root.Children.Count);
342 }
343
344 [TestMethod]
346 {
348
349 Root root = CreateRootInstance();
350 root.Children.Add("1");
351 root.Children.Add("2");
352 root.Children.Add("3");
353 root.BeginEdit();
354 root.Children.Remove(root.Children[0]);
355 root.Children.Remove(root.Children[0]);
356
357 Root copy = root.Clone();
358
359 var deleted = copy.Children.GetDeletedList();
360
361 Assert.AreEqual(2, deleted.Count);
362 Assert.AreEqual("1", deleted[0].Data);
363 Assert.AreEqual("2", deleted[1].Data);
364 Assert.AreEqual(1, root.Children.Count);
365
366 root.CancelEdit();
367
368 deleted = root.Children.GetDeletedList();
369
370 Assert.AreEqual(0, deleted.Count);
371 Assert.AreEqual(3, root.Children.Count);
372
373 }
374
375 [TestMethod]
377 {
379
380 bool changed = false;
381 var obj = new RootList();
382 obj.ListChanged += (o, e) =>
383 {
384 changed = true;
385 };
386 var child = new RootListChild(); // object is marked as child
387
388 Assert.IsTrue(obj.RaiseListChangedEvents);
389 using (obj.SuppressListChangedEvents)
390 {
391 Assert.IsFalse(obj.RaiseListChangedEvents);
392
393 obj.Add(child);
394 }
395 Assert.IsFalse(changed, "Should not raise ListChanged event");
396 Assert.IsTrue(obj.RaiseListChangedEvents);
397 Assert.AreEqual(child, obj[0]);
398 }
399
400 [TestMethod]
401 public async Task ChildEditLevelClone()
402 {
404
405 var list = await CreateRootListInstanceAsync();
406 list.BeginEdit();
407 list.AddNew();
408 var clone = (RootList)((ICloneable)list).Clone();
409 clone.ApplyEdit();
410 }
411
412 [TestMethod]
413 public async Task ChildEditLevelDeleteClone()
414 {
416
417 var list = await CreateRootListInstanceAsync();
418 list.BeginEdit();
419 list.AddNew();
420 list.RemoveAt(0);
421 var clone = (RootList)((ICloneable)list).Clone();
422 clone.ApplyEdit();
423 }
424
425 [TestMethod]
426 public async Task UndoStateStack()
427 {
429
430 var obj = await CreateRootInstanceAsync(new Root.Criteria(""));
431 Assert.AreEqual("", obj.Data);
432 obj.BeginEdit();
433 obj.Data = "1";
434 obj.BeginEdit();
435 obj.Data = "2";
436 Assert.AreEqual("2", obj.Data);
437 obj.CancelEdit();
438 Assert.AreEqual("1", obj.Data);
439 obj.BeginEdit();
440 obj.Data = "2";
441 Assert.AreEqual(2, obj.GetEditLevel());
442 var clone = obj.Clone();
443 Assert.AreEqual(2, clone.GetEditLevel());
444 Assert.AreEqual("2", clone.Data);
445 clone.CancelEdit();
446 Assert.AreEqual("1", clone.Data);
447 clone.CancelEdit();
448 Assert.AreEqual("", clone.Data);
449 }
450
451 [TestCleanup]
453 {
455 }
456
457 private Root CreateRootInstance()
458 {
459 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
460 return dataPortal.Create(new Root.Criteria());
461 }
462
463 private async Task<Root> CreateRootInstanceAsync()
464 {
465 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
466 return await dataPortal.CreateAsync(new Root.Criteria());
467 }
468
469 private async Task<Root> CreateRootInstanceAsync(Root.Criteria criteria)
470 {
471 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
472 return await dataPortal.CreateAsync(criteria);
473 }
474
475 private async Task<RootList> CreateRootListInstanceAsync()
476 {
477 IDataPortal<RootList> dataPortal = _testDIContext.CreateDataPortal<RootList>();
478 return await dataPortal.CreateAsync();
479 }
480
481 private GenRoot CreateGenRootInstance()
482 {
483 IDataPortal<GenRoot> dataPortal = _testDIContext.CreateDataPortal<GenRoot>();
484 return dataPortal.Create(new GenRootBase.Criteria());
485 }
486
487 private Csla.Test.DataBinding.ParentEntity CreateParentEntityInstance()
488 {
489 IDataPortal<Csla.Test.DataBinding.ParentEntity> dataPortal = _testDIContext.CreateDataPortal<Csla.Test.DataBinding.ParentEntity>();
490 return dataPortal.Create();
491 }
492
493 private NameValueListObj GetNameValueListObjInstance()
494 {
495 IDataPortal<NameValueListObj> dataPortal = _testDIContext.CreateDataPortal<NameValueListObj>();
496 return dataPortal.Fetch();
497 }
498
499 }
500
501 public class FormSimulator
502 {
503 private readonly Core.BusinessBase _obj;
504
505 public FormSimulator(Core.BusinessBase obj)
506 {
507 this._obj.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Obj_IsDirtyChanged);
508 this._obj = obj;
509 }
510
511 private void Obj_IsDirtyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
512 { }
513 }
514
515 [Serializable()]
517 {
518 private readonly Core.BusinessBase _obj;
519
520 public SerializableListener(Core.BusinessBase obj)
521 {
522 this._obj.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Obj_IsDirtyChanged);
523 this._obj = obj;
524 }
525
526 public void Obj_IsDirtyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
527 { }
528 }
529}
T Clone()
Creates a clone of the object.
Definition: BusinessBase.cs:79
bool ContainsDeleted(C item)
Returns true if the internal deleted list contains the specified child object.
PropertyChangedEventHandler PropertyChanged
Implements a serialization-safe PropertyChanged event.
Definition: BindableBase.cs:40
static void ClassInitialize(TestContext context)
override bool Equals(object obj)
override bool IsDirty
start editing
void Obj_IsDirtyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
Static dictionary-like class that offers similar functionality to GlobalContext This is used in tests...
Definition: TestResults.cs:21
static void Reinitialise()
Reinitialise the dictionary, clearing any existing results, ready for the next test
Definition: TestResults.cs:69
static string GetResult(string key)
Get a result of an operation from the underlying results dictionary
Definition: TestResults.cs:49
Type to carry context information for DI in unit tests
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
Task< object > CreateAsync(params object[] criteria)
Starts an asynchronous data portal operation to create a business object.
object Execute(object obj)
Called to execute a Command object on the server.
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.