Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions document/bridge/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.kenn.io/docbank/document"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -89,6 +90,117 @@ func TestBridgeContractNormativeDocumentsAreStrictAndVersioned(t *testing.T) {
contractObject(t, schema, "$defs", "omission", "properties", "locator")["$ref"])
}

func TestBridgeContractNormativeDocumentsSourceEvidenceBoundsMatchValidator(t *testing.T) {
var schema map[string]any
require.NoError(t, json.Unmarshal(sourceEvidenceSchema, &schema))

tests := []struct {
name string
path []string
want int
setItems func(*document.SourceEvidenceV1, int)
}{
{
name: "heading_path", path: []string{"$defs", "unit", "properties", "heading_path"},
want: 64,
setItems: func(source *document.SourceEvidenceV1, count int) {
source.Units[0].HeadingPath = make([]string, count)
for index := range source.Units[0].HeadingPath {
source.Units[0].HeadingPath[index] = "heading"
}
},
},
{
name: "geometry boxes", path: []string{"$defs", "geometry", "properties", "boxes"},
want: 10_000,
setItems: func(source *document.SourceEvidenceV1, count int) {
geometry := source.Units[0].Regions[0].Geometry
geometry.Boxes = make([]document.EvidenceBoxV1, count)
for index := range geometry.Boxes {
geometry.Boxes[index] = document.EvidenceBoxV1{Left: 1, Top: 1, Right: 2, Bottom: 2}
}
},
},
{
name: "geometry polygons", path: []string{"$defs", "geometry", "properties", "polygons"},
want: 10_000,
setItems: func(source *document.SourceEvidenceV1, count int) {
geometry := source.Units[0].Regions[0].Geometry
geometry.Polygons = make([]document.EvidencePolygonV1, count)
for index := range geometry.Polygons {
geometry.Polygons[index] = document.EvidencePolygonV1{Points: []document.EvidencePointV1{
{X: 1, Y: 1}, {X: 2, Y: 1}, {X: 1, Y: 2},
}}
}
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.want, contractMaxItems(t, schema, test.path...))

for _, boundary := range []struct {
name string
count int
wantErr bool
}{
{name: "at limit", count: test.want},
{name: "over limit", count: test.want + 1, wantErr: true},
} {
t.Run(boundary.name, func(t *testing.T) {
source := contractSourceEvidence()
test.setItems(&source, boundary.count)
err := document.ValidateSourceEvidenceV1(source)
if boundary.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}
})
}
}

func contractMaxItems(t *testing.T, schema map[string]any, path ...string) int {
t.Helper()
value, ok := contractObject(t, schema, path...)["maxItems"].(float64)
require.True(t, ok, "contract path %v has no numeric maxItems", path)
return int(value)
}

func contractSourceEvidence() document.SourceEvidenceV1 {
return document.SourceEvidenceV1{
ContractVersion: document.SourceEvidenceContractV1,
Completeness: document.EvidenceComplete,
Family: "pdf",
UnitKind: document.EvidenceUnitPage,
Units: []document.SourceEvidenceUnitV1{{
Order: 0,
Locator: document.SourceEvidenceLocatorV1{
Kind: document.EvidenceLocatorPage, IndexOrigin: document.EvidenceIndexOriginOne,
Start: 1, End: 1,
},
Regions: []document.SourceEvidenceRegionV1{{
Kind: document.EvidenceRegionParagraph, Order: 0,
ProviderID: "synthetic-region",
TextRange: document.EvidenceTextRangeV1{Start: 0, End: 1},
Geometry: &document.SourceEvidenceGeometryV1{
CoordinateOrigin: document.EvidenceCoordinateTopLeft,
CoordinateSpace: document.EvidenceCoordinatePage,
Height: 100,
Orientation: 0,
Scale: 1,
Unit: document.EvidenceGeometryPixel,
Width: 100,
},
}},
Text: "synthetic evidence",
}},
}
}

func contractObject(t *testing.T, value any, path ...string) map[string]any {
t.Helper()
current := value
Expand Down
6 changes: 3 additions & 3 deletions document/bridge/source-evidence-v1.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@
"type": "object", "additionalProperties": false,
"required": ["coordinate_origin", "coordinate_space", "height", "orientation", "scale", "unit", "width"],
"properties": {
"boxes": {"type": "array", "maxItems": 1000000, "items": {"$ref": "#/$defs/box"}},
"boxes": {"type": "array", "maxItems": 10000, "items": {"$ref": "#/$defs/box"}},
"coordinate_origin": {"enum": ["bottom_left", "top_left"]},
"coordinate_space": {"enum": ["image", "page", "unit"]},
"height": {"type": "integer", "minimum": 0},
"orientation": {"type": "integer"},
"polygons": {"type": "array", "maxItems": 1000000, "items": {"$ref": "#/$defs/polygon"}},
"polygons": {"type": "array", "maxItems": 10000, "items": {"$ref": "#/$defs/polygon"}},
"scale": {"type": "integer", "minimum": 1},
"unit": {"enum": ["normalized", "pixel", "point"]},
"width": {"type": "integer", "minimum": 0}
Expand Down Expand Up @@ -141,7 +141,7 @@
"required": ["locator", "order", "text"],
"properties": {
"confidence": {"$ref": "#/$defs/confidence"},
"heading_path": {"type": "array", "maxItems": 256, "items": {"type": "string", "minLength": 1}},
"heading_path": {"type": "array", "maxItems": 64, "items": {"type": "string", "minLength": 1}},
"locator": {"$ref": "#/$defs/locator"},
"omissions": {"type": "array", "maxItems": 100000, "items": {"$ref": "#/$defs/omission"}},
"order": {"type": "integer", "minimum": 0},
Expand Down