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.
BusyStatusTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="BusyStatusTests.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;
11using System;
12using UnitDriven;
14using System.Threading.Tasks;
15using Csla.TestHelpers;
16using Csla.Test;
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{
31 [TestClass]
33 {
34
35 private static TestDIContext _testDIContext;
36 private static TestDIContext _noCloneOnUpdateDIContext;
37
39 public static void ClassInitialize(TestContext context)
40 {
41 _testDIContext = TestDIContextFactory.CreateDefaultContext();
42 _noCloneOnUpdateDIContext = TestDIContextFactory.CreateContext(opt => opt.DataPortal(cfg => cfg.AutoCloneOnUpdate(false)));
43 }
44
45 [TestMethod]
46 public async Task TestBusy()
47 {
48 IDataPortal<ItemWithAsynchRule> dataPortal = _noCloneOnUpdateDIContext.CreateDataPortal<ItemWithAsynchRule>();
49
51
52 UnitTestContext context = GetContext();
53 var item = await dataPortal.FetchAsync("an id");
54 item.RuleField = "some value";
55 context.Assert.IsTrue(item.IsBusy, "Should be busy");
56 context.Assert.IsFalse(item.IsSavable, "Should not be savable");
57 context.Assert.Success();
58 context.Complete();
59 }
60
61 [TestMethod]
62 public void ListTestBusy()
63 {
64 IDataPortal<ItemWithAsynchRuleList> dataPortal = _noCloneOnUpdateDIContext.CreateDataPortal<ItemWithAsynchRuleList>();
65
67
68 UnitTestContext context = GetContext();
70 items[0].RuleField = "some value";
71 context.Assert.IsTrue(items.IsBusy);
72 context.Assert.IsFalse(items.IsSavable);
73 context.Assert.Success();
74 context.Complete();
75 }
76
77 [TestMethod]
78 public async Task TestSaveWhileBusy()
79 {
80 IDataPortal<ItemWithAsynchRule> dataPortal = _testDIContext.CreateDataPortal<ItemWithAsynchRule>();
81
83
84 UnitTestContext context = GetContext();
85 var item = await dataPortal.FetchAsync("an id");
86 item.RuleField = "some value";
87 context.Assert.IsTrue(item.IsBusy);
88 context.Assert.IsFalse(item.IsSavable);
89
90 try
91 {
92 await item.SaveAsync();
93 }
94 catch (Exception ex)
95 {
96 var error = ex as InvalidOperationException;
97 context.Assert.IsNotNull(error);
98 context.Assert.IsTrue(error.Message.ToLower().Contains("busy"));
99 context.Assert.IsTrue(error.Message.ToLower().Contains("save"));
100 context.Assert.Success();
101 }
102 context.Complete();
103 }
104
105 [TestMethod]
106 public async Task ListTestSaveWhileBusy()
107 {
108 IDataPortal<ItemWithAsynchRuleList> dataPortal = _noCloneOnUpdateDIContext.CreateDataPortal<ItemWithAsynchRuleList>();
109
111
112 UnitTestContext context = GetContext();
114 items[0].RuleField = "some value";
115 context.Assert.IsTrue(items.IsBusy);
116 context.Assert.IsFalse(items.IsSavable);
117
118 try
119 {
120 await items.SaveAsync();
121 }
122 catch (Exception ex)
123 {
124 var error = ex as InvalidOperationException;
125 context.Assert.IsNotNull(error);
126 context.Assert.IsTrue(error.Message.ToLower().Contains("busy"));
127 context.Assert.IsTrue(error.Message.ToLower().Contains("save"));
128 context.Assert.Success();
129 }
130 context.Complete();
131 }
132
133 [TestMethod]
134 public async Task TestNotBusy()
135 {
136 IDataPortal<ItemWithAsynchRule> dataPortal = _testDIContext.CreateDataPortal<ItemWithAsynchRule>();
137
139
140 UnitTestContext context = GetContext();
141 var item = await dataPortal.FetchAsync("an id");
142 item.ValidationComplete += (o2, e2) =>
143 {
144 context.Assert.IsFalse(item.IsBusy);
145 context.Assert.IsTrue(item.IsSavable);
146 context.Assert.Success();
147 };
148 item.RuleField = "some value";
149 context.Assert.IsTrue(item.IsBusy);
150 context.Assert.IsFalse(item.IsSavable);
151 context.Complete();
152 }
153
154 [TestMethod]
155 public void ListTestNotBusy()
156 {
157 IDataPortal<ItemWithAsynchRuleList> dataPortal = _noCloneOnUpdateDIContext.CreateDataPortal<ItemWithAsynchRuleList>();
158
160
161 UnitTestContext context = GetContext();
163 items[0].ValidationComplete += (o2, e2) =>
164 {
165 context.Assert.IsFalse(items.IsBusy);
166 context.Assert.IsTrue(items.IsSavable);
167 context.Assert.Success();
168 };
169
170 items[0].RuleField = "some value";
171 context.Assert.IsTrue(items.IsBusy);
172 context.Assert.IsFalse(items.IsSavable);
173 }
174
175 [TestMethod]
177 {
178 IDataPortal<ItemWithAsynchRuleList> dataPortal = _noCloneOnUpdateDIContext.CreateDataPortal<ItemWithAsynchRuleList>();
179
181
182 UnitTestContext context = GetContext();
184 items[0].ValidationComplete += async (o2, e2) =>
185 {
186 context.Assert.IsFalse(items.IsBusy);
187 context.Assert.IsTrue(items.IsSavable);
188 items = await items.SaveAsync();
189 context.Assert.AreEqual("DataPortal_Update", items[0].OperationResult);
190 context.Assert.Success();
191 };
192
193 items[0].RuleField = "some value";
194 context.Assert.IsTrue(items.IsBusy);
195 context.Assert.IsFalse(items.IsSavable);
196 context.Complete();
197 }
198
199 [TestMethod]
200 [ExpectedException(typeof(InvalidOperationException))]
201 public async Task TestSaveWhileBusyNetOnly()
202 {
203 IDataPortal<ItemWithAsynchRule> dataPortal = _testDIContext.CreateDataPortal<ItemWithAsynchRule>();
204
206
207 UnitTestContext context = GetContext();
208 var item = await dataPortal.FetchAsync("an id");
209 item.RuleField = "some value";
210 context.Assert.IsTrue(item.IsBusy);
211 context.Assert.IsFalse(item.IsSavable);
212 item.Save();
213 context.Assert.Success();
214 context.Complete();
215 }
216
217 [TestMethod]
219 {
220 IDataPortal<ItemWithAsynchRuleList> dataPortal = _noCloneOnUpdateDIContext.CreateDataPortal<ItemWithAsynchRuleList>();
221
223
224 UnitTestContext context = GetContext();
226
227 items[0].RuleField = "some value";
228 context.Assert.IsTrue(items.IsBusy);
229 context.Assert.IsFalse(items.IsSavable);
230 bool gotError = false;
231 try
232 {
233 items.Save();
234 }
235 catch (InvalidOperationException)
236 {
237 gotError = true;
238 }
239 context.Assert.IsTrue(gotError);
240 context.Assert.Success();
241
242 context.Complete();
243
244
245 }
246
247 [TestMethod]
248 public async Task TestSaveWhileNotBusyNetOnly()
249 {
250 IDataPortal<ItemWithAsynchRule> dataPortal = _noCloneOnUpdateDIContext.CreateDataPortal<ItemWithAsynchRule>();
251
253
254 UnitTestContext context = GetContext();
255 var item = await dataPortal.FetchAsync("an id");
256 item.ValidationComplete += (o2, e2) =>
257 {
258 context.Assert.IsFalse(item.IsBusy);
259 context.Assert.IsTrue(item.IsSavable);
260 item = item.Save();
261 context.Assert.AreEqual("DataPortal_Update", item.OperationResult);
262 context.Assert.Success();
263 };
264 item.RuleField = "some value";
265 context.Assert.IsTrue(item.IsBusy);
266 context.Assert.IsFalse(item.IsSavable);
267 context.Complete();
268 }
269
270 [TestMethod]
272 {
273 IDataPortal<ItemWithAsynchRuleList> dataPortal = _noCloneOnUpdateDIContext.CreateDataPortal<ItemWithAsynchRuleList>();
274
276
277 UnitTestContext context = GetContext();
279
280
281
282 items[0].ValidationComplete += (o2, e2) =>
283 {
284 context.Assert.IsFalse(items.IsBusy);
285 context.Assert.IsTrue(items.IsSavable);
286 items = items.Save();
287 context.Assert.AreEqual("DataPortal_Update", items[0].OperationResult);
288 context.Assert.Success();
289 };
290
291 items[0].RuleField = "some value";
292 context.Assert.IsTrue(items.IsBusy);
293 context.Assert.IsFalse(items.IsSavable);
294
295 context.Complete();
296
297 }
298
299 [TestMethod]
301 {
302 IDataPortal<ItemWithAsynchRule> dataPortal = _noCloneOnUpdateDIContext.CreateDataPortal<ItemWithAsynchRule>();
303
305
306 UnitTestContext context = GetContext();
307 var item = await dataPortal.FetchAsync("an id");
308 item.OperationResult = "something";
309 context.Assert.IsFalse(item.IsBusy);
310 context.Assert.IsTrue(item.IsSavable);
311 item = item.Save();
312 context.Assert.AreEqual("DataPortal_Update", item.OperationResult);
313 context.Assert.Success();
314 context.Complete();
315 }
316
317
318 [TestMethod]
320 {
321 IDataPortal<ItemWithAsynchRuleList> dataPortal = _noCloneOnUpdateDIContext.CreateDataPortal<ItemWithAsynchRuleList>();
322
324
325 UnitTestContext context = GetContext();
327
328 items[0].OperationResult = "something";
329 context.Assert.IsFalse(items.IsBusy);
330 context.Assert.IsTrue(items.IsSavable);
331 items = items.Save();
332 context.Assert.AreEqual("DataPortal_Update", items[0].OperationResult);
333 context.Assert.Success();
334 context.Complete();
335 }
336 }
337}
override bool IsBusy
Gets the busy status for this object and its child objects.
virtual bool IsSavable
Returns true if this object has changes, is valid, the user is authorized and the object is not busy.
T Save()
Saves the object to the database.
async Task< T > SaveAsync()
Saves the object to the database.
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
Type to carry context information for DI in unit tests
static ItemWithAsynchRuleList GetListWithItems(IDataPortal< ItemWithAsynchRuleList > dataPortal)
void IsFalse(bool condition)
Definition: Asserter.cs:40
void IsTrue(bool condition)
Definition: Asserter.cs:30
UnitTestContext GetContext()
Definition: TestBase.cs:12
static void ClassInitialize(TestContext context)
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
Task< object > FetchAsync(params object[] criteria)
Starts an asynchronous data portal operation to create a business object.