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:
@@ -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{}{},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user