forked from holochain/holochain-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry_test.go
103 lines (91 loc) · 2.26 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
package holochain
import (
"bytes"
"encoding/json"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestGob(t *testing.T) {
g := GobEntry{C: mkTestHeader("myData")}
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 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"]
}`
ed := EntryDef{Schema: "schema_profile.json"}
if err := writeFile(d, ed.Schema, []byte(schema)); err != nil {
panic(err)
}
Convey("it should validate JSON entries", t, func() {
err := ed.BuildJSONSchemaValidator(d)
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 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))
})
}