2025-03-10 11:03:00 +00:00
|
|
|
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"
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
func TestGetStatus(t *testing.T) {
|
2025-06-03 13:52:10 +00:00
|
|
|
ctx := t.Context()
|
2025-03-10 11:03:00 +00:00
|
|
|
|
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
|
|
2025-03-10 13:00:57 +00:00
|
|
|
svc := client.New(cfg)
|
2025-03-10 11:03:00 +00:00
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
clientId := "clientid"
|
2025-03-10 11:03:00 +00:00
|
|
|
|
|
|
|
|
t.Run("is synced", func(t *testing.T) {
|
2025-04-02 18:50:03 +00:00
|
|
|
pool.ExpectQuery("name: GetClient :one").WithArgs(clientId).WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "name", "can_sync"}).
|
|
|
|
|
AddRow(clientId, "name", true),
|
2025-03-10 11:03:00 +00:00
|
|
|
)
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: IsClientSynced :one").WithArgs(&clientId).WillReturnRows(
|
2025-03-10 11:03:00 +00:00
|
|
|
pgxmock.NewRows([]string{"issynced"}).
|
|
|
|
|
AddRow(true),
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
issynced, err := svc.GetStatus(ctx, clientId)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-03-10 11:03:00 +00:00
|
|
|
assert.Equal(t, client.IN_SYNC, issynced)
|
|
|
|
|
})
|
|
|
|
|
t.Run("not synced", func(t *testing.T) {
|
2025-04-02 18:50:03 +00:00
|
|
|
pool.ExpectQuery("name: GetClient :one").WithArgs(clientId).WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"clientId", "name", "can_sync"}).
|
|
|
|
|
AddRow(clientId, "name", true),
|
2025-03-10 11:03:00 +00:00
|
|
|
)
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: IsClientSynced :one").WithArgs(&clientId).WillReturnRows(
|
2025-03-10 11:03:00 +00:00
|
|
|
pgxmock.NewRows([]string{"issynced"}).
|
|
|
|
|
AddRow(false),
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
issynced, err := svc.GetStatus(ctx, clientId)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-03-10 11:03:00 +00:00
|
|
|
assert.Equal(t, client.NOT_SYNCED, issynced)
|
|
|
|
|
})
|
|
|
|
|
t.Run("not syncing", func(t *testing.T) {
|
2025-04-02 18:50:03 +00:00
|
|
|
pool.ExpectQuery("name: GetClient :one").WithArgs(clientId).WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"clientId", "name", "can_sync"}).
|
|
|
|
|
AddRow(clientId, "name", false),
|
2025-03-10 11:03:00 +00:00
|
|
|
)
|
|
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
issynced, err := svc.GetStatus(ctx, clientId)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-03-10 11:03:00 +00:00
|
|
|
assert.Equal(t, client.NOT_SYNCING, issynced)
|
|
|
|
|
})
|
|
|
|
|
t.Run("db error", func(t *testing.T) {
|
|
|
|
|
errStr := "db fail"
|
2025-04-02 18:50:03 +00:00
|
|
|
pool.ExpectQuery("name: GetClient :one").WithArgs(clientId).
|
2025-03-10 11:03:00 +00:00
|
|
|
WillReturnError(errors.New(errStr))
|
|
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
_, err = svc.GetStatus(ctx, clientId)
|
2025-03-10 11:03:00 +00:00
|
|
|
assert.EqualError(t, err, errStr)
|
|
|
|
|
})
|
|
|
|
|
}
|