Merged in feature/checkbox (pull request #145)

Checkbox Primary Edge Cases

* hascheckboxlogic

* gen

* preppinggen

* exploringcheckbox

* removeunselected

* tests

* failingtest

* failintests

* nomockqueryapi

* db

* name

* nocontainer

* deleterow

* cnc

* extradocsandupdatetextracts

* moretables

* appndedtables

* baseversion
This commit is contained in:
Michael McGuinness
2025-05-20 12:47:22 +00:00
parent beb221937b
commit 61d12bf8df
69 changed files with 223611 additions and 78482 deletions
+76 -67
View File
@@ -2,7 +2,6 @@ package queryapi_test
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
@@ -14,25 +13,23 @@ import (
"queryorchestration/internal/database/repository"
"queryorchestration/internal/serviceconfig"
"queryorchestration/internal/serviceconfig/queue/clientsync"
"queryorchestration/internal/test"
"github.com/labstack/echo/v4"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type ClientConfig struct {
serviceconfig.BaseConfig
clientsync.ConfigProvider
clientsync.ClientSyncConfig
}
func TestCreateClient(t *testing.T) {
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{})
cons := queryapi.NewControllers(&queryapi.Services{
Client: client.New(cfg),
@@ -42,71 +39,62 @@ func TestCreateClient(t *testing.T) {
Name: "example_name",
Id: "external_id",
}
bodyBytes, err := json.Marshal(body)
require.NoError(t, err)
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
ctx, rec := createContextWithBody(t, body)
pool.ExpectExec("name: CreateClient :exec").WithArgs(body.Id, body.Name).
WillReturnResult(pgxmock.NewResult("", 1))
err = cons.CreateClient(ctx)
err := cons.CreateClient(ctx)
require.NoError(t, err)
assert.Equal(t, http.StatusCreated, rec.Code)
assert.Equal(t, fmt.Sprintf("{\"id\":\"%s\"}\n", body.Id), rec.Body.String())
assertBody(t, rec, queryapi.ClientIDBody{
Id: body.Id,
})
client, err := cfg.GetDBQueries().GetClient(t.Context(), "external_id")
require.NoError(t, err)
assert.Equal(t, "example_name", client.Name)
assert.Equal(t, "external_id", client.Clientid)
}
func TestGetClient(t *testing.T) {
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{})
cons := queryapi.NewControllers(&queryapi.Services{
Client: client.New(cfg),
})
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
id := "client_id"
ctx.Set("id", id)
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
Clientid: id,
Name: "client_name",
})
require.NoError(t, err)
err = cfg.GetDBQueries().AddClientCanSync(t.Context(), &repository.AddClientCanSyncParams{
Clientid: id,
Cansync: true,
})
require.NoError(t, err)
pool.ExpectQuery("name: GetClient :one").WithArgs(id).WillReturnRows(
pgxmock.NewRows([]string{"id", "name", "canSync"}).
AddRow("client_id", "client_name", true),
)
ctx, rec := createContext(t)
err = cons.GetClient(ctx, id)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var res queryapi.DocClient
err = json.Unmarshal(rec.Body.Bytes(), &res)
require.NoError(t, err)
assert.EqualExportedValues(t, queryapi.DocClient{
assertBody(t, rec, queryapi.DocClient{
Id: id,
Name: "client_name",
CanSync: true,
}, res)
})
}
func TestUpdateClient(t *testing.T) {
pool, err := pgxmock.NewPool()
require.NoError(t, err)
t.Parallel()
cfg := &ClientConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
net := test.GetNetwork(t)
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
cons := queryapi.NewControllers(&queryapi.Services{
ClientUpdate: clientupdate.New(cfg, &clientupdate.Services{
@@ -114,10 +102,36 @@ func TestUpdateClient(t *testing.T) {
}),
})
cs := false
id := "client_id"
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
Clientid: id,
Name: "client_name",
})
require.NoError(t, err)
newClientName := "new_name"
body := queryapi.ClientUpdate{
CanSync: &cs,
Name: &newClientName,
}
ctx, rec := createContextWithBody(t, body)
err = cons.UpdateClient(ctx, id)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Empty(t, rec.Body.String())
client, err := cfg.GetDBQueries().GetClient(t.Context(), "client_id")
require.NoError(t, err)
assert.Equal(t, "new_name", client.Name)
}
func createContext(t testing.TB) (echo.Context, *httptest.ResponseRecorder) {
return createContextWithBody(t, struct{}{})
}
func createContextWithBody(t testing.TB, body interface{}) (echo.Context, *httptest.ResponseRecorder) {
bodyBytes, err := json.Marshal(body)
require.NoError(t, err)
@@ -125,23 +139,18 @@ func TestUpdateClient(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
id := "clientid"
ctx.Set("id", id)
pool.ExpectQuery("name: GetClient :one").WithArgs(id).WillReturnRows(
pgxmock.NewRows([]string{"id", "name", "canSync"}).
AddRow("clientid", "client_name", true),
)
pool.ExpectBegin()
pool.ExpectExec("name: AddClientCanSync :exec").WithArgs(*body.CanSync, id).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
err = cons.UpdateClient(ctx, id)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Empty(t, rec.Body.String())
return e.NewContext(req, rec), rec
}
func assertBody[T any](t testing.TB, rec *httptest.ResponseRecorder, expected T) {
res := getBody[T](t, rec)
assert.EqualExportedValues(t, expected, res)
}
func getBody[T any](t testing.TB, rec *httptest.ResponseRecorder) T {
var res T
err := json.Unmarshal(rec.Body.Bytes(), &res)
require.NoError(t, err)
return res
}
+37 -90
View File
@@ -1,11 +1,8 @@
package queryapi_test
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
queryapi "queryorchestration/api/queryAPI"
@@ -13,39 +10,36 @@ import (
collectorset "queryorchestration/internal/collector/set"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/serviceconfig"
"queryorchestration/internal/serviceconfig/queue/clientsync"
"queryorchestration/internal/test"
queuemock "queryorchestration/mocks/queue"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/labstack/echo/v4"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestSetCollector(t *testing.T) {
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &struct {
serviceconfig.BaseConfig
clientsync.ClientSyncConfig
}{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
t.Parallel()
cfg := &ClientConfig{}
cfg.ClientSyncURL = "example"
net := test.GetNetwork(t)
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
mockSQS := queuemock.NewMockSQSClient(t)
cfg.QueueClient = mockSQS
id := "clientid"
current := collector.Collector{
ClientID: id,
ActiveVersion: 1,
LatestVersion: 4,
}
av := int32(2)
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
Clientid: id,
Name: "client_name",
})
require.NoError(t, err)
queryId, err := cfg.GetDBQueries().CreateQuery(t.Context(), repository.QuerytypeContextFull)
require.NoError(t, err)
av := int32(1)
cv := int64(1)
body := queryapi.CollectorSet{
ActiveVersion: &av,
@@ -53,18 +47,11 @@ func TestSetCollector(t *testing.T) {
Fields: &[]queryapi.CollectorField{
{
Name: "a",
QueryId: uuid.New(),
QueryId: queryId,
},
},
}
bodyBytes, err := json.Marshal(body)
require.NoError(t, err)
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
ctx, rec := createContextWithBody(t, body)
cons := queryapi.NewControllers(&queryapi.Services{
CollectorSet: collectorset.New(cfg, &collectorset.Services{
@@ -72,36 +59,11 @@ func TestSetCollector(t *testing.T) {
}),
})
ctx.Set("id", id)
pool.ExpectQuery("name: GetCollectorByClientID :one").WithArgs(current.ClientID).
WillReturnRows(
pgxmock.NewRows([]string{"clientId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
AddRow(current.ClientID, current.MinCleanVersion, current.MinTextVersion, current.ActiveVersion, current.LatestVersion, []byte("")),
)
pool.ExpectQuery("name: AllQueriesExist :one").WithArgs([]uuid.UUID{(*body.Fields)[0].QueryId}).
WillReturnRows(
pgxmock.NewRows([]string{"exist"}).
AddRow(true),
)
pool.ExpectBeginTx(pgx.TxOptions{})
pool.ExpectQuery("name: AddLatestCollectorVersion :one").WithArgs(current.ClientID).WillReturnRows(
pgxmock.NewRows([]string{"version"}).
AddRow(int32(5)),
)
pool.ExpectExec("name: SetActiveCollectorVersion :exec").WithArgs(current.ClientID, int32(2)).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectExec("name: SetCollectorCleanVersion :exec").WithArgs(current.ClientID, int32(5), *body.MinimumCleanerVersion).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectExec("name: AddCollectorQuery :exec").WithArgs(current.ClientID, "a", (*body.Fields)[0].QueryId, int32(5)).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
mockSQS.EXPECT().
SendMessage(
mock.Anything,
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
return *in.QueueUrl == cfg.ClientSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", current.ClientID)
return *in.QueueUrl == cfg.GetClientSyncURL() && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", id)
}),
mock.Anything,
).
@@ -113,50 +75,35 @@ func TestSetCollector(t *testing.T) {
assert.Empty(t, rec.Body.String())
}
func TestGetCollectorByclientId(t *testing.T) {
pool, err := pgxmock.NewPool()
require.NoError(t, err)
func TestGetCollectorByClientId(t *testing.T) {
t.Parallel()
cfg := &serviceconfig.BaseConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
net := test.GetNetwork(t)
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
svc := collector.New(cfg)
cons := queryapi.NewControllers(&queryapi.Services{
Collector: svc,
Collector: collector.New(cfg),
})
coll := collector.Collector{
ClientID: "hello",
MinCleanVersion: 1,
MinTextVersion: 2,
}
id := "clientid"
pool.ExpectQuery("name: GetCollectorByClientID :one").WithArgs(id).
WillReturnRows(
pgxmock.NewRows([]string{"clientId", "minCleanVersion", "minTextVersion", "activeVersion", "latestVersion", "fields"}).
AddRow(coll.ClientID, coll.MinCleanVersion, coll.MinTextVersion, int32(1), int32(2), []byte("")),
)
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
Clientid: id,
Name: "client_name",
})
require.NoError(t, err)
ctx, rec := createContext(t)
err = cons.GetCollectorByClientId(ctx, id)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var res queryapi.Collector
err = json.Unmarshal(rec.Body.Bytes(), &res)
require.NoError(t, err)
assert.EqualExportedValues(t, queryapi.Collector{
assertBody(t, rec, queryapi.Collector{
ClientId: id,
MinimumCleanerVersion: coll.MinCleanVersion,
MinimumTextVersion: coll.MinTextVersion,
ActiveVersion: int32(1),
LatestVersion: int32(2),
MinimumCleanerVersion: 0,
MinimumTextVersion: 0,
ActiveVersion: 0,
LatestVersion: 0,
Fields: []queryapi.CollectorField{},
}, res)
})
}
+42 -72
View File
@@ -1,10 +1,7 @@
package queryapi_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
queryapi "queryorchestration/api/queryAPI"
@@ -12,109 +9,82 @@ import (
"queryorchestration/internal/document"
"queryorchestration/internal/serviceconfig"
"queryorchestration/internal/serviceconfig/queue/clientsync"
queuemock "queryorchestration/mocks/queue"
"queryorchestration/internal/test"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestListDocumentsByClientId(t *testing.T) {
pool, err := pgxmock.NewPool()
require.NoError(t, err)
t.Parallel()
cfg := &struct {
serviceconfig.BaseConfig
clientsync.ClientSyncConfig
}{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
mockSQS := queuemock.NewMockSQSClient(t)
cfg.QueueClient = mockSQS
clientId := "clientid"
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
net := test.GetNetwork(t)
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
cons := queryapi.NewControllers(&queryapi.Services{
Document: document.New(cfg),
})
ctx.Set("id", clientId)
doc := queryapi.ListDocuments{
{
Id: uuid.New(),
Hash: "hash",
},
}
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)
pool.ExpectQuery("name: ListDocumentsByClient :many").WithArgs(clientId).
WillReturnRows(
pgxmock.NewRows([]string{"id", "hash"}).
AddRow(doc[0].Id, "hash"),
)
ctx, rec := createContext(t)
err = cons.ListDocumentsByClientId(ctx, clientId)
err = cons.ListDocumentsByClientId(ctx, "client_id")
require.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var res queryapi.ListDocuments
err = json.Unmarshal(rec.Body.Bytes(), &res)
require.NoError(t, err)
assert.EqualExportedValues(t, doc, res)
assertBody(t, rec, queryapi.ListDocuments{
{
Id: docId,
Hash: "hash",
},
})
}
func TestGetDocument(t *testing.T) {
pool, err := pgxmock.NewPool()
require.NoError(t, err)
t.Parallel()
cfg := &struct {
serviceconfig.BaseConfig
clientsync.ClientSyncConfig
}{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
mockSQS := queuemock.NewMockSQSClient(t)
cfg.QueueClient = mockSQS
docId := uuid.New()
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
net := test.GetNetwork(t)
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
cons := queryapi.NewControllers(&queryapi.Services{
Document: document.New(cfg),
})
ctx.Set("id", docId)
doc := queryapi.Document{
Id: docId,
ClientId: "externalID",
Hash: "example",
Fields: map[string]interface{}{
"json": "hello",
},
}
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)
pool.ExpectQuery("name: GetDocumentExternal :one").WithArgs(docId).
WillReturnRows(
pgxmock.NewRows([]string{"id", "clientId", "hash", "fields"}).
AddRow(doc.Id, "externalID", "example", []byte(`{"json": "hello"}`)),
)
ctx, rec := createContext(t)
err = cons.GetDocument(ctx, docId)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var res queryapi.Document
err = json.Unmarshal(rec.Body.Bytes(), &res)
require.NoError(t, err)
assert.EqualExportedValues(t, doc, res)
assertBody(t, rec, queryapi.Document{
Id: docId,
ClientId: "client_id",
Hash: "hash",
Fields: map[string]interface{}{},
})
}
+2 -11
View File
@@ -2,23 +2,17 @@ package queryapi_test
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
queryapi "queryorchestration/api/queryAPI"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTriggerExport(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
ctx, rec := createContext(t)
cons := queryapi.NewControllers(&queryapi.Services{})
@@ -29,10 +23,7 @@ func TestTriggerExport(t *testing.T) {
}
func TestExportState(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
ctx, rec := createContext(t)
cons := queryapi.NewControllers(&queryapi.Services{})
+121 -154
View File
@@ -1,12 +1,10 @@
package queryapi_test
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
queryapi "queryorchestration/api/queryAPI"
"queryorchestration/internal/collector"
@@ -14,29 +12,28 @@ import (
"queryorchestration/internal/document"
"queryorchestration/internal/query"
"queryorchestration/internal/query/result"
resultprocessor "queryorchestration/internal/query/result/processor"
querytest "queryorchestration/internal/query/test"
queryupdate "queryorchestration/internal/query/update"
"queryorchestration/internal/serviceconfig"
"queryorchestration/internal/serviceconfig/queue/queryversionsync"
"queryorchestration/internal/test"
queuemock "queryorchestration/mocks/queue"
"github.com/jackc/pgx/v5/pgtype"
"github.com/stretchr/testify/require"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/labstack/echo/v4"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestCreateQuery(t *testing.T) {
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{})
cons := queryapi.NewControllers(&queryapi.Services{
Query: query.New(cfg),
@@ -45,125 +42,79 @@ func TestCreateQuery(t *testing.T) {
body := queryapi.QueryCreate{
Type: queryapi.CONTEXTFULL,
}
bodyBytes, err := json.Marshal(body)
require.NoError(t, err)
ctx, rec := createContextWithBody(t, body)
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
id := uuid.New()
pool.ExpectBeginTx(pgx.TxOptions{})
pool.ExpectQuery("name: CreateQuery :one").WithArgs(repository.QuerytypeContextFull).WillReturnRows(
pgxmock.NewRows([]string{"id"}).
AddRow(id),
)
pool.ExpectQuery("name: AddLatestQueryVersion :one").WithArgs(id).WillReturnRows(
pgxmock.NewRows([]string{"version"}).
AddRow(int32(1)),
)
pool.ExpectExec("name: AddActiveQueryVersion :exec").WithArgs(id, int32(1)).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
err = cons.CreateQuery(ctx)
err := cons.CreateQuery(ctx)
require.NoError(t, err)
assert.Equal(t, http.StatusCreated, rec.Code)
assert.Equal(t, fmt.Sprintf("{\"id\":\"%s\"}\n", id), rec.Body.String())
res := getBody[queryapi.IdMessage](t, rec)
assert.NotEqual(t, uuid.Nil, res.Id)
}
func TestListQueries(t *testing.T) {
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{})
cons := queryapi.NewControllers(&queryapi.Services{
Query: query.New(cfg),
})
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
ctx, rec := createContext(t)
id := uuid.New()
pool.ExpectQuery("name: ListQueries :many").WithArgs().WillReturnRows(
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
AddRow(id, repository.QuerytypeContextFull, int32(1), int32(2), []byte(""), []uuid.UUID{}),
)
id, err := cfg.GetDBQueries().CreateQuery(t.Context(), repository.QuerytypeContextFull)
require.NoError(t, err)
err = cons.ListQueries(ctx)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var res queryapi.ListQueries
err = json.Unmarshal(rec.Body.Bytes(), &res)
require.NoError(t, err)
assert.ElementsMatch(t, res.Queries, []queryapi.Query{
{
Id: id,
Type: queryapi.CONTEXTFULL,
ActiveVersion: 1,
LatestVersion: 2,
assertBody(t, rec, queryapi.ListQueries{
Queries: []queryapi.Query{
{
Id: id,
Type: queryapi.CONTEXTFULL,
ActiveVersion: 0,
LatestVersion: 0,
},
},
})
}
func TestGetQuery(t *testing.T) {
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{})
cons := queryapi.NewControllers(&queryapi.Services{
Query: query.New(cfg),
})
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
ctx, rec := createContext(t)
id := uuid.New()
ctx.Set("id", id)
pool.ExpectQuery("name: GetQuery :one").WithArgs(id).WillReturnRows(
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
AddRow(id, repository.QuerytypeContextFull, int32(1), int32(2), nil, []uuid.UUID{}),
)
id, err := cfg.GetDBQueries().CreateQuery(t.Context(), repository.QuerytypeContextFull)
require.NoError(t, err)
err = cons.GetQuery(ctx, id)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var res queryapi.Query
err = json.Unmarshal(rec.Body.Bytes(), &res)
require.NoError(t, err)
assert.EqualExportedValues(t, queryapi.Query{
assertBody(t, rec, queryapi.Query{
Id: id,
Type: queryapi.CONTEXTFULL,
ActiveVersion: 1,
LatestVersion: 2,
}, res)
ActiveVersion: 0,
LatestVersion: 0,
})
}
func TestUpdateQuery(t *testing.T) {
pool, err := pgxmock.NewPool()
require.NoError(t, err)
t.Parallel()
cfg := &struct {
serviceconfig.BaseConfig
queryversionsync.QueryVersionSyncConfig
}{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
net := test.GetNetwork(t)
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
mockSQS := queuemock.NewMockSQSClient(t)
cfg.QueueClient = mockSQS
cfg.QueryVersionSyncURL = "here"
@@ -174,32 +125,17 @@ func TestUpdateQuery(t *testing.T) {
}),
})
av := int32(2)
body := queryapi.QueryUpdate{
ActiveVersion: &av,
}
bodyBytes, err := json.Marshal(body)
id, err := cfg.GetDBQueries().CreateQuery(t.Context(), repository.QuerytypeJsonExtractor)
require.NoError(t, err)
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
av := int32(1)
c := `{"path": "val"}`
body := queryapi.QueryUpdate{
ActiveVersion: &av,
Config: &c,
}
ctx, rec := createContextWithBody(t, body)
id := uuid.New()
ctx.Set("id", id)
pool.ExpectQuery("name: GetQuery :one").WithArgs(id).WillReturnRows(
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
AddRow(id, repository.QuerytypeContextFull, int32(1), int32(2), []byte(""), []uuid.UUID{}),
)
pool.ExpectBeginTx(pgx.TxOptions{})
pool.ExpectExec("name: AddActiveQueryVersion :exec").WithArgs(id, av).
WillReturnResult(pgxmock.NewResult("", 1))
pool.ExpectCommit()
mockSQS.EXPECT().
SendMessage(
mock.Anything,
@@ -217,11 +153,10 @@ func TestUpdateQuery(t *testing.T) {
}
func TestTestQuery(t *testing.T) {
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{})
docsvc := document.New(cfg)
col := collector.New(cfg)
@@ -238,51 +173,83 @@ func TestTestQuery(t *testing.T) {
}),
})
doc := document.DocumentSummary{
ID: uuid.New(),
}
params := &result.Process{
QueryID: uuid.New(),
DocumentID: doc.ID,
QueryVersion: int32(3),
}
body := queryapi.QueryTestRequest{
DocumentId: params.DocumentID,
QueryVersion: params.QueryVersion,
}
bodyBytes, err := json.Marshal(body)
contextQueryId, err := que.Create(t.Context(), &resultprocessor.Create{
Type: resultprocessor.TypeContextFull,
})
require.NoError(t, err)
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
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)
strVal := `{"mykey": "example_value", "oldkey": "old_value"}`
_, err = cfg.GetDBQueries().AddResult(t.Context(), &repository.AddResultParams{
Queryid: contextQueryId,
Value: strVal,
Textentryid: textId,
Queryversion: 1,
})
require.NoError(t, err)
reqID := uuid.New()
pool.ExpectQuery("name: GetQueryWithVersion :one").WithArgs(&params.QueryID, &params.QueryVersion).WillReturnRows(
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
AddRow(params.QueryID, repository.QuerytypeJsonExtractor, int32(1), params.QueryVersion+1, []byte("{\"path\":\"oldkey\"}"), []uuid.UUID{reqID}),
)
strVal := "{\"mykey\":\"example_value\",\"oldkey\":\"old_value\"}"
pool.ExpectQuery("name: ListQueryRequirementValues :many").WithArgs(&params.QueryID, &params.QueryVersion, &params.DocumentID).
WillReturnRows(
pgxmock.NewRows([]string{"id", "queryId", "type", "value"}).
AddRow(&uuid.UUID{}, reqID, repository.QuerytypeContextFull, &strVal),
)
pool.ExpectQuery("name: GetActiveQueryConfig :one").WithArgs(params.QueryID).WillReturnRows(
pgxmock.NewRows([]string{"config"}).
AddRow([]byte("{\"path\":\"oldkey\"}")),
)
c := `{"path": "oldkey"}`
queryId, err := que.Create(t.Context(), &resultprocessor.Create{
Type: resultprocessor.TypeJsonExtractor,
Config: &c,
RequiredQueryIDs: &[]uuid.UUID{
contextQueryId,
},
})
require.NoError(t, err)
err = cons.TestQuery(ctx, params.QueryID)
body := queryapi.QueryTestRequest{
DocumentId: docId,
QueryVersion: 1,
}
ctx, rec := createContextWithBody(t, body)
err = cons.TestQuery(ctx, queryId)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var res queryapi.QueryTestResponse
err = json.Unmarshal(rec.Body.Bytes(), &res)
require.NoError(t, err)
assert.EqualExportedValues(t, queryapi.QueryTestResponse{
assertBody(t, rec, queryapi.QueryTestResponse{
Value: "old_value",
}, res)
})
}
+50 -29
View File
@@ -1,60 +1,81 @@
package queryapi_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
queryapi "queryorchestration/api/queryAPI"
"queryorchestration/internal/client"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/export"
"queryorchestration/internal/serviceconfig"
"queryorchestration/internal/test"
"github.com/labstack/echo/v4"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetClientStatus(t *testing.T) {
pool, err := pgxmock.NewPool()
require.NoError(t, err)
cfg := &serviceconfig.BaseConfig{}
cfg.DBPool = pool
cfg.DBQueries = repository.New(pool)
t.Parallel()
cfg := &ClientConfig{}
net := test.GetNetwork(t)
test.CreateDB(t, cfg, net, &test.CreateDatabaseConfig{})
cons := queryapi.NewControllers(&queryapi.Services{
Client: client.New(cfg),
Export: export.New(),
})
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
ctx, rec := createContext(t)
clientId := "clientid"
pool.ExpectQuery("name: GetClient :one").WithArgs(clientId).WillReturnRows(
pgxmock.NewRows([]string{"id", "name", "can_sync"}).
AddRow(clientId, "name", true),
)
pool.ExpectQuery("name: IsClientSynced :one").WithArgs(&clientId).WillReturnRows(
pgxmock.NewRows([]string{"issynced"}).
AddRow(true),
)
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
Clientid: clientId,
Name: "client_name",
})
require.NoError(t, err)
err = cfg.GetDBQueries().AddClientCanSync(t.Context(), &repository.AddClientCanSyncParams{
Clientid: clientId,
Cansync: true,
})
require.NoError(t, err)
err = cons.GetStatusByClientId(ctx, clientId)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var res queryapi.ClientStatusBody
err = json.Unmarshal(rec.Body.Bytes(), &res)
require.NoError(t, err)
assert.EqualExportedValues(t, queryapi.ClientStatusBody{
assertBody(t, rec, queryapi.ClientStatusBody{
Status: queryapi.INSYNC,
}, res)
})
}
func BenchmarkGetClientStatus(b *testing.B) {
cfg := &ClientConfig{}
net := test.GetNetwork(b)
test.CreateDB(b, cfg, net, &test.CreateDatabaseConfig{})
cons := queryapi.NewControllers(&queryapi.Services{
Client: client.New(cfg),
Export: export.New(),
})
ctx, _ := createContext(b)
clientId := "clientid"
err := cfg.GetDBQueries().CreateClient(b.Context(), &repository.CreateClientParams{
Clientid: clientId,
Name: "client_name",
})
require.NoError(b, err)
err = cfg.GetDBQueries().AddClientCanSync(b.Context(), &repository.AddClientCanSyncParams{
Clientid: clientId,
Cansync: true,
})
require.NoError(b, err)
b.ResetTimer()
for b.Loop() {
_ = cons.GetStatusByClientId(ctx, clientId)
}
}