@@ -119,6 +119,53 @@ func TestEntity(t *testing.T) {
119119 })
120120 }
121121 })
122+
123+ t .Run ("UnmarshalJSON" , func (t * testing.T ) {
124+ t .Parallel ()
125+ tests := []struct {
126+ name string
127+ input string
128+ }{
129+ {"explicit" , `{ "__entity": { "type": "Type", "id": "id" } }` },
130+ {"implicit" , `{ "type": "Type", "id": "id" }` },
131+ {"explicit - extra keys in inner" , `{ "__entity": { "type": "Type", "id": "id", "floob": "blah" } }` },
132+ {"implicit - extra keys" , `{ "type": "Type", "id": "id", "floob": "blah" }` },
133+ }
134+
135+ for _ , tt := range tests {
136+ t .Run (tt .name , func (t * testing.T ) {
137+ t .Parallel ()
138+ var got types.EntityUID
139+ err := got .UnmarshalJSON ([]byte (tt .input ))
140+ testutil .OK (t , err )
141+ testutil .Equals (t , got , types .NewEntityUID ("Type" , "id" ))
142+ })
143+ }
144+ })
145+
146+ t .Run ("UnmarshalJSON invalid" , func (t * testing.T ) {
147+ t .Parallel ()
148+ tests := []struct {
149+ name string
150+ input string
151+ }{
152+ {"non-object" , `"floob"` },
153+ {"explicit - wrong type" , `{ "__entity": "wrong type"` },
154+ {"implicit - type wrong" , `{ "type": 123, "id": "123" }` },
155+ {"implicit - id wrong type" , `{ "type": "Type", "id": 123 }` },
156+ {"implicit - only type" , `{ "type": "Type" }` },
157+ {"implicit - only id" , `{ "id": 123 }` },
158+ }
159+
160+ for _ , tt := range tests {
161+ t .Run (tt .name , func (t * testing.T ) {
162+ t .Parallel ()
163+ var got types.EntityUID
164+ err := got .UnmarshalJSON ([]byte (tt .input ))
165+ testutil .Error (t , err )
166+ })
167+ }
168+ })
122169}
123170
124171func TestEntityUIDSet (t * testing.T ) {
0 commit comments