diff --git a/internal/query/list.go b/internal/query/list.go index c07339cd..b071679f 100644 --- a/internal/query/list.go +++ b/internal/query/list.go @@ -14,6 +14,8 @@ type ListFilters struct { } func (s *Service) List(ctx context.Context, filters ListFilters) (*[]Query, error) { + // TODO - use filters + dbQueries, err := s.db.Queries.ListQueries(ctx) if err != nil { return nil, err diff --git a/internal/query/update.go b/internal/query/update.go index 516150c9..e8c88634 100644 --- a/internal/query/update.go +++ b/internal/query/update.go @@ -24,10 +24,15 @@ func (s *Service) Update(ctx context.Context, entity *queryprocessor.Update) err return err } + err = s.submitUpdate(ctx, entity) + if err != nil { + return err + } + return nil } -func (s *Service) submitUpdate(ctx *context.Context, entity *queryprocessor.Update) error { +func (s *Service) submitUpdate(ctx context.Context, entity *queryprocessor.Update) error { // TODO - generate new entity // TODO - submit update - id, type, activeversion, requiredQueryId, Config return nil diff --git a/test/unit/internal/contextfull/creator_test.go b/test/unit/internal/contextfull/creator_test.go new file mode 100644 index 00000000..e09cf28f --- /dev/null +++ b/test/unit/internal/contextfull/creator_test.go @@ -0,0 +1,40 @@ +package document_test + +import ( + "context" + contextfull "queryorchestration/internal/contextFull" + "queryorchestration/internal/database" + "queryorchestration/internal/database/repository" + queryprocessor "queryorchestration/internal/queryProcessor" + "testing" + + "github.com/google/uuid" + "github.com/pashagolub/pgxmock/v3" + "github.com/stretchr/testify/assert" +) + +func TestCreatorValidate(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) + db := &database.Connection{ + Queries: queries, + Pool: pool, + } + + svc := contextfull.NewCreator(db) + assert.NotNil(t, svc) + + entity := &queryprocessor.Create{ + Type: queryprocessor.TypeContextFull, + RequiredQueryIDs: []uuid.UUID{}, + Config: "", + } + + err = svc.Validate(ctx, entity) + assert.Nil(t, err) +} diff --git a/test/unit/internal/contextfull/updator_test.go b/test/unit/internal/contextfull/updator_test.go new file mode 100644 index 00000000..aab5e241 --- /dev/null +++ b/test/unit/internal/contextfull/updator_test.go @@ -0,0 +1,48 @@ +package document_test + +import ( + "context" + contextfull "queryorchestration/internal/contextFull" + "queryorchestration/internal/database" + "queryorchestration/internal/database/repository" + queryprocessor "queryorchestration/internal/queryProcessor" + "testing" + + "github.com/google/uuid" + "github.com/pashagolub/pgxmock/v3" + "github.com/stretchr/testify/assert" +) + +func TestUpdatorValidate(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) + db := &database.Connection{ + Queries: queries, + Pool: pool, + } + + svc := contextfull.NewUpdator(db) + assert.NotNil(t, svc) + + current := &queryprocessor.Query{ + ID: uuid.New(), + Type: queryprocessor.TypeContextFull, + Version: int32(1), + RequiredQueryIDs: []uuid.UUID{}, + Config: "", + } + + entity := &queryprocessor.Update{ + ID: current.ID, + RequiredQueryIDs: []uuid.UUID{}, + Config: "", + } + + err = svc.Validate(ctx, current, entity) + assert.Nil(t, err) +} diff --git a/test/unit/internal/jsonextractor/creator_test.go b/test/unit/internal/jsonextractor/creator_test.go new file mode 100644 index 00000000..59fedf5a --- /dev/null +++ b/test/unit/internal/jsonextractor/creator_test.go @@ -0,0 +1,40 @@ +package document_test + +import ( + "context" + "queryorchestration/internal/database" + "queryorchestration/internal/database/repository" + jsonextractor "queryorchestration/internal/jsonExtractor" + queryprocessor "queryorchestration/internal/queryProcessor" + "testing" + + "github.com/google/uuid" + "github.com/pashagolub/pgxmock/v3" + "github.com/stretchr/testify/assert" +) + +func TestCreatorValidate(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) + db := &database.Connection{ + Queries: queries, + Pool: pool, + } + + svc := jsonextractor.NewCreator(db) + assert.NotNil(t, svc) + + entity := &queryprocessor.Create{ + Type: queryprocessor.TypeJsonExtractor, + RequiredQueryIDs: []uuid.UUID{}, + Config: "", + } + + err = svc.Validate(ctx, entity) + assert.Nil(t, err) +} diff --git a/test/unit/internal/jsonextractor/updator_test.go b/test/unit/internal/jsonextractor/updator_test.go new file mode 100644 index 00000000..ec907c09 --- /dev/null +++ b/test/unit/internal/jsonextractor/updator_test.go @@ -0,0 +1,48 @@ +package document_test + +import ( + "context" + "queryorchestration/internal/database" + "queryorchestration/internal/database/repository" + jsonextractor "queryorchestration/internal/jsonExtractor" + queryprocessor "queryorchestration/internal/queryProcessor" + "testing" + + "github.com/google/uuid" + "github.com/pashagolub/pgxmock/v3" + "github.com/stretchr/testify/assert" +) + +func TestUpdatorValidate(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) + db := &database.Connection{ + Queries: queries, + Pool: pool, + } + + svc := jsonextractor.NewUpdator(db) + assert.NotNil(t, svc) + + current := &queryprocessor.Query{ + ID: uuid.New(), + Type: queryprocessor.TypeJsonExtractor, + Version: int32(1), + RequiredQueryIDs: []uuid.UUID{}, + Config: "", + } + + entity := &queryprocessor.Update{ + ID: current.ID, + RequiredQueryIDs: []uuid.UUID{}, + Config: "", + } + + err = svc.Validate(ctx, current, entity) + assert.Nil(t, err) +} diff --git a/test/unit/internal/query/create_test.go b/test/unit/internal/query/create_test.go new file mode 100644 index 00000000..3db8d948 --- /dev/null +++ b/test/unit/internal/query/create_test.go @@ -0,0 +1,71 @@ +package document_test + +import ( + "context" + "queryorchestration/internal/database" + "queryorchestration/internal/database/repository" + "queryorchestration/internal/query" + queryprocessor "queryorchestration/internal/queryProcessor" + "testing" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgtype" + "github.com/pashagolub/pgxmock/v3" + "github.com/stretchr/testify/assert" +) + +func TestCreate(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) + db := &database.Connection{ + Queries: queries, + Pool: pool, + } + svc := query.New(db) + + config := "{\"path\":\"example_path\"}" + q := query.Query{ + ID: uuid.New(), + Type: queryprocessor.TypeJsonExtractor, + ActiveVersion: int32(1), + LatestVersion: int32(1), + RequiredQueryIDs: []uuid.UUID{ + uuid.New(), + }, + Config: config, + } + create := &queryprocessor.Create{ + Type: q.Type, + RequiredQueryIDs: q.RequiredQueryIDs, + Config: q.Config, + } + + dbReqIDs := make([]pgtype.UUID, len(q.RequiredQueryIDs)) + for index, id := range q.RequiredQueryIDs { + dbReqIDs[index] = database.MustToDBUUID(id) + } + dbType, err := queryprocessor.ToDBQueryType(create.Type) + assert.Nil(t, err) + + pool.ExpectBeginTx(pgx.TxOptions{}) + pool.ExpectQuery("name: CreateQuery :one").WithArgs(dbType).WillReturnRows( + pgxmock.NewRows([]string{"id"}). + AddRow(database.MustToDBUUID(q.ID)), + ) + for _, req := range create.RequiredQueryIDs { + pool.ExpectExec("name: CreateRequiredQuery :exec").WithArgs(database.MustToDBUUID(q.ID), database.MustToDBUUID(req), int32(1)). + WillReturnResult(pgxmock.NewResult("", 1)) + } + pool.ExpectExec("name: CreateQueryConfig :exec").WithArgs(database.MustToDBUUID(q.ID), []byte(create.Config), int32(1)). + WillReturnResult(pgxmock.NewResult("", 1)) + + id, err := svc.Create(ctx, create) + assert.Nil(t, err) + assert.Equal(t, q.ID, id) +} diff --git a/test/unit/internal/query/deprecate_test.go b/test/unit/internal/query/deprecate_test.go new file mode 100644 index 00000000..f17cd82b --- /dev/null +++ b/test/unit/internal/query/deprecate_test.go @@ -0,0 +1,66 @@ +package document_test + +import ( + "context" + "errors" + "queryorchestration/internal/database" + "queryorchestration/internal/database/repository" + "queryorchestration/internal/query" + "testing" + + "github.com/google/uuid" + "github.com/pashagolub/pgxmock/v3" + "github.com/stretchr/testify/assert" +) + +func TestDeprecate(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) + db := &database.Connection{ + Queries: queries, + Pool: pool, + } + svc := query.New(db) + + id := uuid.New() + + pool.ExpectQuery("name: IsQueryDeprecated :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows( + pgxmock.NewRows([]string{"exists"}). + AddRow(false), + ) + pool.ExpectExec("name: DeprecateQuery :exec").WithArgs(database.MustToDBUUID(id)). + WillReturnResult(pgxmock.NewResult("", 1)) + + err = svc.Deprecate(ctx, id) + assert.Nil(t, err) + + pool.ExpectQuery("name: IsQueryDeprecated :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows( + pgxmock.NewRows([]string{"exists"}). + AddRow(true), + ) + + err = svc.Deprecate(ctx, id) + assert.Nil(t, err) + + pool.ExpectQuery("name: IsQueryDeprecated :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows( + pgxmock.NewRows([]string{"exists"}), + ) + + err = svc.Deprecate(ctx, id) + assert.NotNil(t, err) + + pool.ExpectQuery("name: IsQueryDeprecated :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows( + pgxmock.NewRows([]string{"exists"}). + AddRow(false), + ) + pool.ExpectExec("name: DeprecateQuery :exec").WithArgs(database.MustToDBUUID(id)). + WillReturnError(errors.New("unable to deprecate query")) + + err = svc.Deprecate(ctx, id) + assert.NotNil(t, err) +} diff --git a/test/unit/internal/query/list_test.go b/test/unit/internal/query/list_test.go new file mode 100644 index 00000000..c1baddd3 --- /dev/null +++ b/test/unit/internal/query/list_test.go @@ -0,0 +1,59 @@ +package document_test + +import ( + "context" + "queryorchestration/internal/database" + "queryorchestration/internal/database/repository" + "queryorchestration/internal/query" + queryprocessor "queryorchestration/internal/queryProcessor" + "testing" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgtype" + "github.com/pashagolub/pgxmock/v3" + "github.com/stretchr/testify/assert" +) + +func TestList(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) + db := &database.Connection{ + Queries: queries, + Pool: pool, + } + svc := query.New(db) + + config := "{\"path\":\"example_path\"}" + q := query.Query{ + ID: uuid.New(), + Type: queryprocessor.TypeJsonExtractor, + ActiveVersion: int32(1), + LatestVersion: int32(1), + RequiredQueryIDs: []uuid.UUID{ + uuid.New(), + }, + Config: config, + } + + dbReqIDs := make([]pgtype.UUID, len(q.RequiredQueryIDs)) + for index, id := range q.RequiredQueryIDs { + dbReqIDs[index] = database.MustToDBUUID(id) + } + + filters := query.ListFilters{} + + pool.ExpectQuery("name: ListQueries :many").WithArgs().WillReturnRows( + pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}). + AddRow(database.MustToDBUUID(q.ID), repository.QuerytypeJsonExtractor, q.ActiveVersion, q.LatestVersion, []byte(config), dbReqIDs), + ) + + resList, err := svc.List(ctx, filters) + assert.Nil(t, err) + + assert.EqualExportedValues(t, []query.Query{q}, *resList) +} diff --git a/test/unit/internal/query/parse_test.go b/test/unit/internal/query/parse_test.go index 67869f91..7c8134ca 100644 --- a/test/unit/internal/query/parse_test.go +++ b/test/unit/internal/query/parse_test.go @@ -1,7 +1,9 @@ package document_test import ( + "queryorchestration/internal/database" "queryorchestration/internal/database/repository" + "queryorchestration/internal/query" queryprocessor "queryorchestration/internal/queryProcessor" "testing" @@ -10,65 +12,76 @@ import ( "github.com/stretchr/testify/assert" ) -func TestParseDBCollectorQuery(t *testing.T) { - dbResult := repository.GetCollectorQueriesRow{ - Collectorid: pgtype.UUID{}, - Queryid: pgtype.UUID{}, - Requiredids: []pgtype.UUID{}, - Type: repository.NullQuerytype{Valid: true, Querytype: repository.QuerytypeJsonExtractor}, - Queryversion: pgtype.Int4{}, +func TestParseQuery(t *testing.T) { + q := &query.Query{ + ID: uuid.New(), + Type: queryprocessor.TypeJsonExtractor, + ActiveVersion: int32(1), + LatestVersion: int32(2), + RequiredQueryIDs: []uuid.UUID{ + uuid.New(), + }, + Config: "example", } - value, err := queryprocessor.ParseDBCollectorQuery(&dbResult) - assert.Nil(t, err) - assert.Equal(t, uuid.Nil, value.ID) - assert.Equal(t, []uuid.UUID{}, value.RequiredQueryIDs) - assert.Equal(t, int32(0), value.Version) - assert.Equal(t, queryprocessor.Type(queryprocessor.TypeJsonExtractor), value.Type) - dbResult.Type = repository.NullQuerytype{} - _, err = queryprocessor.ParseDBCollectorQuery(&dbResult) - assert.EqualError(t, err, "invalid database query type") + out := query.ParseQuery(q) + assert.EqualExportedValues(t, queryprocessor.Query{ + ID: q.ID, + Type: q.Type, + Version: q.ActiveVersion, + RequiredQueryIDs: q.RequiredQueryIDs, + Config: q.Config, + }, *out) } -func TestParseDBNullType(t *testing.T) { - qType := repository.NullQuerytype{Valid: true, Querytype: repository.QuerytypeJsonExtractor} - value, err := queryprocessor.ParseDBNullType(qType) +func TestParseDBListQuery(t *testing.T) { + q := &repository.ListQueriesRow{ + ID: database.MustToDBUUID(uuid.New()), + Type: repository.QuerytypeContextFull, + Activeversion: int32(1), + Latestversion: int32(2), + Requiredids: []pgtype.UUID{ + database.MustToDBUUID(uuid.New()), + }, + Config: []byte("example"), + } + + out, err := query.ParseDBListQuery(q) assert.Nil(t, err) - assert.Equal(t, queryprocessor.Type(queryprocessor.TypeJsonExtractor), value) - - qType = repository.NullQuerytype{} - _, err = queryprocessor.ParseDBNullType(qType) - assert.EqualError(t, err, "invalid database query type") - - qType = repository.NullQuerytype{Valid: true} - _, err = queryprocessor.ParseDBNullType(qType) - assert.EqualError(t, err, "invalid database query type") + assert.EqualExportedValues(t, query.Query{ + ID: database.MustToUUID(q.ID), + Type: queryprocessor.TypeContextFull, + ActiveVersion: q.Activeversion, + LatestVersion: q.Latestversion, + RequiredQueryIDs: []uuid.UUID{ + database.MustToUUID(q.Requiredids[0]), + }, + Config: string(q.Config), + }, *out) } -func TestParseDBType(t *testing.T) { - qType := repository.QuerytypeJsonExtractor - value, err := queryprocessor.ParseDBType(qType) - assert.Nil(t, err) - assert.Equal(t, queryprocessor.Type(queryprocessor.TypeJsonExtractor), value) +func TestParseDBGetQuery(t *testing.T) { + q := &repository.GetQueryRow{ + ID: database.MustToDBUUID(uuid.New()), + Type: repository.QuerytypeContextFull, + Activeversion: int32(1), + Latestversion: int32(2), + Requiredids: []pgtype.UUID{ + database.MustToDBUUID(uuid.New()), + }, + Config: []byte("example"), + } - qType = repository.QuerytypeContextFull - value, err = queryprocessor.ParseDBType(qType) + out, err := query.ParseDBGetQuery(q) assert.Nil(t, err) - assert.Equal(t, queryprocessor.Type(queryprocessor.TypeContextFull), value) -} - -func TestToDBQueryType(t *testing.T) { - dbQueryType := queryprocessor.Type(queryprocessor.TypeJsonExtractor) - value, err := queryprocessor.ToDBQueryType(dbQueryType) - assert.Nil(t, err) - assert.Equal(t, repository.Querytype(repository.QuerytypeJsonExtractor), value) - - dbQueryType = queryprocessor.Type(-1) - _, err = queryprocessor.ToDBQueryType(dbQueryType) - assert.EqualError(t, err, "invalid database query type") - - dbQueryType = queryprocessor.Type(queryprocessor.TypeContextFull) - value, err = queryprocessor.ToDBQueryType(dbQueryType) - assert.Nil(t, err) - assert.Equal(t, repository.Querytype(repository.QuerytypeContextFull), value) + assert.EqualExportedValues(t, query.Query{ + ID: database.MustToUUID(q.ID), + Type: queryprocessor.TypeContextFull, + ActiveVersion: q.Activeversion, + LatestVersion: q.Latestversion, + RequiredQueryIDs: []uuid.UUID{ + database.MustToUUID(q.Requiredids[0]), + }, + Config: string(q.Config), + }, *out) } diff --git a/test/unit/internal/query/test_test.go b/test/unit/internal/query/test_test.go new file mode 100644 index 00000000..ed8349e8 --- /dev/null +++ b/test/unit/internal/query/test_test.go @@ -0,0 +1,38 @@ +package document_test + +import ( + "context" + "queryorchestration/internal/database" + "queryorchestration/internal/database/repository" + "queryorchestration/internal/query" + "testing" + + "github.com/google/uuid" + "github.com/pashagolub/pgxmock/v3" + "github.com/stretchr/testify/assert" +) + +func TestTest(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) + db := &database.Connection{ + Queries: queries, + Pool: pool, + } + svc := query.New(db) + + params := &query.Test{ + QueryID: uuid.New(), + DocumentID: uuid.New(), + QueryVersion: int32(1), + } + + result, err := svc.Test(ctx, *params) + assert.Nil(t, err) + assert.Empty(t, result) +} diff --git a/test/unit/internal/query/update_test.go b/test/unit/internal/query/update_test.go new file mode 100644 index 00000000..cfcea8e5 --- /dev/null +++ b/test/unit/internal/query/update_test.go @@ -0,0 +1,62 @@ +package document_test + +import ( + "context" + "queryorchestration/internal/database" + "queryorchestration/internal/database/repository" + "queryorchestration/internal/query" + queryprocessor "queryorchestration/internal/queryProcessor" + "testing" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgtype" + "github.com/pashagolub/pgxmock/v3" + "github.com/stretchr/testify/assert" +) + +func TestUpdate(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) + db := &database.Connection{ + Queries: queries, + Pool: pool, + } + svc := query.New(db) + + config := "{\"path\":\"example_path\"}" + existing := query.Query{ + ID: uuid.New(), + Type: queryprocessor.TypeJsonExtractor, + ActiveVersion: int32(1), + LatestVersion: int32(1), + RequiredQueryIDs: []uuid.UUID{ + uuid.New(), + }, + Config: config, + } + update := &queryprocessor.Update{ + ID: existing.ID, + RequiredQueryIDs: []uuid.UUID{ + uuid.New(), + }, + Config: config, + } + + dbReqIDs := make([]pgtype.UUID, len(existing.RequiredQueryIDs)) + for index, id := range existing.RequiredQueryIDs { + dbReqIDs[index] = database.MustToDBUUID(id) + } + + pool.ExpectQuery("name: GetQuery :one").WithArgs(database.MustToDBUUID(update.ID)).WillReturnRows( + pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}). + AddRow(database.MustToDBUUID(existing.ID), repository.QuerytypeJsonExtractor, existing.ActiveVersion, existing.LatestVersion, []byte(config), dbReqIDs), + ) + + err = svc.Update(ctx, update) + assert.Nil(t, err) +} diff --git a/test/unit/internal/queryProcessor/parse_test.go b/test/unit/internal/queryProcessor/parse_test.go new file mode 100644 index 00000000..8f0d37fb --- /dev/null +++ b/test/unit/internal/queryProcessor/parse_test.go @@ -0,0 +1,90 @@ +package document_test + +import ( + "queryorchestration/internal/database/repository" + queryprocessor "queryorchestration/internal/queryProcessor" + "testing" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5/pgtype" + "github.com/stretchr/testify/assert" +) + +func TestParseDBCollectorQuery(t *testing.T) { + dbResult := repository.GetCollectorQueriesRow{ + Collectorid: pgtype.UUID{}, + Queryid: pgtype.UUID{}, + Requiredids: []pgtype.UUID{}, + Type: repository.NullQuerytype{Valid: true, Querytype: repository.QuerytypeJsonExtractor}, + Queryversion: pgtype.Int4{}, + } + value, err := queryprocessor.ParseDBCollectorQuery(&dbResult) + assert.Nil(t, err) + assert.Equal(t, uuid.Nil, value.ID) + assert.Equal(t, []uuid.UUID{}, value.RequiredQueryIDs) + assert.Equal(t, int32(0), value.Version) + assert.Equal(t, queryprocessor.Type(queryprocessor.TypeJsonExtractor), value.Type) + + dbResult.Type = repository.NullQuerytype{} + _, err = queryprocessor.ParseDBCollectorQuery(&dbResult) + assert.EqualError(t, err, "invalid database query type") +} + +func TestParseDBNullType(t *testing.T) { + qType := repository.NullQuerytype{Valid: true, Querytype: repository.QuerytypeJsonExtractor} + value, err := queryprocessor.ParseDBNullType(qType) + assert.Nil(t, err) + assert.Equal(t, queryprocessor.Type(queryprocessor.TypeJsonExtractor), value) + + qType = repository.NullQuerytype{} + _, err = queryprocessor.ParseDBNullType(qType) + assert.EqualError(t, err, "invalid database query type") + + qType = repository.NullQuerytype{Valid: true} + _, err = queryprocessor.ParseDBNullType(qType) + assert.EqualError(t, err, "invalid database query type") +} + +func TestParseDBType(t *testing.T) { + qType := repository.QuerytypeJsonExtractor + value, err := queryprocessor.ParseDBType(qType) + assert.Nil(t, err) + assert.Equal(t, queryprocessor.Type(queryprocessor.TypeJsonExtractor), value) + + qType = repository.QuerytypeContextFull + value, err = queryprocessor.ParseDBType(qType) + assert.Nil(t, err) + assert.Equal(t, queryprocessor.Type(queryprocessor.TypeContextFull), value) +} + +func TestToDBQueryType(t *testing.T) { + dbQueryType := queryprocessor.Type(queryprocessor.TypeJsonExtractor) + value, err := queryprocessor.ToDBQueryType(dbQueryType) + assert.Nil(t, err) + assert.Equal(t, repository.Querytype(repository.QuerytypeJsonExtractor), value) + + dbQueryType = queryprocessor.Type(-1) + _, err = queryprocessor.ToDBQueryType(dbQueryType) + assert.EqualError(t, err, "invalid database query type") + + dbQueryType = queryprocessor.Type(queryprocessor.TypeContextFull) + value, err = queryprocessor.ToDBQueryType(dbQueryType) + assert.Nil(t, err) + assert.Equal(t, repository.Querytype(repository.QuerytypeContextFull), value) +} + +func TestToDBNullQueryType(t *testing.T) { + dbQueryType := queryprocessor.Type(queryprocessor.TypeJsonExtractor) + value, err := queryprocessor.ToDBNullQueryType(dbQueryType) + assert.Nil(t, err) + assert.Equal(t, repository.NullQuerytype{Valid: true, Querytype: repository.QuerytypeJsonExtractor}, value) + + dbQueryType = queryprocessor.Type(-1) + _, err = queryprocessor.ToDBNullQueryType(dbQueryType) + assert.EqualError(t, err, "invalid database query type") + + dbQueryType = queryprocessor.Type(queryprocessor.TypeContextFull) + value, err = queryprocessor.ToDBNullQueryType(dbQueryType) + assert.Nil(t, err) + assert.Equal(t, repository.NullQuerytype{Valid: true, Querytype: repository.QuerytypeContextFull}, value) +}