Skip to content

Commit

Permalink
more json schema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Apr 5, 2024
1 parent 428c674 commit c1136da
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ require github.com/xeipuuv/gojsonschema v1.2.0
require (
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -12,3 +13,6 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
34 changes: 34 additions & 0 deletions schemautils/schemautils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"commonmeta/metadata"
"commonmeta/schemautils"
"encoding/json"

"fmt"
"log"
"os"
"testing"

"sigs.k8s.io/yaml"
)

func TestJSONSchemaErrors(t *testing.T) {
Expand Down Expand Up @@ -60,8 +63,11 @@ func TestJSONSchemaErrorsTestdata(t *testing.T) {
}

testCases := []testCase{
{meta: "journal_article.commonmeta.json", schema: "commonmeta_v0.12", want: 2},
{meta: "citeproc.json", schema: "csl-data", want: 0},
{meta: "datacite.json", schema: "datacite-v4.5", want: 3},
{meta: "datacite-instrument.json", schema: "datacite-v4.5", want: 27},
{meta: "datacite_software_version.json", schema: "datacite-v4.5", want: 7},
}
for _, tc := range testCases {
data, err := os.ReadFile("../testdata/" + tc.meta)
Expand All @@ -75,3 +81,31 @@ func TestJSONSchemaErrorsTestdata(t *testing.T) {
}
}
}

func TestJSONSchemaErrorsTestdataYAML(t *testing.T) {
t.Parallel()
type testCase struct {
meta string
schema string
want int
}

testCases := []testCase{
{meta: "CITATION.cff", schema: "cff_v1.2.0", want: 0},
}
for _, tc := range testCases {
data, err := os.ReadFile("../testdata/" + tc.meta)
if err != nil {
fmt.Print(err)
}
YAMLdata, err := yaml.YAMLToJSON(data)
if err != nil {
fmt.Print(err)
}
result := schemautils.JSONSchemaErrors(YAMLdata, tc.schema)
got := len(result.Errors())
if tc.want != got {
t.Errorf("want %d, got %d", tc.want, got)
}
}
}

0 comments on commit c1136da

Please sign in to comment.