fix: dedupe compares bytes to characters, so every Unicode body appended a duplicate (#51)
fix: dedupe compares bytes to characters, so every Unicode body appended a duplicate (#51) Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
This commit was merged in pull request #51.
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package db_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/david/knox/internal/db"
|
||||
)
|
||||
|
||||
// TestRecordObservationDeduplicatesMultibyteContent pins the byte-vs-character
|
||||
// bug: SQLite's LENGTH() counts characters on TEXT, while Go's len() counts
|
||||
// bytes. Comparing the two made every body containing an em dash, a curly quote
|
||||
// or any non-ASCII text look changed, so each sweep appended a duplicate
|
||||
// observation with an identical content hash.
|
||||
func TestRecordObservationDeduplicatesMultibyteContent(t *testing.T) {
|
||||
kdb, raw := openBoth(t)
|
||||
|
||||
rec := db.ObservationRecord{
|
||||
Fingerprint: "fp-multibyte",
|
||||
SourceID: "test",
|
||||
Title: "Typographic punctuation",
|
||||
Summary: "em dash and curly quotes",
|
||||
Content: "— “quoted” and an em dash — plus ünïcödé",
|
||||
ContentHash: "hash-1",
|
||||
IngesterVersion: "test/v1",
|
||||
Trigger: "test",
|
||||
}
|
||||
|
||||
if _, isNew, err := kdb.RecordObservation(rec); err != nil || !isNew {
|
||||
t.Fatalf("first record = (isNew=%v, err=%v), want a new observation", isNew, err)
|
||||
}
|
||||
if _, isNew, err := kdb.RecordObservation(rec); err != nil || isNew {
|
||||
t.Fatalf("second record = (isNew=%v, err=%v), want a deduped no-op", isNew, err)
|
||||
}
|
||||
|
||||
var rows int
|
||||
if err := raw.QueryRow(
|
||||
`SELECT COUNT(*) FROM observations WHERE fingerprint='fp-multibyte'`,
|
||||
).Scan(&rows); err != nil {
|
||||
t.Fatalf("count: %v", err)
|
||||
}
|
||||
if rows != 1 {
|
||||
t.Fatalf("observations = %d, want 1 (identical multibyte content must not append a version)", rows)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRecordObservationAppendsOnRealChange is the other half of the contract:
|
||||
// fixing the length comparison must not turn dedupe into "ignore everything".
|
||||
func TestRecordObservationAppendsOnRealChange(t *testing.T) {
|
||||
kdb, raw := openBoth(t)
|
||||
|
||||
base := db.ObservationRecord{
|
||||
Fingerprint: "fp-change",
|
||||
SourceID: "test",
|
||||
Content: "first version — with a dash",
|
||||
ContentHash: "hash-a",
|
||||
IngesterVersion: "test/v1",
|
||||
Trigger: "test",
|
||||
}
|
||||
if _, _, err := kdb.RecordObservation(base); err != nil {
|
||||
t.Fatalf("first: %v", err)
|
||||
}
|
||||
|
||||
base.Content = "second version — with a longer dash"
|
||||
base.ContentHash = "hash-b"
|
||||
if _, isNew, err := kdb.RecordObservation(base); err != nil || isNew {
|
||||
t.Fatalf("changed record = (isNew=%v, err=%v), want a new version, not a new fingerprint", isNew, err)
|
||||
}
|
||||
|
||||
var versions int
|
||||
if err := raw.QueryRow(
|
||||
`SELECT COUNT(*) FROM observations WHERE fingerprint='fp-change'`,
|
||||
).Scan(&versions); err != nil {
|
||||
t.Fatalf("count: %v", err)
|
||||
}
|
||||
if versions != 2 {
|
||||
t.Fatalf("versions = %d, want 2 (a changed body must still append)", versions)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user