Exemplo n.º 1
0
        public void RepositoryTest_Update_Company_Success()
        {
            //Arrange
            var            dateTime = DateTime.Now;
            CompanyContact wc       = ((CompanyContact)FakeDataFactory.Get <CompanyContact>(
                                           taxId: 1,
                                           name: "Name1",
                                           address: "address11",
                                           phoneNumber: "000000001",
                                           url: "www.url1.com"
                                           ));
            var contactsDto = new List <ContactDto>()
            {
                FakeDataFactory.GetDto <CompanyDto>()
            };
            var repository = new ContactRepository(contactsDto);

            //Act
            repository.Update(wc, 1);

            //Assert
            Assert.AreEqual(contactsDto.Count, 1);
            Assert.IsInstanceOfType(contactsDto[0], typeof(CompanyDto));
            Assert.AreEqual(contactsDto[0].Address, wc.Address);
            Assert.AreEqual(contactsDto[0].Name, wc.Name);
            Assert.AreEqual(contactsDto[0].PhoneNumber, wc.PhoneNumber);
            Assert.AreEqual(contactsDto[0].TaxId, wc.TaxId);
            Assert.AreEqual(((CompanyDto)contactsDto[0]).Url, wc.Url);
        }
Exemplo n.º 2
0
        public void RepositoryTest_Update_TypeMisMatch_Fail()
        {
            //Arrange
            var     dateTime = DateTime.Now;
            WorkDto wDto     = ((WorkDto)FakeDataFactory.GetDto <WorkDto>(
                                    taxId: 1,
                                    name: "Name1",
                                    address: "address11",
                                    company: "Company1",
                                    dateModified: dateTime,
                                    emailAddress: "[email protected]",
                                    phoneNumber: "000000001",
                                    title: "Title1",
                                    url: "www.url1.com"
                                    ));
            var contactsDto = new List <ContactDto>()
            {
                wDto
            };
            var repository = new ContactRepository(contactsDto);

            //Act
            var result = repository.Update(FakeDataFactory.Get <FriendContact>(), 1);

            //Assert
            Assert.AreEqual(result, false);
        }
Exemplo n.º 3
0
        public void RepositoryTest_Update_TaxIdUnique_Fail()
        {
            //Arrange
            var     dateTime = DateTime.Now;
            WorkDto wDto     = ((WorkDto)FakeDataFactory.GetDto <WorkDto>(
                                    taxId: 2
                                    ));
            var contactsDto = new List <ContactDto>()
            {
                FakeDataFactory.GetDto <WorkDto>(
                    taxId: 1
                    ),
                wDto
            };
            var repository     = new ContactRepository(contactsDto);
            var updatedContact = wDto.ToContact();

            updatedContact.TaxId = 1;

            //Act
            var result = repository.Update(updatedContact, 2);

            //Assert
            Assert.AreEqual(result, false);
        }
Exemplo n.º 4
0
        public void ContactUiTest_PrintSuccessful()
        {
            //Arrange
            Contact contact1 = FakeDataFactory.Get <FriendContact>(1, "name", null, null, null, null, null, null, null, null);
            Contact contact2 = FakeDataFactory.Get <WorkContact>(2, "name", null, null, null, null, null, null, null, null);
            Contact contact3 = FakeDataFactory.Get <CompanyContact>(3, "name", null, null, null, null, null, null, null, null);

            Mock <IRepository <Contact> > rep = ContactsUiTest.mockRepository.Create <IRepository <Contact> >();
            List <Contact> contacts           = new List <Contact>()
            {
                contact1, contact2, contact3
            };

            rep.Setup(r => r.Get())
            .Returns(contacts);

            var contactsUi = new ContactsUi(setupConsole(new[] { "print" }), rep.Object);

            //Act
            contactsUi.ProcessLineCommand();
            string output = outputBuilder.ToString();

            //Assert
            Assert.IsTrue(output.Contains(
                              "Friend\r\n-------\r\nName: name\r\nTax ID: 1\r\nAddress: \r\nPhone Number: \r\nEmail Address: \r\nBirthday: \r\nDate Created:"
                              ));
            Assert.IsTrue(output.Contains(
                              "Work\r\n-------\r\nName: name\r\nTax ID: 2\r\nAddress: \r\nPhone Number: \r\nTitle: \r\nCompany: \r\nEmail Address: \r\nURL: \r\nDate Created:"
                              ));
            Assert.IsTrue(output.Contains(
                              "Company\r\n-------\r\nName: name\r\nTax ID: 3\r\nAddress: \r\nPhone Number: \r\nURL: \r\nDate Created: "
                              ));
        }
