Merged in feature/textExtractionsPart2 (pull request #193)
Continue finishing the parts of the text extraction plan * add missing fields
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"queryorchestration/internal/test"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/labstack/echo/v4"
|
||||
openapi_types "github.com/oapi-codegen/runtime/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -639,6 +640,192 @@ func TestCreateFieldExtraction_VersionIncrement(t *testing.T) {
|
||||
assert.Equal(t, "Version 2", *currentResp.SingleFields.ContractTitle)
|
||||
}
|
||||
|
||||
func TestGetFieldExtractionByVersion_Success(t *testing.T) {
|
||||
ctrl, cfg, documentID, _ := setupFieldExtractionsTestController(t)
|
||||
ctx := t.Context()
|
||||
|
||||
// Create multiple versions with dates for coverage
|
||||
svc := fieldextraction.New(cfg)
|
||||
filename := "test.pdf"
|
||||
|
||||
for i := 1; i <= 3; i++ {
|
||||
contractTitle := fmt.Sprintf("Contract Version %d", i)
|
||||
clientName := fmt.Sprintf("Client %d", i)
|
||||
// Include dates to test date conversion coverage
|
||||
effectiveDate := pgtype.Date{Time: time.Date(2024, 1, i, 0, 0, 0, 0, time.UTC), Valid: true}
|
||||
terminationDate := pgtype.Date{Time: time.Date(2024, 12, i, 0, 0, 0, 0, time.UTC), Valid: true}
|
||||
_, err := svc.CreateFieldExtraction(ctx, &fieldextraction.CreateFieldExtractionInput{
|
||||
DocumentID: documentID,
|
||||
SingleFields: &repository.AddFieldExtractionParams{
|
||||
Documentid: documentID,
|
||||
Filename: &filename,
|
||||
Contracttitle: &contractTitle,
|
||||
Clientname: &clientName,
|
||||
Aaretederivedeffectivedt: effectiveDate,
|
||||
Aaretederivedterminationdt: terminationDate,
|
||||
Createdby: fmt.Sprintf("user%d@example.com", i),
|
||||
},
|
||||
ArrayFields: []*repository.AddFieldExtractionArrayFieldParams{
|
||||
{
|
||||
Exhibittitle: ptr(fmt.Sprintf("Exhibit V%d", i)),
|
||||
},
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Test retrieving version 2
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/field-extractions/version?documentId=%s&version=2", documentID), nil)
|
||||
rec := httptest.NewRecorder()
|
||||
c := e.NewContext(req, rec)
|
||||
|
||||
params := queryapi.GetFieldExtractionByVersionParams{
|
||||
DocumentId: queryapi.DocumentID(documentID),
|
||||
Version: 2,
|
||||
}
|
||||
|
||||
err := ctrl.GetFieldExtractionByVersion(c, params)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
// Parse response
|
||||
var resp queryapi.FieldExtractionResponse
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &resp)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify we got version 2
|
||||
assert.Equal(t, documentID, uuid.UUID(resp.DocumentId))
|
||||
assert.Equal(t, int32(2), resp.Version)
|
||||
assert.Equal(t, "user2@example.com", string(resp.CreatedBy))
|
||||
assert.Equal(t, "Contract Version 2", *resp.SingleFields.ContractTitle)
|
||||
assert.Equal(t, "Client 2", *resp.SingleFields.ClientName)
|
||||
// Verify dates are returned correctly
|
||||
require.NotNil(t, resp.SingleFields.AareteDerivedEffectiveDt, "Expected effective date in response")
|
||||
require.NotNil(t, resp.SingleFields.AareteDerivedTerminationDt, "Expected termination date in response")
|
||||
assert.Equal(t, 2024, resp.SingleFields.AareteDerivedEffectiveDt.Year())
|
||||
assert.Equal(t, time.January, resp.SingleFields.AareteDerivedEffectiveDt.Month())
|
||||
assert.Equal(t, 2, resp.SingleFields.AareteDerivedEffectiveDt.Day())
|
||||
require.Len(t, resp.ArrayFields, 1)
|
||||
assert.Equal(t, "Exhibit V2", *resp.ArrayFields[0].ExhibitTitle)
|
||||
|
||||
// Also test retrieving version 1
|
||||
req = httptest.NewRequest(http.MethodGet, fmt.Sprintf("/field-extractions/version?documentId=%s&version=1", documentID), nil)
|
||||
rec = httptest.NewRecorder()
|
||||
c = e.NewContext(req, rec)
|
||||
|
||||
params.Version = 1
|
||||
err = ctrl.GetFieldExtractionByVersion(c, params)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
err = json.Unmarshal(rec.Body.Bytes(), &resp)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, int32(1), resp.Version)
|
||||
assert.Equal(t, "Contract Version 1", *resp.SingleFields.ContractTitle)
|
||||
assert.Equal(t, "Exhibit V1", *resp.ArrayFields[0].ExhibitTitle)
|
||||
}
|
||||
|
||||
func TestGetFieldExtractionByVersion_NotFound(t *testing.T) {
|
||||
ctrl, cfg, documentID, _ := setupFieldExtractionsTestController(t)
|
||||
ctx := t.Context()
|
||||
|
||||
// Create only one version
|
||||
svc := fieldextraction.New(cfg)
|
||||
filename := "test.pdf"
|
||||
contractTitle := "Test Contract"
|
||||
_, err := svc.CreateFieldExtraction(ctx, &fieldextraction.CreateFieldExtractionInput{
|
||||
DocumentID: documentID,
|
||||
SingleFields: &repository.AddFieldExtractionParams{
|
||||
Documentid: documentID,
|
||||
Filename: &filename,
|
||||
Contracttitle: &contractTitle,
|
||||
Createdby: "user@example.com",
|
||||
},
|
||||
ArrayFields: []*repository.AddFieldExtractionArrayFieldParams{},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Try to get version 5 (which doesn't exist)
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/field-extractions/version?documentId=%s&version=5", documentID), nil)
|
||||
rec := httptest.NewRecorder()
|
||||
c := e.NewContext(req, rec)
|
||||
|
||||
params := queryapi.GetFieldExtractionByVersionParams{
|
||||
DocumentId: queryapi.DocumentID(documentID),
|
||||
Version: 5,
|
||||
}
|
||||
|
||||
err = ctrl.GetFieldExtractionByVersion(c, params)
|
||||
require.Error(t, err)
|
||||
httpErr, ok := err.(*echo.HTTPError)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, http.StatusNotFound, httpErr.Code)
|
||||
}
|
||||
|
||||
func TestGetFieldExtractionByVersion_InvalidDocumentID(t *testing.T) {
|
||||
ctrl, _, _, _ := setupFieldExtractionsTestController(t)
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodGet, "/field-extractions/version?documentId=&version=1", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
c := e.NewContext(req, rec)
|
||||
|
||||
params := queryapi.GetFieldExtractionByVersionParams{
|
||||
DocumentId: queryapi.DocumentID(uuid.Nil),
|
||||
Version: 1,
|
||||
}
|
||||
|
||||
err := ctrl.GetFieldExtractionByVersion(c, params)
|
||||
require.Error(t, err)
|
||||
httpErr, ok := err.(*echo.HTTPError)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, http.StatusBadRequest, httpErr.Code)
|
||||
}
|
||||
|
||||
func TestGetFieldExtractionByVersion_InvalidVersion(t *testing.T) {
|
||||
ctrl, _, documentID, _ := setupFieldExtractionsTestController(t)
|
||||
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/field-extractions/version?documentId=%s&version=0", documentID), nil)
|
||||
rec := httptest.NewRecorder()
|
||||
c := e.NewContext(req, rec)
|
||||
|
||||
params := queryapi.GetFieldExtractionByVersionParams{
|
||||
DocumentId: queryapi.DocumentID(documentID),
|
||||
Version: 0, // Invalid: version must be >= 1
|
||||
}
|
||||
|
||||
err := ctrl.GetFieldExtractionByVersion(c, params)
|
||||
require.Error(t, err)
|
||||
httpErr, ok := err.(*echo.HTTPError)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, http.StatusBadRequest, httpErr.Code)
|
||||
}
|
||||
|
||||
func TestGetFieldExtractionByVersion_NoExtractions(t *testing.T) {
|
||||
ctrl, _, documentID, _ := setupFieldExtractionsTestController(t)
|
||||
|
||||
// Document exists but has no field extractions
|
||||
e := echo.New()
|
||||
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/field-extractions/version?documentId=%s&version=1", documentID), nil)
|
||||
rec := httptest.NewRecorder()
|
||||
c := e.NewContext(req, rec)
|
||||
|
||||
params := queryapi.GetFieldExtractionByVersionParams{
|
||||
DocumentId: queryapi.DocumentID(documentID),
|
||||
Version: 1,
|
||||
}
|
||||
|
||||
err := ctrl.GetFieldExtractionByVersion(c, params)
|
||||
require.Error(t, err)
|
||||
httpErr, ok := err.(*echo.HTTPError)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, http.StatusNotFound, httpErr.Code)
|
||||
}
|
||||
|
||||
// Helper function to create string pointer
|
||||
func ptr(s string) *string {
|
||||
return &s
|
||||
|
||||
Reference in New Issue
Block a user