Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.03 KB | None | 0 0
  1. using NUnit.Framework;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. //using ExtendedDatabase;
  6.  
  7. namespace Tests
  8. {
  9.     using ExtendedDatabase;
  10.  
  11.     public class ExtendedDatabaseTests
  12.     {
  13.         public Person pesho;
  14.         public Person gosho;
  15.  
  16.         [SetUp]
  17.         public void TestInit()
  18.         {
  19.             pesho = new Person(114560, "Pesho");
  20.             gosho = new Person(447788556699, "Gosho");
  21.         }
  22.  
  23.         [Test]
  24.         public void ConstructorShoudInitializeCollection()
  25.         {
  26.             var expected = new Person[] { pesho, gosho };
  27.  
  28.             var db = new ExtendedDatabase(expected);
  29.  
  30.             var actual = expected;
  31.  
  32.             Assert.That(actual, Is.EqualTo(expected));
  33.         }
  34.  
  35.         [Test]
  36.         public void AddShouldAddValidPerson()
  37.         {
  38.             var persons = new Person[] { pesho, gosho };
  39.             var db = new ExtendedDatabase(persons);
  40.             var newPerson = new Person(554466, "Stamat");
  41.             db.Add(newPerson);
  42.  
  43.             var expected = new Person[] { pesho, gosho, newPerson };
  44.             var actual = expected;
  45.  
  46.             Assert.That(actual, Is.EqualTo(expected));
  47.         }
  48.  
  49.         [Test]
  50.         public void AddSameUsernameShouldThrow()
  51.         {
  52.             var persons = new Person[] { pesho, gosho };
  53.             var db = new ExtendedDatabase(persons);
  54.             var newPerson = new Person(554466, "Gosho");
  55.  
  56.             Assert.That(() => db.Add(newPerson), Throws.InvalidOperationException);
  57.         }
  58.  
  59.         [Test]
  60.         public void AddSameIdShouldThrow()
  61.         {
  62.             var persons = new Person[] { pesho, gosho };
  63.             var db = new ExtendedDatabase(persons);
  64.             var newPerson = new Person(114560, "Stamat");
  65.  
  66.             Assert.That(() => db.Add(newPerson), Throws.InvalidOperationException);
  67.         }
  68.  
  69.         [Test]
  70.         public void RemoveShouldRemove()
  71.         {
  72.             var persons = new Person[] { pesho, gosho };
  73.             var db = new ExtendedDatabase(persons);
  74.             db.Remove();
  75.  
  76.            
  77.             var expected = new Person[] { pesho };
  78.             var actual = expected;
  79.  
  80.             Assert.That(actual, Is.EqualTo(expected));
  81.         }
  82.  
  83.         [Test]
  84.         public void RemoveEmptyCollectionShouldThrow()
  85.         {
  86.             var persons = new Person[] { };
  87.             var db = new ExtendedDatabase(persons);
  88.  
  89.             Assert.That(() => db.Remove(), Throws.InvalidOperationException);
  90.         }
  91.  
  92.         [Test]
  93.         public void FindByUsernameExistingPersonShouldReturnPerson()
  94.         {
  95.             var persons = new Person[] { pesho, gosho };
  96.             var db = new ExtendedDatabase(persons);
  97.  
  98.             var expected = pesho;
  99.             var actual = db.FindByUsername("Pesho");
  100.  
  101.             Assert.That(actual, Is.EqualTo(expected));
  102.         }
  103.  
  104.         [Test]
  105.         public void FindByUsernameNonExistingPersonShouldThrow()
  106.         {
  107.             var persons = new Person[] { pesho, gosho };
  108.             var db = new ExtendedDatabase(persons);
  109.  
  110.             Assert.That(() => db.FindByUsername("Stamat"), Throws.InvalidOperationException);
  111.         }
  112.  
  113.         [Test]
  114.         public void FindByUsernameNullArgumentShouldThrow()
  115.         {
  116.             var persons = new Person[] { pesho, gosho };
  117.             var db = new ExtendedDatabase(persons);
  118.  
  119.             Assert.That(() => db.FindByUsername(null), Throws.ArgumentNullException);
  120.         }
  121.  
  122.         [Test]
  123.         public void FindByUsernameIsCaseSensitive()
  124.         {
  125.             var persons = new Person[] { pesho, gosho };
  126.             var db = new ExtendedDatabase(persons);
  127.  
  128.             Assert.That(() => db.FindByUsername("GOSHO"), Throws.InvalidOperationException);
  129.         }
  130.  
  131.         [Test]
  132.         public void FindByIdExistingPersonShouldReturnPerson()
  133.         {
  134.             var persons = new Person[] { pesho, gosho };
  135.             var db = new ExtendedDatabase(persons);
  136.  
  137.             var expected = pesho;
  138.             var actual = db.FindById(114560);
  139.  
  140.             Assert.That(actual, Is.EqualTo(expected));
  141.         }
  142.  
  143.         [Test]
  144.         public void FindByIdNonExistingPersonShouldThrow()
  145.         {
  146.             var persons = new Person[] { pesho, gosho };
  147.             var db = new ExtendedDatabase(persons);
  148.  
  149.             Assert.That(() => db.FindById(558877), Throws.InvalidOperationException);
  150.         }
  151.  
  152.         [Test]
  153.         public void FindByUsernameNegativeArgumentShouldThrow()
  154.         {
  155.             var persons = new Person[] { pesho, gosho };
  156.             var db = new ExtendedDatabase(persons);
  157.  
  158.             Assert.That(() => db.FindById(-5), Throws.Exception);
  159.         }
  160.  
  161.         [Test]
  162.         [TestCase(1, "Pesho")]
  163.         [TestCase(444, "Iva")]
  164.         [TestCase(999, "Sxema")]
  165.         [TestCase(444, "Rima")]
  166.         [TestCase(111, "Yuna")]
  167.         public void Person_SuccessfulDataPass(long id, string userName)
  168.         {
  169.             Person person = new Person(id, userName);
  170.             long expectedId = id;
  171.             long actualId = person.Id;
  172.             string expectedUsername = userName;
  173.             string actualUsername = person.UserName;
  174.  
  175.             Assert.AreEqual(expectedId, actualId);
  176.             Assert.AreEqual(expectedUsername, actualUsername);
  177.         }
  178.  
  179.         [Test]
  180.         [TestCase(1)]
  181.         [TestCase(5)]
  182.         [TestCase(10)]
  183.         [TestCase(15)]
  184.         [TestCase(16)]
  185.         public void ExtendedDB_Constructor_CorrectAmountOfData(int count)
  186.         {
  187.             Person[] people = new Person[count];
  188.             for (int i = 0; i < people.Length; i++)
  189.             {
  190.                 people[i] = new Person(i, "Ivan" + i);
  191.             }
  192.  
  193.             ExtendedDatabase extDb = new ExtendedDatabase(people);
  194.  
  195.             int expected = people.Length;
  196.             int actual = extDb.Count;
  197.  
  198.             Assert.AreEqual(expected, actual);
  199.         }
  200.  
  201.         [Test]
  202.         public void ConstructorPersonShouldInitializeCollection()
  203.         {
  204.             Assert.IsNotNull(pesho);
  205.         }
  206.         [Test]
  207.         public void ConstructorExtendedDBShouldInitializeCollection()
  208.         {
  209.             var expected = new Person[] { pesho, gosho };
  210.  
  211.             var db = new ExtendedDatabase(expected);
  212.  
  213.             Assert.IsNotNull(db);
  214.         }
  215.  
  216.         [Test]
  217.         [TestCase(17)]
  218.         [TestCase(18)]
  219.         [TestCase(22)]
  220.         [TestCase(555)]
  221.         [TestCase(100)]
  222.         [TestCase(1000)]
  223.         public void AddRange_Throws_ArgumentException_WhenCountMoreThan16(int count)
  224.         {
  225.             Person[] data = new Person[count];
  226.             Assert.Throws<ArgumentException>
  227.                 (() => new ExtendedDatabase(data));
  228.         }
  229.  
  230.         [Test]
  231.         [TestCase(13)]
  232.         [TestCase(15)]
  233.         [TestCase(14)]
  234.         [TestCase(7)]
  235.         [TestCase(1)]
  236.         public void Add_WorksCorrectly_WhenCountIsLessThan16(int count)
  237.         {
  238.             Person[] people = new Person[count];
  239.             for (int i = 0; i < people.Length; i++)
  240.             {
  241.                 people[i] = new Person(i, "Ivan" + i);
  242.             }
  243.             ExtendedDatabase extDb = new ExtendedDatabase(people);
  244.  
  245.             extDb.Add(new Person(200, "Ivan" + 200));
  246.  
  247.             int expected = people.Length + 1;
  248.             int actual = extDb.Count;
  249.  
  250.             Assert.AreEqual(expected, actual);
  251.         }
  252.  
  253.         [Test]
  254.         [TestCase(16)]
  255.         public void Add_Throws_InvalidOperationException_WhenCountIs16(int count)
  256.         {
  257.             Person[] people = new Person[count];
  258.             for (int i = 0; i < people.Length; i++)
  259.             {
  260.                 people[i] = new Person(i, "Ivan" + i);
  261.             }
  262.             ExtendedDatabase extDb = new ExtendedDatabase(people);
  263.  
  264.             Assert.Throws<InvalidOperationException>
  265.                 (() => extDb.Add(new Person(200, "Ivan" + 200)));
  266.  
  267.         }
  268.  
  269.  
  270.         [Test]
  271.         [TestCase(16)]
  272.         [TestCase(14)]
  273.         [TestCase(11)]
  274.         [TestCase(7)]
  275.         public void Add_Throws_InvalidOperationException_UserNameExists(int count)
  276.         {
  277.             Person[] people = new Person[count];
  278.             for (int i = 0; i < people.Length; i++)
  279.             {
  280.                 people[i] = new Person(i, "Ivan" + i);
  281.             }
  282.             ExtendedDatabase extDb = new ExtendedDatabase(people);
  283.  
  284.             Person newExistingPerson = new Person(222, "Ivan" + 5);
  285.  
  286.             Assert.Throws<InvalidOperationException>
  287.                 (() => extDb.Add(newExistingPerson));
  288.  
  289.         }
  290.  
  291.         [Test]
  292.         [TestCase(16)]
  293.         [TestCase(14)]
  294.         [TestCase(11)]
  295.         [TestCase(7)]
  296.         public void Add_Throws_InvalidOperationException_IdExists(int count)
  297.         {
  298.             Person[] people = new Person[count];
  299.             for (int i = 0; i < people.Length; i++)
  300.             {
  301.                 people[i] = new Person(i, "Ivan" + i);
  302.             }
  303.             ExtendedDatabase extDb = new ExtendedDatabase(people);
  304.  
  305.             Person newExistingPerson = new Person(5, "Ivan" + 222);
  306.  
  307.             Assert.Throws<InvalidOperationException>
  308.                 (() => extDb.Add(newExistingPerson));
  309.  
  310.         }
  311.  
  312.         [Test]
  313.         [TestCase(1)]
  314.         public void Remove_Throws_InvalidOperationException_CountIs0(int count)
  315.         {
  316.             Person[] people = new Person[count];
  317.             for (int i = 0; i < people.Length; i++)
  318.             {
  319.                 people[i] = new Person(i, "Ivan" + i);
  320.             }
  321.             ExtendedDatabase extDb = new ExtendedDatabase(people);
  322.  
  323.             extDb.Remove();
  324.  
  325.             Assert.Throws<InvalidOperationException>
  326.                 (() => extDb.Remove());
  327.         }
  328.  
  329.  
  330.         [Test]
  331.         [TestCase(3)]
  332.         [TestCase(15)]
  333.         [TestCase(13)]
  334.         [TestCase(11)]
  335.         [TestCase(16)]
  336.         public void Remove_Works_Correctly(int count)
  337.         {
  338.             Person[] people = new Person[count];
  339.             for (int i = 0; i < people.Length; i++)
  340.             {
  341.                 people[i] = new Person(i, "Ivan" + i);
  342.             }
  343.             ExtendedDatabase extDb = new ExtendedDatabase(people);
  344.  
  345.             extDb.Remove();
  346.             extDb.Remove();
  347.             extDb.Remove();
  348.  
  349.             int expected = people.Length - 3;
  350.             int actual = extDb.Count;
  351.  
  352.             Assert.AreEqual(expected, actual);
  353.             Assert.Throws<InvalidOperationException>
  354.                 (() => extDb.FindById(people.Length - 2));
  355.             Assert.Throws<InvalidOperationException>
  356.                 (() => extDb.FindByUsername("Ivan" + (people.Length - 2)));
  357.         }
  358.  
  359.  
  360.         [Test]
  361.         [TestCase(1)]
  362.         [TestCase(11)]
  363.         [TestCase(14)]
  364.         [TestCase(16)]
  365.         [TestCase(8)]
  366.         public void FindByUserName_Throws_InvalidOperationException_NoUserWithSuchName(int count)
  367.         {
  368.             Person[] people = new Person[count];
  369.             for (int i = 0; i < people.Length; i++)
  370.             {
  371.                 people[i] = new Person(i, "Ivan" + i);
  372.             }
  373.             ExtendedDatabase extDb = new ExtendedDatabase(people);
  374.  
  375.             Assert.Throws<InvalidOperationException>
  376.                 (() => extDb.FindByUsername("Pesho"));
  377.         }
  378.  
  379.         [Test]
  380.         [TestCase(1)]
  381.         [TestCase(11)]
  382.         [TestCase(14)]
  383.         [TestCase(16)]
  384.         [TestCase(8)]
  385.         public void FindByUserName_Throws_ArgumentNullException_UserNameGivenIsNull(int count)
  386.         {
  387.             Person[] people = new Person[count];
  388.             for (int i = 0; i < people.Length; i++)
  389.             {
  390.                 people[i] = new Person(i, "Ivan" + i);
  391.             }
  392.             ExtendedDatabase extDb = new ExtendedDatabase(people);
  393.  
  394.             Assert.Throws<ArgumentNullException>
  395.                 (() => extDb.FindByUsername(null));
  396.         }
  397.  
  398.  
  399.  
  400.         [Test]
  401.         [TestCase(1)]
  402.         [TestCase(11)]
  403.         [TestCase(14)]
  404.         [TestCase(16)]
  405.         [TestCase(8)]
  406.         public void FindById_Throws_InvalidOperationException_NoSuchID(int count)
  407.         {
  408.             Person[] people = new Person[count];
  409.             for (int i = 0; i < people.Length; i++)
  410.             {
  411.                 people[i] = new Person(i, "Ivan" + i);
  412.             }
  413.             ExtendedDatabase extDb = new ExtendedDatabase(people);
  414.  
  415.             Assert.Throws<InvalidOperationException>
  416.                 (() => extDb.FindById(123456));
  417.         }
  418.  
  419.         [Test]
  420.         [TestCase(1)]
  421.         [TestCase(11)]
  422.         [TestCase(14)]
  423.         [TestCase(16)]
  424.         [TestCase(8)]
  425.         public void FindById_Throws_ArgumentOutOfRangeException_NegativeID(int count)
  426.         {
  427.             Person[] people = new Person[count];
  428.             for (int i = 0; i < people.Length; i++)
  429.             {
  430.                 people[i] = new Person(i, "Ivan" + i);
  431.             }
  432.             ExtendedDatabase extDb = new ExtendedDatabase(people);
  433.  
  434.             Assert.Throws<ArgumentOutOfRangeException>
  435.                 (() => extDb.FindById(-3));
  436.         }
  437.     }
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement