package clientsyncrunner_test import ( "context" "encoding/json" "fmt" "testing" clientsyncrunner "queryorchestration/api/clientSyncRunner" clientsync "queryorchestration/internal/client/sync" "queryorchestration/internal/database" "queryorchestration/internal/database/repository" "queryorchestration/internal/server/runner" "queryorchestration/internal/serviceconfig/queue/documentsync" queuemock "queryorchestration/mocks/queue" "github.com/aws/aws-sdk-go-v2/service/sqs" "github.com/aws/aws-sdk-go-v2/service/sqs/types" "github.com/go-playground/validator/v10" "github.com/google/uuid" "github.com/pashagolub/pgxmock/v3" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) type ClientSyncConfig struct { runner.BaseConfig documentsync.DocSyncConfig } func TestQueryRunner(t *testing.T) { ctx := context.Background() pool, err := pgxmock.NewPool() require.NoError(t, err) cfg := &ClientSyncConfig{} cfg.DBPool = pool cfg.DBQueries = repository.New(pool) mockSQS := queuemock.NewMockSQSClient(t) cfg.QueueClient = mockSQS cfg.DocumentSyncURL = "/i/am/here" svc := clientsync.New(cfg) runner := clientsyncrunner.New(validator.New(), &clientsyncrunner.Services{ ClientSync: svc, }) assert.NotNil(t, runner) t.Run("valid", func(t *testing.T) { bod := clientsyncrunner.Body{ ID: uuid.New(), } bodyBytes, err := json.Marshal(bod) assert.NoError(t, err) body := string(bodyBytes) msg := &types.Message{ Body: &body, } docId := uuid.New() total := int64(1) pool.ExpectQuery("name: ListDocumentIDsBatch :many").WithArgs(database.MustToDBUUID(bod.ID), int32(100), int32(0)). WillReturnRows( pgxmock.NewRows([]string{"id", "totalCount"}). AddRow(database.MustToDBUUID(docId), &total), ) mockSQS.EXPECT(). SendMessage( mock.Anything, mock.MatchedBy(func(in *sqs.SendMessageInput) bool { return *in.QueueUrl == cfg.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docId.String()) }), mock.Anything, ). Return(&sqs.SendMessageOutput{}, nil) assert.True(t, runner.Process(ctx, msg)) }) t.Run("invalid body", func(t *testing.T) { body := "definitely invalid" msgId := "id" msg := &types.Message{ Body: &body, MessageId: &msgId, } assert.True(t, runner.Process(ctx, msg)) }) }