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.
ChildDataPortalTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ChildDataPortalTests.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.Linq;
11using System.Text;
12using System.Threading.Tasks;
13using Csla;
15using Csla.TestHelpers;
16#if !NUNIT
17using Microsoft.VisualStudio.TestTools.UnitTesting;
18#else
19using NUnit.Framework;
20using TestClass = NUnit.Framework.TestFixtureAttribute;
21using TestInitialize = NUnit.Framework.SetUpAttribute;
22using TestCleanup = NUnit.Framework.TearDownAttribute;
23using TestMethod = NUnit.Framework.TestAttribute;
24#endif
25
27{
28 [TestClass]
30 {
31 private static TestDIContext _testDIContext;
32
34 public static void ClassInitialize(TestContext context)
35 {
36 _testDIContext = TestDIContextFactory.CreateDefaultContext();
37 }
38
39 [TestMethod]
40 public async Task CreateChildNoCriteria()
41 {
42 var dp = _testDIContext.CreateChildDataPortal<TestChild>();
43 var child = await dp.CreateChildAsync();
44 Assert.AreEqual("none", child.Name);
45 }
46
47 [TestMethod]
48 public async Task CreateChildInt32Criteria()
49 {
50 var dp = _testDIContext.CreateChildDataPortal<TestChild>();
51 var child = await dp.CreateChildAsync(123);
52 Assert.AreEqual("Int32", child.Name);
53 }
54
55 [TestMethod]
56 public async Task CreateChildMultipleCriteria()
57 {
58 var dp = _testDIContext.CreateChildDataPortal<TestChild>();
59 var child = await dp.CreateChildAsync("abc", 123);
60 Assert.AreEqual("2", child.Name);
61 }
62
63 [TestMethod]
64 public async Task FetchChildNoCriteria()
65 {
66 var dp = _testDIContext.CreateChildDataPortal<TestChild>();
67 var child = await dp.FetchChildAsync();
68 Assert.AreEqual("none", child.Name);
69 }
70
71 [TestMethod]
72 public async Task FetchChildInt32Criteria()
73 {
74 var dp = _testDIContext.CreateChildDataPortal<TestChild>();
75 var child = await dp.FetchChildAsync(123);
76 Assert.AreEqual("Int32", child.Name);
77 }
78
79 [TestMethod]
80 public async Task FetchChildMultipleCriteria()
81 {
82 var dp = _testDIContext.CreateChildDataPortal<TestChild>();
83 var child = await dp.FetchChildAsync("abc", 123);
84 Assert.AreEqual("2", child.Name);
85 }
86
87 [TestMethod]
88 public async Task UpdateChild()
89 {
90 var dp = _testDIContext.CreateChildDataPortal<TestChild>();
91 var child = await dp.FetchChildAsync();
92 await dp.UpdateChildAsync(child, "update", 123);
93 Assert.AreEqual("update/123", child.Name);
94 }
95
96 [TestMethod]
97 public async Task DeleteSelfChild()
98 {
99 var dp = _testDIContext.CreateChildDataPortal<TestChild>();
100 var child = await dp.FetchChildAsync();
101 child.MarkForDeletion();
102 await dp.UpdateChildAsync(child, "deleteme", 123);
103 Assert.AreEqual("deleteme", child.Name);
104 }
105
106 [TestMethod]
108 {
109 var dp = _testDIContext.CreateChildDataPortal<TestChildDeleteFallback>();
110 var child = await dp.FetchChildAsync();
111 child.MarkForDeletion();
112 await dp.UpdateChildAsync(child, "update", 123);
113 Assert.AreEqual("deleted", child.Name);
114 }
115 }
116
118 public class TestChild : BusinessBase<TestChild>
119 {
120 public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(nameof(Name));
121 public string Name
122 {
123 get { return GetProperty(NameProperty); }
124 set { SetProperty(NameProperty, value); }
125 }
126
127 public void MarkForDeletion()
128 {
129 MarkDeleted();
130 }
131
132 [CreateChild]
133 private async Task CreateChild()
134 {
135 await Task.Delay(0);
136 Name = "none";
137 return;
138 }
139
140 [CreateChild]
141 private async Task CreateChild(int i)
142 {
143 await Task.Delay(0);
144 Name = "Int32";
145 return;
146 }
147
148 [CreateChild]
149 private async Task CreateChild(object o)
150 {
151 await Task.Delay(0);
152 if (o == null)
153 Name = "null";
154 else
155 Name = "bad";
156 return;
157 }
158
159 [CreateChild]
160 private async Task CreateChild(string s, int i)
161 {
162 await Task.Delay(0);
163 Name = "2";
164 return;
165 }
166
167 [FetchChild]
168 private async Task FetchChild()
169 {
170 await Task.Delay(0);
171 Name = "none";
172 return;
173 }
174
175 [FetchChild]
176 private async Task FetchChild(object o)
177 {
178 await Task.Delay(0);
179 if (o == null)
180 Name = "null";
181 else if (o is Int32)
182 Name = "Int32";
183 else
184 Name = "bad";
185 return;
186 }
187
188 [FetchChild]
189 private async Task FetchChild(string s, int i)
190 {
191 await Task.Delay(0);
192 Name = "2";
193 return;
194 }
195
196 [UpdateChild]
197 private async Task UpdateChild(string s, int i)
198 {
199 await Task.Delay(0);
200 Name = $"{s}/{i}";
201 }
202
203 [InsertChild]
204 private async Task InsertChild(params object[] parameters)
205 {
206 await Task.Delay(0);
207 Name = parameters[0].ToString();
208 }
209
210 [DeleteSelfChild]
211 private async Task DeleteChild(string s, int i)
212 {
213 await Task.Delay(0);
214 Name = s;
215 }
216 }
217
219 public class TestChildDeleteFallback : BusinessBase<TestChildDeleteFallback>
220 {
221 public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(nameof(Name));
222 public string Name
223 {
224 get { return GetProperty(NameProperty); }
225 set { SetProperty(NameProperty, value); }
226 }
227
228 public void MarkForDeletion()
229 {
230 MarkDeleted();
231 }
232
233 [FetchChild]
234 private async Task FetchChild()
235 {
236 await Task.Delay(0);
237 Name = "none";
238 return;
239 }
240
241 [DeleteSelfChild]
242 private void DeleteSelf()
243 {
244 Name = "deleted";
245 }
246 }
247}
This is the base class from which most business objects will be derived.
Definition: BusinessBase.cs:38
Maintains metadata about a property.
static void ClassInitialize(TestContext context)
static readonly PropertyInfo< string > NameProperty
static readonly PropertyInfo< string > NameProperty
Type to carry context information for DI in unit tests
@ Serializable
Prevents updating or inserting until the transaction is complete.