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.
DataPortalChildTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="DataPortalChildTests.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.Text;
11using Csla.TestHelpers;
12
13#if !NUNIT
14using Microsoft.VisualStudio.TestTools.UnitTesting;
15
16#else
17using NUnit.Framework;
18using TestClass = NUnit.Framework.TestFixtureAttribute;
19using TestInitialize = NUnit.Framework.SetUpAttribute;
20using TestCleanup = NUnit.Framework.TearDownAttribute;
21using TestMethod = NUnit.Framework.TestAttribute;
22#endif
23
25{
26 [TestClass()]
28 {
29 private static TestDIContext _testDIContext;
30
32 public static void ClassInitialize(TestContext context)
33 {
34 _testDIContext = TestDIContextFactory.CreateDefaultContext();
35 }
36
37 [TestMethod]
38 public void CreateAndSaveChild()
39 {
40 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
41
42 Root root = dataPortal.Create();
43 root.Data = "a";
44 root.Child.Data = "b";
45
46 Assert.IsTrue(root.IsDirty, "Root should be dirty");
47 Assert.IsTrue(root.Child.IsNew, "Child should be new");
48 Assert.IsTrue(root.Child.IsDirty, "Child should be dirty");
49 Assert.AreEqual("Created", root.Child.Status, "Child status incorrect after create");
50
51 root = root.Save();
52
53 Assert.IsFalse(root.IsDirty, "Root should not be dirty");
54 Assert.IsFalse(root.IsNew, "Root should not be new");
55 Assert.IsFalse(root.Child.IsDirty, "Child should not be dirty");
56 Assert.IsFalse(root.Child.IsNew, "Child should not be new");
57 Assert.AreEqual("Inserted", root.Child.Status, "Child status incorrect after Save");
58 }
59
60 [TestMethod]
62 {
63 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
64
65 Root root = dataPortal.Create();
66
67 root.Child.DeleteChild();
68 Assert.IsTrue(root.Child.IsDeleted, "Child should be marked for deletion");
69 Assert.IsTrue(root.Child.IsDirty, "Child should be dirty");
70
71 root = root.Save();
72
73 Assert.IsTrue(root.IsDirty, "Root should be dirty after Save");
74 Assert.IsFalse(root.IsNew, "Root should not be new");
75 Assert.IsTrue(root.Child.IsDirty, "Child should be dirty");
76 Assert.IsTrue(root.Child.IsNew, "Child should be new");
77 Assert.AreEqual("Created", root.Child.Status, "Child status incorrect");
78 }
79
80 [TestMethod]
81 public void FetchAndSaveChild()
82 {
83 IChildDataPortal<Child> childDataPortal = _testDIContext.CreateChildDataPortal<Child>();
84 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
85
86 Root root = dataPortal.Create();
87 root.FetchChild(childDataPortal);
88
89 Assert.IsFalse(root.Child.IsNew, "Child should not be new");
90 Assert.IsFalse(root.Child.IsDirty, "Child should not be dirty");
91 Assert.AreEqual("Fetched", root.Child.Status, "Child status incorrect after fetch");
92
93 root.Child.Data = "b";
94
95 Assert.IsTrue(root.Child.IsDirty, "Child should be dirty");
96
97 root = root.Save();
98
99 Assert.IsFalse(root.IsDirty, "Root should not be dirty");
100 Assert.IsFalse(root.IsNew, "Root should not be new");
101 Assert.IsFalse(root.Child.IsDirty, "Child should not be dirty");
102 Assert.IsFalse(root.Child.IsNew, "Child should not be new");
103 Assert.AreEqual("Updated", root.Child.Status, "Child status incorrect after Save");
104 }
105
106 [TestMethod]
108 {
109 IChildDataPortal<Child> childDataPortal = _testDIContext.CreateChildDataPortal<Child>();
110 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
111
112 Root root = dataPortal.Create();
113 root.FetchChild(childDataPortal);
114
115 Assert.IsFalse(root.Child.IsNew, "Child should not be new");
116 Assert.IsFalse(root.Child.IsDirty, "Child should not be dirty");
117 Assert.AreEqual("Fetched", root.Child.Status, "Child status incorrect after fetch");
118
119 root.Child.DeleteChild();
120 Assert.IsTrue(root.Child.IsDeleted, "Child should be marked for deletion");
121 Assert.IsTrue(root.Child.IsDirty, "Child should be dirty");
122
123 root = root.Save();
124
125 Assert.IsTrue(root.IsDirty, "Root should be dirty after Save");
126 Assert.IsFalse(root.IsNew, "Root should not be new");
127 Assert.IsTrue(root.Child.IsDirty, "Child should be dirty after Save");
128 Assert.IsTrue(root.Child.IsNew, "Child should be new after Save");
129 Assert.AreEqual("Deleted", root.Child.Status, "Child status incorrect after Save");
130 }
131
132 [TestMethod]
134 {
135 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
136
137 Root root = dataPortal.Fetch();
138 var list = root.ChildList;
139 Assert.IsFalse(root.ChildList.IsDirty, "Child list should not be dirty");
140 Assert.AreEqual("Fetched", root.ChildList.Status, "Child list status incorrect after fetch");
141
142 list.Add(NewChild());
143
144 Assert.IsTrue(root.ChildList.IsDirty, "Child list should be dirty after add");
145 Assert.IsTrue(root.ChildList[0].IsDirty, "Child should be dirty after add");
146 Assert.IsTrue(root.ChildList[0].IsNew, "Child should be new after add");
147
148 root = root.Save();
149
150 Assert.IsFalse(root.IsDirty, "Root should not be dirty after Save");
151 Assert.IsFalse(root.IsNew, "Root should not be new");
152 Assert.IsFalse(root.ChildList.IsDirty, "Child should not be dirty after Save");
153 Assert.AreEqual("Updated", root.ChildList.Status, "Child list status incorrect after Save");
154 Assert.IsFalse(root.ChildList[0].IsDirty, "Child should not be dirty after Save");
155 Assert.IsFalse(root.ChildList[0].IsNew, "Child should not be new after Save");
156 Assert.AreEqual("Inserted", root.ChildList[0].Status, "Child status incorrect after Save");
157 }
158
159 [TestMethod]
161 {
162 IDataPortal<Root> dataPortal = _testDIContext.CreateDataPortal<Root>();
163
164 Root root = dataPortal.Fetch();
165 root.Data = "root";
166 var oneChild = root.Child;
167 oneChild.Data = "random";
168
169 var list = root.ChildList;
170 Assert.IsFalse(root.ChildList.IsDirty, "Child list should not be dirty");
171 Assert.AreEqual("Fetched", root.ChildList.Status, "Child list status incorrect after fetch");
172
173
174
175 list.Add(NewChild());
176
177 Assert.IsTrue(root.ChildList.IsDirty, "Child list should be dirty after add");
178 Assert.IsTrue(root.ChildList[0].IsDirty, "Child should be dirty after add");
179 Assert.IsTrue(root.ChildList[0].IsNew, "Child should be new after add");
180
181 root = root.Save();
182
183 Assert.IsFalse(root.IsDirty, "Root should not be dirty after Save");
184 Assert.IsFalse(root.IsNew, "Root should not be new");
185 Assert.IsFalse(root.ChildList.IsDirty, "Child should not be dirty after Save");
186 Assert.AreEqual("Updated", root.ChildList.Status, "Child list status incorrect after Save");
187 Assert.IsFalse(root.ChildList[0].IsDirty, "Child should not be dirty after Save");
188 Assert.IsFalse(root.ChildList[0].IsNew, "Child should not be new after Save");
189 Assert.AreEqual("Inserted", root.ChildList[0].Status, "Child status incorrect after Save");
190
191 Assert.AreEqual("root", root.ChildList[0].RootData, "Parent data is not correct after Save in the list");
192 Assert.AreEqual("root", root.Child.RootData, "Parent data is not correct after Save in one child");
193 }
194
195 private Child NewChild()
196 {
197 IChildDataPortal<Child> childDataPortal = _testDIContext.CreateChildDataPortal<Child>();
198
199 return childDataPortal.CreateChild();
200 }
201 }
202}
static void ClassInitialize(TestContext context)
void FetchChild(IChildDataPortal< Child > childDataPortal)
Type to carry context information for DI in unit tests
Interface defining the members of the child data portal type.
object CreateChild(params object[] criteria)
Called by a factory method in a business class to create a new object, which is loaded with default v...
Interface defining the members of the data portal type.
Definition: IDataPortalT.cs:17
object Fetch(params object[] criteria)
Called by a factory method in a business class to retrieve an object, which is loaded with values fro...
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...