Files
query-orchestration/internal/client/status_test.go
T

76 lines
2.1 KiB
Go
Raw Normal View History

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) {
2025-06-03 13:52:10 +00:00
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)
2025-03-19 11:54:14 +00:00
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)
2025-03-19 11:54:14 +00:00
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)
2025-03-19 11:54:14 +00:00
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)
})
}