|
| 1 | +package pgvector |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "gotest.tools/v3/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestEnableInPostgreSQL(t *testing.T) { |
| 14 | + expected := errors.New("whoops") |
| 15 | + exec := func( |
| 16 | + _ context.Context, stdin io.Reader, stdout, stderr io.Writer, command ...string, |
| 17 | + ) error { |
| 18 | + assert.Assert(t, stdout != nil, "should capture stdout") |
| 19 | + assert.Assert(t, stderr != nil, "should capture stderr") |
| 20 | + |
| 21 | + assert.Assert(t, strings.Contains(strings.Join(command, "\n"), |
| 22 | + `SELECT datname FROM pg_catalog.pg_database`, |
| 23 | + ), "expected all databases and templates") |
| 24 | + |
| 25 | + b, err := io.ReadAll(stdin) |
| 26 | + assert.NilError(t, err) |
| 27 | + assert.Equal(t, string(b), strings.Trim(` |
| 28 | +SET client_min_messages = WARNING; CREATE EXTENSION IF NOT EXISTS vector; ALTER EXTENSION vector UPDATE; |
| 29 | + `, "\t\n")) |
| 30 | + |
| 31 | + return expected |
| 32 | + } |
| 33 | + |
| 34 | + ctx := context.Background() |
| 35 | + assert.Equal(t, expected, EnableInPostgreSQL(ctx, exec)) |
| 36 | +} |
| 37 | + |
| 38 | +func TestDisableInPostgreSQL(t *testing.T) { |
| 39 | + expected := errors.New("whoops") |
| 40 | + exec := func( |
| 41 | + _ context.Context, stdin io.Reader, stdout, stderr io.Writer, command ...string, |
| 42 | + ) error { |
| 43 | + assert.Assert(t, stdout != nil, "should capture stdout") |
| 44 | + assert.Assert(t, stderr != nil, "should capture stderr") |
| 45 | + |
| 46 | + assert.Assert(t, strings.Contains(strings.Join(command, "\n"), |
| 47 | + `SELECT datname FROM pg_catalog.pg_database`, |
| 48 | + ), "expected all databases and templates") |
| 49 | + |
| 50 | + b, err := io.ReadAll(stdin) |
| 51 | + assert.NilError(t, err) |
| 52 | + assert.Equal(t, string(b), strings.Trim(` |
| 53 | +SET client_min_messages = WARNING; DROP EXTENSION IF EXISTS vector; |
| 54 | + `, "\t\n")) |
| 55 | + |
| 56 | + return expected |
| 57 | + } |
| 58 | + |
| 59 | + ctx := context.Background() |
| 60 | + assert.Equal(t, expected, DisableInPostgreSQL(ctx, exec)) |
| 61 | +} |
0 commit comments