notifier/test/repo_test.go
2024-04-22 18:59:07 +03:00

97 lines
2.1 KiB
Go

package test
import (
"context"
"fmt"
"mailnotifier/internal/initialize"
"mailnotifier/internal/models"
"mailnotifier/internal/repository"
"testing"
"time"
)
func TestInsertAndGetMany(t *testing.T) {
crx := context.Background()
mdb, err := initialize.MongoInit(crx, initialize.Config{
MongoHost: "127.0.0.1",
MongoPort: "27020",
MongoUser: "test",
MongoPassword: "test",
MongoDatabase: "admin",
MongoAuth: "admin",
})
if err != nil {
panic(err)
}
repo := repository.NewRepository(mdb.Collection("notify"))
ctx := context.Background()
testMessages := []models.Message{
{
AccountID: "1",
Email: "test",
ServiceKey: "1",
SendRegistration: false,
SendNoneCreated: false,
SendUnpublished: true,
SendPaid: false,
SendAt: time.Now().AddDate(0, 0, -12),
},
{
AccountID: "2",
Email: "test2",
ServiceKey: "2",
SendRegistration: false,
SendNoneCreated: true,
SendUnpublished: false,
SendPaid: true,
SendAt: time.Now().AddDate(0, 0, -7),
},
{
AccountID: "3",
Email: "test3",
ServiceKey: "3",
SendRegistration: false,
SendNoneCreated: false,
SendUnpublished: false,
SendPaid: false,
SendAt: time.Now().AddDate(0, 0, -14),
},
{
AccountID: "4",
Email: "test4",
ServiceKey: "4",
SendRegistration: true,
SendNoneCreated: true,
SendUnpublished: true,
SendPaid: true,
SendAt: time.Now(),
},
{
AccountID: "5",
Email: "test5",
ServiceKey: "5",
SendRegistration: true,
SendNoneCreated: true,
SendUnpublished: true,
SendPaid: true,
SendAt: time.Now().AddDate(0, 0, -15),
},
}
for _, msg := range testMessages {
if err := repo.Insert(ctx, msg); err != nil {
fmt.Println(err)
}
}
result, err := repo.GetMany(ctx)
if err != nil {
fmt.Println(err)
}
if len(result) != 3 {
t.Errorf("ожидал %d , получил %d", 3, len(result))
}
}