This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathentry_test.go
130 lines (113 loc) · 3.07 KB
/
entry_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package holochain
import (
"bytes"
"encoding/json"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"path/filepath"
"testing"
)
func TestEntryConstants(t *testing.T) {
Convey("entry constants should have the right values", t, func() {
So(SysEntryTypePrefix, ShouldEqual, "%")
So(VirtualEntryTypePrefix, ShouldEqual, "%%")
So(DataFormatJSON, ShouldEqual, "json")
So(DataFormatString, ShouldEqual, "string")
So(DataFormatRawJS, ShouldEqual, "js")
So(DataFormatRawZygo, ShouldEqual, "zygo")
So(Public, ShouldEqual, "public")
So(Partial, ShouldEqual, "partial")
So(Private, ShouldEqual, "private")
})
}
func TestGob(t *testing.T) {
g := GobEntry{C: mkTestHeader("evenNumbers")}
v, err := g.Marshal()
Convey("it should encode", t, func() {
So(err, ShouldBeNil)
})
var g2 GobEntry
err = g2.Unmarshal(v)
Convey("it should decode", t, func() {
sg1 := fmt.Sprintf("%v", g)
sg2 := fmt.Sprintf("%v", g)
So(err, ShouldBeNil)
So(sg1, ShouldEqual, sg2)
})
}
func TestJSONEntry(t *testing.T) {
/* Not yet implemented or used
g := JSONEntry{C:Config{Port:8888}}
v,err := g.Marshal()
ExpectNoErr(t,err)
var g2 JSONEntry
err = g2.Unmarshal(v)
ExpectNoErr(t,err)
if g2!=g {t.Error("expected JSON match! "+fmt.Sprintf("%v",g)+" "+fmt.Sprintf("%v",g2))}
*/
}
func testValidateJSON(ed EntryDef, err error) {
So(err, ShouldBeNil)
So(ed.validator, ShouldNotBeNil)
profile := `{"firstName":"Eric","lastName":"H-B"}`
var input interface{}
if err = json.Unmarshal([]byte(profile), &input); err != nil {
panic(err)
}
err = ed.validator.Validate(input)
So(err, ShouldBeNil)
profile = `{"firstName":"Eric"}`
if err = json.Unmarshal([]byte(profile), &input); err != nil {
panic(err)
}
err = ed.validator.Validate(input)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "validator schema_profile.json failed: object property 'lastName' is required")
}
func TestJSONSchemaValidator(t *testing.T) {
d, _ := setupTestService()
defer CleanupTestDir(d)
schema := `{
"title": "Profile Schema",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": ["firstName", "lastName"]
}`
edf := EntryDefFile{SchemaFile: "schema_profile.json"}
if err := WriteFile([]byte(schema), d, edf.SchemaFile); err != nil {
panic(err)
}
ed := EntryDef{Name: "schema_profile.json"}
Convey("it should validate JSON entries from schema file", t, func() {
err := ed.BuildJSONSchemaValidator(filepath.Join(d, ed.Name))
testValidateJSON(ed, err)
})
Convey("it should validate JSON entries from string", t, func() {
err := ed.BuildJSONSchemaValidatorFromString(schema)
testValidateJSON(ed, err)
})
}
func TestMarshalEntry(t *testing.T) {
e := GobEntry{C: "some data"}
Convey("it should round-trip", t, func() {
var b bytes.Buffer
err := MarshalEntry(&b, &e)
So(err, ShouldBeNil)
var ne Entry
ne, err = UnmarshalEntry(&b)
So(err, ShouldBeNil)
So(fmt.Sprintf("%v", ne), ShouldEqual, fmt.Sprintf("%v", &e))
})
}