Skip to content

Commit ce98f84

Browse files
authored
Merge pull request #209 from swordqiu/hotfix/qj-convert-tristate-revisit
fix: convert to tristate revisited
2 parents ce9f710 + ce90c1e commit ce98f84

6 files changed

Lines changed: 495 additions & 90 deletions

File tree

backends/clickhouse/column_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"yunion.io/x/jsonutils"
2323
"yunion.io/x/pkg/tristate"
24+
"yunion.io/x/pkg/util/timeutils"
2425
"yunion.io/x/sqlchemy"
2526
)
2627

@@ -282,3 +283,61 @@ func TestConvertString(t *testing.T) {
282283
}
283284
}
284285
}
286+
287+
func TestIdempotentConvertValue(t *testing.T) {
288+
cases := []struct {
289+
in interface{}
290+
want interface{}
291+
col sqlchemy.IColumnSpec
292+
}{
293+
{
294+
in: tristate.False,
295+
want: uint8(0),
296+
col: &triCol,
297+
},
298+
{
299+
in: tristate.True,
300+
want: uint8(1),
301+
col: &triCol,
302+
},
303+
{
304+
in: tristate.None,
305+
want: sql.NullInt32{},
306+
col: &triCol,
307+
},
308+
{
309+
in: true,
310+
want: uint8(1),
311+
col: &boolCol,
312+
},
313+
{
314+
in: false,
315+
want: uint8(0),
316+
col: &boolCol,
317+
},
318+
{
319+
in: "0",
320+
want: uint8(0),
321+
col: &boolCol,
322+
},
323+
{
324+
in: "1",
325+
want: uint8(1),
326+
col: &boolCol,
327+
},
328+
{
329+
in: "2025-05-31T12:00:00Z",
330+
want: func() time.Time {
331+
tm, _ := timeutils.ParseTimeStr("2025-05-31T12:00:00Z")
332+
return tm
333+
}(),
334+
col: &dateCol,
335+
},
336+
}
337+
for _, c := range cases {
338+
got := c.col.ConvertFromValue(c.col.ConvertFromValue(c.in))
339+
if got != c.want {
340+
t.Errorf("%s [%#v] want: %#v got: %#v", c.col.DefinitionString(), c.in, c.want, got)
341+
}
342+
}
343+
}

backends/dameng/column_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"yunion.io/x/jsonutils"
2323
"yunion.io/x/pkg/tristate"
24+
"yunion.io/x/pkg/util/timeutils"
2425
"yunion.io/x/sqlchemy"
2526
)
2627

@@ -229,3 +230,61 @@ func TestConvertString(t *testing.T) {
229230
}
230231
}
231232
}
233+
234+
func TestIdempotentConvertValue(t *testing.T) {
235+
cases := []struct {
236+
in interface{}
237+
want interface{}
238+
col sqlchemy.IColumnSpec
239+
}{
240+
{
241+
in: tristate.False,
242+
want: 0,
243+
col: &triCol,
244+
},
245+
{
246+
in: tristate.True,
247+
want: 1,
248+
col: &triCol,
249+
},
250+
{
251+
in: tristate.None,
252+
want: sql.NullInt32{},
253+
col: &triCol,
254+
},
255+
{
256+
in: true,
257+
want: 1,
258+
col: &boolCol,
259+
},
260+
{
261+
in: false,
262+
want: 0,
263+
col: &boolCol,
264+
},
265+
{
266+
in: "0",
267+
want: 0,
268+
col: &boolCol,
269+
},
270+
{
271+
in: "1",
272+
want: 1,
273+
col: &boolCol,
274+
},
275+
{
276+
in: "2025-05-31T12:00:00Z",
277+
want: func() time.Time {
278+
tm, _ := timeutils.ParseTimeStr("2025-05-31T12:00:00Z")
279+
return tm
280+
}(),
281+
col: &dateCol,
282+
},
283+
}
284+
for _, c := range cases {
285+
got := c.col.ConvertFromValue(c.col.ConvertFromValue(c.in))
286+
if got != c.want {
287+
t.Errorf("%s [%#v] want: %#v got: %#v", c.col.DefinitionString(), c.in, c.want, got)
288+
}
289+
}
290+
}

