package queryservice_test import ( "encoding/json" "net/http" "net/http/httptest" "strings" "testing" queryservice "queryorchestration/api/queryService" "queryorchestration/internal/database" "queryorchestration/internal/database/repository" "queryorchestration/internal/document" "queryorchestration/internal/serviceconfig" "queryorchestration/internal/serviceconfig/queue/clientsync" queuemock "queryorchestration/mocks/queue" "github.com/go-playground/validator/v10" "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) cfg := &struct { serviceconfig.BaseConfig clientsync.ClientSyncConfig }{} cfg.DBPool = pool cfg.DBQueries = repository.New(pool) mockSQS := queuemock.NewMockSQSClient(t) cfg.QueueClient = mockSQS clientId := 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) cons := queryservice.NewControllers(validator.New(), &queryservice.Services{ Document: document.New(cfg), }) ctx.Set("id", clientId) doc := queryservice.ListDocuments{ { Id: uuid.New(), Bucket: "bucketone", Key: "keyone", }, } pool.ExpectQuery("name: ListDocumentsByClientId :many").WithArgs(database.MustToDBUUID(clientId)). WillReturnRows( pgxmock.NewRows([]string{"id", "bucket", "key"}). AddRow(database.MustToDBUUID(doc[0].Id), doc[0].Bucket, doc[0].Key), ) err = cons.ListDocumentsByClientId(ctx, clientId) assert.NoError(t, err) assert.Equal(t, http.StatusOK, rec.Code) var res queryservice.ListDocuments err = json.Unmarshal(rec.Body.Bytes(), &res) assert.NoError(t, err) assert.EqualExportedValues(t, doc, res) }