Exemplo n.º 5
0
        public void RepositoryTest_Update_Friend_Success()
        {
            //Arrange
            var           dateTime = DateTime.Now;
            FriendContact wc       = ((FriendContact)FakeDataFactory.Get <FriendContact>(
                                          taxId: 10,
                                          name: "Name1",
                                          address: "address11",
                                          company: "Company1",
                                          dateModified: dateTime,
                                          emailAddress: "[email protected]",
                                          phoneNumber: "000000001",
                                          birthday: "01/01/2000"
                                          ));
            var contactsDto = new List <ContactDto>()
            {
                FakeDataFactory.GetDto <FriendDto>()
            };
            var repository = new ContactRepository(contactsDto);

            //Act
            repository.Update(wc, 1);

            //Assert
            Assert.AreEqual(contactsDto.Count, 1);
            Assert.IsInstanceOfType(contactsDto[0], typeof(FriendDto));
            Assert.AreEqual(contactsDto[0].Address, wc.Address);
            Assert.AreEqual(contactsDto[0].Name, wc.Name);
            Assert.AreEqual(contactsDto[0].PhoneNumber, wc.PhoneNumber);
            Assert.AreEqual(contactsDto[0].TaxId, wc.TaxId);
            Assert.AreEqual(((FriendDto)contactsDto[0]).Birthday, wc.Birthday);
        }
Exemplo n.º 6
0
        public void ContactUiTest_Add_TaxIdMustbeUnique_Fail()
        {
            //Arrange
            var inputParms = new List <string>();

            inputParms.AddRange(new[] { "add", "friend" });
            inputParms.AddRange(FakeData.GetFriendContactData());

            List <Contact> contacts = new List <Contact>()
            {
                FakeDataFactory.Get <FriendContact>(taxId: 3)
            };

            StringBuilder outputBuilder = new StringBuilder(string.Empty);

            mockConsole.Setup(x => x.ReadLine()).Returns(new Queue <string>(inputParms.ToArray()).Dequeue);
            mockConsole.Setup(x => x.WriteLine(It.IsAny <object>())).Callback((object x) => { outputBuilder.Append(x.ToString() + "\r\n"); });

            Mock <IRepository <Contact> > rep = ContactsUiTest.mockRepository.Create <IRepository <Contact> >();

            rep.Setup(r => r.Add(It.IsAny <Contact>())).Returns(true)
            .Callback((Contact x) => { contacts.Add(x); });
            rep.Setup(r => r.ContainsKey(It.IsAny <int>())).Returns <int>(
                x => (contacts.Where(w => w.TaxId == x).Count() > 0));

            var contactsUi = new ContactsUi(mockConsole.Object, rep.Object);

            //Act Assert
            var returnValue = contactsUi.ProcessLineCommand();
        }
Exemplo n.º 7
0
        public void ContactUiTest_EditWorkSuccessful()
        {
            //Arrange
            var inputParms = new Queue <string>(new[]  {
                "edit", "1", "new name", "1", "", "", "new title", "new co", "*****@*****.**",
                "www.newurl.com"
            });
            Func <string> consoledequeue = () =>
            {
                if (inputParms.Count == 0)
                {
                    return(string.Empty);
                }
                return(inputParms.Dequeue());
            };

            StringBuilder outputBuilder = new StringBuilder(string.Empty);

            mockConsole.Setup(x => x.ReadLine()).Returns(consoledequeue);
            mockConsole.Setup(x => x.WriteLine(It.IsAny <object>())).Callback((object x) => { outputBuilder.Append(x.ToString() + "\r\n"); });

            var            contact  = FakeDataFactory.Get <WorkContact>(name: "name", taxId: 1);
            List <Contact> contacts = new List <Contact>()
            {
                contact
            };
            Mock <IRepository <Contact> > rep = ContactsUiTest.mockRepository.Create <IRepository <Contact> >();

            rep.Setup(r => r.Get(It.IsAny <int>()))
            .Returns((int x) => contacts.Where(w => w.TaxId == x).FirstOrDefault());

            rep.Setup(r => r.Update(It.IsAny <Contact>(), It.IsAny <int>())).Returns(true)
            .Callback <Contact, int>((x, y) => { contacts.Remove(contact); contacts.Add(x); });

            rep.Setup(r => r.ContainsKey(It.IsAny <int>())).Returns <int>(
                x => (contacts.Where(w => w.TaxId == x).Count() > 0));

            var contactsUi = new ContactsUi(mockConsole.Object, rep.Object);

            //Act
            var returnValue = contactsUi.ProcessLineCommand();

            //Assert
            Assert.AreEqual(true, returnValue);
            Assert.AreEqual("new name", contacts[0].Name);
            Assert.AreEqual("new title", ((WorkContact)contacts[0]).Title);
            Assert.AreEqual("new co", ((WorkContact)contacts[0]).Company);
            Assert.AreEqual("*****@*****.**", ((WorkContact)contacts[0]).EmailAddress);
            Assert.AreEqual("www.newurl.com", ((WorkContact)contacts[0]).Url);
        }
