moretesting
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package document_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gotemplate/internal/collector"
|
||||
"gotemplate/internal/database"
|
||||
"gotemplate/internal/database/repository"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestService(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)
|
||||
svc := collector.New(queries)
|
||||
assert.NotNil(t, svc)
|
||||
}
|
||||
|
||||
func TestByJobId(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)
|
||||
|
||||
jobID := uuid.New()
|
||||
fullCollector := collector.Collector{
|
||||
ID: uuid.New(),
|
||||
MinCleanVersion: int32(1),
|
||||
MinTextVersion: int32(1),
|
||||
}
|
||||
|
||||
db.ExpectQuery("name: GetCollectorFromJobID :one").WithArgs(database.MustToDBUUID(jobID)).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion"}).
|
||||
AddRow(database.MustToDBUUID(fullCollector.ID), database.MustToDBUUID(jobID), fullCollector.MinCleanVersion, fullCollector.MinTextVersion),
|
||||
)
|
||||
|
||||
coll, err := collector.NewByJobId(ctx, queries, jobID)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualExportedValues(t, fullCollector, *coll)
|
||||
|
||||
db.ExpectQuery("name: GetCollectorFromJobID :one").WithArgs(database.MustToDBUUID(jobID)).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion"}),
|
||||
)
|
||||
|
||||
_, err = collector.NewByJobId(ctx, queries, jobID)
|
||||
assert.EqualError(t, err, "no rows in result set")
|
||||
}
|
||||
@@ -8,11 +8,11 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestToDBUUID(t *testing.T) {
|
||||
func TestMustToDBUUID(t *testing.T) {
|
||||
id := uuid.New()
|
||||
|
||||
dbID, err := database.ToDBUUID(id)
|
||||
dbID := database.MustToDBUUID(id)
|
||||
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, true, dbID.Valid)
|
||||
assert.Equal(t, id.String(), uuid.UUID(dbID.Bytes).String())
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSync(t *testing.T) {
|
||||
func TestSyncIsSynced(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
db, err := pgxmock.NewConn()
|
||||
@@ -23,7 +23,6 @@ func TestSync(t *testing.T) {
|
||||
defer db.Close(ctx)
|
||||
|
||||
queries := repository.New(db)
|
||||
docSvc := document.New(queries)
|
||||
|
||||
doc := document.Document{
|
||||
ID: uuid.New(),
|
||||
@@ -35,14 +34,13 @@ func TestSync(t *testing.T) {
|
||||
dbJobID := database.MustToDBUUID(doc.JobID)
|
||||
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),
|
||||
)
|
||||
|
||||
dbDocID := database.MustToDBUUID(doc.ID)
|
||||
db.ExpectQuery("name: ListResultsByDocumentID :many").WithArgs(dbDocID, minCleanVersion, minTextVersion).
|
||||
db.ExpectQuery("name: ListResultsByDocumentID :many").WithArgs(database.MustToDBUUID(doc.ID), minCleanVersion, minTextVersion).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "queryVersion"}).
|
||||
AddRow(pgtype.UUID{}, pgtype.UUID{}, int32(1)),
|
||||
@@ -53,6 +51,123 @@ func TestSync(t *testing.T) {
|
||||
AddRow(dbCollectorId, pgtype.UUID{}, repository.NullQuerytype{Querytype: repository.QuerytypeJsonExtractor, Valid: true}, pgtype.UUID{}, pgtype.Int4{Int32: int32(1), Valid: true}),
|
||||
)
|
||||
|
||||
docSvc := document.New(queries)
|
||||
err = docSvc.Sync(ctx, &doc)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestSyncNoJob(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)
|
||||
|
||||
doc := document.Document{
|
||||
ID: uuid.New(),
|
||||
JobID: uuid.New(),
|
||||
Name: "document_name",
|
||||
}
|
||||
|
||||
dbJobID := database.MustToDBUUID(doc.JobID)
|
||||
db.ExpectQuery("name: GetCollectorFromJobID :one").WithArgs(dbJobID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "jobId", "minCleanVersion", "minTextVersion"}),
|
||||
)
|
||||
|
||||
docSvc := document.New(queries)
|
||||
err = docSvc.Sync(ctx, &doc)
|
||||
assert.EqualError(t, err, "no rows in result set")
|
||||
}
|
||||
|
||||
func TestSyncNoResultsNoCollector(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)
|
||||
|
||||
doc := document.Document{
|
||||
ID: uuid.New(),
|
||||
JobID: uuid.New(),
|
||||
Name: "document_name",
|
||||
}
|
||||
|
||||
dbCollectorId := database.MustToDBUUID(uuid.New())
|
||||
dbJobID := database.MustToDBUUID(doc.JobID)
|
||||
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),
|
||||
)
|
||||
db.ExpectQuery("name: ListResultsByDocumentID :many").WithArgs(database.MustToDBUUID(doc.ID), minCleanVersion, minTextVersion).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "queryVersion"}),
|
||||
)
|
||||
db.ExpectQuery("name: GetCollectorQueries :many").WithArgs(dbCollectorId).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"collectorId", "queryId", "type", "requiredQueryId", "queryVersion"}),
|
||||
)
|
||||
|
||||
docSvc := document.New(queries)
|
||||
err = docSvc.Sync(ctx, &doc)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
doc := document.Document{
|
||||
ID: uuid.New(),
|
||||
JobID: uuid.New(),
|
||||
Name: "document_name",
|
||||
}
|
||||
|
||||
dbCollectorId := database.MustToDBUUID(uuid.New())
|
||||
dbJobID := database.MustToDBUUID(doc.JobID)
|
||||
dbQueryID := database.MustToDBUUID(uuid.New())
|
||||
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),
|
||||
)
|
||||
db.ExpectQuery("name: ListResultsByDocumentID :many").WithArgs(database.MustToDBUUID(doc.ID), minCleanVersion, minTextVersion).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "queryVersion"}),
|
||||
)
|
||||
db.ExpectQuery("name: GetCollectorQueries :many").WithArgs(dbCollectorId).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"collectorId", "queryId", "type", "requiredQueryId", "queryVersion"}).
|
||||
AddRow(dbCollectorId, dbQueryID, repository.NullQuerytype{Querytype: repository.QuerytypeJsonExtractor, Valid: true}, pgtype.UUID{}, pgtype.Int4{Int32: int32(1), Valid: true}),
|
||||
)
|
||||
db.ExpectQuery("name: ListResultValuesByID :many").WithArgs([]pgtype.UUID{}).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "value"}),
|
||||
)
|
||||
|
||||
docSvc := document.New(queries)
|
||||
err = docSvc.Sync(ctx, &doc)
|
||||
assert.EqualError(t, err, "JSON Extraction requires 1 result")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package document_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gotemplate/internal/database/repository"
|
||||
"gotemplate/internal/query"
|
||||
"testing"
|
||||
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestService(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)
|
||||
svc := query.New(queries)
|
||||
assert.NotNil(t, svc)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package document_test
|
||||
|
||||
import (
|
||||
"gotemplate/internal/database/repository"
|
||||
"gotemplate/internal/result"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseResultValue(t *testing.T) {
|
||||
dbResult := repository.ListResultValuesByIDRow{
|
||||
ID: pgtype.UUID{},
|
||||
Queryid: pgtype.UUID{},
|
||||
Value: "",
|
||||
}
|
||||
|
||||
value := result.ParseResultValue(dbResult)
|
||||
assert.Equal(t, dbResult.Value, value.Value)
|
||||
assert.Equal(t, uuid.Nil, value.ID)
|
||||
assert.Equal(t, uuid.Nil, value.QueryID)
|
||||
}
|
||||
Reference in New Issue
Block a user