backends/mysql/column_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ package mysql
1717
import (
1818
"database/sql"
1919
"testing"
20+
"time"
2021

2122
"yunion.io/x/jsonutils"
2223
"yunion.io/x/pkg/tristate"
24+
"yunion.io/x/pkg/util/timeutils"
2325
"yunion.io/x/sqlchemy"
2426
)
2527

@@ -239,3 +241,61 @@ func TestConvertString(t *testing.T) {
239241
}
240242
}
241243
}
244+
245+
func TestIdempotentConvertValue(t *testing.T) {
246+
cases := []struct {
247+
in interface{}
248+
want interface{}
249+
col sqlchemy.IColumnSpec
250+
}{
251+
{
252+
in: tristate.False,
253+
want: 0,
254+
col: &triCol,
255+
},
256+
{
257+
in: tristate.True,
258+
want: 1,
259+
col: &triCol,
260+
},
261+
{
262+
in: tristate.None,
263+
want: sql.NullInt32{},
264+
col: &triCol,
265+
},
266+
{
267+
in: true,
268+
want: 1,
269+
col: &boolCol,
270+
},
271+
{
272+
in: false,
273+
want: 0,
274+
col: &boolCol,
275+
},
276+
{
277+
in: "0",
278+
want: 0,
279+
col: &boolCol,
280+
},
281+
{
282+
in: "1",
283+
want: 1,
284+
col: &boolCol,
285+
},
286+
{
287+
in: "2025-05-31T12:00:00Z",
288+
want: func() time.Time {
289+
tm, _ := timeutils.ParseTimeStr("2025-05-31T12:00:00Z")
290+
return tm
291+
}(),
292+
col: &dateCol,
293+
},
294+
}
295+
for _, c := range cases {
296+
got := c.col.ConvertFromValue(c.col.ConvertFromValue(c.in))
297+
if got != c.want {
298+
t.Errorf("%s [%#v] want: %#v got: %#v", c.col.DefinitionString(), c.in, c.want, got)
299+
}
300+
}
301+
}

backends/sqlite/column_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ package sqlite
1717
import (
1818
"database/sql"
1919
"testing"
20+
"time"
2021

2122
"yunion.io/x/jsonutils"
2223

2324
"yunion.io/x/pkg/tristate"
25+
"yunion.io/x/pkg/util/timeutils"
2426

2527
"yunion.io/x/sqlchemy"
2628
)
@@ -200,3 +202,61 @@ func TestConvertString(t *testing.T) {
200202
}
201203
}
202204
}
205+
206+
func TestIdempotentConvertValue(t *testing.T) {
207+
cases := []struct {
208+
in interface{}
209+
want interface{}
210+
col sqlchemy.IColumnSpec
211+
}{
212+
{
213+
in: tristate.False,
214+
want: 0,
215+
col: &triCol,
216+
},
217+
{
218+
in: tristate.True,
219+
want: 1,
220+
col: &triCol,
221+
},
222+
{
223+
in: tristate.None,
224+
want: sql.NullInt32{},
225+
col: &triCol,
226+
},
227+
{
228+
in: true,
229+
want: 1,
230+
col: &boolCol,
231+
},
232+
{
233+
in: false,
234+
want: 0,
235+
col: &boolCol,
236+
},
237+
{
238+
in: "0",
239+
want: 0,
240+
col: &boolCol,
241+
},
242+
{
243+
in: "1",
244+
want: 1,
245+
col: &boolCol,
246+
},
247+
{
248+
in: "2025-05-31T12:00:00Z",
249+
want: func() time.Time {
250+
tm, _ := timeutils.ParseTimeStr("2025-05-31T12:00:00Z")
251+
return tm
252+
}(),
253+
col: &dateCol,
254+
},
255+
}
256+
for _, c := range cases {
257+
got := c.col.ConvertFromValue(c.col.ConvertFromValue(c.in))
258+
if got != c.want {
259+
t.Errorf("%s [%#v] want: %#v got: %#v", c.col.DefinitionString(), c.in, c.want, got)
260+
}
261+
}
262+
}

0 commit comments

Comments
 (0)