-
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: postgres syntax errors for pointers and slices #877
- Loading branch information
Showing
5 changed files
with
261 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package pgdialect | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/uptrace/bun/schema" | ||
) | ||
|
||
type tag struct { | ||
ID int32 | ||
Label string | ||
} | ||
|
||
func ptr[T any](v T) *T { | ||
return &v | ||
} | ||
|
||
func TestArrayAppend(t *testing.T) { | ||
tcases := []struct { | ||
input interface{} | ||
out string | ||
}{ | ||
{ | ||
input: []byte{1, 2}, | ||
out: `'{1,2}'`, | ||
}, | ||
{ | ||
input: []*byte{ptr(byte(1)), ptr(byte(2))}, | ||
out: `'{1,2}'`, | ||
}, | ||
{ | ||
input: []int{1, 2}, | ||
out: `'{1,2}'`, | ||
}, | ||
{ | ||
input: []*int{ptr(1), ptr(2)}, | ||
out: `'{1,2}'`, | ||
}, | ||
{ | ||
input: []string{"foo", "bar"}, | ||
out: `'{"foo","bar"}'`, | ||
}, | ||
{ | ||
input: []*string{ptr("foo"), ptr("bar")}, | ||
out: `'{"foo","bar"}'`, | ||
}, | ||
{ | ||
input: []tag{{1, "foo1"}, {2, "bar"}}, | ||
out: `'[{"ID":1,"Label":"foo1"},{"ID":2,"Label":"bar"}]'`, | ||
}, | ||
{ | ||
input: &[]tag{{1, "foo2"}, {2, "bar"}}, | ||
out: `'[{"ID":1,"Label":"foo2"},{"ID":2,"Label":"bar"}]'`, | ||
}, | ||
{ | ||
input: &[]*tag{{1, "foo3"}, {2, "bar"}}, | ||
out: `'[{"ID":1,"Label":"foo3"},{"ID":2,"Label":"bar"}]'`, | ||
}, | ||
{ | ||
input: ptr(&[]*tag{{1, "foo4"}, {2, "bar"}}), | ||
out: `'[{"ID":1,"Label":"foo4"},{"ID":2,"Label":"bar"}]'`, | ||
}, | ||
{ | ||
input: []**tag{ptr(ptr(tag{1, "foo5"})), ptr(ptr(tag{2, "bar"}))}, | ||
out: `'[{"ID":1,"Label":"foo5"},{"ID":2,"Label":"bar"}]'`, | ||
}, | ||
{ | ||
input: &[]**tag{ptr(ptr(tag{1, "foo6"})), ptr(ptr(tag{2, "bar"}))}, | ||
out: `'[{"ID":1,"Label":"foo6"},{"ID":2,"Label":"bar"}]'`, | ||
}, | ||
{ | ||
input: ptr(&[]**tag{ptr(ptr(tag{1, "foo7"})), ptr(ptr(tag{2, "bar"}))}), | ||
out: `'[{"ID":1,"Label":"foo7"},{"ID":2,"Label":"bar"}]'`, | ||
}, | ||
{ | ||
input: ptr(ptr(&[]**tag{ptr(ptr(tag{1, "foo8"})), ptr(ptr(tag{2, "bar"}))})), | ||
out: `'[{"ID":1,"Label":"foo8"},{"ID":2,"Label":"bar"}]'`, | ||
}, | ||
} | ||
|
||
for _, tcase := range tcases { | ||
out, err := Array(tcase.input).AppendQuery(schema.NewFormatter(New()), []byte{}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if string(out) != tcase.out { | ||
t.Errorf("expected output to be %s, was %s", tcase.out, string(out)) | ||
} | ||
} | ||
} | ||
|
||
func Test_appendJSONUnquoted(t *testing.T) { | ||
type args struct { | ||
b []byte | ||
jsonb []byte | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []byte | ||
}{ | ||
{ | ||
name: "Test foo", | ||
args: args{ | ||
b: []byte{}, | ||
jsonb: []byte(`{"foo":'\bar\u0000\'}\0`), | ||
}, | ||
want: []byte(`{"foo":''\bar\\u0000\'}\0`), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := appendJSONUnquoted(tt.args.b, tt.args.jsonb); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("appendJSONUnquoted() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters