package main import ( "encoding/json" "fmt" "net/url" "strings" "testing" ) // A hyphenated term must match its words in sequence, not just the first one. func TestHyphenatedTermMatchesInSequence(t *testing.T) { hit := Document{ID: "hit", FullText: "A cyber attack on the power grid."} miss := Document{ID: "miss", FullText: "Cyber policy, and an attack helicopter."} for _, d := range []*Document{&hit, &miss} { setBodySearchText(d) d.StemmedText, d.SearchText = d.StemmedFull, d.FoldedFull } for _, field := range []string{"FT", "TE"} { if !compileQuery([]string{"cyber-attack"}, []string{field}, []string{"and"}).matches(&hit) { t.Errorf("%s: cyber-attack should match text containing the words in sequence", field) } if compileQuery([]string{"cyber-attack"}, []string{field}, []string{"and"}).matches(&miss) { t.Errorf("%s: cyber-attack matched a document where the words are apart", field) } } // The words on their own still match both, as before. if !compileQuery([]string{"cyber"}, []string{"FT"}, []string{"and"}).matches(&miss) { t.Error("a plain word should still match") } } // A value page's publication-year bars count every year a document was // published in, because each bar links to a search that matches any of them. func TestPubYearsOf(t *testing.T) { var d Document if err := json.Unmarshal([]byte(`{"publication": [ {"publisher": "The Guardian", "date_published": "2013-06-06"}, {"publisher": "Le Monde", "date_published": "2014-10-21"}, {"publisher": "Der Spiegel", "date_published": "2013-12-30"}, {"publisher": "No date"}, {"publisher": "Bad date", "date_published": "201"} ]}`), &d); err != nil { t.Fatal(err) } if got := fmt.Sprint(pubYearsOf(&d)); got != "[2013 2014]" { t.Errorf("pubYearsOf = %s, want [2013 2014]", got) } if got := fmt.Sprint(pubYearsOf(&Document{})); got != "[]" { t.Errorf("pubYearsOf(no publications) = %s, want []", got) } } // A saved search's label must name the type filter, or two saved searches // differing only by Reported/SIDtoday are indistinguishable. func TestSearchLabelIncludesTypeFilter(t *testing.T) { entry := func(v url.Values) string { return url.QueryEscape(v.Encode()) } base := url.Values{"fqv": {"xkeyscore"}, "fqf": {"TE"}, "fqc": {"and"}} typed := url.Values{"fqv": {"xkeyscore"}, "fqf": {"TE"}, "fqc": {"and"}, "type": {"REPORTED"}} both := url.Values{"fqv": {"xkeyscore"}, "fqf": {"TE"}, "fqc": {"and"}, "type": {"SIDTODAY"}, "coll": {"news"}} got := corpus().parseSearchEntries([]string{entry(typed), entry(base), entry(both)}) if len(got) != 3 { t.Fatalf("parsed %d entries, want 3", len(got)) } if !strings.Contains(got[0].Label, "REPORTED") { t.Errorf("label %q should name the type filter", got[0].Label) } if got[0].Label == got[1].Label { t.Errorf("searches differing only by type share the label %q", got[0].Label) } // Collection and type share one bracket rather than two. if !strings.Contains(got[2].Label, "(news, SIDTODAY)") { t.Errorf("label %q should list collection and type together", got[2].Label) } }