1
0
mirror of https://github.com/ebosas/microservices.git synced 2025-06-06 22:16:11 +02:00
microservices/cmd/database/database_test.go

56 lines
1.3 KiB
Go
Raw Permalink Normal View History

2021-11-20 08:58:37 +02:00
package main
import (
"encoding/json"
"testing"
"time"
"github.com/ebosas/microservices/internal/models"
"github.com/DATA-DOG/go-sqlmock"
"github.com/streadway/amqp"
)
2021-11-20 11:17:35 +02:00
// TestInsertToDB tests a message insertion into a database
2021-11-20 08:58:37 +02:00
func TestInsertToDB(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock connection: %s", err)
}
defer db.Close()
now := time.Now().UnixMilli()
var tests = []struct {
message string
source string
time int64
}{
{"Hello", "back", now},
{"Another test!", "back", now},
{"1", "front", now},
{" ", "back", now - 60*60*1000},
}
for i, test := range tests {
mock.ExpectExec("insert into messages").WithArgs(test.message, test.time/1000).WillReturnResult(sqlmock.NewResult(int64(i), 1))
d := testArguments(t, test.message, test.source, test.time)
insertToDB(*d, db)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unfulfilled expectations: %s", err)
}
}
}
// testArguments produces arguments for the function being tested
func testArguments(t *testing.T, message, source string, time int64) *amqp.Delivery {
inputMsg := models.Message{Text: message, Source: source, Time: time}
messageJson, err := json.Marshal(inputMsg)
if err != nil {
t.Fatalf("marshal message: %s", err)
}
d := &amqp.Delivery{Body: messageJson}
return d
}