Esempio n. 1
0
    def __init__(self):
        controller = ContactManager()

        while True:
            line = input()

            if line == "":
                exit(0)

            commands = line.split(" ")

            if commands[0] == "AC":
                name = commands[1]
                email_address = input()
                phone_number = input()

                if controller.has_contact(name):
                    print("The contact already exists.")
                else:
                    controller.create_contact(name, email_address,
                                              phone_number)
                    print("Contact successfully created.")

            elif commands[0] == "ECE":
                name = commands[1]
                email_address = input()

                if not controller.has_contact(name):
                    print("No such contact.")
                elif not controller.is_valid_email_address(email_address):
                    print("Invalid email")
                else:
                    controller.change_email_address(name, email_address)
                    print("Contact successfully updated.")

            elif commands[0] == "ECP":
                name = commands[1]
                phone_number = input()

                if not controller.has_contact(name):
                    print("No such contact.")
                elif not controller.is_valid_phone_number(phone_number):
                    print("Invalid phone number")
                else:
                    controller.change_phone_number(name, phone_number)
                    print("Contact successfully updated.")

            elif commands[0] == "LC":
                if not controller.has_contacts():
                    print("No contacts available.")
                else:
                    contacts = controller.get_contacts()
                    for contact in contacts:
                        print(contact.get_name())

            elif commands[0] == "SC":
                name = commands[1]
                if not controller.has_contact(name):
                    print("No such contact.")
                else:
                    contact = controller.get_contact(name)
                    print(contact.get_name())
                    print(contact.get_email_address())
                    print(contact.get_phone_number())

            elif commands[0] == "DC":
                name = commands[1]
                name = commands[1]
                if not controller.has_contact(name):
                    print("No such contact.")
                else:
                    controller.delete_contact(name)
                    print("Contact deleted.")

            elif commands[0] == "FC":
                name = commands[1]
                if not controller.has_contacts():
                    print("No contacts available.")
                else:
                    contacts = controller.find_contacts(name)
                    if not contacts:
                        print("No results.")
                    else:
                        for contact in contacts:
                            print(contact.get_name())
class TestContactManager(unittest.TestCase):
    def setUp(self):
        self.cm = ContactManager()
        self.contact = Contact("Bob", "*****@*****.**", "123456789")

    def add_contact(self):
        self.cm.contacts.append(self.contact)

    def test_has_contacts(self):
        self.assertFalse(self.cm.has_contacts())
        self.add_contact()
        self.assertTrue(self.cm.has_contacts())

    def test_get_contacts(self):
        self.assertEqual(self.cm.get_contacts(), [])
        self.add_contact()
        self.assertEqual(self.cm.get_contacts(), [self.contact])

    def test_get_contact(self):
        self.assertIsNone(self.cm.get_contact(self.contact.get_name()))
        self.add_contact()
        self.assertEqual(self.cm.get_contact(self.contact.get_name()),
                         self.contact)

    def test_has_contact(self):
        self.assertFalse(self.cm.has_contact(self.contact.get_name()))
        self.add_contact()
        self.assertTrue(self.cm.has_contact(self.contact.get_name()))

    def test_create_contact(self):
        self.cm.create_contact(self.contact.get_name(),
                               self.contact.get_email_address(),
                               self.contact.get_phone_number())
        self.assertEqual(self.cm.get_contacts()[0].get_name(),
                         self.contact.get_name())
        self.assertEqual(self.cm.get_contacts()[0].get_email_address(),
                         self.contact.get_email_address())
        self.assertEqual(self.cm.get_contacts()[0].get_phone_number(),
                         self.contact.get_phone_number())

    def test_delete_contact(self):
        self.add_contact()
        self.cm.delete_contact(self.contact.get_name())
        self.assertListEqual(self.cm.get_contacts(), [])

    def test_change_email_address(self):
        self.add_contact()
        self.assertEqual(
            self.cm.get_contact(self.contact.get_name()).get_email_address(),
            self.contact.get_email_address())
        self.cm.change_email_address(self.contact.get_name(),
                                     "*****@*****.**")
        self.assertEqual(
            self.cm.get_contact(self.contact.get_name()).get_email_address(),
            "*****@*****.**")

    def test_is_valid_email_address(self):
        self.assertTrue(self.cm.is_valid_email_address("[email protected]"))
        self.assertFalse(self.cm.is_valid_email_address("a@a"))
        self.assertFalse(self.cm.is_valid_email_address(""))

    def test_change_phone_number(self):
        self.add_contact()
        self.assertEqual(
            self.cm.get_contact(self.contact.get_name()).get_phone_number(),
            self.contact.get_phone_number())
        self.cm.change_phone_number(self.contact.get_name(), "987654321")
        self.assertEqual(
            self.cm.get_contact(self.contact.get_name()).get_phone_number(),
            "987654321")

    def test_is_valid_phone_number(self):
        self.assertTrue(self.cm.is_valid_phone_number("999999999"))
        self.assertFalse(self.cm.is_valid_phone_number("99999999"))
        self.assertFalse(self.cm.is_valid_phone_number(""))
        self.assertFalse(self.cm.is_valid_phone_number("9999999999"))

    def test_find_contacts(self):
        self.add_contact()
        contacts = self.cm.find_contacts("alice")
        self.assertListEqual(contacts, [])
        contacts = self.cm.find_contacts("o")
        self.assertListEqual(contacts, [self.contact])
        contacts = self.cm.find_contacts("")
        self.assertListEqual(contacts, [self.contact])