Skip to content

Commit 43f86de

Browse files
Test jsonb
1 parent e80bfcc commit 43f86de

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

dao/dbmodel/jsonb_test.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dbmodel_test
22

33
import (
4-
"encoding/json"
54
"testing"
65

76
"github.com/go-feature-flag/app-api/dao/dbmodel"
@@ -17,24 +16,27 @@ var theTestMap = map[string]interface{}{
1716
},
1817
}
1918

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"}}`
2520

2621
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")
2928
}
30-
b, err := json.Marshal(theTest)
31-
assert.Nil(t, err)
32-
assert.Equal(t, theTestString, string(b))
3329
}
3430

3531
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)
4042
}

0 commit comments

Comments
 (0)