Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / internal/modules/matching/merge_test.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 720d47fae476b0d38477ae4401697d372b1ea2bc |
| Branch | feature/opportunity-to-outcome |
| Path or archive member | internal/modules/matching/merge_test.go |
| Original SHA-256 | 16bafd6f4cf5c2024bd371e8ccb4eb03daa42e967dbe006298bcf8d87370fc4e |
| Git blob SHA | 380c1569f38cfcd1e82da5afb449579ee8d4f8cd |
| Reading-copy SHA-256 | 16bafd6f4cf5c2024bd371e8ccb4eb03daa42e967dbe006298bcf8d87370fc4e |
Recovered source transcript
Download complete source text ↓140 lines · 6.3 KB
package matching
import (
"strings"
"testing"
)
// The table and column names are interpolated into the repoint statement. They come from the catalog and
// cannot realistically be anything else, which is exactly why the guard exists: a value nobody doubts is a
// value nobody examines.
func TestBareIdentifier(t *testing.T) {
for _, good := range []string{"candidates", "candidate_id", "recruiter_activities", "owner_user_id", "a1"} {
if !bareIdentifier(good) {
t.Errorf("%q is a real identifier and was rejected", good)
}
}
for _, bad := range []string{
"", "Candidates", "candidate id", "candidates;DROP TABLE users", `candidates"`,
"1candidates", "candidates--", "public.candidates", strings.Repeat("a", 64),
} {
if bareIdentifier(bad) {
t.Errorf("%q must not pass the identifier guard", bad)
}
}
}
// THE STRUCTURAL GUARANTEES. Each of these exists because the obvious implementation gets it wrong, and each
// would fail silently rather than loudly.
func TestMergeSafetyProperties(t *testing.T) {
src := readFile(t, "merge.go")
// 1. Tables come from the CATALOG, never a hand list. A list in Go is wrong the first time somebody adds
// a table and forgets, and the merge then reports success while leaving history on a tombstone.
if !strings.Contains(src, "mm_candidate_referencing_tables()") {
t.Error("the merge no longer reads the referencing tables from the catalog")
}
body := functionBody(src, "func (s *Service) MergeCandidates(")
if body == "" {
t.Fatal("MergeCandidates not found")
}
// A literal table list appearing here would mean somebody reintroduced the hand maintained version.
for _, hardcoded := range []string{`"applications"`, `"recruiter_activities"`, `"candidate_documents"`} {
if strings.Contains(body, hardcoded) {
t.Errorf("a table name %s is hard coded in the merge; it must come from the catalog", hardcoded)
}
}
// 2. A savepoint per table. Without it the first unique collision aborts the whole transaction, and a
// collision is the COMMON case when two rows are the same person.
if !strings.Contains(body, "sp, e := tx.Begin(ctx)") {
t.Error("the per table savepoint is gone; one collision will abort the entire merge")
}
if !strings.Contains(body, "isUniqueViolation(e)") {
t.Error("a unique violation is no longer handled as a collision")
}
// 3. The loser is a TOMBSTONE. Deleting it would destroy the subject of a scoring decision that has to
// stay reconstructable.
if strings.Contains(body, "DELETE FROM candidates") {
t.Error("the merge deletes a candidate row; it must tombstone with merged_into_id instead")
}
if !strings.Contains(body, "merged_into_id = $2") {
t.Error("the tombstone pointer is not set, so nothing can resolve to the kept record")
}
// 4. Fields are FILLED IN, never overwritten. Overwriting would let a merge quietly replace a verified
// clearance with an unverified one, which is the worst thing this operation could do.
if !strings.Contains(body, "coalesce(nullif(btrim(k.email),''), m.email)") {
t.Error("the field reconciliation no longer prefers the kept record's own values")
}
if strings.Contains(body, "clearance_level =") {
t.Error("the merge writes clearance_level; a merge must never change a clearance")
}
}
// Duplicate detection SUGGESTS and never acts, and the two rules must be labelled honestly: an email match is
// strong, a name and location match is two different people often enough that calling it a match is wrong.
func TestDuplicateDetectionIsHonest(t *testing.T) {
src := readFile(t, "merge.go")
body := functionBody(src, "func (s *Service) FindDuplicates(")
if body == "" {
t.Fatal("FindDuplicates not found")
}
if strings.Contains(body, "UPDATE") || strings.Contains(body, "DELETE") || strings.Contains(body, "INSERT") {
t.Error("duplicate detection writes something; it must only ever suggest")
}
if !strings.Contains(body, "'strong'") || !strings.Contains(body, "'weak'") {
t.Error("the two rules are no longer distinguished by strength")
}
// A tombstone must never be offered as a duplicate, or a merged record gets re suggested forever.
if strings.Count(body, "deleted_at IS NULL") < 4 {
t.Error("duplicate detection does not exclude deleted and merged records on both sides of both rules")
}
}
func TestUniqueViolationDetection(t *testing.T) {
for _, s := range []string{
"ERROR: duplicate key value violates unique constraint",
"SQLSTATE 23505",
`unique constraint "uq_thing"`,
} {
if !isUniqueViolation(errString(s)) {
t.Errorf("%q should be recognised as a unique violation", s)
}
}
if isUniqueViolation(errString("connection refused")) {
t.Error("an unrelated error was treated as a unique violation, which would silently drop rows")
}
}
type errString string
func (e errString) Error() string { return string(e) }
// A table the application role may not update is a DESIGN DECISION elsewhere in the schema, not an accident.
// aedt_candidate_notices has no UPDATE grant because a disclosure notice sent to a candidate is a fact about
// that candidate, and repointing it at somebody else would falsify a compliance record to make a tidy merge.
// The merge must record that it left them and carry on, not fail.
func TestPermissionDeniedIsRespectedNotFatal(t *testing.T) {
if !isPermissionDenied(errString("ERROR: permission denied for table aedt_candidate_notices (SQLSTATE 42501)")) {
t.Error("a permission denial must be recognised")
}
if isPermissionDenied(errString("duplicate key value violates unique constraint")) {
t.Error("a unique violation must not be mistaken for a permission denial, they are handled differently")
}
body := functionBody(readFile(t, "merge.go"), "func (s *Service) MergeCandidates(")
if !strings.Contains(body, "res.Retained = append(res.Retained, r.table)") {
t.Error("a permission denied table is no longer recorded as retained")
}
if !strings.Contains(body, `r.table == "candidate_merges"`) {
t.Error("the merge ledger is no longer excluded; a merge would rewrite the record of past merges")
}
// Retained and Dropped must stay DIFFERENT fields. Collapsing them would hide the difference between a
// duplicate that was removed and a compliance record that was deliberately preserved.
src := readFile(t, "merge.go")
if !strings.Contains(src, "Retained []string") || !strings.Contains(src, "Dropped map[string]int") {
t.Error("retained and dropped must remain distinct in the result")
}
}