Skip to content

Instantly share code, notes, and snippets.

@isaacssemugenyi
Created December 21, 2025 22:13
Show Gist options
  • Select an option

  • Save isaacssemugenyi/6245ca5b05551fa9c4a11b8d53b86772 to your computer and use it in GitHub Desktop.

Select an option

Save isaacssemugenyi/6245ca5b05551fa9c4a11b8d53b86772 to your computer and use it in GitHub Desktop.
"""
Characterization tests for legacy address_book.py
"""
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"
def test_add_contact_fails_when_name_missing():
result = address_book.add_contact(
"",
"0700000002",
"test@email.com",
"Kampala"
)
assert result is False
assert len(address_book.get_all_contacts()) == 0
def test_add_contact_fails_for_duplicate_phone():
address_book.add_contact("John", "0700000003", "", "")
result = address_book.add_contact("Jane", "0700000003", "", "")
assert result is False
assert len(address_book.get_all_contacts()) == 1
def test_find_contact_by_phone():
address_book.add_contact("Alice", "0700000004", "alice@email.com", "")
contact = address_book.find_contact_by_phone("0700000004")
assert contact is not None
assert contact["name"] == "Alice"
def test_find_contact_returns_none_when_not_found():
contact = address_book.find_contact_by_phone("0999999999")
assert contact is None
def test_update_existing_contact():
address_book.add_contact("Bob", "0700000005", "", "")
result = address_book.update_contact(
"0700000005",
"Bobby",
"bob@email.com",
"Ntinda"
)
assert result is True
updated = address_book.find_contact_by_phone("0700000005")
assert updated["name"] == "Bobby"
assert updated["email"] == "bob@email.com"
assert updated["address"] == "Ntinda"
assert "updated_at" in updated
def test_update_non_existent_contact():
result = address_book.update_contact(
"0700000999",
"Nobody",
"",
""
)
assert result is False
def test_delete_existing_contact():
address_book.add_contact("Carl", "0700000006", "", "")
result = address_book.delete_contact("0700000006")
assert result is True
assert len(address_book.get_all_contacts()) == 0
def test_delete_non_existent_contact():
result = address_book.delete_contact("0700000123")
assert result is False
def test_search_contacts_by_keyword():
address_book.add_contact("Daniel", "0700000007", "dan@email.com", "")
address_book.add_contact("Sarah", "0700000008", "sarah@email.com", "")
results = address_book.search_contacts("Daniel")
assert len(results) == 1
assert results[0]["name"] == "Daniel"
/***************************************
* Characterization Tests for AddressBook
***************************************/
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');
});
test('does not add contact when name is missing', () => {
const book = require('./addressbook');
const result = book.addContact(
'',
'0700000002',
'test@email.com',
'Kampala'
);
expect(result).toBe(false);
expect(book.getAllContacts().length).toBe(0);
});
test('does not add contact with duplicate phone number', () => {
const book = require('./addressbook');
book.addContact('John', '0700000003', '', '');
const result = book.addContact('Jane', '0700000003', '', '');
expect(result).toBe(false);
expect(book.getAllContacts().length).toBe(1);
});
test('finds a contact by phone number', () => {
const book = require('./addressbook');
book.addContact('Alice', '0700000004', 'alice@email.com', '');
const contact = book.findContactByPhone('0700000004');
expect(contact).not.toBeNull();
expect(contact.name).toBe('Alice');
});
test('returns null when searching for non-existent phone', () => {
const book = require('./addressbook');
const contact = book.findContactByPhone('0999999999');
expect(contact).toBeNull();
});
test('updates an existing contact', () => {
const book = require('./addressbook');
book.addContact('Bob', '0700000005', '', '');
const result = book.updateContact(
'0700000005',
'Bobby',
'bob@email.com',
'Ntinda'
);
expect(result).toBe(true);
const updated = book.findContactByPhone('0700000005');
expect(updated.name).toBe('Bobby');
expect(updated.email).toBe('bob@email.com');
expect(updated.address).toBe('Ntinda');
expect(updated.updatedAt).toBeDefined();
});
test('returns false when updating a non-existent contact', () => {
const book = require('./addressbook');
const result = book.updateContact(
'0700000999',
'Nobody',
'',
''
);
expect(result).toBe(false);
});
test('deletes an existing contact', () => {
const book = require('./addressbook');
book.addContact('Carl', '0700000006', '', '');
const result = book.deleteContact('0700000006');
expect(result).toBe(true);
expect(book.getAllContacts().length).toBe(0);
});
test('returns false when deleting non-existent contact', () => {
const book = require('./addressbook');
const result = book.deleteContact('0700000123');
expect(result).toBe(false);
});
test('searches contacts by keyword across name, phone, and email', () => {
const book = require('./addressbook');
book.addContact('Daniel', '0700000007', 'dan@email.com', '');
book.addContact('Sarah', '0700000008', 'sarah@email.com', '');
const results = book.searchContacts('Daniel');
expect(results.length).toBe(1);
expect(results[0].name).toBe('Daniel');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment