67 lines
2.3 KiB
Go
67 lines
2.3 KiB
Go
package db
|
|||
|
|
|
||
|
|
import "testing"
|
||
|
|
|
||
|
|
// TestContactCreateUpdateDelete exercises the full contact CRUD flow.
|
||
|
|
func TestContactCreateUpdateDelete(t *testing.T) {
|
||
|
|
d := openTestDB(t)
|
||
|
|
const mailboxID = int64(1)
|
||
|
|
|
||
|
|
id, err := d.CreateContact(mailboxID, "jane@example.com", "Jane Doe", "555-1234")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
got, err := d.GetContactByID(mailboxID, id)
|
||
|
|
if err != nil || got == nil || got.Name != "Jane Doe" || got.Phone != "555-1234" {
|
||
|
|
t.Fatalf("expected created contact, got %+v (err=%v)", got, err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := d.UpdateContact(mailboxID, id, "jane@example.com", "Jane D.", ""); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
updated, err := d.GetContactByID(mailboxID, id)
|
||
|
|
if err != nil || updated == nil || updated.Name != "Jane D." || updated.Phone != "" {
|
||
|
|
t.Fatalf("expected updated contact with phone cleared, got %+v (err=%v)", updated, err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := d.DeleteContact(mailboxID, id); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
gone, err := d.GetContactByID(mailboxID, id)
|
||
|
|
if err != nil || gone != nil {
|
||
|
|
t.Fatalf("expected contact deleted, got %+v (err=%v)", gone, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestContactEmailUniquePerMailbox confirms the UNIQUE(mailbox_id, email) constraint
|
||
|
|
// rejects a second contact with the same email in the same mailbox.
|
||
|
|
func TestContactEmailUniquePerMailbox(t *testing.T) {
|
||
|
|
d := openTestDB(t)
|
||
|
|
const mailboxID = int64(1)
|
||
|
|
|
||
|
|
if _, err := d.CreateContact(mailboxID, "dup@example.com", "First", ""); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if _, err := d.CreateContact(mailboxID, "dup@example.com", "Second", ""); err == nil {
|
||
|
|
t.Fatal("expected a UNIQUE constraint error for a duplicate email in the same mailbox")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestSuggestRecipientsIncludesContacts confirms a saved contact shows up in compose
|
||
|
|
// autocomplete, formatted "Name <email>" like the message-history-derived entries.
|
||
|
|
func TestSuggestRecipientsIncludesContacts(t *testing.T) {
|
||
|
|
d := openTestDB(t)
|
||
|
|
const mailboxID = int64(1)
|
||
|
|
|
||
|
|
if _, err := d.CreateContact(mailboxID, "alice@example.com", "Alice Smith", ""); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
suggestions, err := d.SuggestRecipients(mailboxID, "Alice")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if len(suggestions) != 1 || suggestions[0] != "Alice Smith <alice@example.com>" {
|
||
|
|
t.Fatalf("expected contact suggested as 'Alice Smith <alice@example.com>', got %+v", suggestions)
|
||
|
|
}
|
||
|
|
}
|