Skip to content

Commit 57a317d

Browse files
committed
Add test for safe casting
1 parent dafbba9 commit 57a317d

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

model/model_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package model_test
22

33
import (
44
"encoding/json"
5-
geojson "github.com/paulmach/go.geojson"
65
"testing"
76
"time"
87

8+
geojson "github.com/paulmach/go.geojson"
9+
910
"github.com/phoops/ngsiv2/model"
1011
)
1112

@@ -296,6 +297,30 @@ func TestEntityUnmarshal(t *testing.T) {
296297
t.Fatalf("Expected value to be a Point got %v", err)
297298
}
298299
}
300+
301+
nastyBoolean := `
302+
{
303+
"id": "NastyBool1",
304+
"valid": {
305+
"metadata": {},
306+
"type": "Boolean",
307+
"value": 1
308+
}
309+
}
310+
`
311+
312+
nastyBoolEntity := &model.Entity{}
313+
if err := json.Unmarshal([]byte(nastyBoolean), nastyBoolEntity); err != nil {
314+
t.Fatalf("Error unmarshaling entity: %v", err)
315+
}
316+
317+
if _, err := nastyBoolEntity.GetAttributeAsBoolean("valid"); err != nil {
318+
if err != model.ErrInvalidCastingAttributeEntity {
319+
t.Fatalf("Expected casting error on nasty boolean, got: %v", err)
320+
}
321+
} else {
322+
t.Fatal("Expected error on getting a nasty boolean, got nil")
323+
}
299324
}
300325

301326
func TestEntityMarshal(t *testing.T) {

0 commit comments

Comments
 (0)