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.
AsynchDataPortalTest.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="AsynchDataPortalTest.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Create is an exception , if BO does not have DP_Create() overload</summary>
7//-----------------------------------------------------------------------
8
9using System.Collections.Generic;
10using System.Data;
11using System.Linq;
12using Csla.Test.Basic;
13using System.Threading.Tasks;
14
15#if !NUNIT
16using Microsoft.VisualStudio.TestTools.UnitTesting;
17#else
18using NUnit.Framework;
19using TestClass = NUnit.Framework.TestFixtureAttribute;
20using TestInitialize = NUnit.Framework.SetUpAttribute;
21using TestCleanup = NUnit.Framework.TearDownAttribute;
22using TestMethod = NUnit.Framework.TestAttribute;
23#endif
24
25using System.Threading;
26using System.Globalization;
27using UnitDriven;
31using System;
32using Csla.TestHelpers;
34
36{
37 [TestClass]
39 {
40 private CultureInfo CurrentCulture;
41 private CultureInfo CurrentUICulture;
42 private static TestDIContext _testDIContext;
43
45 public static void ClassInitialize(TestContext context)
46 {
47 _testDIContext = TestDIContextFactory.CreateDefaultContext();
48 }
49
50 [TestInitialize]
51 public void Setup()
52 {
53 CurrentCulture = Thread.CurrentThread.CurrentCulture;
54 CurrentUICulture = Thread.CurrentThread.CurrentUICulture;
55 }
56
57 [TestCleanup]
58 public void Cleanup()
59 {
60 Thread.CurrentThread.CurrentCulture = CurrentCulture;
61 Thread.CurrentThread.CurrentUICulture = CurrentUICulture;
62 }
63
64 #region Create
65
66 [TestMethod]
68 {
69 IDataPortal<Single> dataPortal = _testDIContext.CreateDataPortal<Single>();
70
71 var created = await dataPortal.CreateAsync();
72 Assert.AreEqual(created.Id, 0);
73 }
74
75 [TestMethod]
77 {
78 IDataPortal<Single> dataPortal = _testDIContext.CreateDataPortal<Single>();
79
80 var created = await dataPortal.CreateAsync(100);
81 Assert.AreEqual(created.Id, 100);
82 }
83
84 [TestMethod]
85 [ExpectedException(typeof(Csla.DataPortalException))]
86 public async Task BeginCreate_with_exception()
87 {
88 IDataPortal<Single> dataPortal = _testDIContext.CreateDataPortal<Single>();
89
90 await dataPortal.CreateAsync(9999);
91 }
92
93 [TestMethod]
94 public async Task CreateAsync_NoCriteria()
95 {
96 IDataPortal<Single> dataPortal = _testDIContext.CreateDataPortal<Single>();
97
98 var result = await dataPortal.CreateAsync();
99 Assert.IsNotNull(result);
100 Assert.AreEqual(0, result.Id);
101 }
102
103 [TestMethod]
104 public async Task CreateAsync_WithCriteria()
105 {
106 IDataPortal<Single2> dataPortal = _testDIContext.CreateDataPortal<Single2>();
107
108 var result = await dataPortal.CreateAsync(123);
109 Assert.IsNotNull(result);
110 Assert.AreEqual(123, result.Id);
111 }
112
113
114 [TestMethod]
116 {
117 var lck = new AutoResetEvent(false);
118 new Action(async () =>
119 {
120 IDataPortal<Single2> dataPortal = _testDIContext.CreateDataPortal<Single2>();
121
122 try
123 {
124 var result = await dataPortal.CreateAsync(9999);
125 Assert.Fail("Expected exception not thrown");
126 }
127 catch (Exception ex)
128 {
129 Assert.IsInstanceOfType(ex, typeof(Csla.DataPortalException));
130 }
131 finally
132 {
133 lck.Set();
134 }
135 }).Invoke();
136 lck.WaitOne();
137 }
138
139 [TestMethod]
140 [Timeout(1000)]
141 public async Task CreateAsync_Parrallel()
142 {
143 IDataPortal<SingleWithFactory> dataPortal = _testDIContext.CreateDataPortal<SingleWithFactory>();
144
145 var list = new List<int>(500);
146 for (var i = 0; i < 500; i++)
147 {
148 list.Add(i);
149 }
150
151 var tasks = list.AsParallel().Select(x => dataPortal.CreateAsync());
152 await Task.WhenAll(tasks);
153 }
154
155 #endregion
156
157 #region Fetch
158
159 [TestMethod]
160 public async Task FetchAsync_NoCriteria()
161 {
162 IDataPortal<Single2> dataPortal = _testDIContext.CreateDataPortal<Single2>();
163
164 var result = await dataPortal.FetchAsync();
165 Assert.IsNotNull(result);
166 Assert.AreEqual(0, result.Id);
167 }
168
169 [TestMethod]
170 public async Task FetchAsync_WithCriteria()
171 {
172 IDataPortal<Single2> dataPortal = _testDIContext.CreateDataPortal<Single2>();
173
174 var result = await dataPortal.FetchAsync(123);
175 Assert.IsNotNull(result);
176 Assert.AreEqual(123, result.Id);
177 }
178
179
180 [TestMethod]
182 {
183 var lck = new AutoResetEvent(false);
184 new Action(async () =>
185 {
186 IDataPortal<Single2> dataPortal = _testDIContext.CreateDataPortal<Single2>();
187
188 try
189 {
190 var result = await dataPortal.FetchAsync(9999);
191 Assert.Fail("Expected exception not thrown");
192 }
193 catch (Exception ex)
194 {
195 Assert.IsInstanceOfType(ex, typeof(Csla.DataPortalException));
196 }
197 finally
198 {
199 lck.Set();
200 }
201 }).Invoke();
202 lck.WaitOne();
203 }
204
205 [TestMethod]
206 [Timeout(1000)]
207 public async Task FetchAsync_Parrallel()
208 {
209 IDataPortal<SingleWithFactory> dataPortal = _testDIContext.CreateDataPortal<SingleWithFactory>();
210
211 var list = new List<int>(500);
212 for (var i = 0; i < 500; i++)
213 {
214 list.Add(i);
215 }
216
217 var tasks = list.AsParallel().Select(x => dataPortal.FetchAsync());
218 await Task.WhenAll(tasks);
219 }
220
221 #endregion
222
223#if DEBUG
224
225 [TestMethod]
226 public async Task SaveAsync()
227 {
228 IDataPortal<Single2> dataPortal = _testDIContext.CreateDataPortal<Single2>();
229
230 var result = await dataPortal.CreateAsync(0);
231 Assert.IsNotNull(result);
232 Assert.AreEqual(0, result.Id);
233 Assert.IsTrue(result.IsNew);
234 Assert.IsTrue(result.IsDirty);
235 result = await result.SaveAsync();
236 Assert.IsFalse(result.IsNew);
237 Assert.IsFalse(result.IsDirty);
238 }
239
240 [TestMethod]
241 public async Task SaveAsyncWithException()
242 {
243 var context = GetContext();
244 await context.Assert.Try(async () =>
245 {
246 IDataPortal<Single2> dataPortal = _testDIContext.CreateDataPortal<Single2>();
247
248 var result = await dataPortal.CreateAsync(555);
249 Assert.IsNotNull(result);
250 Assert.AreEqual(555, result.Id);
251 Assert.IsTrue(result.IsNew);
252 Assert.IsTrue(result.IsDirty);
253 var lck = new AutoResetEvent(false);
254 new Action(async () =>
255 {
256 try
257 {
258 result = await result.SaveAsync();
259 Assert.Fail("Expected exception not thrown");
260 }
261 catch (Exception ex)
262 {
263 context.Assert.IsTrue(ex.GetType() == typeof(Csla.DataPortalException));
264 }
265 finally
266 {
267 lck.Set();
268 }
269 }).Invoke();
270 lck.WaitOne();
271 context.Assert.Success();
272 });
273 context.Complete();
274 }
275
276#endif
277
278 #region ExecuteCommand
279
280 [TestMethod]
282 {
283 IDataPortal<CommandObject> dataPortal = _testDIContext.CreateDataPortal<CommandObject>();
284
285 var command = dataPortal.Create();
286 var result = await dataPortal.ExecuteAsync(command);
287 Assert.AreEqual("Executed", result.AProperty);
288 }
289
290 [TestMethod]
291 public async Task ExecuteAsync()
292 {
293 IDataPortal<SingleCommand> dataPortal = _testDIContext.CreateDataPortal<SingleCommand>();
294
295 SingleCommand cmd = dataPortal.Create(123);
296 var result = await dataPortal.ExecuteAsync(cmd);
297 Assert.IsNotNull(result);
298 Assert.AreEqual(124, result.Value);
299 }
300
301 [TestMethod]
303 {
304 var lck = new AutoResetEvent(false);
305 new Action(async () =>
306 {
307 IDataPortal<SingleCommand> dataPortal = _testDIContext.CreateDataPortal<SingleCommand>();
308
309 try
310 {
311 var cmd = dataPortal.Create(555);
312 var result = await dataPortal.ExecuteAsync(cmd);
313 Assert.Fail("Expected exception not thrown");
314 }
315 catch (Exception ex)
316 {
317 Assert.IsInstanceOfType(ex, typeof(Csla.DataPortalException));
318 }
319 finally
320 {
321 lck.Set();
322 }
323 }).Invoke();
324 lck.WaitOne();
325 }
326 #endregion
327
332 [TestMethod]
334 {
335 IDataPortal<CustomerWO_DP_XYZ> dataPortal = _testDIContext.CreateDataPortal<CustomerWO_DP_XYZ>();
336
337 await dataPortal.CreateAsync();
338 }
339
340 [TestMethod]
342 {
343 IDataPortal<AsyncPortalWithCulture> dataPortal = _testDIContext.CreateDataPortal<AsyncPortalWithCulture>();
344
345 string expectedCulture = Thread.CurrentThread.CurrentCulture.Name;
346 string expectedUICulture = Thread.CurrentThread.CurrentUICulture.Name;
347
348 var command = dataPortal.Create();
349 var result = await dataPortal.ExecuteAsync(command);
350 Assert.AreEqual(expectedCulture, result.CurrentCulture);
351 Assert.AreEqual(expectedUICulture, result.CurrentUICulture);
352 }
353 }
354}
This exception is returned for any errors occurring during the server-side DataPortal invocation.
async Task BeginCreate_Calling_BO_Without_DP_CREATE_Returns_no_Error_info()
Create is an exception , if BO does not have DP_Create() overload with that signature,...
static void ClassInitialize(TestContext context)
async Task ExecuteCommand_called_without_UserState_results_in_UserState_defaulted_to_Null_server()
Type to carry context information for DI in unit tests
UnitTestContext GetContext()
Definition: TestBase.cs:12
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.
Task< object > ExecuteAsync(object command)
Called by a factory method in a business class or by the UI to execute a command object.
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...
Task< object > FetchAsync(params object[] criteria)
Starts an asynchronous data portal operation to create a business object.