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
+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)
})
}