2025-01-24 16:12:25 +00:00
|
|
|
package document_test
|
|
|
|
|
|
|
|
|
|
import (
|
2025-03-05 12:05:46 +00:00
|
|
|
"testing"
|
|
|
|
|
|
2025-01-24 16:12:25 +00:00
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
"queryorchestration/internal/document"
|
2025-12-11 14:25:51 +00:00
|
|
|
"queryorchestration/internal/fieldextraction"
|
2025-01-31 13:43:55 +00:00
|
|
|
"queryorchestration/internal/serviceconfig"
|
2025-12-11 14:25:51 +00:00
|
|
|
"queryorchestration/internal/test"
|
2025-01-24 16:12:25 +00:00
|
|
|
|
2025-03-04 15:51:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2025-01-24 16:12:25 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/pashagolub/pgxmock/v3"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
2025-12-11 14:25:51 +00:00
|
|
|
type TestConfig struct {
|
|
|
|
|
serviceconfig.BaseConfig
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-17 18:14:15 +00:00
|
|
|
func TestGetSummary(t *testing.T) {
|
2025-06-03 13:52:10 +00:00
|
|
|
ctx := t.Context()
|
2025-01-24 16:12:25 +00:00
|
|
|
|
|
|
|
|
pool, err := pgxmock.NewPool()
|
2025-03-04 15:51:03 +00:00
|
|
|
require.NoError(t, err)
|
2025-01-31 13:43:55 +00:00
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
2025-01-24 16:12:25 +00:00
|
|
|
|
2025-01-31 13:43:55 +00:00
|
|
|
svc := document.New(cfg)
|
2025-01-24 16:12:25 +00:00
|
|
|
|
2025-03-17 18:14:15 +00:00
|
|
|
doc := document.DocumentSummary{
|
2025-03-10 11:03:00 +00:00
|
|
|
ID: uuid.New(),
|
2025-04-02 18:50:03 +00:00
|
|
|
ClientID: "example",
|
2025-03-10 11:03:00 +00:00
|
|
|
Hash: "example_hash",
|
2025-01-24 16:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
2025-03-20 11:06:41 +00:00
|
|
|
pool.ExpectQuery("name: GetDocumentSummary :one").WithArgs(doc.ID).
|
2025-01-24 16:12:25 +00:00
|
|
|
WillReturnRows(
|
2025-03-10 11:03:00 +00:00
|
|
|
pgxmock.NewRows([]string{"id", "clientId", "hash"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(doc.ID, doc.ClientID, doc.Hash),
|
2025-01-24 16:12:25 +00:00
|
|
|
)
|
|
|
|
|
|
2025-03-17 18:14:15 +00:00
|
|
|
adoc, err := svc.GetSummary(ctx, doc.ID)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-03-17 18:14:15 +00:00
|
|
|
assert.Equal(t, &doc, adoc)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetExternal(t *testing.T) {
|
2025-06-03 13:52:10 +00:00
|
|
|
ctx := t.Context()
|
2025-03-17 18:14:15 +00:00
|
|
|
|
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
cfg := &serviceconfig.BaseConfig{}
|
|
|
|
|
cfg.DBPool = pool
|
|
|
|
|
cfg.DBQueries = repository.New(pool)
|
|
|
|
|
|
|
|
|
|
svc := document.New(cfg)
|
|
|
|
|
|
|
|
|
|
doc := document.DocumentExternal{
|
|
|
|
|
Id: uuid.New(),
|
|
|
|
|
ClientID: "externalID",
|
|
|
|
|
Hash: "example",
|
|
|
|
|
Fields: map[string]interface{}{
|
|
|
|
|
"json": "hello",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-02 18:50:03 +00:00
|
|
|
pool.ExpectQuery("name: GetDocumentExternal :one").WithArgs(doc.Id).
|
2025-03-17 18:14:15 +00:00
|
|
|
WillReturnRows(
|
|
|
|
|
pgxmock.NewRows([]string{"id", "clientId", "hash", "fields"}).
|
2025-03-20 11:06:41 +00:00
|
|
|
AddRow(doc.Id, "externalID", "example", []byte(`{"json": "hello"}`)),
|
2025-03-17 18:14:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
adoc, err := svc.GetExternal(ctx, doc.Id)
|
2025-03-19 11:54:14 +00:00
|
|
|
require.NoError(t, err)
|
2025-01-24 16:12:25 +00:00
|
|
|
assert.Equal(t, &doc, adoc)
|
|
|
|
|
}
|
2025-12-11 14:25:51 +00:00
|
|
|
|
|
|
|
|
func TestGetEnriched(t *testing.T) {
|
|
|
|
|
ctx := t.Context()
|
|
|
|
|
cfg := &TestConfig{}
|
|
|
|
|
test.CreateDB(t, cfg)
|
|
|
|
|
svc := document.New(cfg)
|
|
|
|
|
|
|
|
|
|
// Create test client
|
|
|
|
|
clientID := "test-client-enriched"
|
|
|
|
|
err := cfg.GetDBQueries().CreateClient(ctx, &repository.CreateClientParams{
|
|
|
|
|
Clientid: clientID,
|
|
|
|
|
Name: "Test Client Enriched",
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
t.Run("get enriched document without text record", func(t *testing.T) {
|
|
|
|
|
filename := "test-enriched.pdf"
|
|
|
|
|
originalPath := "/documents/test-enriched.pdf"
|
|
|
|
|
documentID, err := cfg.GetDBQueries().CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
|
|
|
Clientid: clientID,
|
|
|
|
|
Hash: "enrichedhash1",
|
|
|
|
|
Filename: &filename,
|
|
|
|
|
Originalpath: &originalPath,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Get enriched document without text record
|
|
|
|
|
result, err := svc.GetEnriched(ctx, documentID, false)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.NotNil(t, result)
|
|
|
|
|
assert.Equal(t, documentID, result.ID)
|
|
|
|
|
assert.Equal(t, clientID, result.ClientID)
|
|
|
|
|
assert.Equal(t, "enrichedhash1", result.Hash)
|
|
|
|
|
assert.False(t, result.HasTextRecord)
|
|
|
|
|
assert.Equal(t, filename, *result.Filename)
|
|
|
|
|
assert.Equal(t, originalPath, *result.OriginalPath)
|
|
|
|
|
assert.Nil(t, result.TextRecord)
|
|
|
|
|
assert.Empty(t, result.Labels)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("get enriched document with text record requested but not present", func(t *testing.T) {
|
|
|
|
|
filename := "test-enriched-no-text.pdf"
|
|
|
|
|
documentID, err := cfg.GetDBQueries().CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
|
|
|
Clientid: clientID,
|
|
|
|
|
Hash: "enrichedhash2",
|
|
|
|
|
Filename: &filename,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Get enriched document with text record requested
|
|
|
|
|
result, err := svc.GetEnriched(ctx, documentID, true)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.NotNil(t, result)
|
|
|
|
|
assert.Equal(t, documentID, result.ID)
|
|
|
|
|
assert.False(t, result.HasTextRecord)
|
|
|
|
|
assert.Nil(t, result.TextRecord) // No text record exists
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("get enriched document with text record", func(t *testing.T) {
|
|
|
|
|
filename := "test-enriched-with-text.pdf"
|
|
|
|
|
documentID, err := cfg.GetDBQueries().CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
|
|
|
Clientid: clientID,
|
|
|
|
|
Hash: "enrichedhash3",
|
|
|
|
|
Filename: &filename,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Create field extraction for this document
|
|
|
|
|
fieldSvc := fieldextraction.New(cfg)
|
|
|
|
|
contractTitle := "Test Contract"
|
|
|
|
|
input := &fieldextraction.CreateFieldExtractionInput{
|
|
|
|
|
DocumentID: documentID,
|
|
|
|
|
SingleFields: &repository.AddFieldExtractionParams{
|
|
|
|
|
Documentid: documentID,
|
|
|
|
|
Filename: &filename,
|
|
|
|
|
Contracttitle: &contractTitle,
|
|
|
|
|
Createdby: "user123",
|
|
|
|
|
},
|
|
|
|
|
ArrayFields: []*repository.AddFieldExtractionArrayFieldParams{},
|
|
|
|
|
}
|
|
|
|
|
_, err = fieldSvc.CreateFieldExtraction(ctx, input)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Get enriched document with text record
|
|
|
|
|
result, err := svc.GetEnriched(ctx, documentID, true)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.NotNil(t, result)
|
|
|
|
|
assert.Equal(t, documentID, result.ID)
|
|
|
|
|
assert.True(t, result.HasTextRecord)
|
|
|
|
|
assert.NotNil(t, result.TextRecord)
|
|
|
|
|
assert.Equal(t, documentID, result.TextRecord.DocumentID)
|
|
|
|
|
assert.Equal(t, int32(1), result.TextRecord.Version)
|
|
|
|
|
assert.Equal(t, "user123", result.TextRecord.CreatedBy)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("get enriched document with text record but not requesting it", func(t *testing.T) {
|
|
|
|
|
filename := "test-enriched-skip-text.pdf"
|
|
|
|
|
documentID, err := cfg.GetDBQueries().CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
|
|
|
Clientid: clientID,
|
|
|
|
|
Hash: "enrichedhash4",
|
|
|
|
|
Filename: &filename,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Create field extraction
|
|
|
|
|
fieldSvc := fieldextraction.New(cfg)
|
|
|
|
|
contractTitle := "Test Contract Skip"
|
|
|
|
|
input := &fieldextraction.CreateFieldExtractionInput{
|
|
|
|
|
DocumentID: documentID,
|
|
|
|
|
SingleFields: &repository.AddFieldExtractionParams{
|
|
|
|
|
Documentid: documentID,
|
|
|
|
|
Filename: &filename,
|
|
|
|
|
Contracttitle: &contractTitle,
|
|
|
|
|
Createdby: "user456",
|
|
|
|
|
},
|
|
|
|
|
ArrayFields: []*repository.AddFieldExtractionArrayFieldParams{},
|
|
|
|
|
}
|
|
|
|
|
_, err = fieldSvc.CreateFieldExtraction(ctx, input)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Get enriched document without requesting text record
|
|
|
|
|
result, err := svc.GetEnriched(ctx, documentID, false)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.NotNil(t, result)
|
|
|
|
|
assert.True(t, result.HasTextRecord) // Has text record
|
|
|
|
|
assert.Nil(t, result.TextRecord) // But not fetched
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("return error for non-existent document", func(t *testing.T) {
|
|
|
|
|
nonExistentID := uuid.New()
|
|
|
|
|
_, err := svc.GetEnriched(ctx, nonExistentID, false)
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
})
|
|
|
|
|
}
|