예제 #1
0
        public void CsvReader_AddWorksAfterRead()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                ContactStore store = new ContactStore();
                store.Add(Contact.Create("first", "ln", "street", "city", "st", "zip"));
                store.Add(Contact.Create("second", "ln", "street", "city", "st", "zip"));
                store.Add(Contact.Create("third", "ln", "street", "city", "st", "zip"));

                CsvContactWriter writer = new CsvContactWriter();

                writer.Write(ms, store.Contacts);

                ms.Seek(0, SeekOrigin.Begin);

                CsvContactReader reader   = new CsvContactReader();
                List <Contact>   contacts = new List <Contact>(reader.Read(ms));

                Assert.AreEqual(3, contacts.Count, "There should be 3 contacts");
                Assert.AreEqual("first", contacts[0].FirstName);
                Assert.AreEqual("second", contacts[1].FirstName);
                Assert.AreEqual("third", contacts[2].FirstName);

                store.Add(Contact.Create("fourth", "ln", "street", "city", "st", "zip"));
                Assert.AreEqual(4, store.Contacts.Count(), "There should be four contacts");

                ContactFieldFilter filter = new ContactFieldFilter();
                filter.SetFirstName("fourth");

                var found = store.Search(filter);
                Assert.AreEqual(1, found.Count(), "There should have been one item found");
                Assert.AreEqual("fourth", found.First().FirstName, "The contact name was wrong");
            }
        }
예제 #2
0
        public void CsvWriterWrites()
        {
            using (MemoryStream stream = new MemoryStream())
            {
                ContactStore store = new ContactStore();
                store.Add(Contact.Create("first", "ln", "street", "city", "st", "zip"));
                store.Add(Contact.Create("second", "ln", "street", "city", "st", "zip"));
                store.Add(Contact.Create("third", "ln", "street", "city", "st", "zip"));

                CsvContactWriter writer = new CsvContactWriter();

                writer.Write(stream, store.Contacts);
                stream.Flush();
                stream.Seek(0, SeekOrigin.Begin);

                StreamReader sr = new StreamReader(stream);

                string header = sr.ReadLine();
                string first  = sr.ReadLine();
                string second = sr.ReadLine();
                string third  = sr.ReadLine();

                Assert.AreEqual("ID,FirstName,LastName,StreetAddress,City,State,PostalCode", header,
                                "Header row is not equal to expected");

                Assert.AreEqual("1,first,ln,street,city,st,zip", first);
                Assert.AreEqual("2,second,ln,street,city,st,zip", second);
                Assert.AreEqual("3,third,ln,street,city,st,zip", third);

                Assert.AreEqual(-1, sr.Peek());
            }
        }
        public void RemoveMiddle()
        {
            ContactStore store = new ContactStore();
            Contact      a     = store.Add(Contact.Create("a", "ln", "street", "city", "st", "zip"));
            Contact      b     = store.Add(Contact.Create("b", "ln", "street", "city", "st", "zip"));
            Contact      c     = store.Add(Contact.Create("c", "ln", "street", "city", "st", "zip"));

            Remove(store, b);

            Assert.AreEqual(2, store.Contacts.Count());
            Assert.AreEqual("a", store.Contacts.First().FirstName);
            Assert.AreEqual("c", store.Contacts.Last().FirstName);
        }
        public void RemoveWithFilterNoMatch()
        {
            ContactStore store      = new ContactStore();
            Contact      c1_with_id = store.Add(Contact.Create("fn1", "ln1", "street1", "city1", "st1", "zip1"));
            Contact      c2_with_id = store.Add(Contact.Create("fn2", "ln2", "street2", "city2", "st2", "zip2"));

            ContactFieldFilter filter = new ContactFieldFilter();

            filter.SetID(5);

            Contact removed;

            Assert.IsFalse(store.Remove(filter, out removed));
        }
        public void RemoveAll()
        {
            ContactStore store = new ContactStore();
            Contact      a     = store.Add(Contact.Create("a", "ln", "street", "city", "st", "zip"));
            Contact      b     = store.Add(Contact.Create("b", "ln", "street", "city", "st", "zip"));
            Contact      c     = store.Add(Contact.Create("c", "ln", "street", "city", "st", "zip"));

            Assert.AreEqual(3, store.Contacts.Count());

            Remove(store, a);
            Remove(store, b);
            Remove(store, c);

            Assert.AreEqual(0, store.Contacts.Count());
        }
        public void RemoveRemovesExactWhenSameContentDifferentIDs()
        {
            ContactStore store      = new ContactStore();
            Contact      c1_with_id = store.Add(Contact.Create("fn", "ln", "street", "city", "st", "zip"));
            Contact      c2_with_id = store.Add(Contact.Create("fn", "ln", "street", "city", "st", "zip"));
            Contact      c3_with_id = store.Add(Contact.Create("fn", "ln", "street", "city", "st", "zip"));
            Contact      c4_with_id = store.Add(Contact.Create("fn", "ln", "street", "city", "st", "zip"));

            Assert.AreEqual(4, store.Contacts.Count());
            Remove(store, c3_with_id);

            CollectionAssert.AreEqual(
                new List <int> {
                1, 2, 4
            },
                store.Contacts.Select(c => c.ID).ToList());
        }
