astanchev

ExtendedDatabase Tests

Aug 1st, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. namespace Tests
  2. {
  3. using System;
  4. using NUnit.Framework;
  5.  
  6. //using ExtendedDatabase;
  7.  
  8. public class ExtendedDatabaseTests
  9. {
  10. private Person personOne;
  11. private Person personTwo;
  12. private Person testPerson;
  13. private ExtendedDatabase testDatabase;
  14. private const long testID = 1003;
  15. private const string testUsername = "Gosho";
  16.  
  17. [SetUp]
  18. public void Setup()
  19. {
  20. this.personOne = new Person(1001, "Ivan");
  21. this.personTwo = new Person(1002, "Mitko");
  22. this.testPerson = new Person(testID, testUsername);
  23.  
  24. this.testDatabase = new ExtendedDatabase(personOne, personTwo);
  25. }
  26.  
  27. [Test]
  28. public void TestConstructorCreatesCorrectDatabase()
  29. {
  30. ExtendedDatabase newDatabase = new ExtendedDatabase(personOne, personTwo);
  31.  
  32. int expectedCountOfPersons = 2;
  33. int actualCountOfPersons = newDatabase.Count;
  34.  
  35. Assert.AreEqual(expectedCountOfPersons, actualCountOfPersons);
  36. }
  37.  
  38.  
  39. [Test]
  40. public void TestConstructorShouldThrowExceptionWithMoreThan16People()
  41. {
  42. Person[] people = CreatePeople(17);
  43.  
  44. Assert.That(() => new ExtendedDatabase(people), Throws.ArgumentException);
  45. }
  46.  
  47. [Test]
  48. public void TestConstructorShouldThrowExceptionWithEmptyPeople()
  49. {
  50. Person[] people = new Person[17];
  51.  
  52. Assert.That(() => new ExtendedDatabase(people), Throws.ArgumentException);
  53. }
  54.  
  55. [Test]
  56. public void TestAddCorrectlyUser()
  57. {
  58. this.testDatabase.Add(testPerson);
  59.  
  60. int expectedCountOfPersons = 3;
  61. int actualCountOfPersons = this.testDatabase.Count;
  62.  
  63. Assert.AreEqual(expectedCountOfPersons, actualCountOfPersons);
  64. }
  65.  
  66. [Test]
  67. public void TesAddShouldNotAddExistingUsername()
  68. {
  69. this.testDatabase.Add(testPerson);
  70. Person newPerson = new Person(1111, testUsername);
  71.  
  72. Assert.That(() => testDatabase.Add(newPerson), Throws.InvalidOperationException);
  73. }
  74.  
  75. [Test]
  76. public void TesAddShouldNotAddExistingId()
  77. {
  78. this.testDatabase.Add(testPerson);
  79. Person newPerson = new Person(testID, "Pesho");
  80.  
  81. Assert.That(() => testDatabase.Add(newPerson), Throws.InvalidOperationException);
  82. }
  83.  
  84. [Test]
  85. public void TestAddInFullDatabase()
  86. {
  87. Person[] people = CreatePeople(15);
  88.  
  89. ExtendedDatabase newDatabase = new ExtendedDatabase(people);
  90. newDatabase.Add(personOne);
  91.  
  92.  
  93. Assert.Throws<InvalidOperationException>(() => newDatabase.Add(personTwo));
  94.  
  95. }
  96.  
  97. [Test]
  98. public void TestRemoveCorrectly()
  99. {
  100. testDatabase.Remove();
  101.  
  102. int expectedCountOfPersons = 1;
  103. int actualCountOfPersons = this.testDatabase.Count;
  104.  
  105. Assert.AreEqual(expectedCountOfPersons, actualCountOfPersons);
  106. }
  107.  
  108. [Test]
  109. public void TestRemoveFromEmptyDatabase()
  110. {
  111. ExtendedDatabase newDatabase = new ExtendedDatabase();
  112.  
  113. Assert.Throws<InvalidOperationException>(() => newDatabase.Remove());
  114. }
  115.  
  116. [Test]
  117. public void TestFindByUsernameNonExistingUserNameThrowException()
  118. {
  119. Assert.Throws<InvalidOperationException>(() => this.testDatabase.FindByUsername(testUsername));
  120. }
  121.  
  122. [Test]
  123. public void TestFindByUsernameNullArgumentThrowException()
  124. {
  125. Assert.Throws<ArgumentNullException>(() => this.testDatabase.FindByUsername(null));
  126. }
  127.  
  128. [Test]
  129. public void TestFindByUsernameIsCaseSensitive()
  130. {
  131. this.testDatabase.Add(testPerson);
  132.  
  133. Assert.Throws<InvalidOperationException>(() => this.testDatabase.FindByUsername(testUsername.ToLower()));
  134. }
  135.  
  136. [Test]
  137. public void TestFindByUsernameWorksCorrectly()
  138. {
  139. this.testDatabase.Add(testPerson);
  140.  
  141. Person expectedPerson = testPerson;
  142. Person actualPerson = this.testDatabase.FindByUsername(testUsername);
  143.  
  144. Assert.AreEqual(expectedPerson, actualPerson);
  145. }
  146.  
  147. [Test]
  148. public void TestFindByIDExistingIDThrowException()
  149. {
  150. Assert.Throws<InvalidOperationException>(() => this.testDatabase.FindById(testID));
  151. }
  152.  
  153. [Test]
  154. public void TestFindByIDNegativeIDThrowException()
  155. {
  156. Assert.Throws<ArgumentOutOfRangeException>(() => this.testDatabase.FindById(-testID));
  157. }
  158.  
  159. [Test]
  160. public void TestFindByIDWorksCorrectly()
  161. {
  162. this.testDatabase.Add(testPerson);
  163.  
  164. Person expectedPerson = testPerson;
  165. Person actualPerson = this.testDatabase.FindById(testID);
  166.  
  167. Assert.AreEqual(expectedPerson, actualPerson);
  168. }
  169.  
  170. private Person[] CreatePeople(int countOfPeople)
  171. {
  172. Person[] people = new Person[countOfPeople];
  173.  
  174. for (int i = 0; i < countOfPeople; i++)
  175. {
  176. long id = 2000 + i;
  177. string userName = "Petar" + (char)('a' + i);
  178.  
  179. Person person = new Person(id, userName);
  180. people[i] = person;
  181. }
  182.  
  183. return people;
  184. }
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment