package queryapi_test import ( "encoding/json" "fmt" "net/http" "net/http/httptest" "strings" "testing" queryapi "queryorchestration/api/queryAPI" "queryorchestration/internal/client" clientupdate "queryorchestration/internal/client/update" "queryorchestration/internal/database/repository" "queryorchestration/internal/serviceconfig" "queryorchestration/internal/serviceconfig/queue/clientsync" "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 } func TestCreateClient(t *testing.T) { pool, err := pgxmock.NewPool() require.NoError(t, err) cfg := &serviceconfig.BaseConfig{} cfg.DBPool = pool cfg.DBQueries = repository.New(pool) cons := queryapi.NewControllers(&queryapi.Services{ Client: client.New(cfg), }) body := queryapi.ClientCreate{ 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) pool.ExpectExec("name: CreateClient :exec").WithArgs(body.Id, body.Name). WillReturnResult(pgxmock.NewResult("", 1)) 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()) } func TestGetClient(t *testing.T) { pool, err := pgxmock.NewPool() require.NoError(t, err) cfg := &serviceconfig.BaseConfig{} cfg.DBPool = pool cfg.DBQueries = repository.New(pool) 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) pool.ExpectQuery("name: GetClient :one").WithArgs(id).WillReturnRows( pgxmock.NewRows([]string{"id", "name", "canSync"}). AddRow("client_id", "client_name", true), ) 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{ Id: id, Name: "client_name", CanSync: true, }, res) } func TestUpdateClient(t *testing.T) { pool, err := pgxmock.NewPool() require.NoError(t, err) cfg := &ClientConfig{} cfg.DBPool = pool cfg.DBQueries = repository.New(pool) cons := queryapi.NewControllers(&queryapi.Services{ ClientUpdate: clientupdate.New(cfg, &clientupdate.Services{ Client: client.New(cfg), }), }) cs := false body := queryapi.ClientUpdate{ CanSync: &cs, } 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) 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()) }