Exemplo n.º 8
0
        public void ContactUiTest_EditToDuplicateTaxIdThrowsArgumentException()
        {
            //Arrange
            var           inputParms     = new Queue <string>(new[] { "edit", "1", "newName", "2" });
            Func <string> consoledequeue = () =>
            {
                if (inputParms.Count == 0)
                {
                    return(string.Empty);
                }
                return(inputParms.Dequeue());
            };

            StringBuilder outputBuilder = new StringBuilder(string.Empty);

            mockConsole.Setup(x => x.ReadLine()).Returns(consoledequeue);
            mockConsole.Setup(x => x.WriteLine(It.IsAny <object>())).Callback((object x) => { outputBuilder.Append(x.ToString() + "\r\n"); });

            var            contact1 = FakeDataFactory.Get <FriendContact>(name: "name", taxId: 1);
            var            contact2 = FakeDataFactory.Get <FriendContact>(name: "name", taxId: 2);
            List <Contact> contacts = new List <Contact>()
            {
                contact1, contact2
            };
            Mock <IRepository <Contact> > rep = ContactsUiTest.mockRepository.Create <IRepository <Contact> >();

            rep.Setup(r => r.Get(It.IsAny <int>()))
            .Returns((int x) => contacts.Where(w => w.TaxId == x).FirstOrDefault());

            rep.Setup(r => r.Update(It.IsAny <Contact>(), It.IsAny <int>())).Returns(true)
            .Callback <Contact, int>((x, y) => {
                if (y != x.TaxId && contacts.Any(w => w.TaxId == y))
                {
                    throw new ArgumentException();
                }
            });

            rep.Setup(r => r.ContainsKey(It.IsAny <int>())).Returns <int>(
                x => (contacts.Where(w => w.TaxId == x).Count() > 0));

            var contactsUi = new ContactsUi(mockConsole.Object, rep.Object);

            //Act
            contactsUi.ProcessLineCommand();

            //Assert
        }
Exemplo n.º 9
0
        public void RepositoryTest_Remove_Success()
        {
            //Arrange
            ContactDto contact = FakeDataFactory.GetDto <WorkDto>();

            var contactsDto = new List <ContactDto>()
            {
                contact
            };
            var repository = new ContactRepository(contactsDto);

            //Act
            bool result = repository.Remove(contact.ToContact());

            //Assert
            Assert.AreEqual(contactsDto.Count, 0);
        }
Exemplo n.º 10
0
        public void ContactUiTest_SearchByNameNotFoundThrowsArgumentException()
        {
            //Arrange
            var            contact  = FakeDataFactory.Get <FriendContact>(name: "name", taxId: 1);
            List <Contact> contacts = new List <Contact>()
            {
                contact
            };
            Mock <IRepository <Contact> > rep = ContactsUiTest.mockRepository.Create <IRepository <Contact> >();
            var contactsUi = new ContactsUi(setupConsole(new[] { "search", "wrong" }), rep.Object);

            rep.Setup(r => r.Get(It.IsAny <string>()))
            .Returns((string x) => contacts.Where(w => w.Name == x).FirstOrDefault());

            //Act
            contactsUi.ProcessLineCommand();

            //Assert
        }
