fork of indigo with slightly nicer lexgen
at main 2.1 kB view raw
1package data 2 3import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/bluesky-social/indigo/atproto/syntax" 8 9 "github.com/ipfs/go-cid" 10 "github.com/stretchr/testify/assert" 11) 12 13func TestSimpleValidation(t *testing.T) { 14 assert := assert.New(t) 15 16 s := "a string" 17 assert.NoError(Validate(map[string]interface{}{ 18 "a": 5, 19 "b": 123, 20 "c": s, 21 "d": &s, 22 })) 23 assert.NoError(Validate(map[string]interface{}{ 24 "$type": "com.example.thing", 25 "a": 5, 26 })) 27 assert.Error(Validate(map[string]interface{}{ 28 "$type": 123, 29 "a": 5, 30 })) 31 assert.Error(Validate(map[string]interface{}{ 32 "$type": "", 33 "a": 5, 34 })) 35} 36 37func TestSyntaxSerialize(t *testing.T) { 38 assert := assert.New(t) 39 40 atid, err := syntax.ParseAtIdentifier("did:web:example.com") 41 assert.NoError(err) 42 obj := map[string]interface{}{ 43 "at-identifier": atid, 44 "at-uri": syntax.ATURI("at://did:abc:123/io.nsid.someFunc/record-key"), 45 "cid-string": syntax.CID("bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a"), 46 "datetime": syntax.Datetime("2023-10-30T22:25:23Z"), 47 "did": syntax.DID("did:web:example.com"), 48 "handle": syntax.Handle("blah.example.com"), 49 "language": syntax.Language("us"), 50 "nsid": syntax.NSID("com.example.blah"), 51 "recordkey": syntax.RecordKey("self"), 52 "tid": syntax.TID("3kao2cl6lyj2p"), 53 "uri": syntax.URI("https://example.com/file"), 54 } 55 assert.NoError(Validate(obj)) 56 _, err = MarshalCBOR(obj) 57 assert.NoError(err) 58 _, err = json.Marshal(obj) 59 assert.NoError(err) 60} 61 62func TestExtractBlobs(t *testing.T) { 63 assert := assert.New(t) 64 65 cid1, _ := cid.Parse("bafkreiccldh766hwcnuxnf2wh6jgzepf2nlu2lvcllt63eww5p6chi4ity") 66 obj := map[string]interface{}{ 67 "a": 5, 68 "b": 123, 69 "c": map[string]interface{}{ 70 "blb": Blob{ 71 Size: 567, 72 MimeType: "image/jpeg", 73 Ref: CIDLink(cid1), 74 }, 75 }, 76 "d": []interface{}{ 77 123, 78 Blob{ 79 Size: 123, 80 MimeType: "image/png", 81 Ref: CIDLink(cid1), 82 }, 83 }, 84 } 85 blbs := ExtractBlobs(obj) 86 assert.Equal(2, len(blbs)) 87}