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.test/BasicModern/BasicModernTests.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using Csla.TestHelpers;
7
8#if !NUNIT
9using Microsoft.VisualStudio.TestTools.UnitTesting;
10#else
11using NUnit.Framework;
12using TestClass = NUnit.Framework.TestFixtureAttribute;
13using TestInitialize = NUnit.Framework.SetUpAttribute;
14using TestCleanup = NUnit.Framework.TearDownAttribute;
15using TestMethod = NUnit.Framework.TestAttribute;
16#endif
17
19{
20 [TestClass]
21 public class BasicModernTests
22 {
23 private static TestDIContext _testDIContext;
24
26 public static void ClassInitialize(TestContext context)
27 {
28 _testDIContext = TestDIContextFactory.CreateDefaultContext();
29 }
30
31 [TestMethod]
33 {
34 var oldSetting = Configuration.ConfigurationManager.AppSettings["CslaSerializationFormatter"];
35 try
36 {
37 Configuration.ConfigurationManager.AppSettings.Set("CslaSerializationFormatter", "MobileFormatter");
38
39 var root = NewRoot();
40
41 var originalRootId = root.Id;
42
43 root.BeginEdit();
44 root.Id = originalRootId + 1;
45 root.CancelEdit();
46
47 Assert.AreEqual(originalRootId, root.Id);
48
49 root.BeginEdit();
50 root.Id = originalRootId + 1;
51 root.ApplyEdit();
52
53 Assert.AreEqual(originalRootId + 1, root.Id);
54 }
55 finally
56 {
57 Configuration.ConfigurationManager.AppSettings.Set("CslaSerializationFormatter", oldSetting);
58 }
59 }
60
61 [TestMethod]
63 {
64 var oldSetting = Configuration.ConfigurationManager.AppSettings["CslaSerializationFormatter"];
65 try
66 {
67 Configuration.ConfigurationManager.AppSettings.Set("CslaSerializationFormatter", "MobileFormatter");
68
69 var original = NewRoot();
70
71 original.Name = "Test Root";
72
73 var child = original.Children.AddNew();
74
75 child.Name = "TestChild";
76
77 var copy = original.Clone();
78
79 Assert.IsFalse(ReferenceEquals(original, copy));
80 Assert.AreEqual(original.Id, copy.Id);
81 Assert.AreEqual(original.IsDirty, copy.IsDirty);
82 Assert.AreEqual(original.IsSelfDirty, copy.IsSelfDirty);
83
84 for(var i = 0; i < original.Children.Count; i++)
85 {
86 Assert.IsFalse(ReferenceEquals(original.Children[i], copy.Children[i]));
87 Assert.AreEqual(original.Children[i].Name, copy.Children[i].Name);
88 Assert.AreEqual(original.Children[i].IsDirty, copy.Children[i].IsDirty);
89 }
90 }
91 finally
92 {
93 Configuration.ConfigurationManager.AppSettings.Set("CslaSerializationFormatter", oldSetting);
94 }
95 }
96
97 [TestMethod]
98 public void CreateGraph()
99 {
100 var graph = NewRoot();
101 Assert.IsTrue(graph.IsNew, "IsNew");
102 Assert.IsFalse(graph.IsValid, "IsValid");
103 Assert.AreEqual(0, graph.Children.Count, "Children count");
104 }
105
106 [TestMethod]
108 {
109 var graph = NewRoot();
110 var changed = new List<string>();
111 graph.PropertyChanged += (o, e) =>
112 {
113 changed.Add(e.PropertyName);
114 };
115
116 graph.MakeOld();
117
118 // TODO: Are these assumptions about what should happen actually correct?
119 Assert.IsTrue(changed.Contains("IsDirty"), "IsDirty did not change as expected");
120 Assert.IsTrue(changed.Contains("IsSelfDirty"), "IsSelfDirtynot as expected");
121 Assert.IsFalse(changed.Contains("IsValid"), "IsValid changed; that was not expected");
122 Assert.IsFalse(changed.Contains("IsSelfValid"), "IsSelfValid changed; that was not expected");
123 Assert.IsTrue(changed.Contains("IsSavable"), "IsSavable did not change as expected");
124 Assert.IsTrue(changed.Contains("IsNew"), "IsNew did not change as expected");
125 Assert.IsFalse(changed.Contains("IsDeleted"), "IsDeleted changed; that was not expected");
126 }
127
128 [TestMethod]
130 {
131 var graph = NewRoot();
132 graph.Name = "abc";
133 graph = graph.Save();
134 var changed = new List<string>();
135 graph.PropertyChanged += (o, e) =>
136 {
137 changed.Add(e.PropertyName);
138 };
139
140 graph.Delete();
141
142 Assert.IsTrue(changed.Contains("IsDirty"), "IsDirty did not change as was expected");
143 Assert.IsTrue(changed.Contains("IsSelfDirty"), "IsSelfDirty did not change as we expected");
144 Assert.IsFalse(changed.Contains("IsValid"), "IsValid changed; that was not expected");
145 Assert.IsFalse(changed.Contains("IsSelfValid"), "IsSelfValid changed; that was not expected");
146 Assert.IsTrue(changed.Contains("IsSavable"), "IsSavable did not change as we expected");
147 Assert.IsFalse(changed.Contains("IsNew"), "IsNew changed; that was not expected");
148 Assert.IsTrue(changed.Contains("IsDeleted"), "IsDeleted did not change as we expected");
149 }
150
151 [TestMethod]
153 {
155 var graph = NewRoot();
156 var changed = new List<string>();
157 graph.PropertyChanged += (o, e) =>
158 {
159 changed.Add(e.PropertyName);
160 };
161
162 graph.Id = 123;
163
164 Assert.IsTrue(changed.Contains("Id"), "Id did not change as we expected");
165 Assert.IsFalse(changed.Contains("IsDirty"), "IsDirty changed; that was not expected");
166 Assert.IsFalse(changed.Contains("IsSelfDirty"), "IsSelfDirty changed; that was not expected");
167 Assert.IsTrue(changed.Contains("IsValid"), "IsValid did not change as we expected");
168 Assert.IsTrue(changed.Contains("IsSelfValid"), "IsSelfValid did not change as we expected");
169 Assert.IsTrue(changed.Contains("IsSavable"), "IsSavable did not change as we expected");
170 Assert.IsFalse(changed.Contains("IsNew"), "IsNew changed; that was not expected");
171 Assert.IsFalse(changed.Contains("IsDeleted"), "IsDeleted changed; that was not expected");
172 }
173
174 [TestMethod]
176 {
178 var graph = NewRoot();
179 var changed = new List<string>();
180 graph.PropertyChanged += (o, e) =>
181 {
182 changed.Add(e.PropertyName);
183 };
184
185 graph.Name = "abc";
186
187 Assert.IsTrue(changed.Contains("Name"), "Name");
188 Assert.IsFalse(changed.Contains("IsDirty"), "IsDirty");
189 Assert.IsFalse(changed.Contains("IsSelfDirty"), "IsSelfDirty");
190 Assert.IsTrue(changed.Contains("IsValid"), "IsValid");
191 Assert.IsTrue(changed.Contains("IsSelfValid"), "IsSelfValid");
192 Assert.IsTrue(changed.Contains("IsSavable"), "IsSavable");
193 Assert.IsFalse(changed.Contains("IsNew"), "IsNew");
194 Assert.IsFalse(changed.Contains("IsDeleted"), "IsDeleted");
195
196 graph = graph.Save();
197 changed = new List<string>();
198 graph.PropertyChanged += (o, e) =>
199 {
200 changed.Add(e.PropertyName);
201 };
202
203 Assert.IsFalse(graph.IsDirty, "IsDirty should be false");
204
205 graph.Name = "def";
206
207 Assert.IsTrue(graph.IsDirty, "IsDirty should be true");
208
209 Assert.IsTrue(changed.Contains("Name"), "Name after save");
210 Assert.IsTrue(changed.Contains("IsDirty"), "IsDirty after save");
211 Assert.IsTrue(changed.Contains("IsSelfDirty"), "IsSelfDirty after save");
212 Assert.IsTrue(changed.Contains("IsValid"), "IsValid after save");
213 Assert.IsTrue(changed.Contains("IsSelfValid"), "IsSelfValid after save");
214 Assert.IsTrue(changed.Contains("IsSavable"), "IsSavable after save");
215 Assert.IsFalse(changed.Contains("IsNew"), "IsNew after save");
216 Assert.IsFalse(changed.Contains("IsDeleted"), "IsDeleted after save");
217 }
218
219 [TestMethod]
221 {
222 IChildDataPortal<Child> childDataPortal = _testDIContext.CreateChildDataPortal<Child>();
223
225 var graph = NewRoot();
226 var changed = new List<string>();
227 graph.PropertyChanged += (o, e) =>
228 {
229 changed.Add(e.PropertyName);
230 };
231 graph.Name = "abc";
232 changed.Clear();
233 graph.Children.Add(childDataPortal.FetchChild(123, "xyz"));
234
235 Assert.IsTrue(graph.IsDirty, "IsDirty should be true");
236
237 Assert.IsFalse(changed.Contains("Children"), "Children after add");
238 Assert.IsTrue(changed.Contains("IsDirty"), "IsDirty after add");
239 Assert.IsFalse(changed.Contains("IsSelfDirty"), "IsSelfDirty after add");
240 Assert.IsTrue(changed.Contains("IsValid"), "IsValid after add");
241 Assert.IsFalse(changed.Contains("IsSelfValid"), "IsSelfValid after add");
242 Assert.IsTrue(changed.Contains("IsSavable"), "IsSavable after add");
243 Assert.IsFalse(changed.Contains("IsNew"), "IsNew after add");
244 Assert.IsFalse(changed.Contains("IsDeleted"), "IsDeleted after add");
245
246 graph = graph.Save();
247 changed.Clear();
248 graph.PropertyChanged += (o, e) =>
249 {
250 changed.Add(e.PropertyName);
251 };
252
253 Assert.IsFalse(graph.IsDirty, "IsDirty should be false");
254
255 graph.Children[0].Name = "mnop";
256
257 Assert.IsTrue(graph.IsDirty, "IsDirty should be true");
258
259 Assert.IsFalse(changed.Contains("Children"), "Children after add");
260 Assert.IsTrue(changed.Contains("IsDirty"), "IsDirty after add");
261 Assert.IsFalse(changed.Contains("IsSelfDirty"), "IsSelfDirty after add");
262 Assert.IsTrue(changed.Contains("IsValid"), "IsValid after add");
263 Assert.IsFalse(changed.Contains("IsSelfValid"), "IsSelfValid after add");
264 Assert.IsTrue(changed.Contains("IsSavable"), "IsSavable after add");
265 Assert.IsFalse(changed.Contains("IsNew"), "IsNew after add");
266 Assert.IsFalse(changed.Contains("IsDeleted"), "IsDeleted after add");
267 }
268
269 private Root NewRoot()
270 {
271 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
272
273 return dataPortal.Create();
274 }
275 }
276}
Provides consistent context information between the client and server DataPortal objects.
static PropertyChangedModes PropertyChangedMode
Gets or sets a value specifying how CSLA .NET should raise PropertyChanged events.
PropertyChangedModes
Enum representing the way in which CSLA .NET should raise PropertyChanged events.
Type to carry context information for DI in unit tests
Interface defining the members of the child data portal type.
object FetchChild(params object[] criteria)
Called by a factory method in a business class to retrieve an object, which is loaded with values fro...
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...