cleaned up all the tests and refactored the db class

This commit is contained in:
Greg Gauthier 2025-05-05 17:09:30 +01:00
parent 6dbd58faf4
commit 858e5bb9e8
3 changed files with 91 additions and 109 deletions

View File

@ -77,7 +77,7 @@
::METHOD addContact ::METHOD addContact
expose db expose db
use arg contactDict /* Use a Rexx 'directory' to pass multiple values around */ use arg contactDict /* Use a Rexx 'directory' to pass multiple values around */
sql = "INSERT INTO contacts (first_name, last_name) VALUES ('"contactDict~firstName"', '"contactDict~lastName"')" sql = "INSERT INTO contacts (first_name, last_name) VALUES ('"contactDict~First_Name"', '"contactDict~Last_Name"')"
rc = db~exec(sql) rc = db~exec(sql)
if rc \= 0 then do if rc \= 0 then do
say "Error adding contact:" db~errMsg() say "Error adding contact:" db~errMsg()
@ -108,43 +108,6 @@
return return
END END
return contacts[1] /* Rexx is 1-indexed */ return contacts[1] /* Rexx is 1-indexed */
/***
returnedContent~firstName = contact["FIRST_NAME"]
returnedContent~lastName = contact["LAST_NAME"]
sql2 = "SELECT * FROM email_addresses WHERE contact_id = "contactId
emails = db~exec(sql2,.true,.ooSQLite~OO_ARRAY_OF_DIRECTORIES)
email_array = .Array~new()
do row over emails
email_array.append(row["EMAIL"])
END
returnedContent~emailAddresses = email_array
sql3 = "SELECT * FROM phone_numbers WHERE contact_id = "contactId
phones = db~exec(sql3,.true,.ooSQLite~OO_ARRAY_OF_DIRECTORIES)
phone_dir = .Directory~new()
phone_array = .Array~new()
do phone over phones
phone_dir~put(phone["NUMBER"], phone["TYPE"])
phone_array.append(phone_dir)
END
returnedContent~phoneNumbers = phone_array
sql4 = "SELECT * FROM addresses WHERE contact_id = "contactId
addresses = db~exec(sql3,.true,.ooSQLite~~OO_ARRAY_OF_DIRECTORIES)
address_dir = .Dictionary~new()
address_array = .Array~new()
do address over addresses
address_dir~type = address["TYPE"]
address_dir~street = address["STREET"]
address_dir~city = address["CITY"]
address_dir~state = address["STATE"]
address_dir~postal_code = address["POSTAL_CODE"]
address_dir~country = address["COUNTRY"]
address_array.append(address_dir)
END
returnedContent~addresses = address_array
return returnedContent
***/
::METHOD getAllContacts ::METHOD getAllContacts
expose db expose db
@ -180,18 +143,6 @@
return -1 return -1
end end
return rc return rc
/**
self~removeContactPhones(contactId)
self~removeContactEmails(contactId)
self~removeContactAddresses(contactId)
/* Then add new entries */
self~addPhoneNumber(contactId, contactDict["PHONE_TYPE"], contactDict["PHONE_NUMBER"])
self~addEmailAddress(contactId, contactDict["EMAIL_TYPE"], contactDict["EMAIL_ADDRESS"])
self~addAddress(contactId, contactDict~addressType, contactDict~street, contactDict~city, contactDict~state,
contactDict~postalCode, contactDict~country)
return 0
**/
::METHOD deleteContact ::METHOD deleteContact
expose db expose db
@ -253,17 +204,15 @@
::METHOD deletePhoneNumber ::METHOD deletePhoneNumber
expose db expose db
use arg phoneId use arg phoneId
sql = "DELETE FROM phone_numbers WHERE id = "phoneId
stmt = db~prepare("DELETE FROM phone_numbers WHERE id = ?") rc = db~exec(sql)
stmt~bind(1, phoneId) if rc \= 0 then DO
stmt~step Say "Unable to delete phone id "phoneId
return rc
return db~changes() > 0 END
return rc
/* Email address operations */ /* Email address operations */
::METHOD addEmailAddress ::METHOD addEmailAddress
expose db expose db
use arg contactId, emailType, emailAddress use arg contactId, emailType, emailAddress
@ -306,11 +255,13 @@
expose db expose db
use arg emailId use arg emailId
stmt = db~prepare("DELETE FROM email_addresses WHERE id = ?") sql = "DELETE FROM email_addresses WHERE id = "emailId
stmt~bind(1, emailId) rc = db~exec(sql)
stmt~step if rc \= 0 then do
say "Error deleting email address:" db~errMsg()
return db~changes() > 0 return -1
end
return rc
/* Physical address operations */ /* Physical address operations */
@ -408,4 +359,3 @@
return -1 return -1
end end
return 0 return 0

