github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/utils/sqlutil/types_test.go (about) 1 package sqlutil 2 3 import ( 4 "database/sql" 5 "database/sql/driver" 6 "encoding/json" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 11 "github.com/jxskiss/gopkg/v2/easy/ezmap" 12 ) 13 14 func TestTypes(t *testing.T) { 15 var testcases = []struct { 16 a driver.Valuer 17 b sql.Scanner 18 }{ 19 { 20 a: &JSON{Map: ezmap.Map{"a": "1", "b": float64(2), "c": []any{"a", "b", "c"}}}, 21 b: &JSON{}, 22 }, 23 } 24 25 for _, tc := range testcases { 26 buf, err := tc.a.Value() 27 assert.Nil(t, err) 28 assert.NotNil(t, buf) 29 30 err = tc.b.Scan(buf) 31 assert.Nil(t, err) 32 assert.Equal(t, tc.a.(*JSON).GetString("a"), "1") 33 assert.Equal(t, tc.a.(*JSON).GetFloat("b"), float64(2)) 34 assert.Equal(t, tc.a.(*JSON).MustGet("c"), tc.b.(*JSON).MustGet("c")) 35 } 36 } 37 38 type Record struct { 39 ID int64 `json:"id" db:"db"` // bigint unsigned not null primary_key 40 Column1 string `json:"column1" db:"column1"` // varchar(32) not null 41 Extra LazyBinary `json:"extra" db:"extra"` // blob 42 } 43 44 func (p *Record) GetExtra() (map[string]any, error) { 45 unmarshaler := func(b []byte) (any, error) { 46 var out = map[string]any{} 47 var err error 48 if len(b) > 0 { 49 err = json.Unmarshal(b, &out) 50 } 51 return out, err 52 } 53 54 extra, err := p.Extra.Get(unmarshaler) 55 if err != nil { 56 return nil, err 57 } 58 return extra.(map[string]any), nil 59 } 60 61 func (p *Record) SetExtra(extra map[string]any) { 62 buf, _ := json.Marshal(extra) 63 p.Extra.Set(buf, extra) 64 } 65 66 func TestLazyBinary(t *testing.T) { 67 extra := map[string]any{ 68 "a": "123", 69 "b": "234", 70 "c": "345", 71 } 72 extraBuf, _ := json.Marshal(extra) 73 74 row := &Record{} 75 assert.Len(t, row.Extra.GetBytes(), 0) 76 _, err := row.GetExtra() 77 assert.Nil(t, err) 78 79 row.Extra.Set(extraBuf, nil) 80 assert.Len(t, row.Extra.GetBytes(), len(extraBuf)) 81 82 got1, _ := row.GetExtra() 83 got2, _ := row.GetExtra() 84 assert.Equal(t, extra, got1) 85 assert.Equal(t, extra, got2) 86 87 delete(extra, "a") 88 row.SetExtra(extra) 89 got3, _ := row.GetExtra() 90 assert.Equal(t, extra, got3) 91 92 row.Extra.Set(extraBuf, nil) 93 got4, _ := row.GetExtra() 94 assert.Len(t, got4, 3) 95 } 96 97 func TestBitmap(t *testing.T) { 98 bm := NewBitmap[int](nil) 99 assert.False(t, bm.Get(0x1)) 100 assert.False(t, bm.Get(0x2)) 101 assert.False(t, bm.Get(0x4)) 102 assert.Equal(t, 0, bm.Underlying()) 103 104 bm.Set(0x1) 105 bm.Set(0x4) 106 bm.Set(0x8) 107 assert.True(t, bm.Get(0x1)) 108 assert.False(t, bm.Get(0x2)) 109 assert.True(t, bm.Get(0x4)) 110 assert.True(t, bm.Get(0x8)) 111 assert.False(t, bm.Get(0x10)) 112 assert.Equal(t, 13, bm.Underlying()) 113 114 bm.Set(0x10) 115 bm.Clear(0x4) 116 assert.False(t, bm.Get(0x4)) 117 bm.Clear(0x9) 118 assert.False(t, bm.Get(0x1)) 119 assert.False(t, bm.Get(0x8)) 120 assert.Equal(t, 16, bm.Underlying()) 121 122 value, err := bm.Value() 123 assert.Nil(t, err) 124 assert.Equal(t, int64(16), value) 125 } 126 127 func TestJSONMarshal(t *testing.T) { 128 data := JSON{ 129 Map: map[string]any{ 130 "a": 123, 131 }, 132 } 133 want := `{"a":123}` 134 got, err := data.MarshalJSON() 135 assert.Nil(t, err) 136 assert.Equal(t, want, string(got)) 137 138 data = JSON{} 139 want = "null" 140 got, err = data.MarshalJSON() 141 assert.Nil(t, err) 142 assert.Equal(t, want, string(got)) 143 } 144 145 func TestJSONUnmarshal(t *testing.T) { 146 data := []byte(`{"a":123}`) 147 obj := JSON{} 148 err := obj.UnmarshalJSON(data) 149 assert.Nil(t, err) 150 assert.Equal(t, ezmap.Map{"a": 123.0}, obj.Map) 151 152 data = []byte("null") 153 obj = JSON{} 154 err = obj.UnmarshalJSON(data) 155 assert.Nil(t, err) 156 assert.Nil(t, obj.Map) 157 }