1
1
package dbmodel_test
2
2
3
3
import (
4
- "encoding/json"
5
4
"testing"
6
5
7
6
"github.com/go-feature-flag/app-api/dao/dbmodel"
@@ -17,24 +16,27 @@ var theTestMap = map[string]interface{}{
17
16
},
18
17
}
19
18
20
- var theTestString = `{"field":{"another_key":"another_value","key":"value","nested_key":{"child_key":"child_value"}}}`
21
-
22
- type TestStruct struct {
23
- Field dbmodel.JSONB `json:"field"`
24
- }
19
+ var theTestString = `{"another_key":"another_value","key":"value","nested_key":{"child_key":"child_value"}}`
25
20
26
21
func TestJSONBMarshalling (t * testing.T ) {
27
- theTest := TestStruct {
28
- Field : theTestMap ,
22
+ d := theTestMap
23
+ s , _ := dbmodel .JSONB (d ).Value ()
24
+ if v , ok := s .([]byte ); ok {
25
+ assert .JSONEq (t , theTestString , string (v ))
26
+ } else {
27
+ assert .False (t , true , "should be a byte slice" )
29
28
}
30
- b , err := json .Marshal (theTest )
31
- assert .Nil (t , err )
32
- assert .Equal (t , theTestString , string (b ))
33
29
}
34
30
35
31
func TestJSONBUnmarshalling (t * testing.T ) {
36
- theTest := TestStruct {}
37
- err := json .Unmarshal ([]byte (theTestString ), & theTest )
38
- assert .Nil (t , err )
39
- assert .Equal (t , dbmodel .JSONB (theTestMap ), theTest .Field )
32
+ var theTest dbmodel.JSONB
33
+ err := theTest .Scan ([]byte (theTestString ))
34
+ assert .NoError (t , err )
35
+ assert .Equal (t , dbmodel .JSONB (theTestMap ), theTest )
36
+ }
37
+
38
+ func TestJSONBUnmarshalling_wrongType (t * testing.T ) {
39
+ var theTest dbmodel.JSONB
40
+ err := theTest .Scan ([]byte ("toto" ))
41
+ assert .Error (t , err )
40
42
}
0 commit comments