def test_default_phonebook_should_be_empty(self): '''check if a new phonebook is empty''' phonebook = PhoneBook() phonebook.initialize() expected = 0 actual = len(phonebook.get_contacts()) self.assertEqual(expected, actual)
def create_from_input(person_name: str, phone_number: str, phonebook: PhoneBook) -> None: ''' create from input ''' contact = Contact(phonebook.create_id(), person_name, phone_number) phonebook.save_contact(contact) print_list(phonebook.get_contacts())
def test_create_and_delete_positive(self): """should raise TypeError when use a int as name""" phonebook = PhoneBook() create_from_input("jeif", "1234567890", phonebook) contact = phonebook.get_contacts_by_name("jeif")[0] delete_from_input(str(contact.get_contact_id()), phonebook) contact = phonebook.get_contacts_by_id(contact.get_contact_id()) self.assertIsNone(contact)
def test_allow_duplicate(self): '''check if the database allow duplicates''' phonebook = PhoneBook() phonebook.initialize() contact1 = Contact(phonebook.create_id(), "A", "1234567890") contact2 = Contact(phonebook.create_id(), "A", "0987654321") phonebook.save_contact(contact1) phonebook.save_contact(contact2) self.assertEqual(2, len(phonebook.get_contacts()))
def test_add_contact(self): '''test if you can add a contact''' phonebook = PhoneBook() phonebook.initialize() expected_contact = Contact(phonebook.create_id(), "A", "0123456789") phonebook.save_contact(expected_contact) actual_contact = phonebook.get_contacts_by_name("A")[0] self.assertEqual(expected_contact.name, actual_contact.name) self.assertEqual(expected_contact.phonenumber, actual_contact.phonenumber)
def delete_from_input(contact_id: str, phonebook: PhoneBook): ''' delete from input ''' contact_id = int(contact_id) contact = phonebook.get_contacts_by_id(contact_id) if contact is None: print("contact does not exist") return phonebook.delete_contact(contact, contact_id) list_phonenumbers = phonebook.get_contacts() if len(list_phonenumbers) == 1 or len(list_phonenumbers) > 1: print_list(phonebook.get_contacts()) else: print("there are no contacts in phonebook now")
def test_delete_first_contact_named_a(self): '''try delete the first contact name a''' phonebook = PhoneBook() phonebook.initialize() phonebook.save_contact( Contact(phonebook.create_id(), "A", "1234567890")) phonebook.save_contact( Contact(phonebook.create_id(), "A", "0987654321")) self.assertEqual(2, len(phonebook.get_contacts())) contact = phonebook.get_contacts_by_id(1) phonebook.delete_contact(contact, 1) self.assertEqual(len(phonebook.get_contacts()), 1) contact = phonebook.get_contacts_by_id(1) self.assertIsNone(contact)
def test_delete_all_contacts_named_a(self): '''check if the delete all contacts by name function works''' phonebook = PhoneBook() phonebook.initialize() phonebook.save_contact( Contact(phonebook.create_id(), "A", "1234567890")) phonebook.save_contact( Contact(phonebook.create_id(), "A", "0987654321")) self.assertEqual(2, len(phonebook.get_contacts())) phonebook.delete_all_contacts_by_name("A") self.assertEqual(0, len(phonebook.get_contacts()))
def setUp(self): phonebook = PhoneBook() phonebook.cursor.execute( 'drop table phonebook' )
def test_matching_existing_negative(self): '''try to add a contact that doesn't have the same properties should work''' phonebook = PhoneBook() phonebook.initialize() phonebook.save_contact( Contact(phonebook.create_id(), "A", "1234567890")) self.assertEqual(len(phonebook.get_contacts()), 1) phonebook.save_contact( Contact(phonebook.create_id(), "B", "0987654321")) self.assertEqual(len(phonebook.get_contacts()), 2) contact = Contact(phonebook.create_id(), "c", "0987654321") self.assertEqual(phonebook.matching_existing(contact), False)
def test_no_duplicate_contact(self): '''try to add a contact with the same properties should not work''' phonebook = PhoneBook() phonebook.initialize() phonebook.save_contact( Contact(phonebook.create_id(), "A", "1234567890")) self.assertEqual(1, len(phonebook.get_contacts())) phonebook.save_contact( Contact(phonebook.create_id(), "A", "1234567890")) self.assertEqual(1, len(phonebook.get_contacts()))
print("contact does not exist") return phonebook.delete_contact(contact, contact_id) list_phonenumbers = phonebook.get_contacts() if len(list_phonenumbers) == 1 or len(list_phonenumbers) > 1: print_list(phonebook.get_contacts()) else: print("there are no contacts in phonebook now") if __name__ == "__main__": phone_book = PhoneBook() # main loop while True: CMD_PART_A = "You can read, create and delete. " CMD_PART_B = "If you don't want to use the code anymore, " CMD_PART_C = "you can type 'quit' or 'exit'. " CMD_PART_D = "What do you want to do? " cmd = input(CMD_PART_A + CMD_PART_B + CMD_PART_C + CMD_PART_D) if cmd == "read": print_list(phone_book.get_contacts()) if cmd == "create": p = input("person's name? ") pn = input("phone number? ")