mirror of
https://github.com/axllent/mailpit.git
synced 2025-04-23 12:18:56 +02:00
Tests: Add message tag tests
This commit is contained in:
parent
a63952aee6
commit
3ad62769a6
@ -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) {
|
func Store(body []byte) (string, error) {
|
||||||
// Parse message body with enmime
|
// Parse message body with enmime
|
||||||
env, err := enmime.ReadEnvelope(bytes.NewReader(body))
|
env, err := enmime.ReadEnvelope(bytes.NewReader(body))
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/axllent/mailpit/config"
|
|
||||||
"github.com/axllent/mailpit/utils/logger"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
testTextEmail []byte
|
|
||||||
testMimeEmail []byte
|
|
||||||
testRuns = 100
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTextEmailInserts(t *testing.T) {
|
func TestTextEmailInserts(t *testing.T) {
|
||||||
@ -60,8 +49,6 @@ func TestMimeEmailInserts(t *testing.T) {
|
|||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
assertEqualStats(t, 0, 0)
|
|
||||||
|
|
||||||
for i := 0; i < testRuns; i++ {
|
for i := 0; i < testRuns; i++ {
|
||||||
if _, err := Store(testMimeEmail); err != nil {
|
if _, err := Store(testMimeEmail); err != nil {
|
||||||
t.Log("error ", err)
|
t.Log("error ", err)
|
||||||
@ -84,8 +71,6 @@ func TestMimeEmailInserts(t *testing.T) {
|
|||||||
assertEqual(t, CountTotal(), 0, "incorrect number of mime emails deleted")
|
assertEqual(t, CountTotal(), 0, "incorrect number of mime emails deleted")
|
||||||
|
|
||||||
t.Logf("Deleted %d mime emails in %s", testRuns, time.Since(delStart))
|
t.Logf("Deleted %d mime emails in %s", testRuns, time.Since(delStart))
|
||||||
|
|
||||||
assertEqualStats(t, 0, 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRetrieveMimeEmail(t *testing.T) {
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
43
storage/tags_test.go
Normal file
43
storage/tags_test.go
Normal file
@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
57
storage/test_shared.go
Normal file
57
storage/test_shared.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user