-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTestCourseProvider.cs
562 lines (468 loc) · 12.4 KB
/
TestCourseProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TestCourseProvider.cs" company="U of R">
// 2009
// </copyright>
// <summary>
// Defines the TestCourseProvider type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace DataAccessLayerTest
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using ClassLibrary;
using DataAccessLayer;
using MbUnit.Framework;
using Microdesk.Utility.UnitTest;
using NHibernate.Criterion;
/// <summary>
/// Testing data access layer
/// </summary>
[TestFixture]
public class TestCourseProvider : DatabaseUnitTestBase
{
#region Constants
/// <summary>
/// A60 Char String
/// </summary>
private const string A60CharString = "012345678901234567890123456789012345678901234567890123456789";
#endregion
#region Fields
/// <summary>
/// Course provider
/// </summary>
private CourseProvider provider;
/// <summary>
/// ISession object
/// </summary>
private NHibernate.ISession session;
/// <summary>
/// Session Manager
/// </summary>
private SessionManager sessionManager;
#endregion
#region Public Methods
/// <summary>
/// Testing random generator
/// </summary>
[Test]
public void TestRandom()
{
var rnd = new Random();
var names = new List<string>();
for (int i = 0; i < 1000000; i++)
{
var sb = new StringBuilder();
for (int j = 0; j < 10; j++)
{
try
{
sb.Append((char) rnd.Next(Convert.ToInt32("a"), Convert.ToInt32("z")));
}
catch (ArgumentOutOfRangeException)
{
}
}
names.Add(sb.ToString());
}
var matches = new List<string>();
var sw = new Stopwatch();
sw.Start();
foreach (string s in names)
{
if (s.StartsWith("d"))
{
matches.Add(s);
}
}
sw.Stop();
Console.WriteLine(
"standart foreach: {0}",
sw.Elapsed.TotalMilliseconds);
sw.Reset();
sw.Start();
// var matchesLinq = (from s in names
// where s.StartsWith("d")
// select s).ToArray();
sw.Stop();
Console.WriteLine(
"linq to object: {0}",
sw.Elapsed.TotalMilliseconds);
}
/// <summary>
/// Generate random numbers
/// </summary>
[Test]
public void GenerateRandomNumbers()
{
var r = new Random();
for (int i = 0; i < 20; i++)
{
Console.WriteLine(r.Next(1, 10));
}
}
/// <summary>
/// Add course with exception test
/// </summary>
[Test]
// [ExpectedException(typeof(NHibernate.HibernateException))]
[ExpectedException(typeof(ClassLibrary.Rules.RuleViolationException))]
// [ExpectedException(typeof(System.ArgumentOutOfRangeException))]
public void AddCourseThrowsExceptionOnFail()
{
provider.AddCourse(BuildInvalidCourse());
}
/// <summary>
/// Cann add course
/// </summary>
[Test]
public void CanAddCourse()
{
// DaysOfWeek = "MWF"
var course = new Course
{
CourseName = "CS199",
Monday = true,
Wednesday = true,
Friday = true,
StartHour = 13,
StartMinute = 0,
EndHour = 13,
EndMinute = 50,
AssignedProfessor = null
};
int newIdentity = provider.AddCourse(course);
Course testCourse = provider.GetCourseByID(newIdentity);
Assert.IsNotNull(testCourse);
}
/// <summary>
/// Determines whether this instance [can delete course].
/// </summary>
[Test]
public void CanDeleteCourse()
{
Course course = provider.GetCourseByID(28);
provider.DeleteCourse(course);
course = provider.GetCourseByID(28);
Assert.IsNull(course);
}
/// <summary>
/// Can evict course from session test
/// </summary>
[Test]
public void CanEvictCourseFromSession()
{
Course course = provider.GetCourseByID(3);
Assert.IsTrue(session.Contains(course));
session.Evict(course);
Assert.IsFalse(session.Contains(course));
}
/// <summary>
/// can get all courses
/// </summary>
[Test]
public void CanGetAllCourses()
{
HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
Assert.AreEqual(18, provider.GetAllCourses().Count);
}
/// <summary>
/// can get all non nullable prof id
/// </summary>
[Test]
public void CanGetAllNonNullableProfID()
{
Assert.AreEqual(4, provider.GetNonNullableProfID().Count);
}
/// <summary>
/// Can get all nullable prof id
/// </summary>
[Test]
public void CanGetAllNullableProfID()
{
Assert.AreEqual(20, provider.GetNullableProfID().Count);
}
/// <summary>
/// can get course 3
/// </summary>
[Test]
public void CanGetCourse3()
{
Assert.AreEqual(3, provider.GetCourseByID(3).CourseId);
}
/// <summary>
/// can get customer by arbitary critiries
/// </summary>
[Test]
public void CanGetCustomersByArbitraryCriteria()
{
DetachedCriteria cs201Course = DetachedCriteria.For<Course>()
.Add(Restrictions.Eq("CourseName", "CS201"));
IList<Course> courses = provider.FindByDetachedCriteria(cs201Course);
Assert.AreEqual(1, courses.Count);
}
/// <summary>
/// Can save or update courses
/// </summary>
[Test]
// [ExpectedException(typeof(NHibernate.NonUniqueObjectException))]
[ExpectedException(typeof(NHibernate.Exceptions.GenericADOException))]
public void CanSaveOrUpdateCourses()
{
IList<Course> courses = provider.GetAllCourses();
if (courses.Count == 0)
{
}
// Assert.Warning("You aren't testing what you think you are here!");
const string NewCourseCourseName = "CSTesting";
foreach (Course c in courses)
{
c.CourseName = NewCourseCourseName;
}
var c1 = new Course
{
CourseName = NewCourseCourseName,
Monday = true,
Wednesday = true,
Friday = true,
StartHour = 13,
StartMinute = 0,
EndHour = 14,
EndMinute = 15
};
var c2 = new Course
{
CourseName = NewCourseCourseName,
Monday = true,
Wednesday = true,
Friday = true,
StartHour = 13,
StartMinute = 0,
EndHour = 14,
EndMinute = 15
};
courses.Add(c1);
courses.Add(c2);
int initialListCount = courses.Count;
provider.SaveOrUpdateCourses(courses);
int testListCount = provider.GetAllCourses().Count;
Assert.AreEqual(initialListCount, testListCount);
}
/// <summary>
/// Can throw exception on concurrency violation
/// </summary>
[Test]
[ExpectedException(typeof(NHibernate.StaleObjectStateException))]
public void CanThrowExceptionOnConcurrencyViolation()
{
// using (session)
// {
Course c1 = provider.GetCourseByID(3);
// }
ResetSessionForProvider();
// using (session)
// {
Course c2 = provider.GetCourseByID(3);
// }
ResetSessionForProvider();
c1.CourseName = "hello";
c2.CourseName = "goodbye";
// using (session)
// {
provider.UpdateCourse(c1);
// }
ResetSessionForProvider();
// using (session)
// {
provider.UpdateCourse(c2);
// }
}
/// <summary>
/// Can update course
/// </summary>
[Test]
public void CanUpdateCourse()
{
Course course = provider.GetCourseByID(131);
const string UpdataedName = "CS299";
course.CourseName = UpdataedName;
provider.UpdateCourse(course);
course = provider.GetCourseByID(131);
Assert.AreEqual(UpdataedName, course.CourseName);
}
/// <summary>
/// Can update course and reset assigned professor
/// </summary>
[Test]
public void CanUpdateCourseAndResetAssignedProfessor()
{
Course course = provider.GetCourseByID(3);
course.AssignedProfessor = null;
provider.UpdateCourse(course);
course = provider.GetCourseByID(3);
Assert.AreEqual(null, course.AssignedProfessor);
}
/// <summary>
/// Course can reaasociate with new session and set non dirty
/// </summary>
[Test]
public void CourseCanBeReassociatedWithNewSessionAndSetNonDirty()
{
Course course = provider.GetCourseByID(3);
Assert.IsTrue(session.Contains(course));
ResetSessionForProvider();
Assert.IsFalse(session.Contains(course));
session.Lock(course, NHibernate.LockMode.None);
provider.UpdateCourse(course);
Assert.IsTrue(session.Contains(course));
}
/// <summary>
/// course can reassociated with new session
/// </summary>
[Test]
public void CourseCanBeReassociatedWithNewSession()
{
Course course = provider.GetCourseByID(3);
Assert.IsTrue(session.Contains(course));
ResetSessionForProvider();
Assert.IsFalse(session.Contains(course));
provider.UpdateCourse(course);
Assert.IsTrue(session.Contains(course));
}
/// <summary>
/// Course is not associated with new session
/// </summary>
[Test]
public void CourseIsNotAssociatedWithNewSession()
{
Course course = provider.GetCourseByID(3);
Assert.IsTrue(session.Contains(course));
ResetSessionForProvider();
Assert.IsFalse(session.Contains(course));
}
/// <summary>
/// Get my test data xml file
/// </summary>
public void GetMyTestDataXMLFile()
{
var i = 0;
SaveTestDatabase();
}
// [Test]
// public void SaveOrUpdateCourseThrowsExceptionOnFail()
// {
// IList<Course> courses = provider.GetNullableProfID();
// if (courses.Count == 0)
// {
// }
// //Assert.Warning("You aren't testing what you think you are here!");
// const string newCourseName = "CS999";
// foreach (Course c in courses)
// {
// c.CourseName = newCourseName;
// }
// Course c1;
// try
// {
// c1 = BuildInvalidCourse();
// }
// catch (System.ArgumentOutOfRangeException)
// {
// return;
// }
// var c2 = new Course { CourseName = newCourseName, AssignedProfessor = null };
// try
// {
// courses.Add(c1);
// courses.Add(c2);
// }
// catch (System.ArgumentOutOfRangeException)
// {
// ResetSessionForProvider();
// }
// try
// {
// provider.SaveOrUpdateCourses(courses);
// }
// catch (NHibernate.HibernateException)
// {
// ResetSessionForProvider();
// }
// int testListCount = provider.GetNullableProfID().Count;
// Assert.AreEqual(26, testListCount);
// }
/// <summary>
/// Setup database
/// </summary>
[SetUp]
public void Setup()
{
DatabaseSetUp();
session = sessionManager.GetSession();
provider = new CourseProvider(session);
}
/// <summary>
/// Tear down database
/// </summary>
[TearDown]
public void TearDown()
{
DatabaseTearDown();
}
/// <summary>
/// Test fixture setup
/// </summary>
[FixtureSetUp]
public void TestFixtureSetup()
{
DatabaseFixtureSetUp();
sessionManager = new SessionManager();
}
/// <summary>
/// Test fixture tear down
/// </summary>
[FixtureTearDown]
public void TestFixtureTearDown()
{
DatabaseFixtureTearDown();
}
/// <summary>
/// Update course can throw exception on fail
/// </summary>
[Test]
// [ExpectedException(typeof(NHibernate.HibernateException))]
[ExpectedException(typeof(ClassLibrary.Rules.RuleViolationException))]
// [ExpectedException(typeof(System.ArgumentOutOfRangeException))]
public void UpdateCourseCanThrowExceptionOnFail()
{
Course course = provider.GetCourseByID(28);
course.CourseName = A60CharString;
provider.UpdateCourse(course);
}
#endregion
#region Private Methods
/// <summary>
/// Builds invalid course
/// </summary>
/// <returns>
/// Course object
/// </returns>
public static Course BuildInvalidCourse()
{
// course name is assigned a string with more than 50 chars length
return new Course { CourseName = A60CharString };
}
/// <summary>
/// Reset Session for provider
/// </summary>
private void ResetSessionForProvider()
{
session.Close();
session = sessionManager.GetSession();
provider.Session = session;
}
#endregion
}
}