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.
AsyncRuleTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="AsyncRuleTests.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>This only works on Silverlight because when run through NUnit it is not running</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Linq;
11using System.Text;
12using System.Threading;
13using UnitDriven;
14using System.Threading.Tasks;
15using Csla.TestHelpers;
16
17#if NUNIT
18using NUnit.Framework;
19using TestClass = NUnit.Framework.TestFixtureAttribute;
20using TestInitialize = NUnit.Framework.SetUpAttribute;
21using TestCleanup = NUnit.Framework.TearDownAttribute;
22using TestMethod = NUnit.Framework.TestAttribute;
23#elif MSTEST
24using Microsoft.VisualStudio.TestTools.UnitTesting;
25#endif
26
28{
29#if TESTING
30 [System.Diagnostics.DebuggerStepThrough]
31#endif
32 [TestClass]
33 public class AsyncRuleTests : TestBase
34 {
35 private static TestDIContext _testDIContext;
36
38 public static void ClassInitialize(TestContext context)
39 {
40 _testDIContext = TestDIContextFactory.CreateDefaultContext();
41 }
42
43 [TestMethod]
44 public void TestAsyncRulesValid()
45 {
46 IDataPortal<HasAsyncRule> dataPortal = _testDIContext.CreateDataPortal<HasAsyncRule>();
47
48 UnitTestContext context = GetContext();
49
50 HasAsyncRule har = dataPortal.Create();
51 context.Assert.IsTrue(har.IsValid, "IsValid 1");
52
53 har.ValidationComplete += (o, e) =>
54 {
55 context.Assert.IsTrue(har.IsValid, "IsValid 2");
56 context.Assert.Success();
57 };
58 har.Name = "success";
59 context.Complete();
60 }
61
62 [TestMethod]
63 public void TestAsyncRuleError()
64 {
65 IDataPortal<HasAsyncRule> dataPortal = _testDIContext.CreateDataPortal<HasAsyncRule>();
66
67 UnitTestContext context = GetContext();
68
69 HasAsyncRule har = dataPortal.Create();
70 context.Assert.IsTrue(har.IsValid, "IsValid 1");
71
72 har.ValidationComplete += (o, e) =>
73 {
74 context.Assert.IsFalse(har.IsValid, "IsValid 2");
75 context.Assert.AreEqual(1, har.BrokenRulesCollection.Count);
76 context.Assert.Success();
77 };
78 har.Name = "error";
79 context.Complete();
80 }
81
82 [TestMethod]
83 public void InvalidAsyncRule()
84 {
85 IDataPortal<HasInvalidAsyncRule> dataPortal = _testDIContext.CreateDataPortal<HasInvalidAsyncRule>();
86
88
89 UnitTestContext context = GetContext();
90 var root = dataPortal.Create();
91 root.ValidationComplete += (o, e) =>
92 {
93 context.Assert.IsFalse(root.IsValid);
94 context.Assert.AreEqual(1, root.GetBrokenRules().Count);
95 context.Assert.AreEqual("Operation is not valid due to the current state of the object.", root.GetBrokenRules()[0].Description);
96 context.Assert.Success();
97 };
98 root.Validate();
99 context.Complete();
100 }
101
102 [Ignore] // frequently times out on appveyor server
103 [TestMethod]
105 {
106 UnitTestContext context = GetContext();
107
108 context.Assert.Try(() =>
109 {
110 int iterations = 20;
111 int completed = 0;
112 for (int x = 0; x < iterations; x++)
113 {
114 HasAsyncRule har = new HasAsyncRule();
115 har.ValidationComplete += (o, e) =>
116 {
117 context.Assert.AreEqual("error", har.Name);
118 context.Assert.AreEqual(1, har.BrokenRulesCollection.Count);
119 System.Diagnostics.Debug.WriteLine(har.BrokenRulesCollection.Count);
120 completed++;
121 if (completed == iterations)
122 context.Assert.Success();
123 };
124
125 // set this to error so we can verify that all 6 rules get run for
126 // each object. This is essentially the only way to communicate back
127 // with the object except byref properties.
128 har.Name = "error";
129 }
130 });
131
132 context.Complete();
133 }
134
135
136 [TestMethod]
138 {
139 IDataPortal<AsyncRuleRoot> dataPortal = _testDIContext.CreateDataPortal<AsyncRuleRoot>();
140
141 UnitTestContext context = GetContext();
142
143 var har = dataPortal.Create();
144 context.Assert.IsTrue(string.IsNullOrEmpty(har.CustomerNumber));
145 context.Assert.IsTrue(string.IsNullOrEmpty(har.CustomerName));
146 context.Assert.IsFalse(har.IsValid, "IsValid 1");
147
148
149 har.ValidationComplete += (o, e) =>
150 {
151 context.Assert.IsFalse(string.IsNullOrEmpty(har.CustomerNumber));
152 context.Assert.IsFalse(string.IsNullOrEmpty(har.CustomerName));
153
154 context.Assert.IsTrue(har.IsValid, "IsValid 2");
155 context.Assert.Success();
156 };
157 har.CustomerNumber = "123456";
158
159 context.Complete();
160 }
161
162 [TestMethod]
163 public async Task TestAsyncAwaitRule()
164 {
165 IDataPortal<AsyncRuleRoot> dataPortal = _testDIContext.CreateDataPortal<AsyncRuleRoot>();
166
167 var har = dataPortal.Create();
168 var tcs = new TaskCompletionSource<bool>();
169 har.ValidationComplete += (o, e) =>
170 {
171 Assert.AreEqual("abc", har.AsyncAwait, "ends with value");
172 tcs.SetResult(true);
173 };
174 har.AsyncAwait = "123456";
175 await tcs.Task;
176 }
177
178 }
179}
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 void ClassInitialize(TestContext context)
Type to carry context information for DI in unit tests
void IsFalse(bool condition)
Definition: Asserter.cs:40
void Try(Action p)
Definition: Asserter.cs:60
UnitTestContext GetContext()
Definition: TestBase.cs:12
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...