Merged in feature/batch-status (pull request #215)

Implement and test the batch status feature

* working
This commit is contained in:
Jay Brown
2026-03-12 18:53:42 +00:00
parent 8a4029058b
commit aafe7d5b5f
39 changed files with 4252 additions and 516 deletions
+130
View File
@@ -6,7 +6,10 @@ import (
"bytes"
"fmt"
"testing"
"time"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/serviceconfig/objectstore"
"queryorchestration/internal/test"
"github.com/aws/aws-sdk-go-v2/aws"
@@ -105,3 +108,130 @@ func TestMeasureFileSize_Integration_NonExistentKey(t *testing.T) {
assert.Error(t, err, "measureFileSize should return an error for non-existent key")
assert.Contains(t, err.Error(), "HeadObject failed", "error should indicate HeadObject failure")
}
// TestDocumentInitCreate_ReturnsCreateResult verifies that Create() returns a
// *CreateResult with correct IsDuplicate, BatchID, and Filename fields for both
// new and duplicate documents.
func TestDocumentInitCreate_ReturnsCreateResult(t *testing.T) {
ctx := t.Context()
// Set up DB, S3, and SQS
cfg := &DocInitConfig{}
test.CreateDB(t, cfg)
acfg := test.CreateAWSContainer(t, cfg)
test.SetQueueClient(t, ctx, cfg, acfg.ExternalEndpoint)
cfg.DocumentSyncURL = test.CreateQueue(t, cfg, test.DocSyncRunnerName)
test.SetStoreClient(t, ctx, cfg, acfg.ExternalEndpoint)
test.CreateBucket(t, cfg)
svc := New(cfg)
clientID := "create_result_" + uuid.New().String()[:8]
err := cfg.GetDBQueries().CreateClient(ctx, &repository.CreateClientParams{
Clientid: clientID,
Name: "Test CreateResult",
})
require.NoError(t, err)
// Create a batch
batchID, err := cfg.GetDBQueries().CreateBatchUpload(ctx, &repository.CreateBatchUploadParams{
ClientID: clientID,
OriginalFilename: "test.zip",
TotalDocuments: 2,
})
require.NoError(t, err)
// Upload a file to S3
location := objectstore.BucketKey{
Location: objectstore.Import,
ClientID: clientID,
CreatedAt: time.Now().UTC(),
EntityID: uuid.New(),
BatchID: &batchID,
}
testContent := []byte("test content for create result")
_, err = cfg.GetStoreClient().PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(cfg.GetBucket()),
Key: aws.String(location.String()),
Body: bytes.NewReader(testContent),
})
require.NoError(t, err)
filename := "invoice.pdf"
uniqueHash := "create_result_hash_" + uuid.New().String()[:8]
// Test 1: New document returns IsDuplicate=false with correct fields
result, err := svc.Create(ctx, &Create{
Bucket: cfg.GetBucket(),
Key: location,
Hash: uniqueHash,
Filename: &filename,
})
require.NoError(t, err)
require.NotNil(t, result)
assert.NotEqual(t, uuid.Nil, result.ID, "new document should have a valid ID")
assert.False(t, result.IsDuplicate, "new document should not be a duplicate")
assert.NotNil(t, result.BatchID, "batch document should have BatchID set")
assert.Equal(t, batchID, *result.BatchID, "BatchID should match the batch")
assert.NotNil(t, result.Filename, "Filename should be set")
assert.Equal(t, filename, *result.Filename, "Filename should match")
// Test 2: Duplicate document (same hash) returns IsDuplicate=true
location2 := objectstore.BucketKey{
Location: objectstore.Import,
ClientID: clientID,
CreatedAt: time.Now().UTC(),
EntityID: uuid.New(),
BatchID: &batchID,
}
_, err = cfg.GetStoreClient().PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(cfg.GetBucket()),
Key: aws.String(location2.String()),
Body: bytes.NewReader(testContent),
})
require.NoError(t, err)
dupResult, err := svc.Create(ctx, &Create{
Bucket: cfg.GetBucket(),
Key: location2,
Hash: uniqueHash, // Same hash triggers duplicate
Filename: &filename,
})
require.NoError(t, err)
require.NotNil(t, dupResult)
assert.Equal(t, result.ID, dupResult.ID, "duplicate should reference the original document ID")
assert.True(t, dupResult.IsDuplicate, "second document with same hash should be duplicate")
assert.NotNil(t, dupResult.BatchID, "duplicate should still have BatchID")
assert.Equal(t, batchID, *dupResult.BatchID, "duplicate BatchID should match")
// Test 3: Non-batch document has nil BatchID
location3 := objectstore.BucketKey{
Location: objectstore.Import,
ClientID: clientID,
CreatedAt: time.Now().UTC(),
EntityID: uuid.New(),
// No BatchID
}
_, err = cfg.GetStoreClient().PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(cfg.GetBucket()),
Key: aws.String(location3.String()),
Body: bytes.NewReader(testContent),
})
require.NoError(t, err)
nonBatchResult, err := svc.Create(ctx, &Create{
Bucket: cfg.GetBucket(),
Key: location3,
Hash: "nonbatch_hash_" + uuid.New().String()[:8],
})
require.NoError(t, err)
require.NotNil(t, nonBatchResult)
assert.False(t, nonBatchResult.IsDuplicate, "non-batch new document should not be duplicate")
assert.Nil(t, nonBatchResult.BatchID, "non-batch document should have nil BatchID")
assert.Nil(t, nonBatchResult.Filename, "non-batch document should have nil Filename")
}