From 83f289eb40b34c07ad6b5df1099a7b6427e9be40 Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Sat, 30 Jul 2022 23:48:57 +1200 Subject: [PATCH] Add search tests --- storage/database_test.go | 75 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/storage/database_test.go b/storage/database_test.go index 9d473bc..c2152ff 100644 --- a/storage/database_test.go +++ b/storage/database_test.go @@ -1,12 +1,15 @@ package storage import ( + "bytes" "fmt" "io/ioutil" + "math/rand" "testing" "time" "github.com/axllent/mailpit/config" + "github.com/jhillyerd/enmime" ) var ( @@ -123,6 +126,78 @@ func TestRetrieveMimeEmail(t *testing.T) { assertEqual(t, len(attachmentData.Content), msg.Attachments[0].Size, "attachment size does not match") inlineData, err := GetAttachmentPart(DefaultMailbox, id, msg.Inline[0].PartID) assertEqual(t, len(inlineData.Content), msg.Inline[0].Size, "inline attachment size does not match") + + db.Close() +} + +func TestSearch(t *testing.T) { + setup() + + for i := 0; i < 1000; i++ { + msg := enmime.Builder(). + From(fmt.Sprintf("From %d", i), fmt.Sprintf("from-%d@example.com", i)). + Subject(fmt.Sprintf("Subject line %d end", i)). + Text([]byte(fmt.Sprintf("This is the email body %d .", i))). + To(fmt.Sprintf("To %d", i), fmt.Sprintf("to-%d@example.com", i)) + + env, err := msg.Build() + if err != nil { + t.Log("error ", err) + t.Fail() + } + + buf := new(bytes.Buffer) + + if err := env.Encode(buf); err != nil { + t.Log("error ", err) + t.Fail() + } + + if _, err := Store(DefaultMailbox, buf.Bytes()); err != nil { + t.Log("error ", err) + t.Fail() + } + } + + for i := 1; i < 101; i++ { + // search a random something that will return a single result + searchIndx := rand.Intn(4) + 1 + var search string + switch searchIndx { + case 1: + search = fmt.Sprintf("from-%d@example.com", i) + case 2: + search = fmt.Sprintf("to-%d@example.com", i) + case 3: + search = fmt.Sprintf("Subject line %d end", i) + default: + search = fmt.Sprintf("the email body %d jdsauk dwqmdqw", i) + } + + summaries, err := Search(DefaultMailbox, search, 0, 200) + if err != nil { + t.Log("error ", err) + t.Fail() + } + + assertEqual(t, len(summaries), 1, "1 search result expected") + + assertEqual(t, summaries[0].From.Name, fmt.Sprintf("From %d", i), "\"From\" name does not match") + assertEqual(t, summaries[0].From.Address, fmt.Sprintf("from-%d@example.com", i), "\"From\" address does not match") + assertEqual(t, summaries[0].To[0].Name, fmt.Sprintf("To %d", i), "\"To\" name does not match") + assertEqual(t, summaries[0].To[0].Address, fmt.Sprintf("to-%d@example.com", i), "\"To\" address does not match") + assertEqual(t, summaries[0].Subject, fmt.Sprintf("Subject line %d end", i), "\"Subject\" does not match") + } + + // search something that will return 200 rsults + summaries, err := Search(DefaultMailbox, "This is the email body", 0, 200) + if err != nil { + t.Log("error ", err) + t.Fail() + } + assertEqual(t, len(summaries), 200, "200 search results expected") + + db.Close() } func BenchmarkImportText(b *testing.B) {