package clientsync import ( "context" "fmt" "testing" "queryorchestration/internal/database/repository" "queryorchestration/internal/document" "queryorchestration/internal/serviceconfig" "queryorchestration/internal/serviceconfig/queue/documentsync" queuemock "queryorchestration/mocks/queue" "github.com/aws/aws-sdk-go-v2/service/sqs" "github.com/google/uuid" "github.com/pashagolub/pgxmock/v3" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) type ClientSyncConfig struct { serviceconfig.BaseConfig documentsync.DocSyncConfig } func TestTrigger(t *testing.T) { t.Run("two batches", func(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 := New(cfg) clientId := "huuh" docs := []document.DocumentSummary{ { ID: uuid.New(), ClientID: clientId, }, { ID: uuid.New(), ClientID: clientId, }, } svc.batchSize = 1 total := int64(2) pool.ExpectQuery("name: ListDocumentIDsBatch :many").WithArgs(clientId, svc.batchSize, int32(0)). WillReturnRows( pgxmock.NewRows([]string{"id", "totalCount"}). AddRow(&docs[0].ID, &total), ) mockSQS.EXPECT(). SendMessage( mock.Anything, mock.MatchedBy(func(in *sqs.SendMessageInput) bool { return *in.QueueUrl == cfg.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docs[0].ID.String()) }), mock.Anything, ). Return(&sqs.SendMessageOutput{}, nil) pool.ExpectQuery("name: ListDocumentIDsBatch :many").WithArgs(clientId, svc.batchSize, int32(1)). WillReturnRows( pgxmock.NewRows([]string{"id", "totalCount"}). AddRow(&docs[1].ID, &total), ) mockSQS.EXPECT(). SendMessage( mock.Anything, mock.MatchedBy(func(in *sqs.SendMessageInput) bool { return *in.QueueUrl == cfg.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docs[1].ID.String()) }), mock.Anything, ). Return(&sqs.SendMessageOutput{}, nil) err = svc.Sync(ctx, clientId) require.NoError(t, err) }) t.Run("empty batch", func(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 := New(cfg) clientId := "hola" svc.batchSize = 1 pool.ExpectQuery("name: ListDocumentIDsBatch :many").WithArgs(clientId, svc.batchSize, int32(0)). WillReturnRows( pgxmock.NewRows([]string{"id", "totalCount"}), ) err = svc.Sync(ctx, clientId) require.NoError(t, err) }) }