#!/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 */ .local~home = SysGetpwnam("gmgauthier", "d") .local~projectRoot = .home||"/Projects/rexx-address-book" .local~appPkg = .projectRoot||"/app" .local~dbPath = .projectRoot||"/db/test_contacts.sqlite" /* Use a test database */ tests = .TestSuite~new() DO tests~TestCreateContact tests~TestGetContact tests~TestGetAllContacts tests~TestAddPhoneNumber tests~TestAddEmailAddress tests~TestAddRealAddress tests~TestFullDetailRetrieval tests~TestUpdateContact END tests~tearDown exit 0 ::requires 'app/appdb.cls' ::requires 'ooSQLite.cls' ::requires "rxunixsys" LIBRARY ::requires 'ncurses.cls' ::requires 'app/utils.rex' ::CLASS TestSuite ::METHOD init self~setUp() ::METHOD setUp expose db contactDict if SysFileExists(.dbPath) then do call SysFileDelete .dbPath say "Deleted existing test database." end say "Initializing test database at:" .dbPath 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~firstName = "John" contactDict~lastName = "Doe" ::METHOD TestCreateContact expose db contactDict /* Test contact creation */ say "" say "=== TESTING CONTACT CREATION ===" say "Creating contact:" contactDict~firstName contactDict~lastName 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 TestDeletion say "" say "=== TESTING DELETION ===" /* Test deleting an email */ say "Deleting personal email..." result = db~deleteEmailAddress(personalEmailId) say "Deletion successful:" result /* Get contact to verify deletion */ contact = db~getContact(contactId) say "Email addresses after deletion:" do email over contact['emails'] say " " email['type']":" email['email'] "(ID:" email['id']")" end ::METHOD OtherStuff /* Test deleting the entire contact */ say "Deleting entire contact..." result = db~deleteContact(contactId) say "Deletion successful:" result /* Try to retrieve the deleted contact */ contact = db~getContact(contactId) if contact = .nil then say "Contact successfully deleted - could not retrieve contact with ID" contactId else say "ERROR: Contact was not properly deleted!" /* Test searching functionality */ say "" say "=== TESTING SEARCH FUNCTIONALITY ===" /* Add multiple contacts for search testing */ say "Adding test contacts for search..." db~addContact("Jane", "Smith") db~addContact("John", "Johnson") db~addContact("Bob", "Smith") db~addContact("Sarah", "Williams") /* Search for contacts */ say "Searching for 'Smith'..." results = db~searchContacts("Smith") say "Found" results~items() "contacts:" do contact over results say " " contact['firstName'] contact['lastName'] "(ID:" contact['id']")" end say "Searching for 'John'..." results = db~searchContacts("John") say "Found" results~items() "contacts:" do contact over results say " " contact['firstName'] contact['lastName'] "(ID:" contact['id']")" end