Binary file not shown.

View File

@ -25,6 +25,11 @@ DO
tests~TestAddRealAddress tests~TestAddRealAddress
tests~TestFullDetailRetrieval tests~TestFullDetailRetrieval
tests~TestUpdateContact tests~TestUpdateContact
tests~TestSearchForContact
tests~TestEmailDeletion
tests~TestPhoneNumberDeletion
tests~TestDeleteEntireContact
END END
tests~tearDown tests~tearDown
@ -81,8 +86,8 @@ exit 0
phoneArray~append(phoneDict2) phoneArray~append(phoneDict2)
contactDict = .Directory~new() contactDict = .Directory~new()
contactDict~firstName = "John" contactDict~First_Name = "John"
contactDict~lastName = "Doe" contactDict~Last_Name = "Doe"
::METHOD TestCreateContact ::METHOD TestCreateContact
@ -90,7 +95,7 @@ exit 0
/* Test contact creation */ /* Test contact creation */
say "" say ""
say "=== TESTING CONTACT CREATION ===" say "=== TESTING CONTACT CREATION ==="
say "Creating contact:" contactDict~firstName contactDict~lastName say "Creating contact:" contactDict~First_Name contactDict~Last_Name
contactId = db~addContact(contactDict) contactId = db~addContact(contactDict)
say "Contact created with ID:" contactId say "Contact created with ID:" contactId
@ -265,62 +270,89 @@ exit 0
say "ERROR: Failed to retrieve updated contact details!" say "ERROR: Failed to retrieve updated contact details!"
end end
::METHOD TestDeletion ::METHOD TestSearchForContact
say "" expose db
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 */ /* Test searching functionality */
say "" say ""
say "=== TESTING SEARCH FUNCTIONALITY ===" say "=== TESTING SEARCH FUNCTIONALITY ==="
/* Add multiple contacts for search testing */ first_names = .Array~of("Jane", "John", "Bob", "Sarah", "Tom", "Davey")
say "Adding test contacts for search..." last_names = .Array~of("Smith", "Johnson", "Smith", "Williams", "Jones", "Jones")
db~addContact("Jane", "Smith") Do i = 1 to 6
db~addContact("John", "Johnson") tempDict = .Directory~new()
db~addContact("Bob", "Smith") tempDict~First_Name = first_names[i]; tempDict~Last_Name = last_names[i];
db~addContact("Sarah", "Williams") contactId = db~addContact(tempDict)
END
/* Search for contacts */ /* Search for contacts */
say "Searching for 'Smith'..." say "Searching for 'Smith'..."
results = db~searchContacts("Smith") results = db~searchContacts("Smith")
say "Found" results~items() "contacts:" say "Found" results~items() "contacts:"
do contact over results do contact over results
say " " contact['firstName'] contact['lastName'] "(ID:" contact['id']")" say " " contact['FIRST_NAME'] contact['LAST_NAME'] "(ID:" contact['ID']")"
end end
say "Searching for 'John'..." say "Searching for 'John'..."
results = db~searchContacts("John") results = db~searchContacts("John")
say "Found" results~items() "contacts:" say "Found" results~items() "contacts:"
do contact over results do contact over results
say " " contact['firstName'] contact['lastName'] "(ID:" contact['id']")" say " " contact['FIRST_NAME'] contact['LAST_NAME'] "(ID:" contact['ID']")"
end 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!"