50 lines
1.5 KiB
Go
50 lines
1.5 KiB
Go
package document_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"gotemplate/internal/database"
|
|
"gotemplate/internal/database/repository"
|
|
"gotemplate/internal/result"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestStore(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)
|
|
|
|
resultStore := result.ResultStore{
|
|
QueryID: uuid.New(),
|
|
DocumentID: uuid.New(),
|
|
Value: "val",
|
|
CleanVersion: int32(1),
|
|
TextVersion: int32(1),
|
|
QueryVersion: int32(1),
|
|
}
|
|
|
|
db.ExpectExec("name: SetResult :exec").WithArgs(pgxmock.AnyArg(), database.MustToDBUUID(resultStore.QueryID), database.MustToDBUUID(resultStore.DocumentID), resultStore.Value, resultStore.CleanVersion, resultStore.TextVersion, resultStore.QueryVersion).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
|
|
id, err := result.Store(ctx, queries, &resultStore)
|
|
assert.Nil(t, err)
|
|
assert.NotNil(t, id)
|
|
|
|
errr := "database failing"
|
|
db.ExpectExec("name: SetResult :exec").WithArgs(pgxmock.AnyArg(), database.MustToDBUUID(resultStore.QueryID), database.MustToDBUUID(resultStore.DocumentID), resultStore.Value, resultStore.CleanVersion, resultStore.TextVersion, resultStore.QueryVersion).
|
|
WillReturnError(fmt.Errorf(errr))
|
|
|
|
id, err = result.Store(ctx, queries, &resultStore)
|
|
assert.EqualError(t, err, errr)
|
|
assert.Equal(t, uuid.Nil, id)
|
|
}
|