Files
gf/container/gvar/gvar_z_unit_slice_test.go
ivothgle d353bf0fbc feat(contrib/drivers/pgsql): more field types converting support (#3737)
This pull request significantly improves PostgreSQL array type handling
and conversion in the `pgsql` driver, providing more accurate type
mapping and conversion logic, especially for array types. It introduces
comprehensive documentation, refactors conversion logic to use the `pq`
package for array types, and adds extensive unit tests to ensure
correctness and error handling. Additionally, minor enhancements and
clarifications are made to upsert formatting and table field queries.

### PostgreSQL Array Type Handling and Conversion

* Refactored `CheckLocalTypeForField` and `ConvertValueForLocal` methods
in `contrib/drivers/pgsql/pgsql_convert.go` to accurately map PostgreSQL
array types (such as `_int2`, `_int4`, `_int8`, `_float4`, `_float8`,
`_bool`, `_varchar`, `_text`, `_char`, `_bpchar`, `_numeric`,
`_decimal`, `_money`, `_bytea`) to their corresponding Go types, using
the `pq` package for conversion. Added detailed documentation and
mapping tables for supported types.
[[1]](diffhunk://#diff-a3b1e68bfa29fbcfda7c703bbe875fa82e958f6c3ad942ef82193a9dd8ad67e2R46-R63)
[[2]](diffhunk://#diff-a3b1e68bfa29fbcfda7c703bbe875fa82e958f6c3ad942ef82193a9dd8ad67e2L56-R103)
[[3]](diffhunk://#diff-a3b1e68bfa29fbcfda7c703bbe875fa82e958f6c3ad942ef82193a9dd8ad67e2R112-R209)

* Added comprehensive unit tests in
`contrib/drivers/pgsql/pgsql_z_unit_convert_test.go` to verify type
mapping and conversion for all supported array types, including error
cases for invalid input.

### Utility and API Improvements

* Added a new `Bools()` method to the `gvar.Var` type in
`container/gvar/gvar_slice.go` for converting values to `[]bool`, with
corresponding unit tests in `container/gvar/gvar_z_unit_slice_test.go`.
[[1]](diffhunk://#diff-32e887e540e0170f785508d105cb794e4d54d854b53b6950973c80022973c490R11-R15)
[[2]](diffhunk://#diff-01453eca4d4b3e35d07ca105cb924c6441d0cd9df6cbcc337a89832c8d53057fR24-R41)

### SQL Formatting and Documentation

* Improved documentation and formatting in the upsert logic of
`contrib/drivers/pgsql/pgsql_format_upsert.go` to clarify the use of
`EXCLUDED` in PostgreSQL's `ON CONFLICT DO UPDATE`.
* Enhanced readability of the table field query in
`contrib/drivers/pgsql/pgsql_table_fields.go` by reformatting SQL and
clarifying field extraction.

---------

Co-authored-by: hailaz <739476267@qq.com>
Co-authored-by: houseme <housemecn@gmail.com>
2025-12-08 11:18:45 +08:00

134 lines
3.1 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gvar_test
import (
"testing"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/test/gtest"
)
func TestVar_Ints(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var arr = []int{1, 2, 3, 4, 5}
objOne := gvar.New(arr, true)
t.Assert(objOne.Ints()[0], arr[0])
})
}
func TestVar_Bools(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var arr = []bool{true, false, true, false}
objOne := gvar.New(arr, true)
t.AssertEQ(objOne.Bools(), arr)
})
gtest.C(t, func(t *gtest.T) {
var arr = []int{1, 0, 1, 0}
objOne := gvar.New(arr, true)
t.AssertEQ(objOne.Bools(), []bool{true, false, true, false})
})
gtest.C(t, func(t *gtest.T) {
var arr = []string{"true", "false", "1", "0"}
objOne := gvar.New(arr, true)
t.AssertEQ(objOne.Bools(), []bool{true, false, true, false})
})
}
func TestVar_Uints(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var arr = []int{1, 2, 3, 4, 5}
objOne := gvar.New(arr, true)
t.Assert(objOne.Uints()[0], arr[0])
})
}
func TestVar_Int64s(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var arr = []int{1, 2, 3, 4, 5}
objOne := gvar.New(arr, true)
t.Assert(objOne.Int64s()[0], arr[0])
})
}
func TestVar_Uint64s(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var arr = []int{1, 2, 3, 4, 5}
objOne := gvar.New(arr, true)
t.Assert(objOne.Uint64s()[0], arr[0])
})
}
func TestVar_Floats(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var arr = []float64{1, 2, 3, 4, 5}
objOne := gvar.New(arr, true)
t.Assert(objOne.Floats()[0], arr[0])
})
}
func TestVar_Float32s(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var arr = []float32{1, 2, 3, 4, 5}
objOne := gvar.New(arr, true)
t.AssertEQ(objOne.Float32s(), arr)
})
}
func TestVar_Float64s(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var arr = []float64{1, 2, 3, 4, 5}
objOne := gvar.New(arr, true)
t.AssertEQ(objOne.Float64s(), arr)
})
}
func TestVar_Strings(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var arr = []string{"hello", "world"}
objOne := gvar.New(arr, true)
t.Assert(objOne.Strings()[0], arr[0])
})
}
func TestVar_Interfaces(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var arr = []int{1, 2, 3, 4, 5}
objOne := gvar.New(arr, true)
t.Assert(objOne.Interfaces(), arr)
})
}
func TestVar_Slice(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var arr = []int{1, 2, 3, 4, 5}
objOne := gvar.New(arr, true)
t.Assert(objOne.Slice(), arr)
})
}
func TestVar_Array(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var arr = []int{1, 2, 3, 4, 5}
objOne := gvar.New(arr, false)
t.Assert(objOne.Array(), arr)
})
}
func TestVar_Vars(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var arr = []int{1, 2, 3, 4, 5}
objOne := gvar.New(arr, false)
t.Assert(len(objOne.Vars()), 5)
t.Assert(objOne.Vars()[0].Int(), 1)
t.Assert(objOne.Vars()[4].Int(), 5)
objEmpty := gvar.New([]int{})
t.Assert(objEmpty.Vars(), nil)
})
}