Skip to content

Instantly share code, notes, and snippets.

@isaacssemugenyi
Last active December 17, 2025 07:08
Show Gist options
  • Select an option

  • Save isaacssemugenyi/4d867a090fa5396db6ea75ed223dcb44 to your computer and use it in GitHub Desktop.

Select an option

Save isaacssemugenyi/4d867a090fa5396db6ea75ed223dcb44 to your computer and use it in GitHub Desktop.
√ adds a contact successfully with valid data
√ does not add contact when name is missing
√ does not add contact with duplicate phone number
√ finds a contact by phone number
√ returns null when searching for non-existent phone
√ updates an existing contact
√ returns false when updating a non-existent contact
√ deletes an existing contact
√ returns false when deleting non-existent contact
√ searches contacts by keyword across name, phone, and email
import importlib
import address_book
import pytest
@pytest.fixture(autouse=True)
def reset_address_book():
# Reload module before every test to reset globals
importlib.reload(address_book)
def test_add_contact_successfully():
result = address_book.add_contact(
"John Doe",
"0700000001",
"john@email.com",
"Kampala"
)
assert result is True
contacts = address_book.get_all_contacts()
assert len(contacts) == 1
assert contacts[0]["name"] == "John Doe"
if __name__ == "__main__":
add_contact("John Doe", "0700000001", "john@email.com", "Kampala")
add_contact("Jane Smith", "0700000002", "", "")
update_contact("0700000001", "John D.", None, "Ntinda")
print(get_all_contacts())
describe('Legacy Address Book – Characterization Tests', () => {
beforeEach(() => {
// Reset module state before every test
jest.resetModules();
});
test('adds a contact successfully with valid data', () => {
const book = require('./addressbook');
const result = book.addContact(
'John Doe',
'0700000001',
'john@email.com',
'Kampala'
);
expect(result).toBe(true);
const contacts = book.getAllContacts();
expect(contacts.length).toBe(1);
expect(contacts[0].name).toBe('John Doe');
expect(contacts[0].phone).toBe('0700000001');
});
})
if (require.main === module) {
addContact('John Doe', '0700000001', 'john@email.com', 'Kampala');
addContact("Jane Smith", "0700000002", "", "");
updateContact("0700000001", "John D.", null, "Ntinda");
console.log(getAllContacts());
console.log(findContactByPhone("0700000002"));
console.log(searchContacts("John"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment