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.
ContextTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ContextTests.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//-----------------------------------------------------------------------
8
9using System;
10using System.Linq;
11using System.Data;
12using System.Data.SqlClient;
13using Csla.Data;
14using System.Configuration;
15
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]
29 public class ContextTests
30 {
31 private const string TestDBConnection = nameof(WellKnownValues.DataPortalTestDatabase);
32 private const string InvalidTestDBConnection = "DataPortalTestDatabaseConnectionStringXXXXXXX";
33
34 private const string ConnectionWithMissingDB = nameof(WellKnownValues.DataPortalTestDatabaseWithInvalidDBValue);
35
36
37 #region Invalid connection strings
38 [TestMethod]
39 [ExpectedException(typeof(System.Collections.Generic.KeyNotFoundException))]
41 {
42 using (var objectContextManager = ConnectionManager<SqlConnection>.GetManager(InvalidTestDBConnection, true))
43 {
44 }
45 }
46
47 [TestMethod]
48 [ExpectedException(typeof(System.Collections.Generic.KeyNotFoundException))]
50 {
51 using (var objectContextManager = ContextManager<TestLinqToSqlContextDataContext>.GetManager(InvalidTestDBConnection, true))
52 {
53 }
54 }
55
56 [TestMethod]
57 [ExpectedException(typeof(System.Collections.Generic.KeyNotFoundException))]
59 {
60 using (var objectContextManager = ObjectContextManager<DataPortalTestDatabaseEntities>.GetManager("DataPortalTestDatabaseEntitiesxxxxxx", true))
61 {
62 }
63 }
64
65#if DEBUG
66 [TestMethod]
67 [ExpectedException(typeof(SqlException))]
68
69 public void ConnectionSetting_with_Invalid_DB_Throws_ConfigurationErrorsException_for_SqlConnection()
70 {
71 //throws SqlException
72 using (var objectContextManager = ConnectionManager<SqlConnection>.GetManager(ConnectionWithMissingDB, true))
73 {
74 }
75 }
76#endif
77
78#if DEBUG
79
80 [TestMethod]
81 [ExpectedException(typeof(SqlException))]
82
83 public void ConnectionSetting_with_Invalid_DB_Throws_ConfigurationErrorsException_for_LinqToSqlContextDataContext()
84 {
85 using (var objectContextManager = ContextManager<TestLinqToSqlContextDataContext>.GetManager(ConnectionWithMissingDB, true))
86 {
87 Assert.IsNotNull(objectContextManager);
88 //throws SqlException
89 var count = objectContextManager.DataContext.Table1s.GetNewBindingList().Count;
90 }
91 }
92
93 [TestMethod]
94 [ExpectedException(typeof(EntityException))]
95 [TestCategory("SkipWhenLiveUnitTesting")]
96 public void ConnectionSetting_with_Invalid_DB_Throws_ConfigurationErrorsException_for_EntitiesContextDataContext()
97 {
98 using (var objectContextManager = ObjectContextManager<DataPortalTestDatabaseEntities>.GetManager(WellKnownValues.EntityConnectionWithMissingDBConnectionStringName, true))
99 {
100 Assert.IsNotNull(objectContextManager);
101 //Throws EntityException
102 var table = (from p in objectContextManager.ObjectContext.Table2
103 select p).ToList();
104 }
105 }
106
107
108#endif
109 #endregion
110
111 #region Data
112
113#if DEBUG
114 [TestMethod]
115
116 public void ExecuteReader_on_Table2_returns_reader_with_3_fields()
117 {
118 using (var objectContextManager = ConnectionManager<SqlConnection>.GetManager(TestDBConnection, true))
119 {
120 Assert.IsNotNull(objectContextManager);
121 using (var command = new SqlCommand("Select * From Table2", objectContextManager.Connection))
122 {
123 command.CommandType = CommandType.Text;
124 using (var reader = new Csla.Data.SafeDataReader(command.ExecuteReader()))
125 Assert.IsTrue(reader.FieldCount == 3, "Did not get reader");
126 }
127 }
128 }
129#endif
130
131#if DEBUG
132 [TestMethod]
133
134 public void Table1_retreived_through_LingToSqlDataContext_has_records()
135 {
136 using (var objectContextManager = ContextManager<TestLinqToSqlContextDataContext>.GetManager(TestDBConnection, true))
137 {
138 Assert.IsNotNull(objectContextManager);
139 Assert.IsTrue(objectContextManager.DataContext.Table1s.GetNewBindingList().Count > 0, "Data in table is missing");
140 }
141 }
142
143 [TestMethod]
144 [TestCategory("SkipWhenLiveUnitTesting")]
145 public void Table2_retreived_through_LingToEntitiesDataContext_has_records()
146 {
147 using (var objectContextManager = ObjectContextManager<DataPortalTestDatabaseEntities>.GetManager(nameof(WellKnownValues.DataPortalTestDatabaseEntities), true))
148 {
149 Assert.IsNotNull(objectContextManager);
150
151 var query = from p in objectContextManager.ObjectContext.Table2
152 select p;
153
154 Assert.IsTrue(query.ToList().Count > 0, "Data in table is missing");
155 }
156 }
157#endif
158 #endregion
159
160 #region Transaction Manager
161
162#if DEBUG
163 [TestMethod]
164
165 public void Using_TransactionManager_Insert_of_2records_rolls_back_if_second_record_fails_insert()
166 {
167 ApplicationContext.LocalContext.Clear();
168 var list = TransactionContextUserList.GetList();
169 int counter = list.Count;
170
171 list.Add(new TransactionContextUser
172 {
173 FirstName = "First",
174 LastName = "Last",
175 SmallColumn = "aaaa"
176 });
177
178 list.Add(new TransactionContextUser
179 {
180 FirstName = "First1",
181 LastName = "Last1",
182 SmallColumn = "bbbbbbbbbbbbbb"
183 });
184
185 bool gotError = false;
186 try
187 {
188 list.Save();
189 }
190 catch (DataPortalException ex)
191 {
192 // will be thrown from SQL server
193 gotError = true;
194 }
195
196 Assert.IsTrue(gotError, "SQL should have thrown an error");
197 int tCount = 0;
198 foreach (var r in ApplicationContext.LocalContext.Keys)
199 if (r.ToString().StartsWith("__transaction:"))
200 tCount++;
201 Assert.AreEqual(0, tCount, "Transaction context should have been null");
202
203 list = TransactionContextUserList.GetList();
204 Assert.AreEqual(counter, list.Count, "Data should not have been saved.");
205 }
206
207
208 [TestMethod]
209
210 public void Using_TransactionManager_Insert_2records_increases_count_by2_then_removing_them_decreases_count_by2()
211 {
212 ApplicationContext.LocalContext.Clear();
213 var list = TransactionContextUserList.GetList();
214 int beforeInsertCount = list.Count;
215
216 list.AddRange(new[]
217 {
218 new TransactionContextUser
219 {
220 FirstName = "First",
221 LastName = "Last",
222 SmallColumn = "aaaa"
223 },
224 new TransactionContextUser
225 {
226 FirstName = "First1",
227 LastName = "Last",
228 SmallColumn = "bbb"
229 }
230 });
231
232 list.Save();
233
234 int tCount = 0;
235 foreach (var r in ApplicationContext.LocalContext.Keys)
236 if (r.ToString().StartsWith("__transaction:"))
237 tCount++;
238 Assert.AreEqual(0, tCount, "Transaction context should have been null");
239
240 list = TransactionContextUserList.GetList();
241 Assert.AreEqual(beforeInsertCount + 2, list.Count, "Data should have been saved.");
242
243 list.Remove(list.Last(o => o.LastName == "Last"));
244 list.Remove(list.Last(o => o.LastName == "Last"));
245
246 list.Save();
247
248 tCount = 0;
249 foreach (var r in ApplicationContext.LocalContext.Keys)
250 if (r.ToString().StartsWith("__transaction:"))
251 tCount++;
252 Assert.AreEqual(0, tCount, "Transaction context should have been null");
253
254 list = TransactionContextUserList.GetList();
255 Assert.AreEqual(beforeInsertCount, list.Count, "Data should not have been saved.");
256 }
257
258 [TestMethod]
259
260 public void TestTransactionsManaagerConnectionProperty()
261 {
262 using (var manager = TransactionManager<SqlConnection, SqlTransaction>.GetManager(TestDBConnection, true))
263 {
264 Assert.AreSame(manager.Connection, manager.Transaction.Connection, "COnnection is not correct");
265 Assert.IsNotNull(manager.Connection, "Connection should not be null.");
266 Assert.IsNotNull(manager.Transaction, "Transaction should not be null.");
267 }
268 }
269#endif
270
271#endregion
272
273 }
274}
Provides an automated way to reuse open database connections within the context of a single data port...
ConnectionManager GetManager(string database)
Gets the ConnectionManager object for the specified database.
Provides an automated way to reuse LINQ data context objects within the context of a single data port...
ContextManager< C > GetManager(string database)
Gets the ContextManager object for the specified database.
Provides an automated way to reuse Entity Framework object context objects within the context of a si...
ObjectContextManager< C > GetManager(string database)
Gets the ObjectContextManager object for the specified database.
This is an IDataReader that 'fixes' any null values before they are returned to our business code.
Provides an automated way to reuse open database connections and associated ADO.NET transactions with...
TransactionManager< C, T > GetManager(string database)
Gets the TransactionManager object for the specified database.
void InvalidConnectionSetting_Throws_ConfigurationErrorsException_for_SqlConnection()
Definition: ContextTests.cs:40
void InvalidConnectionSetting_Throws_ConfigurationErrorsException_for_EntitiesContextDataContext()
Definition: ContextTests.cs:58
void InvalidConnectionSetting_Throws_ConfigurationErrorsException_for_LinqToSqlContextDataContext()
Definition: ContextTests.cs:49
static string DataPortalTestDatabase
static string DataPortalTestDatabaseWithInvalidDBValue