From 3ad62769a6ce634d34d1784e4101a8da70efea7f Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Sun, 24 Sep 2023 19:08:47 +1300 Subject: [PATCH] Tests: Add message tag tests --- storage/database.go | 3 ++- storage/database_test.go | 56 --------------------------------------- storage/tags_test.go | 43 ++++++++++++++++++++++++++++++ storage/test_shared.go | 57 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 57 deletions(-) create mode 100644 storage/tags_test.go create mode 100644 storage/test_shared.go diff --git a/storage/database.go b/storage/database.go index 1fc4ba1..1800339 100644 --- a/storage/database.go +++ b/storage/database.go @@ -203,7 +203,8 @@ func Close() { } } -// Store will save an email to the database tables +// Store will save an email to the database tables. +// Returns the database ID of the saved message. func Store(body []byte) (string, error) { // Parse message body with enmime env, err := enmime.ReadEnvelope(bytes.NewReader(body)) diff --git a/storage/database_test.go b/storage/database_test.go index df26744..8d284df 100644 --- a/storage/database_test.go +++ b/storage/database_test.go @@ -1,19 +1,8 @@ package storage import ( - "fmt" - "os" "testing" "time" - - "github.com/axllent/mailpit/config" - "github.com/axllent/mailpit/utils/logger" -) - -var ( - testTextEmail []byte - testMimeEmail []byte - testRuns = 100 ) func TestTextEmailInserts(t *testing.T) { @@ -60,8 +49,6 @@ func TestMimeEmailInserts(t *testing.T) { start := time.Now() - assertEqualStats(t, 0, 0) - for i := 0; i < testRuns; i++ { if _, err := Store(testMimeEmail); err != nil { t.Log("error ", err) @@ -84,8 +71,6 @@ func TestMimeEmailInserts(t *testing.T) { assertEqual(t, CountTotal(), 0, "incorrect number of mime emails deleted") t.Logf("Deleted %d mime emails in %s", testRuns, time.Since(delStart)) - - assertEqualStats(t, 0, 0) } func TestRetrieveMimeEmail(t *testing.T) { @@ -156,44 +141,3 @@ func BenchmarkImportMime(b *testing.B) { } } - -func setup() { - logger.NoLogging = true - config.MaxMessages = 0 - config.DataFile = "" - - if err := InitDB(); err != nil { - panic(err) - } - - var err error - - testTextEmail, err = os.ReadFile("testdata/plain-text.eml") - if err != nil { - panic(err) - } - - testMimeEmail, err = os.ReadFile("testdata/mime-attachment.eml") - if err != nil { - panic(err) - } -} - -func assertEqual(t *testing.T, a interface{}, b interface{}, message string) { - if a == b { - return - } - message = fmt.Sprintf("%s: \"%v\" != \"%v\"", message, a, b) - t.Fatal(message) -} - -func assertEqualStats(t *testing.T, total int, unread int) { - s := StatsGet() - if total != s.Total { - t.Fatalf("Incorrect total mailbox stats: \"%d\" != \"%d\"", total, s.Total) - } - - if unread != s.Unread { - t.Fatalf("Incorrect unread mailbox stats: \"%d\" != \"%d\"", unread, s.Unread) - } -} diff --git a/storage/tags_test.go b/storage/tags_test.go new file mode 100644 index 0000000..ded3df8 --- /dev/null +++ b/storage/tags_test.go @@ -0,0 +1,43 @@ +package storage + +import ( + "fmt" + "testing" +) + +func TestTags(t *testing.T) { + setup() + defer Close() + + t.Log("Testing setting & getting tags") + + ids := []string{} + + for i := 0; i < 10; i++ { + id, err := Store(testMimeEmail) + if err != nil { + t.Log("error ", err) + t.Fail() + } + ids = append(ids, id) + } + + for i := 0; i < 10; i++ { + if err := SetTags(ids[i], []string{fmt.Sprintf("Tag-%d", i)}); err != nil { + t.Log("error ", err) + t.Fail() + } + } + + for i := 0; i < 10; i++ { + message, err := GetMessage(ids[i]) + if err != nil { + t.Log("error ", err) + t.Fail() + } + + if len(message.Tags) != 1 || message.Tags[0] != fmt.Sprintf("Tag-%d", i) { + t.Fatal("Message tags do not match") + } + } +} diff --git a/storage/test_shared.go b/storage/test_shared.go new file mode 100644 index 0000000..d17cfbd --- /dev/null +++ b/storage/test_shared.go @@ -0,0 +1,57 @@ +package storage + +import ( + "fmt" + "os" + "testing" + + "github.com/axllent/mailpit/config" + "github.com/axllent/mailpit/utils/logger" +) + +var ( + testTextEmail []byte + testMimeEmail []byte + testRuns = 100 +) + +func setup() { + logger.NoLogging = true + config.MaxMessages = 0 + config.DataFile = "" + + if err := InitDB(); err != nil { + panic(err) + } + + var err error + + testTextEmail, err = os.ReadFile("testdata/plain-text.eml") + if err != nil { + panic(err) + } + + testMimeEmail, err = os.ReadFile("testdata/mime-attachment.eml") + if err != nil { + panic(err) + } +} + +func assertEqual(t *testing.T, a interface{}, b interface{}, message string) { + if a == b { + return + } + message = fmt.Sprintf("%s: \"%v\" != \"%v\"", message, a, b) + t.Fatal(message) +} + +func assertEqualStats(t *testing.T, total int, unread int) { + s := StatsGet() + if total != s.Total { + t.Fatalf("Incorrect total mailbox stats: \"%d\" != \"%d\"", total, s.Total) + } + + if unread != s.Unread { + t.Fatalf("Incorrect unread mailbox stats: \"%d\" != \"%d\"", unread, s.Unread) + } +}