Skip to content

Commit 28a01eb

Browse files
committed
bumped coverage
1 parent 9ac3549 commit 28a01eb

4 files changed

Lines changed: 606 additions & 0 deletions

File tree

datamodel/high/base/schema_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,3 +1870,40 @@ func TestSchema_RenderInlineWithContext_Error(t *testing.T) {
18701870
assert.Nil(t, result)
18711871
assert.Contains(t, err.Error(), "circular reference")
18721872
}
1873+
1874+
// TestNewSchema_Id tests that the $id field is correctly mapped from low to high level
1875+
func TestNewSchema_Id(t *testing.T) {
1876+
yml := `type: object
1877+
$id: "https://example.com/schemas/pet.json"
1878+
description: A pet schema`
1879+
1880+
var idxNode yaml.Node
1881+
_ = yaml.Unmarshal([]byte(yml), &idxNode)
1882+
1883+
var lowSch lowbase.Schema
1884+
_ = low.BuildModel(idxNode.Content[0], &lowSch)
1885+
_ = lowSch.Build(context.Background(), idxNode.Content[0], nil)
1886+
1887+
highSch := NewSchema(&lowSch)
1888+
1889+
assert.Equal(t, "https://example.com/schemas/pet.json", highSch.Id)
1890+
assert.Equal(t, "object", highSch.Type[0])
1891+
assert.Equal(t, "A pet schema", highSch.Description)
1892+
}
1893+
1894+
// TestNewSchema_Id_Empty tests that empty $id results in empty string
1895+
func TestNewSchema_Id_Empty(t *testing.T) {
1896+
yml := `type: object
1897+
description: A schema without $id`
1898+
1899+
var idxNode yaml.Node
1900+
_ = yaml.Unmarshal([]byte(yml), &idxNode)
1901+
1902+
var lowSch lowbase.Schema
1903+
_ = low.BuildModel(idxNode.Content[0], &lowSch)
1904+
_ = lowSch.Build(context.Background(), idxNode.Content[0], nil)
1905+
1906+
highSch := NewSchema(&lowSch)
1907+
1908+
assert.Equal(t, "", highSch.Id)
1909+
}

datamodel/low/base/schema_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2675,3 +2675,81 @@ func TestSchemaDynamicValue_Hash_IsB(t *testing.T) {
26752675
assert.False(t, value.IsA())
26762676
assert.True(t, value.IsB())
26772677
}
2678+
2679+
// TestSchema_Id tests that the $id field is correctly extracted and included in the hash
2680+
func TestSchema_Id(t *testing.T) {
2681+
yml := `type: object
2682+
$id: "https://example.com/schemas/pet.json"
2683+
description: A pet schema`
2684+
2685+
var idxNode yaml.Node
2686+
_ = yaml.Unmarshal([]byte(yml), &idxNode)
2687+
2688+
var sch Schema
2689+
err := low.BuildModel(idxNode.Content[0], &sch)
2690+
assert.NoError(t, err)
2691+
2692+
err = sch.Build(context.Background(), idxNode.Content[0], nil)
2693+
assert.NoError(t, err)
2694+
2695+
assert.Equal(t, "https://example.com/schemas/pet.json", sch.Id.Value)
2696+
assert.NotNil(t, sch.Id.KeyNode)
2697+
assert.NotNil(t, sch.Id.ValueNode)
2698+
}
2699+
2700+
// TestSchema_Id_Hash tests that $id is included in the schema hash
2701+
func TestSchema_Id_Hash(t *testing.T) {
2702+
yml1 := `type: object
2703+
$id: "https://example.com/schemas/a.json"
2704+
description: Schema A`
2705+
2706+
yml2 := `type: object
2707+
$id: "https://example.com/schemas/b.json"
2708+
description: Schema A`
2709+
2710+
yml3 := `type: object
2711+
description: Schema A`
2712+
2713+
var node1, node2, node3 yaml.Node
2714+
_ = yaml.Unmarshal([]byte(yml1), &node1)
2715+
_ = yaml.Unmarshal([]byte(yml2), &node2)
2716+
_ = yaml.Unmarshal([]byte(yml3), &node3)
2717+
2718+
var sch1, sch2, sch3 Schema
2719+
_ = low.BuildModel(node1.Content[0], &sch1)
2720+
_ = sch1.Build(context.Background(), node1.Content[0], nil)
2721+
2722+
_ = low.BuildModel(node2.Content[0], &sch2)
2723+
_ = sch2.Build(context.Background(), node2.Content[0], nil)
2724+
2725+
_ = low.BuildModel(node3.Content[0], &sch3)
2726+
_ = sch3.Build(context.Background(), node3.Content[0], nil)
2727+
2728+
hash1 := sch1.Hash()
2729+
hash2 := sch2.Hash()
2730+
hash3 := sch3.Hash()
2731+
2732+
// Different $id values should produce different hashes
2733+
assert.NotEqual(t, hash1, hash2)
2734+
// Schema without $id should differ from schema with $id
2735+
assert.NotEqual(t, hash1, hash3)
2736+
assert.NotEqual(t, hash2, hash3)
2737+
}
2738+
2739+
// TestSchema_Id_Empty tests that empty $id is not set
2740+
func TestSchema_Id_Empty(t *testing.T) {
2741+
yml := `type: object
2742+
description: A schema without $id`
2743+
2744+
var idxNode yaml.Node
2745+
_ = yaml.Unmarshal([]byte(yml), &idxNode)
2746+
2747+
var sch Schema
2748+
err := low.BuildModel(idxNode.Content[0], &sch)
2749+
assert.NoError(t, err)
2750+
2751+
err = sch.Build(context.Background(), idxNode.Content[0], nil)
2752+
assert.NoError(t, err)
2753+
2754+
assert.True(t, sch.Id.IsEmpty())
2755+
}

0 commit comments

Comments
 (0)