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_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 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()))
    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_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)
Exemplo n.º 6
0
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())
Exemplo n.º 7
0
    def matching_existing(self, contact: Contact) -> None:
        '''check if there is aready a contact with the same properties'''
        # get the contact's name and phonenumber
        contact_phonenumber = contact.get_phone_number()
        contact_name = contact.get_name()

        # loop through all the rows
        for each_row in self.cursor.execute('''
            select * from phonebook
            '''):

            # see if that contact have the same phonenumber as the one that was inputed
            same_phonenumber = each_row[2] == contact_phonenumber
            # see if that contact have the same name as the one that was inputed
            same_name = each_row[1] == contact_name

            # if both true return true
            if same_phonenumber is True and same_name is True:
                return True

        # after looking through all the contacts and still none, return flase
        return False
    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)
Exemplo n.º 9
0
    def save_contact(self, contact: Contact) -> None:
        '''save a contact to the database'''

        # reutrn if there's already a contact with the same properties
        if self.matching_existing(contact) is True:
            return

        # validate phonenumber
        if not self.validate_phone_number(contact.get_phone_number()):
            return

        # add contact to database
        print("adding...")
        self.cursor.execute(
            '''
            insert into phonebook values (null, :name, :phonenumber)
            ''', {
                "name": contact.get_name(),
                "phonenumber": contact.get_phone_number()
            })

        self.connection.commit()
        print("added")
Exemplo n.º 10
0
    def get_contacts_by_id(self, contact_id: int) -> Contact:
        '''get a contact whose id is the one inputed'''
        cursor = self.cursor.execute(
            '''
            select * from phonebook where id = :id
            ''', {'id': contact_id})

        row_list = cursor.fetchall()

        if len(row_list) == 0:
            return None

        row = row_list[0]
        contact_id = row[0]
        contact_name = row[1]
        contact_phonenumber = row[2]

        contact = Contact(contact_id, contact_name, contact_phonenumber)

        return contact
Exemplo n.º 11
0
    def get_contacts_by_name(self, name: str) -> list[Contact]:
        '''get the contacts who has a certain name'''

        # create an empty contact list first
        contact_list = list[Contact]()

        for each_row in self.cursor.execute(
                '''
            select * from phonebook where name = :name
            ''', {"name": name}):
            # get contact name and phonenumber from this row
            contact_id = each_row[0]
            contact_name = each_row[1]
            contact_phonenumber = each_row[2]

            # assemble id, name and phonenumber into a contact
            contact = Contact(contact_id, contact_name, contact_phonenumber)

            # add the assembled contact into the list
            contact_list.append(contact)

        # return list
        return contact_list
Exemplo n.º 12
0
    def get_contacts(self) -> list[Contact]:
        """get the contacts of this list"""

        # create an empty contact list first
        contact_list = list[Contact]()

        # select all contacts from phonebook
        results = self.cursor.execute('select * from phonebook')

        for each_row in results:
            # get contact name and phonenumber from this row
            name = each_row[1]
            phonenumber = each_row[2]
            contact_id = each_row[0]

            # assemble name and phonenumber into a contact
            contact = Contact(contact_id, name, phonenumber)

            # add the assembled contact into the list
            contact_list.append(contact)

        # return the list
        return contact_list