1
0
mirror of https://github.com/axllent/mailpit.git synced 2025-03-11 14:59:57 +02:00

Add search tests

This commit is contained in:
Ralph Slooten 2022-07-30 23:48:57 +12:00
parent 7fd73a6fdb
commit 83f289eb40

View File

@ -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 <jdsauk;dwqmdqw;>.", 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) {