64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
package document_test
|
|
|
|
import (
|
|
"context"
|
|
"gotemplate/internal/database"
|
|
"gotemplate/internal/database/repository"
|
|
"gotemplate/internal/document"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestSync(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
db, err := pgxmock.NewConn()
|
|
if err != nil {
|
|
t.Fatalf("failed to open pgxmock database: %v", err)
|
|
}
|
|
defer db.Close(ctx)
|
|
|
|
queries := repository.New(db)
|
|
docSvc := document.New(ctx, queries)
|
|
|
|
doc := document.Document{
|
|
ID: uuid.New(),
|
|
JobID: uuid.New(),
|
|
Name: "document_name",
|
|
}
|
|
|
|
dbCollectorId, err := database.ToDBUUID(uuid.New())
|
|
assert.Nil(t, err)
|
|
dbJobID, err := database.ToDBUUID(doc.JobID)
|
|
assert.Nil(t, err)
|
|
minCleanVersion := int32(1)
|
|
minTextVersion := int32(1)
|
|
db.ExpectQuery("name: GetCollectorFromJobID :one").WithArgs(dbJobID).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion"}).
|
|
AddRow(dbCollectorId, dbJobID, minCleanVersion, minTextVersion),
|
|
)
|
|
assert.Nil(t, err)
|
|
|
|
dbDocID, err := database.ToDBUUID(doc.ID)
|
|
assert.Nil(t, err)
|
|
db.ExpectQuery("name: ListResultsByDocumentID :many").WithArgs(dbDocID, minCleanVersion, minTextVersion).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "queryId", "queryVersion"}).
|
|
AddRow(pgtype.UUID{}, pgtype.UUID{}, int32(1)),
|
|
)
|
|
db.ExpectQuery("name: GetCollectorQueries :many").WithArgs(dbCollectorId).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"collectorId", "queryId", "type", "requiredQueryId", "queryVersion"}).
|
|
AddRow(dbCollectorId, pgtype.UUID{}, repository.NullQuerytype{Querytype: repository.QuerytypeJsonExtractor, Valid: true}, pgtype.UUID{}, pgtype.Int4{Int32: int32(1), Valid: true}),
|
|
)
|
|
|
|
err = docSvc.Sync(ctx, &doc)
|
|
|
|
assert.Nil(t, err)
|
|
}
|