-
Notifications
You must be signed in to change notification settings - Fork 24
/
feed_test.go
77 lines (64 loc) · 1.9 KB
/
feed_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package feeds
import (
"fmt"
"testing"
"time"
"github.com/xeipuuv/gojsonschema"
)
const schemaPath = "../../package.schema.json"
type extendPackage struct {
Package
NonConformingField string `json:"non_conforming_field"`
}
var (
schemaLoader = gojsonschema.NewReferenceLoader("file://" + schemaPath)
dummyPackage = Package{
Name: "foobarpackage",
Version: "1.0.0",
CreatedDate: time.Now().UTC(),
Type: "npm",
SchemaVer: schemaVer,
}
)
func TestValidSchema(t *testing.T) {
t.Parallel()
validPackage := gojsonschema.NewGoLoader(dummyPackage)
result, err := gojsonschema.Validate(schemaLoader, validPackage)
if err != nil {
t.Fatal(err)
}
if result.Valid() != true {
out := "The Package json is not valid against the current schema. see errors :\n"
for _, desc := range result.Errors() {
out += fmt.Sprintf("- %s\n", desc)
}
t.Fatal(out)
}
}
func TestInvalidSchema(t *testing.T) {
t.Parallel()
// The Schema defines that additional properties are not valid, ensure enforcement
// against an extra struct field. If an extra field is added, the SchemVer minor should
// be incremented to advertise an additive change.
invalidPackageField := extendPackage{dummyPackage, "extrafield"}
invalidField := gojsonschema.NewGoLoader(invalidPackageField)
result, err := gojsonschema.Validate(schemaLoader, invalidField)
if err != nil {
t.Fatal(err)
}
if result.Valid() {
t.Fatalf("Non-conformant extra field incorrectly validated")
}
// The Schema defines a required pattern for the schema_ver, ensure enforcement against
// empty string.
invalidPackageFormat := dummyPackage
invalidPackageFormat.SchemaVer = ""
invalidFormat := gojsonschema.NewGoLoader(invalidPackageFormat)
result, err = gojsonschema.Validate(schemaLoader, invalidFormat)
if err != nil {
t.Fatal(err)
}
if result.Valid() {
t.Fatalf("Non-conformant field format incorrectly validated")
}
}