github.com/holochain/holochain-proto@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/entry_test.go (about) 1 package holochain 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 . "github.com/smartystreets/goconvey/convey" 8 9 "path/filepath" 10 "testing" 11 ) 12 13 func TestEntryConstants(t *testing.T) { 14 Convey("entry constants should have the right values", t, func() { 15 So(SysEntryTypePrefix, ShouldEqual, "%") 16 So(VirtualEntryTypePrefix, ShouldEqual, "%%") 17 So(DataFormatJSON, ShouldEqual, "json") 18 So(DataFormatString, ShouldEqual, "string") 19 So(DataFormatRawJS, ShouldEqual, "js") 20 So(DataFormatRawZygo, ShouldEqual, "zygo") 21 So(Public, ShouldEqual, "public") 22 So(Partial, ShouldEqual, "partial") 23 So(Private, ShouldEqual, "private") 24 }) 25 } 26 27 func TestGob(t *testing.T) { 28 g := GobEntry{C: mkTestHeader("evenNumbers")} 29 v, err := g.Marshal() 30 Convey("it should encode", t, func() { 31 So(err, ShouldBeNil) 32 }) 33 var g2 GobEntry 34 err = g2.Unmarshal(v) 35 Convey("it should decode", t, func() { 36 sg1 := fmt.Sprintf("%v", g) 37 sg2 := fmt.Sprintf("%v", g) 38 So(err, ShouldBeNil) 39 So(sg1, ShouldEqual, sg2) 40 }) 41 } 42 43 func TestJSONEntry(t *testing.T) { 44 /* Not yet implemented or used 45 g := JSONEntry{C:Config{Port:8888}} 46 v,err := g.Marshal() 47 ExpectNoErr(t,err) 48 var g2 JSONEntry 49 err = g2.Unmarshal(v) 50 ExpectNoErr(t,err) 51 if g2!=g {t.Error("expected JSON match! "+fmt.Sprintf("%v",g)+" "+fmt.Sprintf("%v",g2))} 52 */ 53 } 54 55 func testValidateJSON(ed EntryDef, err error) { 56 So(err, ShouldBeNil) 57 So(ed.validator, ShouldNotBeNil) 58 profile := `{"firstName":"Eric","lastName":"H-B"}` 59 60 var input interface{} 61 if err = json.Unmarshal([]byte(profile), &input); err != nil { 62 panic(err) 63 } 64 err = ed.validator.Validate(input) 65 So(err, ShouldBeNil) 66 profile = `{"firstName":"Eric"}` 67 if err = json.Unmarshal([]byte(profile), &input); err != nil { 68 panic(err) 69 } 70 71 err = ed.validator.Validate(input) 72 So(err, ShouldNotBeNil) 73 So(err.Error(), ShouldEqual, "validator schema_profile.json failed: object property 'lastName' is required") 74 } 75 76 func TestJSONSchemaValidator(t *testing.T) { 77 d, _ := setupTestService() 78 defer CleanupTestDir(d) 79 80 schema := `{ 81 "title": "Profile Schema", 82 "type": "object", 83 "properties": { 84 "firstName": { 85 "type": "string" 86 }, 87 "lastName": { 88 "type": "string" 89 }, 90 "age": { 91 "description": "Age in years", 92 "type": "integer", 93 "minimum": 0 94 } 95 }, 96 "required": ["firstName", "lastName"] 97 }` 98 edf := EntryDefFile{SchemaFile: "schema_profile.json"} 99 100 if err := WriteFile([]byte(schema), d, edf.SchemaFile); err != nil { 101 panic(err) 102 } 103 104 ed := EntryDef{Name: "schema_profile.json"} 105 106 Convey("it should validate JSON entries from schema file", t, func() { 107 err := ed.BuildJSONSchemaValidator(filepath.Join(d, ed.Name)) 108 testValidateJSON(ed, err) 109 }) 110 111 Convey("it should validate JSON entries from string", t, func() { 112 err := ed.BuildJSONSchemaValidatorFromString(schema) 113 testValidateJSON(ed, err) 114 }) 115 } 116 117 func TestMarshalEntry(t *testing.T) { 118 119 e := GobEntry{C: "some data"} 120 121 Convey("it should round-trip", t, func() { 122 var b bytes.Buffer 123 err := MarshalEntry(&b, &e) 124 So(err, ShouldBeNil) 125 var ne Entry 126 ne, err = UnmarshalEntry(&b) 127 So(err, ShouldBeNil) 128 So(fmt.Sprintf("%v", ne), ShouldEqual, fmt.Sprintf("%v", &e)) 129 }) 130 }