e23ada9a8e
Pass Test Query * passfullsutie
185 lines
5.5 KiB
Go
185 lines
5.5 KiB
Go
package query_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/query"
|
|
resultprocessor "queryorchestration/internal/query/result/processor"
|
|
"queryorchestration/internal/serviceconfig"
|
|
"queryorchestration/internal/test"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGet(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
svc := query.New(cfg)
|
|
|
|
config := "{\"path\":\"example_path\"}"
|
|
query := query.Query{
|
|
ID: uuid.New(),
|
|
Type: resultprocessor.TypeJsonExtractor,
|
|
ActiveVersion: int32(1),
|
|
LatestVersion: int32(1),
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
|
uuid.New(),
|
|
},
|
|
Config: &config,
|
|
}
|
|
|
|
dbReqIDs := *query.RequiredQueryIDs
|
|
|
|
pool.ExpectQuery("name: GetQuery :one").WithArgs(query.ID).WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
|
AddRow(query.ID, repository.QuerytypeJsonExtractor, query.ActiveVersion, query.LatestVersion, []byte(config), dbReqIDs),
|
|
)
|
|
|
|
returnQuery, err := svc.Get(ctx, query.ID)
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualExportedValues(t, query, *returnQuery)
|
|
}
|
|
|
|
func TestGetWithVersion(t *testing.T) {
|
|
t.Parallel()
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
net := test.GetNetwork(t)
|
|
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
|
|
|
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
|
|
Clientid: "client_id",
|
|
Name: "client_name",
|
|
})
|
|
require.NoError(t, err)
|
|
docId, err := cfg.GetDBQueries().CreateDocument(t.Context(), &repository.CreateDocumentParams{
|
|
Clientid: "client_id",
|
|
Hash: "hash",
|
|
})
|
|
require.NoError(t, err)
|
|
fill := "fill"
|
|
cleanId, err := cfg.GetDBQueries().AddDocumentClean(t.Context(), &repository.AddDocumentCleanParams{
|
|
Documentid: docId,
|
|
Bucket: &fill,
|
|
Key: &fill,
|
|
Hash: &fill,
|
|
Mimetype: repository.NullCleanmimetype{
|
|
Valid: true,
|
|
Cleanmimetype: repository.CleanmimetypeApplicationPdf,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
err = cfg.GetDBQueries().AddDocumentCleanEntry(t.Context(), &repository.AddDocumentCleanEntryParams{
|
|
Cleanid: cleanId,
|
|
Version: 1,
|
|
})
|
|
require.NoError(t, err)
|
|
textId, err := cfg.GetDBQueries().AddDocumentText(t.Context(), &repository.AddDocumentTextParams{
|
|
Cleanid: cleanId,
|
|
Bucket: fill,
|
|
Key: fill,
|
|
Hash: fill,
|
|
Createdat: pgtype.Timestamp{
|
|
Time: time.Now(),
|
|
Valid: true,
|
|
},
|
|
})
|
|
require.NoError(t, err)
|
|
err = cfg.GetDBQueries().AddDocumentTextEntry(t.Context(), &repository.AddDocumentTextEntryParams{
|
|
Textid: textId,
|
|
Version: 1,
|
|
})
|
|
require.NoError(t, err)
|
|
contextQueryId, err := cfg.GetDBQueries().CreateQuery(t.Context(), repository.QuerytypeContextFull)
|
|
require.NoError(t, err)
|
|
contextVersion, err := cfg.GetDBQueries().AddLatestQueryVersion(t.Context(), contextQueryId)
|
|
require.NoError(t, err)
|
|
err = cfg.GetDBQueries().AddActiveQueryVersion(t.Context(), &repository.AddActiveQueryVersionParams{
|
|
Queryid: contextQueryId,
|
|
Versionid: contextVersion,
|
|
})
|
|
require.NoError(t, err)
|
|
queryId, err := cfg.GetDBQueries().CreateQuery(t.Context(), repository.QuerytypeJsonExtractor)
|
|
require.NoError(t, err)
|
|
latestVersion, err := cfg.GetDBQueries().AddLatestQueryVersion(t.Context(), queryId)
|
|
require.NoError(t, err)
|
|
err = cfg.GetDBQueries().AddActiveQueryVersion(t.Context(), &repository.AddActiveQueryVersionParams{
|
|
Queryid: queryId,
|
|
Versionid: latestVersion,
|
|
})
|
|
require.NoError(t, err)
|
|
err = cfg.GetDBQueries().AddRequiredQuery(t.Context(), &repository.AddRequiredQueryParams{
|
|
Queryid: queryId,
|
|
Requiredqueryid: contextQueryId,
|
|
Addedversion: latestVersion,
|
|
})
|
|
require.NoError(t, err)
|
|
c := `{"path":"oldkey"}`
|
|
err = cfg.GetDBQueries().SetQueryConfig(t.Context(), &repository.SetQueryConfigParams{
|
|
Queryid: queryId,
|
|
Config: []byte(c),
|
|
Addedversion: latestVersion,
|
|
})
|
|
require.NoError(t, err)
|
|
strVal := `{"mykey": "example_value", "oldkey": "old_value"}`
|
|
_, err = cfg.GetDBQueries().AddResult(t.Context(), &repository.AddResultParams{
|
|
Queryid: contextQueryId,
|
|
Value: strVal,
|
|
Textentryid: textId,
|
|
Queryversion: contextVersion,
|
|
})
|
|
require.NoError(t, err)
|
|
latestVersion, err = cfg.GetDBQueries().AddLatestQueryVersion(t.Context(), queryId)
|
|
require.NoError(t, err)
|
|
c = `{"path":"mykey"}`
|
|
err = cfg.GetDBQueries().SetQueryConfig(t.Context(), &repository.SetQueryConfigParams{
|
|
Queryid: queryId,
|
|
Config: []byte(c),
|
|
Addedversion: latestVersion,
|
|
})
|
|
require.NoError(t, err)
|
|
ctx := context.Background()
|
|
|
|
svc := query.New(cfg)
|
|
|
|
returnQuery, err := svc.GetWithVersion(ctx, queryId, 1)
|
|
require.NoError(t, err)
|
|
config := `{"path": "oldkey"}`
|
|
assert.EqualExportedValues(t, query.Query{
|
|
ID: queryId,
|
|
Type: resultprocessor.TypeJsonExtractor,
|
|
ActiveVersion: 1,
|
|
LatestVersion: 2,
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
|
contextQueryId,
|
|
},
|
|
Config: &config,
|
|
}, *returnQuery)
|
|
|
|
returnQuery, err = svc.GetWithVersion(ctx, queryId, 2)
|
|
require.NoError(t, err)
|
|
config = `{"path": "mykey"}`
|
|
assert.EqualExportedValues(t, query.Query{
|
|
ID: queryId,
|
|
Type: resultprocessor.TypeJsonExtractor,
|
|
ActiveVersion: 1,
|
|
LatestVersion: 2,
|
|
RequiredQueryIDs: &[]uuid.UUID{
|
|
contextQueryId,
|
|
},
|
|
Config: &config,
|
|
}, *returnQuery)
|
|
}
|