예제 #7
0
        public void Should_Be_Able_to_Add_Contact()
        {
            ContactStore manager = new ContactStore();
            var          contact = manager.Add(new Contact {
                FirstName = "satish", LastName = "V"
            });

            Assert.Equal(1, contact.Id);
        }
        public void RemoveSkipsFirstSameContentButMissingID()
        {
            ContactStore store      = new ContactStore();
            Contact      c1_with_id = store.Add(Contact.Create("fn", "ln", "street", "city", "st", "zip"));
            Contact      c2_with_id = store.Add(Contact.Create("fn", "ln", "street", "city", "st", "zip"));
            Contact      c3_with_id = store.Add(Contact.Create("fn", "ln", "street", "city", "st", "zip"));
            Contact      c4_with_id = store.Add(Contact.Create("fn", "ln", "street", "city", "st", "zip"));
            Contact      missing_id = Contact.Create("fn", "ln", "street", "city", "st", "zip");

            Assert.AreEqual(4, store.Contacts.Count());
            Remove(store, missing_id);

            CollectionAssert.AreEqual(
                new List <int> {
                1, 2, 3, 4
            },
                store.Contacts.Select(c => c.ID).ToList());
        }
        public void RemoveWithFilter()
        {
            ContactStore store      = new ContactStore();
            Contact      c1_with_id = store.Add(Contact.Create("fn1", "ln1", "street1", "city1", "st1", "zip1"));
            Contact      c2_with_id = store.Add(Contact.Create("fn2", "ln2", "street2", "city2", "st2", "zip2"));
            Contact      c3_with_id = store.Add(Contact.Create("fn3", "ln3", "street3", "city3", "st3", "zip3"));
            Contact      c4_with_id = store.Add(Contact.Create("fn4", "ln4", "street4", "city4", "st4", "zip4"));
            Contact      c5_with_id = store.Add(Contact.Create("fn5", "ln5", "street5", "city5", "st5", "zip5"));
            Contact      c6_with_id = store.Add(Contact.Create("fn6", "ln6", "street6", "city6", "st6", "zip6"));
            Contact      c7_with_id = store.Add(Contact.Create("fn7", "ln7", "street7", "city7", "st7", "zip7"));

            Assert.AreEqual(7, store.Contacts.Count());

            Contact removed;

            ContactFieldFilter filterFN = new ContactFieldFilter();

            filterFN.SetFirstName("fn1");
            Assert.IsTrue(store.Remove(filterFN, out removed));
            Assert.AreEqual(1, removed.ID.Value);

            ContactFieldFilter filterLN = new ContactFieldFilter();

            filterLN.SetLastName("ln2");
            Assert.IsTrue(store.Remove(filterLN, out removed));
            Assert.AreEqual(2, removed.ID.Value);

            ContactFieldFilter filterStreet = new ContactFieldFilter();

            filterStreet.SetStreetAddress("street3");
            Assert.IsTrue(store.Remove(filterStreet, out removed));
            Assert.AreEqual(3, removed.ID.Value);

            ContactFieldFilter filterCity = new ContactFieldFilter();

            filterCity.SetCity("city4");
            Assert.IsTrue(store.Remove(filterCity, out removed));
            Assert.AreEqual(4, removed.ID.Value);

            ContactFieldFilter filterState = new ContactFieldFilter();

            filterState.SetState("st5");
            Assert.IsTrue(store.Remove(filterState, out removed));
            Assert.AreEqual(5, removed.ID.Value);

            ContactFieldFilter filterZip = new ContactFieldFilter();

            filterZip.SetPostalCode("zip6");
            Assert.IsTrue(store.Remove(filterZip, out removed));
            Assert.AreEqual(6, removed.ID.Value);

            ContactFieldFilter filterId = new ContactFieldFilter();

            filterId.SetID(7);
            Assert.IsTrue(store.Remove(filterId, out removed));
            Assert.AreEqual(7, removed.ID.Value);

            Assert.AreEqual(0, store.Contacts.Count());
        }
        public void Menu()
        {
            Console.WriteLine("Size of List is " + store.ContactListSize);

            int flag = 1;

            while (flag != 0)
            {
                Console.WriteLine("\nType keywords to perform Action\n Add \tDisplay Search \tUpdate \tRemove \tExit");
                string choice = Console.ReadLine();
                switch (choice.ToLower())
                {
                case "add":
                    store.Add(ContactDetails());
                    break;

                case "display":
                    Display(store.ContactList);
                    break;

                case "search":
                    try
                    {
                        List <Contact> matching = store.Search(Console.ReadLine());
                        Display(matching);
                    }
                    catch (StudentNotFoundException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    break;

                case "update":
                    Console.WriteLine("Ënter the name you want to update");
                    store.Update(Console.ReadLine());
                    break;

                case "remove":
                    store.Remove(Console.ReadLine());
                    Display(store.ContactList);
                    break;

                case "exit":
                    store.Serialize();
                    flag = 0;
                    break;

                default:
                    Console.WriteLine("Enter Proper KeyWord");
                    break;
                }
            }
        }
        public void RemoveOnly()
        {
            ContactStore store = new ContactStore();
            Contact      c     = Contact.Create("fn", "ln", "street", "city", "st", "zip");

            c = store.Add(c);
            Assert.AreEqual(1, store.Contacts.Count());

            Remove(store, c);
            Assert.AreEqual(0, store.Contacts.Count());
        }
        public void RemoveMissing()
        {
            ContactStore store = new ContactStore();
            Contact      a     = store.Add(Contact.Create("a", "ln", "street", "city", "st", "zip"));
            Contact      b     = store.Add(Contact.Create("b", "ln", "street", "city", "st", "zip"));
            Contact      c     = store.Add(Contact.Create("c", "ln", "street", "city", "st", "zip"));

            Assert.AreEqual(3, store.Contacts.Count());

            Contact d = Contact.Create("d", "ln", "street", "city", "st", "zip");

            Remove(store, d);

            Assert.AreEqual(3, store.Contacts.Count());

            CollectionAssert.AreEqual(
                new List <string> {
                "a", "b", "c"
            },
                store.Contacts.Select(c => c.FirstName).ToList());
        }
예제 #13
0
        public void CsvReader_ReadsHappyPath_RoundTrip()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                ContactStore store = new ContactStore();
                store.Add(Contact.Create("first", "ln", "street", "city", "st", "zip"));
                store.Add(Contact.Create("second", "ln", "street", "city", "st", "zip"));
                store.Add(Contact.Create("third", "ln", "street", "city", "st", "zip"));

                CsvContactWriter writer = new CsvContactWriter();

                writer.Write(ms, store.Contacts);

                ms.Seek(0, SeekOrigin.Begin);

                CsvContactReader reader   = new CsvContactReader();
                List <Contact>   contacts = new List <Contact>(reader.Read(ms));

                Assert.AreEqual(3, contacts.Count, "There should be 3 contacts");
                Assert.AreEqual("first", contacts[0].FirstName);
                Assert.AreEqual("second", contacts[1].FirstName);
                Assert.AreEqual("third", contacts[2].FirstName);
            }
        }
        public void Add_One_Test()
        {
            ContactStore store = new ContactStore();
            Contact      c     = Contact.Create("fn", "ln", "street", "city", "st", "zip");

            Assert.IsFalse(c.ID.HasValue, "New contact should not have an ID");

            Contact c_with_id = store.Add(c);

            Assert.IsFalse(c.ID.HasValue, "Old contact should not have been updated with an ID");
            Assert.IsTrue(c_with_id.ID.HasValue, "Stored contact should have an ID");
            Assert.IsTrue(c_with_id.ID.Value > 0, "The new contact ID should be greater than 0");

            Assert.AreEqual(c_with_id.FirstName, c.FirstName);
            Assert.AreEqual(c_with_id.LastName, c.LastName);
            Assert.AreEqual(c_with_id.StreetAddress, c.StreetAddress);
            Assert.AreEqual(c_with_id.City, c.City);
            Assert.AreEqual(c_with_id.State, c.State);
            Assert.AreEqual(c_with_id.PostalCode, c.PostalCode);
        }
        public void Search_Missing()
        {
            ContactStore store = new ContactStore();

            store.Add(Contact.Create("x", "x", "x", "x", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "x", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "x", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "x", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "x", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "x", "x", "x"));

            ContactFieldFilter filter = new ContactFieldFilter();

            filter.SetID(7);

            Assert.AreEqual(0, store.Search(filter).Count());
        }
