rexx-address-book/tests/test_appdb.rexx

364 lines
12 KiB
Rexx
Executable File

#!/usr/bin/env rexx
/***************************************************************************
* Rexx Address Book - Database Test Script *
* *
* This script tests the database functionality without the ncurses UI. *
* It steps through creating, reading, updating, and deleting contacts *
* along with their associated data. *
***************************************************************************/
/* Setup environment */
call setEnv
tests = .TestSuite~new()
DO
tests~TestCreateContact
tests~TestGetContact
tests~TestGetAllContacts
tests~TestAddPhoneNumber
tests~TestAddEmailAddress
tests~TestAddRealAddress
tests~TestFullDetailRetrieval
tests~TestUpdateContact
tests~TestSearchForContact
tests~TestEmailDeletion
tests~TestPhoneNumberDeletion
tests~TestDeleteEntireContact
END
tests~tearDown
exit 0
::ROUTINE setEnv
.environment~home = SysGetpwnam("gmgauthier", "d")
.environment~projectRoot = .home||"/Projects/rexx-address-book"
.environment~pkgPath = .projectRoot||"/app"
.environment~dbPath = .projectRoot||"/db"
.environment~dbFile = .dbPath||"/test_contacts.sqlite"
.environment["REXX_PATH"] = .projectRoot||";"||.pkgPath||";"||.dbPath||";"
::requires 'app/appdb.cls'
::requires 'app/utils.rex'
::requires 'ooSQLite.cls'
::requires "rxunixsys" LIBRARY
::requires 'ncurses.cls'
::CLASS TestSuite
::METHOD init
self~setUp()
::METHOD setUp
expose db contactDict
if SysFileExists(.dbFile) then do
call SysFileDelete .dbFile
say "Deleted existing test database."
end
say "Initializing test database at:" .dbFile
db = .AddressBookDB~new()
self~setupFixtureData()
::METHOD tearDown
expose db
db~closeDb()
say "Test completed!"
::METHOD setupFixtureData
expose db contactDict addressDict emailArray phoneArray
emailArray = .Array~new()
emailDict1 = .Directory~new()
emailDict1~Type = "HOME"
emailDict1~Number = "john.doe@home.com"
emailArray~append(emailDict1)
emailDict2 = .Directory~new()
emailDict2~Type = "WORK"
emailDict2~Number = "john.doe@work.com"
emailArray~append(emailDict2)
phoneArray = .Array~new()
phoneDict1 = .Directory~new()
phoneDict1~Type = "HOME"
phoneDict1~Number = "123-456-7890"
phoneArray~append(phoneDict1)
phoneDict2 = .Directory~new()
phoneDict2~Type = "WORK"
phoneDict2~Number = "987-654-3210"
phoneArray~append(phoneDict2)
contactDict = .Directory~new()
contactDict~First_Name = "John"
contactDict~Last_Name = "Doe"
::METHOD TestCreateContact
expose db contactDict
/* Test contact creation */
say ""
say "=== TESTING CONTACT CREATION ==="
say "Creating contact:" contactDict~First_Name contactDict~Last_Name
contactId = db~addContact(contactDict)
say "Contact created with ID:" contactId
::METHOD TestGetContact
expose db contactDict
/* Test retrieving contact */
say ""
say "=== TESTING CONTACT RETRIEVAL ==="
contact = db~getContact(1)
if contact \= .nil then do
say "Contact retrieved successfully:"
say " ID:" contact['ID']
say " Name:" contact['FIRST_NAME'] contact['LAST_NAME']
say " Created at:" contact['CREATED_AT']
end
else do
say "ERROR: Failed to retrieve contact!"
end
::METHOD TestGetAllContacts
expose db contactDict
/* Get all contacts */
say ""
say "=== TESTING GET ALL CONTACTS ==="
/* add some extras */
db~addContact(contactDict)
db~addContact(contactDict)
allContacts = db~getAllContacts()
say "Total contacts in database:" allContacts~items()
do contact over allContacts
say " " contact['FIRST_NAME'] contact['LAST_NAME'] "(ID:" contact['ID']")"
end
::METHOD TestAddPhoneNumber
expose db contactDict
/* Test adding phone numbers */
say ""
say "=== TESTING PHONE NUMBER ADDITION ==="
Say "Adding phone numbers for contact id: 1"
say "Adding home phone number..."
homePhoneId = db~addPhoneNumber(1, "Home", "555-123-4567")
say "Added phone with ID:" homePhoneId
say "Adding mobile phone number..."
mobilePhoneId = db~addPhoneNumber(1, "Mobile", "555-987-6543")
say "Added phone with ID:" mobilePhoneId
::METHOD TestAddEmailAddress
expose db contactDict
/* Test adding email addresses */
say ""
say "=== TESTING EMAIL ADDITION ==="
Say "Adding email addresses for contact id: 1"
say "Adding personal email..."
personalEmailId = db~addEmailAddress(1, "Personal", "john.doe@personal.example")
say "Added email with ID:" personalEmailId
say "Adding work email..."
workEmailId = db~addEmailAddress(1, "Work", "john.doe@work.example")
say "Added email with ID:" workEmailId
::METHOD TestAddRealAddress
expose db contactDict
/* Test adding physical address */
say ""
say "=== TESTING ADDRESS ADDITION ==="
Say "Adding real addresses for contact id: 1"
say "Adding home address..."
homeAddressId = db~addRealAddress(1, "Home", "123 Main St", "Anytown", "NY", "12345", "USA")
say "Added address with ID:" homeAddressId
::METHOD TestFullDetailRetrieval
expose db ContactDict
/* Read the complete contact details */
say ""
say "=== TESTING COMPLETE CONTACT RETRIEVAL ==="
contact = db~getContact(1)
if contact \= .nil then do
say "Contact retrieved successfully:"
say " ID:" contact['ID']
say " Name:" contact['FIRST_NAME'] contact['LAST_NAME']
say " Created at:" contact['CREATED_AT']
phones = db~getPhoneNumbers(1)
say "Phone numbers:"
do phone over phones
say " " phone['TYPE']":" phone['NUMBER'] "(ID:" phone['ID']")"
end
emails = db~getEmailAddresses(1)
say "Email addresses:"
do email over emails
say " " email['TYPE']":" email['EMAIL'] "(ID:" email['ID']")"
end
addresses = db~getRealAddresses(1)
say "Addresses:"
do address over addresses
say " " address['TYPE']" (ID: "address['ID']"):"
say " Street:" address['STREET']
say " City:" address['CITY']
say " State:" address['STATE']
say " Postal code:" address['POSTAL_CODE']
say " Country:" address['COUNTRY']
end
end
else do
say "ERROR: Failed to retrieve complete contact details!"
end
::METHOD TestUpdateContact
expose db ContactDict
/* Test updating contact information */
say ""
say "=== TESTING CONTACT UPDATE ==="
say "Updating contact name..."
updatedFirstName = "Jonathan"
updatedLastName = "Dorian"
result = db~updateContact(1, updatedFirstName, updatedLastName)
say "Update successful:" result
say "Updating home phone number..."
result = db~updatePhoneNumber(1, "Home", "555-111-2222")
say "Update successful:" result
say "Updating work email..."
result = db~updateEmailAddress(2, "Work", "jonathan.dorian@hospital.example")
say "Update successful:" result
say "Updating home address..."
result = db~updateRealAddress(1, "Home", "456 Oak Ave", "Springfield", "IL", "54321", "USA")
say "Update successful:" result
/* Read the updated contact details */
say ""
say "=== TESTING UPDATED CONTACT RETRIEVAL ==="
contact = db~getContact(1)
if contact \= .nil then do
say "Updated contact:"
say " ID:" contact['ID']
say " Name:" contact['FIRST_NAME'] contact['LAST_NAME']
say " Created at:" contact['CREATED_AT']
say "Updated phone numbers:"
phones = db~getPhoneNumbers(1)
do phone over phones
say " " phone['TYPE']":" phone['NUMBER'] "(ID:" phone['ID']")"
end
emails = db~getEmailAddresses(1)
say "Email addresses:"
do email over emails
say " " email['TYPE']":" email['EMAIL'] "(ID:" email['ID']")"
end
addresses = db~getRealAddresses(1)
say "Addresses:"
do address over addresses
say " " address['TYPE']" (ID: "address['ID']"):"
say " Street:" address['STREET']
say " City:" address['CITY']
say " State:" address['STATE']
say " Postal code:" address['POSTAL_CODE']
say " Country:" address['COUNTRY']
end
end
else do
say "ERROR: Failed to retrieve updated contact details!"
end
::METHOD TestSearchForContact
expose db
/* Test searching functionality */
say ""
say "=== TESTING SEARCH FUNCTIONALITY ==="
first_names = .Array~of("Jane", "John", "Bob", "Sarah", "Tom", "Davey")
last_names = .Array~of("Smith", "Johnson", "Smith", "Williams", "Jones", "Jones")
Do i = 1 to 6
tempDict = .Directory~new()
tempDict~First_Name = first_names[i]; tempDict~Last_Name = last_names[i];
contactId = db~addContact(tempDict)
END
/* Search for contacts */
say "Searching for 'Smith'..."
results = db~searchContacts("Smith")
say "Found" results~items() "contacts:"
do contact over results
say " " contact['FIRST_NAME'] contact['LAST_NAME'] "(ID:" contact['ID']")"
end
say "Searching for 'John'..."
results = db~searchContacts("John")
say "Found" results~items() "contacts:"
do contact over results
say " " contact['FIRST_NAME'] contact['LAST_NAME'] "(ID:" contact['ID']")"
end
::METHOD TestEmailDeletion
expose db contactDict
say ""
say "=== TESTING EMAIL DELETION ==="
emails = db~getEmailAddresses(1)
say "Email addresses Before deletion:"
do email over emails
say " " email['TYPE']":" email['EMAIL'] "(ID:" email['ID']")"
end
/* Test deleting an email */
say "Deleting personal email..."
result = db~deleteEmailAddress(1)
say "Deletion successful:" result
/* Get contact to verify deletion */
emails = db~getEmailAddresses(1)
say "Email addresses After deletion:"
do email over emails
say " " email['TYPE']":" email['EMAIL'] "(ID:" email['ID']")"
end
::METHOD TestPhoneNumberDeletion
expose db contactDict
say ""
say "=== TESTING PHONE DELETION ==="
phones = db~getPhoneNumbers(1)
say "Phone Numbers Before deletion:"
do phone over phones
say " " phone['TYPE']":" phone['NUMBER'] "(ID:" phone['ID']")"
end
say "Deleting Personal Phone Number..."
result = db~deletePhoneNumber(1)
say "Deletion successful:" result
phones = db~getPhoneNumbers(1)
say "Phone Numbers After deletion:"
do phone over phones
say " " phone['TYPE']":" phone['NUMBER'] "(ID:" phone['ID']")"
end
::METHOD TestDeleteEntireContact
expose db
/* Test deleting the entire contact */
say ""
say "=== TESTING FULL CONTACT DELETION ==="
result = db~deleteContact(1)
say "Deletion successful:" result
/* Try to retrieve the deleted contact */
contact = db~getContact(1)
if contact = .nil then
say "Contact successfully deleted - could not retrieve contact with ID: 1"
else
say "ERROR: Contact was not properly deleted!"