Exemplo n.º 11
0
        public void ContactUiTest_SearchByTaxIdThrowsArgumentException()
        {
            //Arrange
            var            contact  = FakeDataFactory.Get <FriendContact>(name: "name", taxId: 2);
            List <Contact> contacts = new List <Contact>()
            {
                contact
            };
            Mock <IRepository <Contact> > rep = ContactsUiTest.mockRepository.Create <IRepository <Contact> >();

            rep.Setup(r => r.Get(It.IsAny <int>()))
            .Returns((int x) => contacts.Where(w => w.TaxId == x).FirstOrDefault());

            var contactsUi = new ContactsUi(setupConsole(new[] { "search", "1" }), rep.Object);

            //Act
            var returnValue = contactsUi.ProcessLineCommand();

            //Assert
            Assert.AreEqual(true, returnValue);
            Assert.IsTrue(outputBuilder.ToString().Contains("Friend"));
        }
Exemplo n.º 12
0
        public void ContactUiTest_DeleteSuccessful()
        {
            //Arrange
            var           inputParms     = new Queue <string>(new[] { "delete", "1" });
            Func <string> consoledequeue = () =>
            {
                if (inputParms.Count == 0)
                {
                    return(string.Empty);
                }
                return(inputParms.Dequeue());
            };

            StringBuilder outputBuilder = new StringBuilder(string.Empty);

            mockConsole.Setup(x => x.ReadLine()).Returns(consoledequeue);
            mockConsole.Setup(x => x.WriteLine(It.IsAny <object>())).Callback((object x) => { outputBuilder.Append(x.ToString() + "\r\n"); });
            var contact = FakeDataFactory.Get <FriendContact>(name: "name", taxId: 1);

            List <Contact> contacts = new List <Contact>()
            {
                contact
            };
            Mock <IRepository <Contact> > rep = ContactsUiTest.mockRepository.Create <IRepository <Contact> >();

            rep.Setup(r => r.Get(It.IsAny <int>()))
            .Returns((int x) => contacts.Where(w => w.TaxId == x).FirstOrDefault());
            rep.Setup(r => r.Remove(It.IsAny <Contact>())).Returns(true)
            .Callback((Contact x) => contacts.Remove(x));

            var contactsUi = new ContactsUi(mockConsole.Object, rep.Object);

            //Act
            var returnValue = contactsUi.ProcessLineCommand();

            //Assert
            Assert.AreEqual(true, returnValue);
            Assert.AreEqual(0, contacts.Count);
        }
Exemplo n.º 13
0
        public void ContactUiTest_DeleteNotFoundThrowsArgumentException()
        {
            //Arrange
            var contact = FakeDataFactory.Get <FriendContact>(name: "name", taxId: 1);

            List <Contact> contacts = new List <Contact>()
            {
                contact
            };
            Mock <IRepository <Contact> > rep = ContactsUiTest.mockRepository.Create <IRepository <Contact> >();

            rep.Setup(r => r.Get(It.IsAny <int>()))
            .Returns((int x) => contacts.Where(w => w.TaxId == x).FirstOrDefault());
            rep.Setup(r => r.Remove(It.IsAny <Contact>())).Returns(true)
            .Callback((Contact x) => contacts.Remove(x));

            var contactsUi = new ContactsUi(setupConsole(new[] { "delete", "2" }), rep.Object);

            //Act
            contactsUi.ProcessLineCommand();

            //Assert
        }
Exemplo n.º 14
0
        public void RepositoryTest_GetById_Success()
        {
            //Arrange
            WorkDto wc          = (WorkDto)FakeDataFactory.GetDto <WorkDto>();
            var     contactsDto = new List <ContactDto>()
            {
                wc
            };
            var repository = new ContactRepository(contactsDto);

            //Act
            Contact result = repository.Get(1);

            //Assert
            Assert.IsInstanceOfType(result, typeof(WorkContact));
            Assert.AreEqual(result.Address, wc.Address);
            Assert.AreEqual(result.DateCreated, wc.DateCreated);
            Assert.AreEqual(result.DateModified, wc.DateModified);
            Assert.AreEqual(result.Name, wc.Name);
            Assert.AreEqual(result.PhoneNumber, wc.PhoneNumber);
            Assert.AreEqual(result.TaxId, wc.TaxId);
            Assert.AreEqual(((WorkContact)result).Title, wc.Title);
            Assert.AreEqual(((WorkContact)result).Url, wc.Url);
        }