예제 #16
0
        private void Form1_Load(object sender, EventArgs e)
        {
            Button myBtn = (Button)sender;

            switch (myBtn.Name)
            {
            case "button1":
                UpdateGrid();
                break;

            case "button2":
                string  name       = textBox1.Text;
                string  number     = textBox2.Text;
                Contact addContact = new Contact();
                addContact.Name   = name;
                addContact.Number = number;
                addContact.GetGuid();
                cs.Add(addContact);
                cs.Serialize();
                UpdateGrid();
                break;
            }
        }
        public void Add_N_Test()
        {
            ContactStore store = new ContactStore();

            store.Add(Contact.Create("first", "ln", "street", "city", "st", "zip"));
            store.Add(Contact.Create("second", "ln", "street", "city", "st", "zip"));
            store.Add(Contact.Create("third", "ln", "street", "city", "st", "zip"));
            store.Add(Contact.Create("fourth", "ln", "street", "city", "st", "zip"));
            store.Add(Contact.Create("fifth", "ln", "street", "city", "st", "zip"));
            store.Add(Contact.Create("sixth", "ln", "street", "city", "st", "zip"));

            Assert.AreEqual(6, store.Contacts.Count(), "There should be six contacts in the list");

            List <string> expected = new List <string> {
                "fifth", "first", "fourth", "second", "sixth", "third"
            };
            List <string> actual = store.Contacts.Select(c => c.FirstName).ToList();

            CollectionAssert.AreEqual(expected, actual, "The ordering was not as expected");
        }
        public void Search_City()
        {
            ContactStore store = new ContactStore();

            store.Add(Contact.Create("x", "x", "x", "a", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "b", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "c", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "a", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "b", "x", "x"));
            store.Add(Contact.Create("x", "x", "x", "c", "x", "x"));

            ContactFieldFilter filter = new ContactFieldFilter();

            filter.SetCity("b");

            CollectionAssert.AreEqual(
                new List <int> {
                2, 5
            },
                store.Search(filter).Select(c => c.ID).ToList());
        }
        public void Search_MultipleFields()
        {
            ContactStore store = new ContactStore();

            store.Add(Contact.Create("x", "g", "x", "x", "x", "a"));
            store.Add(Contact.Create("x", "g", "x", "x", "x", "b"));
            store.Add(Contact.Create("x", "g", "x", "x", "x", "c"));
            store.Add(Contact.Create("x", "h", "x", "x", "x", "a"));
            store.Add(Contact.Create("x", "h", "x", "x", "x", "b"));
            store.Add(Contact.Create("x", "h", "x", "x", "x", "c"));

            ContactFieldFilter filter = new ContactFieldFilter();

            filter.SetLastName("h");
            filter.SetPostalCode("b");

            CollectionAssert.AreEqual(
                new List <int> {
                5
            },
                store.Search(filter).Select(c => c.ID).ToList());
        }
 public CommandResult Execute()
 {
     return(new NonUndoCommandResult(this, store.Add(contacts)));
 }