package result_test import ( "context" "errors" "queryorchestration/internal/database" "queryorchestration/internal/database/repository" "queryorchestration/internal/query/result" "testing" "github.com/google/uuid" "github.com/jackc/pgx/v5/pgtype" "github.com/pashagolub/pgxmock/v3" "github.com/stretchr/testify/assert" ) func TestStore(t *testing.T) { ctx := context.Background() pool, err := pgxmock.NewPool() if err != nil { t.Fatalf("failed to open pgxmock database: %v", err) } queries := repository.New(pool) resultStore := result.ResultStore{ QueryID: uuid.New(), DocumentID: uuid.New(), Value: "val", CleanVersion: int32(1), TextVersion: int32(1), QueryVersion: int32(1), } pool.ExpectQuery("name: SetResult :one").WithArgs(database.MustToDBUUID(resultStore.QueryID), database.MustToDBUUID(resultStore.DocumentID), resultStore.Value, resultStore.CleanVersion, resultStore.TextVersion, resultStore.QueryVersion). WillReturnRows( pgxmock.NewRows([]string{"id"}).AddRow(pgtype.UUID{}), ) id, err := result.Store(ctx, queries, &resultStore) assert.Nil(t, err) assert.NotNil(t, id) dbErr := "database failing" pool.ExpectQuery("name: SetResult :one").WithArgs(database.MustToDBUUID(resultStore.QueryID), database.MustToDBUUID(resultStore.DocumentID), resultStore.Value, resultStore.CleanVersion, resultStore.TextVersion, resultStore.QueryVersion). WillReturnError(errors.New(dbErr)) id, err = result.Store(ctx, queries, &resultStore) assert.EqualError(t, err, dbErr) assert.Equal(t, uuid.Nil, id) }