Skip to content

Commit

Permalink
Tests: Add search delete tests
Browse files Browse the repository at this point in the history
  • Loading branch information
axllent committed Sep 24, 2023
1 parent de95910 commit a63952a
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 78 deletions.
83 changes: 5 additions & 78 deletions storage/database_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package storage

import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"os"
"testing"
"time"

"github.com/axllent/mailpit/config"
"github.com/axllent/mailpit/utils/logger"
"github.com/jhillyerd/enmime"
)

var (
Expand Down Expand Up @@ -110,11 +107,11 @@ func TestRetrieveMimeEmail(t *testing.T) {
}

assertEqual(t, msg.From.Name, "Sender Smith", "\"From\" name does not match")
assertEqual(t, msg.From.Address, "sender@example.com", "\"From\" address does not match")
assertEqual(t, msg.From.Address, "sender2@example.com", "\"From\" address does not match")
assertEqual(t, msg.Subject, "inline + attachment", "subject does not match")
assertEqual(t, len(msg.To), 1, "incorrect number of recipients")
assertEqual(t, msg.To[0].Name, "Recipient Ross", "\"To\" name does not match")
assertEqual(t, msg.To[0].Address, "recipient@example.com", "\"To\" address does not match")
assertEqual(t, msg.To[0].Address, "recipient2@example.com", "\"To\" address does not match")
assertEqual(t, len(msg.Attachments), 1, "incorrect number of attachments")
assertEqual(t, msg.Attachments[0].FileName, "Sample PDF.pdf", "attachment filename does not match")
assertEqual(t, len(msg.Inline), 1, "incorrect number of inline attachments")
Expand All @@ -135,76 +132,6 @@ func TestRetrieveMimeEmail(t *testing.T) {
assertEqual(t, len(inlineData.Content), msg.Inline[0].Size, "inline attachment size does not match")
}

func TestSearch(t *testing.T) {
setup()
defer Close()

t.Log("Testing search")
for i := 0; i < testRuns; i++ {
msg := enmime.Builder().
From(fmt.Sprintf("From %d", i), fmt.Sprintf("from-%[email protected]", 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-%[email protected]", 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(buf.Bytes()); err != nil {
t.Log("error ", err)
t.Fail()
}
}

for i := 1; i < 51; i++ {
// search a random something that will return a single result
searchIdx := rand.Intn(4) + 1
var search string
switch searchIdx {
case 1:
search = fmt.Sprintf("from-%[email protected]", i)
case 2:
search = fmt.Sprintf("to-%[email protected]", 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(search, 0, 100)
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-%[email protected]", 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-%[email protected]", 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 results
summaries, _, err := Search("This is the email body", 0, testRuns)
if err != nil {
t.Log("error ", err)
t.Fail()
}
assertEqual(t, len(summaries), testRuns, "search results expected")
}

func BenchmarkImportText(b *testing.B) {
setup()
defer Close()
Expand Down Expand Up @@ -241,12 +168,12 @@ func setup() {

var err error

testTextEmail, err = ioutil.ReadFile("testdata/plain-text.eml")
testTextEmail, err = os.ReadFile("testdata/plain-text.eml")
if err != nil {
panic(err)
}

testMimeEmail, err = ioutil.ReadFile("testdata/mime-attachment.eml")
testMimeEmail, err = os.ReadFile("testdata/mime-attachment.eml")
if err != nil {
panic(err)
}
Expand Down
152 changes: 152 additions & 0 deletions storage/search_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package storage

import (
"bytes"
"fmt"
"math/rand"
"testing"

"github.com/jhillyerd/enmime"
)

func TestSearch(t *testing.T) {
setup()
defer Close()

t.Log("Testing search")
for i := 0; i < testRuns; i++ {
msg := enmime.Builder().
From(fmt.Sprintf("From %d", i), fmt.Sprintf("from-%[email protected]", 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-%[email protected]", 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(buf.Bytes()); err != nil {
t.Log("error ", err)
t.Fail()
}
}

for i := 1; i < 51; i++ {
// search a random something that will return a single result
searchIdx := rand.Intn(4) + 1
var search string
switch searchIdx {
case 1:
search = fmt.Sprintf("from-%[email protected]", i)
case 2:
search = fmt.Sprintf("to-%[email protected]", 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(search, 0, 100)
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-%[email protected]", 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-%[email protected]", 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 results
summaries, _, err := Search("This is the email body", 0, testRuns)
if err != nil {
t.Log("error ", err)
t.Fail()
}
assertEqual(t, len(summaries), testRuns, "search results expected")
}

func TestSearchDelete100(t *testing.T) {
setup()
defer Close()

t.Log("Testing search delete of 100 messages")
for i := 0; i < 100; i++ {
if _, err := Store(testTextEmail); err != nil {
t.Log("error ", err)
t.Fail()
}
if _, err := Store(testMimeEmail); err != nil {
t.Log("error ", err)
t.Fail()
}
}

_, total, err := Search("from:[email protected]", 0, 100)
if err != nil {
t.Log("error ", err)
t.Fail()
}

assertEqual(t, total, 100, "100 search results expected")

if err := DeleteSearch("from:[email protected]"); err != nil {
t.Log("error ", err)
t.Fail()
}

_, total, err = Search("from:[email protected]", 0, 100)
if err != nil {
t.Log("error ", err)
t.Fail()
}

assertEqual(t, total, 0, "0 search results expected")
}

func TestSearchDelete1100(t *testing.T) {
setup()
defer Close()

t.Log("Testing search delete of 1100 messages")
for i := 0; i < 1100; i++ {
if _, err := Store(testTextEmail); err != nil {
t.Log("error ", err)
t.Fail()
}
}

_, total, err := Search("from:[email protected]", 0, 100)
if err != nil {
t.Log("error ", err)
t.Fail()
}

assertEqual(t, total, 1100, "100 search results expected")

if err := DeleteSearch("from:[email protected]"); err != nil {
t.Log("error ", err)
t.Fail()
}

_, total, err = Search("from:[email protected]", 0, 100)
if err != nil {
t.Log("error ", err)
t.Fail()
}

assertEqual(t, total, 0, "0 search results expected")
}

0 comments on commit a63952a

Please sign in to comment.