(t *testing.T)
| 21 | ) |
| 22 | |
| 23 | func 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 | |
| 72 | func TestPostgresArrayQuote(t *testing.T) { |
| 73 | db := pg(t) |
nothing calls this directly
no test coverage detected
searching dependent graphs…