package client_test import ( "errors" "testing" "queryorchestration/internal/client" "queryorchestration/internal/database/repository" "queryorchestration/internal/serviceconfig" "github.com/pashagolub/pgxmock/v3" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestGetStatus(t *testing.T) { ctx := t.Context() pool, err := pgxmock.NewPool() require.NoError(t, err) cfg := &serviceconfig.BaseConfig{} cfg.DBPool = pool cfg.DBQueries = repository.New(pool) svc := client.New(cfg) clientId := "clientid" t.Run("is synced", func(t *testing.T) { 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), ) issynced, err := svc.GetStatus(ctx, clientId) require.NoError(t, err) assert.Equal(t, client.IN_SYNC, issynced) }) t.Run("not synced", func(t *testing.T) { pool.ExpectQuery("name: GetClient :one").WithArgs(clientId).WillReturnRows( pgxmock.NewRows([]string{"clientId", "name", "can_sync"}). AddRow(clientId, "name", true), ) pool.ExpectQuery("name: IsClientSynced :one").WithArgs(&clientId).WillReturnRows( pgxmock.NewRows([]string{"issynced"}). AddRow(false), ) issynced, err := svc.GetStatus(ctx, clientId) require.NoError(t, err) assert.Equal(t, client.NOT_SYNCED, issynced) }) t.Run("not syncing", func(t *testing.T) { pool.ExpectQuery("name: GetClient :one").WithArgs(clientId).WillReturnRows( pgxmock.NewRows([]string{"clientId", "name", "can_sync"}). AddRow(clientId, "name", false), ) issynced, err := svc.GetStatus(ctx, clientId) require.NoError(t, err) assert.Equal(t, client.NOT_SYNCING, issynced) }) t.Run("db error", func(t *testing.T) { errStr := "db fail" pool.ExpectQuery("name: GetClient :one").WithArgs(clientId). WillReturnError(errors.New(errStr)) _, err = svc.GetStatus(ctx, clientId) assert.EqualError(t, err, errStr) }) }