MCPcopy Create free account
hub / github.com/uptrace/bun / TestPostgresArray

Function TestPostgresArray

internal/dbtest/pg_test.go:23–70  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

21)
22
23func TestPostgresArray(t *testing.T) {
24 type Model struct {
25 ID int64 `bun:",pk,autoincrement"`
26 Array1 []string `bun:",array"`
27 Array2 *[]string `bun:",array"`
28 Array3 *[]string `bun:",array"`
29 Array4 []*string `bun:",array"`
30 }
31
32 db := pg(t)
33 t.Cleanup(func() { db.Close() })
34 mustResetModel(t, ctx, db, (*Model)(nil))
35
36 str1 := "hello"
37 str2 := "world"
38 model1 := &Model{
39 ID: 123,
40 Array1: []string{"one", "two", "three"},
41 Array2: &[]string{"hello", "world"},
42 Array4: []*string{&str1, &str2},
43 }
44 _, err := db.NewInsert().Model(model1).Exec(ctx)
45 require.NoError(t, err)
46
47 model2 := new(Model)
48 err = db.NewSelect().Model(model2).Scan(ctx)
49 require.NoError(t, err)
50 require.Equal(t, model1, model2)
51
52 var strs []string
53 err = db.NewSelect().Model((*Model)(nil)).
54 Column("array1").
55 Scan(ctx, pgdialect.Array(&strs))
56 require.NoError(t, err)
57 require.Equal(t, []string{"one", "two", "three"}, strs)
58
59 err = db.NewSelect().Model((*Model)(nil)).
60 Column("array3").
61 Scan(ctx, pgdialect.Array(&strs))
62 require.NoError(t, err)
63 require.Nil(t, strs)
64
65 err = db.NewSelect().Model((*Model)(nil)).
66 Column("array4").
67 Scan(ctx, pgdialect.Array(&strs))
68 require.NoError(t, err)
69 require.Equal(t, []string{"hello", "world"}, strs)
70}
71
72func TestPostgresArrayQuote(t *testing.T) {
73 db := pg(t)

Callers

nothing calls this directly

Calls 10

ArrayFunction · 0.92
pgFunction · 0.85
mustResetModelFunction · 0.85
CloseMethod · 0.65
NewInsertMethod · 0.65
NewSelectMethod · 0.65
ExecMethod · 0.45
ModelMethod · 0.45
ScanMethod · 0.45
ColumnMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…