-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
search: add Fuzz test for Similarity
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package search | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
) | ||
|
||
func FuzzSimilarity(f *testing.F) { | ||
// Setup the fuzz corpus | ||
corpusAdd(f, Entity[Value]{}) | ||
corpusAdd(f, Entity[Value]{ | ||
Person: &Person{}, | ||
}) | ||
corpusAdd(f, Entity[Value]{ | ||
Business: &Business{}, | ||
}) | ||
corpusAdd(f, Entity[Value]{ | ||
Organization: &Organization{}, | ||
}) | ||
corpusAdd(f, Entity[Value]{ | ||
Aircraft: &Aircraft{}, | ||
}) | ||
corpusAdd(f, Entity[Value]{ | ||
Vessel: &Vessel{}, | ||
}) | ||
corpusAdd(f, Entity[Value]{ | ||
SanctionsInfo: &SanctionsInfo{}, | ||
}) | ||
corpusAdd(f, Entity[Value]{ | ||
Addresses: []Address{{}}, | ||
CryptoAddresses: []CryptoAddress{{}}, | ||
Affiliations: []Affiliation{{}}, | ||
HistoricalInfo: []HistoricalInfo{{}}, | ||
}) | ||
|
||
// Run the fuzz loop | ||
f.Fuzz(func(t *testing.T, queryData []byte, indexData []byte) { | ||
var query Entity[Value] | ||
err := json.Unmarshal(queryData, &query) | ||
if err != nil { | ||
return | ||
} | ||
|
||
var index Entity[Value] | ||
err = json.Unmarshal(indexData, &index) | ||
if err != nil { | ||
return | ||
} | ||
|
||
Similarity(query, index) | ||
Similarity(index, query) | ||
}) | ||
} | ||
|
||
func corpusAdd(f *testing.F, entity Entity[Value]) { | ||
f.Helper() | ||
|
||
bs, err := json.Marshal(entity) | ||
if err != nil { | ||
f.Fatal(err) | ||
} | ||
f.Add(bs, bs) | ||
} |