Merged in bugfix/querytest (pull request #147)
Pass Test Query * passfullsutie
This commit is contained in:
+120
-24
@@ -3,13 +3,16 @@ 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"
|
||||
@@ -51,38 +54,131 @@ func TestGet(t *testing.T) {
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
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(),
|
||||
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: int32(1),
|
||||
LatestVersion: int32(3),
|
||||
ActiveVersion: 1,
|
||||
LatestVersion: 2,
|
||||
RequiredQueryIDs: &[]uuid.UUID{
|
||||
uuid.New(),
|
||||
contextQueryId,
|
||||
},
|
||||
Config: &config,
|
||||
}
|
||||
}, *returnQuery)
|
||||
|
||||
version := int32(2)
|
||||
|
||||
dbReqIDs := *query.RequiredQueryIDs
|
||||
|
||||
pool.ExpectQuery("name: GetQueryWithVersion :one").WithArgs(&query.ID, &version).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.GetWithVersion(ctx, query.ID, version)
|
||||
returnQuery, err = svc.GetWithVersion(ctx, queryId, 2)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.EqualExportedValues(t, query, *returnQuery)
|
||||
config = `{"path": "mykey"}`
|
||||
assert.EqualExportedValues(t, query.Query{
|
||||
ID: queryId,
|
||||
Type: resultprocessor.TypeJsonExtractor,
|
||||
ActiveVersion: 1,
|
||||
LatestVersion: 2,
|
||||
RequiredQueryIDs: &[]uuid.UUID{
|
||||
contextQueryId,
|
||||
},
|
||||
Config: &config,
|
||||
}, *returnQuery)
|
||||
}
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
package result
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"queryorchestration/internal/database/repository"
|
||||
resultprocessor "queryorchestration/internal/query/result/processor"
|
||||
jsonextractor "queryorchestration/internal/query/types/jsonExtractor"
|
||||
"queryorchestration/internal/serviceconfig"
|
||||
"queryorchestration/internal/serviceconfig/objectstore"
|
||||
"queryorchestration/internal/test"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pashagolub/pgxmock/v3"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -29,33 +31,211 @@ func TestGetValueByType(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetValueWithVersion(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
t.Run("no entry", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
svc := New(cfg, &Services{})
|
||||
|
||||
svc := New(cfg, &Services{})
|
||||
params := &GetValueWithVersionParams{
|
||||
Type: resultprocessor.TypeJsonExtractor,
|
||||
QueryID: uuid.New(),
|
||||
DocumentID: uuid.New(),
|
||||
QueryVersion: 1,
|
||||
}
|
||||
|
||||
params := &GetValueWithVersionParams{
|
||||
Type: resultprocessor.TypeJsonExtractor,
|
||||
QueryID: uuid.New(),
|
||||
DocumentID: uuid.New(),
|
||||
QueryVersion: 1,
|
||||
}
|
||||
_, err := svc.GetValueWithVersion(t.Context(), params)
|
||||
require.EqualError(t, err, "no rows in result set")
|
||||
})
|
||||
t.Run("existing entry", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
value := "exaple_value"
|
||||
pool.ExpectQuery("name: GetResultValueWithVersion :one").WithArgs(¶ms.QueryID, ¶ms.QueryVersion, ¶ms.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "value"}).
|
||||
AddRow(&uuid.UUID{}, &value),
|
||||
)
|
||||
svc := New(cfg, &Services{})
|
||||
|
||||
val, err := svc.GetValueWithVersion(ctx, params)
|
||||
require.NoError(t, err)
|
||||
v := jsonextractor.NewResult(value)
|
||||
assert.Equal(t, v, val)
|
||||
assert.Equal(t, value, v.GetStoreValue())
|
||||
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)
|
||||
filetype := "pdf"
|
||||
part := uint16(1)
|
||||
key := objectstore.BucketKey{
|
||||
CreatedAt: time.Now().UTC(),
|
||||
ClientID: "clientid",
|
||||
EntityID: uuid.New(),
|
||||
Location: objectstore.Import,
|
||||
Part: &part,
|
||||
FileType: &filetype,
|
||||
}
|
||||
err = cfg.GetDBQueries().AddDocumentEntry(t.Context(), &repository.AddDocumentEntryParams{
|
||||
Documentid: docId,
|
||||
Bucket: "bucket",
|
||||
Key: key.String(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
bucket := "bucket"
|
||||
hash := "hash"
|
||||
keyStr := key.String()
|
||||
cleanId, err := cfg.GetDBQueries().AddDocumentClean(t.Context(), &repository.AddDocumentCleanParams{
|
||||
Documentid: docId,
|
||||
Bucket: &bucket,
|
||||
Hash: &hash,
|
||||
Key: &keyStr,
|
||||
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: bucket,
|
||||
Hash: hash,
|
||||
Key: keyStr,
|
||||
Part: part,
|
||||
Createdat: pgtype.Timestamp{
|
||||
Valid: true,
|
||||
Time: time.Now(),
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
err = cfg.GetDBQueries().AddDocumentTextEntry(t.Context(), &repository.AddDocumentTextEntryParams{
|
||||
Textid: textId,
|
||||
Version: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
queryId, err := cfg.GetDBQueries().CreateQuery(t.Context(), repository.QuerytypeJsonExtractor)
|
||||
require.NoError(t, err)
|
||||
queryVersion, err := cfg.GetDBQueries().AddLatestQueryVersion(t.Context(), queryId)
|
||||
require.NoError(t, err)
|
||||
value := "exaple_value"
|
||||
_, err = cfg.GetDBQueries().AddResult(t.Context(), &repository.AddResultParams{
|
||||
Queryid: queryId,
|
||||
Value: value,
|
||||
Textentryid: textId,
|
||||
Queryversion: queryVersion,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
params := &GetValueWithVersionParams{
|
||||
Type: resultprocessor.TypeJsonExtractor,
|
||||
QueryID: queryId,
|
||||
DocumentID: docId,
|
||||
QueryVersion: 1,
|
||||
}
|
||||
|
||||
val, err := svc.GetValueWithVersion(t.Context(), params)
|
||||
require.NoError(t, err)
|
||||
v := jsonextractor.NewResult(value)
|
||||
assert.Equal(t, v, val)
|
||||
assert.Equal(t, value, v.GetStoreValue())
|
||||
})
|
||||
t.Run("not existing version", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
svc := New(cfg, &Services{})
|
||||
|
||||
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)
|
||||
filetype := "pdf"
|
||||
part := uint16(1)
|
||||
key := objectstore.BucketKey{
|
||||
CreatedAt: time.Now().UTC(),
|
||||
ClientID: "clientid",
|
||||
EntityID: uuid.New(),
|
||||
Location: objectstore.Import,
|
||||
Part: &part,
|
||||
FileType: &filetype,
|
||||
}
|
||||
err = cfg.GetDBQueries().AddDocumentEntry(t.Context(), &repository.AddDocumentEntryParams{
|
||||
Documentid: docId,
|
||||
Bucket: "bucket",
|
||||
Key: key.String(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
bucket := "bucket"
|
||||
hash := "hash"
|
||||
keyStr := key.String()
|
||||
cleanId, err := cfg.GetDBQueries().AddDocumentClean(t.Context(), &repository.AddDocumentCleanParams{
|
||||
Documentid: docId,
|
||||
Bucket: &bucket,
|
||||
Hash: &hash,
|
||||
Key: &keyStr,
|
||||
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: bucket,
|
||||
Hash: hash,
|
||||
Key: keyStr,
|
||||
Part: part,
|
||||
Createdat: pgtype.Timestamp{
|
||||
Valid: true,
|
||||
Time: time.Now(),
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
err = cfg.GetDBQueries().AddDocumentTextEntry(t.Context(), &repository.AddDocumentTextEntryParams{
|
||||
Textid: textId,
|
||||
Version: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
queryId, err := cfg.GetDBQueries().CreateQuery(t.Context(), repository.QuerytypeJsonExtractor)
|
||||
require.NoError(t, err)
|
||||
queryVersion, err := cfg.GetDBQueries().AddLatestQueryVersion(t.Context(), queryId)
|
||||
require.NoError(t, err)
|
||||
value := "exaple_value"
|
||||
_, err = cfg.GetDBQueries().AddResult(t.Context(), &repository.AddResultParams{
|
||||
Queryid: queryId,
|
||||
Value: value,
|
||||
Textentryid: textId,
|
||||
Queryversion: queryVersion,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
params := &GetValueWithVersionParams{
|
||||
Type: resultprocessor.TypeJsonExtractor,
|
||||
QueryID: queryId,
|
||||
DocumentID: docId,
|
||||
QueryVersion: 2,
|
||||
}
|
||||
|
||||
_, err = svc.GetValueWithVersion(t.Context(), params)
|
||||
require.EqualError(t, err, "no value found")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ func (s *Service) Process(ctx context.Context, p *Process) (resultprocessor.Valu
|
||||
if processQuery.RequiredQueryIDs != nil {
|
||||
elength = len(*processQuery.RequiredQueryIDs)
|
||||
}
|
||||
slog.Debug("requirements", "length", len(values), "expected_length", elength)
|
||||
slog.Info("requirements", "length", len(values), "expected_length", elength)
|
||||
}
|
||||
|
||||
processor, err := s.getProcessor(query.Type)
|
||||
|
||||
@@ -3,64 +3,138 @@ package result
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"queryorchestration/internal/database/repository"
|
||||
"queryorchestration/internal/query"
|
||||
resultprocessor "queryorchestration/internal/query/result/processor"
|
||||
jsonextractor "queryorchestration/internal/query/types/jsonExtractor"
|
||||
"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 TestProcess(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := pgxmock.NewPool()
|
||||
require.NoError(t, err)
|
||||
t.Parallel()
|
||||
cfg := &serviceconfig.BaseConfig{}
|
||||
cfg.DBPool = pool
|
||||
cfg.DBQueries = repository.New(pool)
|
||||
net := test.GetNetwork(t)
|
||||
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
|
||||
|
||||
svc := Service{
|
||||
cfg: cfg,
|
||||
svc: &Services{
|
||||
Query: query.New(cfg),
|
||||
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)
|
||||
|
||||
qcfg := "{\"path\":\"examplekey\"}"
|
||||
query := &resultprocessor.Query{
|
||||
ID: uuid.New(),
|
||||
Version: 2,
|
||||
RequiredQueryIDs: &[]uuid.UUID{uuid.New()},
|
||||
Config: &qcfg,
|
||||
}
|
||||
params := Process{
|
||||
DocumentID: uuid.New(),
|
||||
QueryID: query.ID,
|
||||
QueryVersion: query.Version,
|
||||
}
|
||||
svc := New(cfg, &Services{
|
||||
Query: query.New(cfg),
|
||||
})
|
||||
|
||||
pool.ExpectQuery("name: GetQueryWithVersion :one").WithArgs(&query.ID, &query.Version).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
|
||||
AddRow(query.ID, repository.QuerytypeJsonExtractor, query.Version, query.Version, []byte(*query.Config), *query.RequiredQueryIDs),
|
||||
)
|
||||
strVal := `{"examplekey":"example_value"}`
|
||||
pool.ExpectQuery("name: ListQueryRequirementValues :many").WithArgs(&query.ID, &query.Version, ¶ms.DocumentID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"id", "queryId", "type", "value"}).
|
||||
AddRow(&uuid.UUID{}, query.ID, repository.QuerytypeContextFull, &strVal),
|
||||
)
|
||||
pool.ExpectQuery("name: GetActiveQueryConfig :one").WithArgs(query.ID).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"config"}).
|
||||
AddRow([]byte(qcfg)),
|
||||
)
|
||||
val, err := svc.Process(t.Context(), &Process{
|
||||
DocumentID: docId,
|
||||
QueryID: queryId,
|
||||
QueryVersion: 1,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, val)
|
||||
assert.Equal(t, "old_value", val.GetStoreValue())
|
||||
|
||||
val, err := svc.Process(ctx, ¶ms)
|
||||
val, err = svc.Process(t.Context(), &Process{
|
||||
DocumentID: docId,
|
||||
QueryID: queryId,
|
||||
QueryVersion: 2,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, val)
|
||||
assert.Equal(t, "example_value", val.GetStoreValue())
|
||||
|
||||
@@ -85,10 +85,6 @@ func TestSet(t *testing.T) {
|
||||
pgxmock.NewRows([]string{"id", "queryId", "type", "value"}).
|
||||
AddRow(&requiredResultId, (*query.RequiredQueryIDs)[0], repository.QuerytypeContextFull, &strVal),
|
||||
)
|
||||
pool.ExpectQuery("name: GetActiveQueryConfig :one").WithArgs(query.ID).WillReturnRows(
|
||||
pgxmock.NewRows([]string{"config"}).
|
||||
AddRow([]byte(qcfg)),
|
||||
)
|
||||
pool.ExpectBegin()
|
||||
resultId := uuid.New()
|
||||
pool.ExpectQuery("name: AddResult :one").WithArgs(query.ID, pgxmock.AnyArg(), textEntryId, query.Version).
|
||||
|
||||
@@ -29,10 +29,12 @@ func TestJSONProcess(t *testing.T) {
|
||||
|
||||
extractor := jsonextractor.NewExtractor(cfg)
|
||||
|
||||
config := "{\"path\":\"key\"}"
|
||||
query := &resultprocessor.Query{
|
||||
ID: uuid.New(),
|
||||
Type: resultprocessor.TypeJsonExtractor,
|
||||
Version: int32(1),
|
||||
Config: &config,
|
||||
}
|
||||
entryValue := "value"
|
||||
|
||||
@@ -41,14 +43,6 @@ func TestJSONProcess(t *testing.T) {
|
||||
contextfull.NewResult(jsonString),
|
||||
}
|
||||
|
||||
config := "{\"path\":\"key\"}"
|
||||
|
||||
pool.ExpectQuery("name: GetActiveQueryConfig :one").WithArgs(query.ID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"config"}).
|
||||
AddRow([]byte(config)),
|
||||
)
|
||||
|
||||
value, err := extractor.Process(ctx, query, values)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, entryValue, value)
|
||||
@@ -87,11 +81,7 @@ func TestJSONProcess(t *testing.T) {
|
||||
func TestJSONProcessJSON(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)
|
||||
|
||||
extractor := jsonextractor.NewExtractor(cfg)
|
||||
|
||||
@@ -107,62 +97,34 @@ func TestJSONProcessJSON(t *testing.T) {
|
||||
contextfull.NewResult(jsonString),
|
||||
}
|
||||
|
||||
config := "{\"path\":\"invalid_key\"}"
|
||||
|
||||
pool.ExpectQuery("name: GetActiveQueryConfig :one").WithArgs(query.ID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"config"}).
|
||||
AddRow([]byte(config)),
|
||||
)
|
||||
|
||||
value, err := extractor.Process(ctx, query, values)
|
||||
assert.EqualError(t, err, "config required")
|
||||
assert.Empty(t, value)
|
||||
|
||||
config := "{\"path\":\"invalid_key\"}"
|
||||
query.Config = &config
|
||||
|
||||
value, err = extractor.Process(ctx, query, values)
|
||||
assert.EqualError(t, err, "JSON path does not exist: invalid_key")
|
||||
assert.Empty(t, value)
|
||||
|
||||
config = ""
|
||||
|
||||
pool.ExpectQuery("name: GetActiveQueryConfig :one").WithArgs(query.ID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"config"}).
|
||||
AddRow([]byte(config)),
|
||||
)
|
||||
|
||||
value, err = extractor.Process(ctx, query, values)
|
||||
assert.EqualError(t, err, "unexpected end of JSON input")
|
||||
assert.Empty(t, value)
|
||||
|
||||
config = "{\"path\":\"\"}"
|
||||
|
||||
pool.ExpectQuery("name: GetActiveQueryConfig :one").WithArgs(query.ID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"config"}).
|
||||
AddRow([]byte(config)),
|
||||
)
|
||||
|
||||
value, err = extractor.Process(ctx, query, values)
|
||||
assert.EqualError(t, err, "JSON path does not exist: ")
|
||||
assert.Empty(t, value)
|
||||
|
||||
config = "{\"path\":}"
|
||||
|
||||
pool.ExpectQuery("name: GetActiveQueryConfig :one").WithArgs(query.ID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"config"}).
|
||||
AddRow([]byte(config)),
|
||||
)
|
||||
|
||||
value, err = extractor.Process(ctx, query, values)
|
||||
assert.EqualError(t, err, "invalid character '}' looking for beginning of value")
|
||||
assert.Empty(t, value)
|
||||
|
||||
pool.ExpectQuery("name: GetActiveQueryConfig :one").WithArgs(query.ID).
|
||||
WillReturnRows(
|
||||
pgxmock.NewRows([]string{"config"}),
|
||||
)
|
||||
|
||||
value, err = extractor.Process(ctx, query, values)
|
||||
assert.EqualError(t, err, "no rows in result set")
|
||||
assert.Empty(t, value)
|
||||
}
|
||||
|
||||
func TestJSONProcessResults(t *testing.T) {
|
||||
|
||||
@@ -26,6 +26,8 @@ func NewExtractor(cfg serviceconfig.ConfigProvider) *Extractor {
|
||||
func (e *Extractor) Process(ctx context.Context, query *resultprocessor.Query, values []resultprocessor.Value) (string, error) {
|
||||
if len(values) != 1 {
|
||||
return "", fmt.Errorf("JSON Extraction requires 1 result")
|
||||
} else if query.Config == nil {
|
||||
return "", fmt.Errorf("config required")
|
||||
}
|
||||
|
||||
value, err := values[0].GetValue(ctx)
|
||||
@@ -33,13 +35,8 @@ func (e *Extractor) Process(ctx context.Context, query *resultprocessor.Query, v
|
||||
return "", err
|
||||
}
|
||||
|
||||
byteConfig, err := e.cfg.GetDBQueries().GetActiveQueryConfig(ctx, query.ID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var config Config
|
||||
err = json.Unmarshal(byteConfig, &config)
|
||||
err = json.Unmarshal([]byte(*query.Config), &config)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user