package store import ( "context" "testing" ) func TestUpdateEndpoint(t *testing.T) { s := testStore(t) ctx := context.Background() id, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "src", Host: "old.host", Port: 143, TLSMode: "plain"}) if err := s.UpdateEndpoint(ctx, Endpoint{ID: id, RoleLabel: "src2", Host: "new.host", Port: 993, TLSMode: "ssl"}); err != nil { t.Fatalf("update: %v", err) } got, _ := s.GetEndpoint(ctx, id) if got.Host != "new.host" || got.Port != 993 || got.TLSMode != "ssl" || got.RoleLabel != "src2" { t.Fatalf("update not applied: %+v", got) } } func TestDeleteAccountCascadesJournal(t *testing.T) { s := testStore(t) ctx := context.Background() ep1, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "s", Host: "a", Port: 993, TLSMode: "ssl"}) ep2, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "d", Host: "b", Port: 993, TLSMode: "ssl"}) taskID, _ := s.CreateTask(ctx, Task{Name: "t", SrcEndpointID: ep1, DstEndpointID: ep2}) accID, _ := s.CreateAccount(ctx, Account{TaskID: taskID, SrcLogin: "u", SrcPassEnc: "x", DstLogin: "v", DstPassEnc: "y"}) _ = s.MarkMigrated(ctx, accID, "INBOX", "") if err := s.DeleteAccount(ctx, accID); err != nil { t.Fatalf("delete account: %v", err) } accs, _ := s.ListAccountsByTask(ctx, taskID) if len(accs) != 0 { t.Fatalf("account not deleted: %d remain", len(accs)) } // journal row must be gone via ON DELETE CASCADE var n int _ = s.Pool.QueryRow(ctx, `SELECT count(*) FROM migrated_messages WHERE account_id=$1`, accID).Scan(&n) if n != 0 { t.Fatalf("migrated_messages not cascaded: %d rows", n) } } func TestDeleteTaskCascades(t *testing.T) { s := testStore(t) ctx := context.Background() ep1, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "s", Host: "a", Port: 993, TLSMode: "ssl"}) ep2, _ := s.CreateEndpoint(ctx, Endpoint{RoleLabel: "d", Host: "b", Port: 993, TLSMode: "ssl"}) taskID, _ := s.CreateTask(ctx, Task{Name: "t", SrcEndpointID: ep1, DstEndpointID: ep2}) accID, _ := s.CreateAccount(ctx, Account{TaskID: taskID, SrcLogin: "u", SrcPassEnc: "x", DstLogin: "v", DstPassEnc: "y"}) _, _ = s.CreateRun(ctx, taskID) if err := s.DeleteTask(ctx, taskID); err != nil { t.Fatalf("delete task: %v", err) } tasks, _ := s.ListTasks(ctx) if len(tasks) != 0 { t.Fatalf("task not deleted: %d remain", len(tasks)) } accs, _ := s.ListAccountsByTask(ctx, taskID) if len(accs) != 0 { t.Fatalf("accounts not cascaded: %d remain", len(accs)) } _ = accID }