|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package pgdumprestore |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + pglib "github.com/xataio/pgstream/internal/postgres" |
| 12 | + "github.com/xataio/pgstream/internal/postgres/mocks" |
| 13 | + "github.com/xataio/pgstream/pkg/log" |
| 14 | + "github.com/xataio/pgstream/pkg/snapshot" |
| 15 | +) |
| 16 | + |
| 17 | +func TestSnapshotGenerator_CreateSnapshot(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + testDump := []byte("test dump") |
| 21 | + testSchema := "test_schema" |
| 22 | + testTable := "test_table" |
| 23 | + errTest := errors.New("oh noes") |
| 24 | + |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + snapshot *snapshot.Snapshot |
| 28 | + conn pglib.Querier |
| 29 | + pgdumpFn pgdumpFn |
| 30 | + pgrestoreFn pgrestoreFn |
| 31 | + |
| 32 | + wantErr error |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "ok", |
| 36 | + snapshot: &snapshot.Snapshot{ |
| 37 | + SchemaName: testSchema, |
| 38 | + TableNames: []string{testTable}, |
| 39 | + }, |
| 40 | + conn: &mocks.Querier{ |
| 41 | + ExecFn: func(ctx context.Context, i uint, query string, args ...any) (pglib.CommandTag, error) { |
| 42 | + require.Equal(t, "CREATE SCHEMA IF NOT EXISTS "+testSchema, query) |
| 43 | + return pglib.CommandTag{}, nil |
| 44 | + }, |
| 45 | + }, |
| 46 | + pgdumpFn: func(po pglib.PGDumpOptions) ([]byte, error) { |
| 47 | + require.Equal(t, pglib.PGDumpOptions{ |
| 48 | + ConnectionString: "source-url", |
| 49 | + Format: "c", |
| 50 | + SchemaOnly: true, |
| 51 | + Schemas: []string{testSchema}, |
| 52 | + Tables: []string{testSchema + "." + testTable}, |
| 53 | + }, po) |
| 54 | + return testDump, nil |
| 55 | + }, |
| 56 | + pgrestoreFn: func(po pglib.PGRestoreOptions, dump []byte) (string, error) { |
| 57 | + require.Equal(t, pglib.PGRestoreOptions{ |
| 58 | + ConnectionString: "target-url", |
| 59 | + SchemaOnly: true, |
| 60 | + }, po) |
| 61 | + require.Equal(t, testDump, dump) |
| 62 | + return "", nil |
| 63 | + }, |
| 64 | + |
| 65 | + wantErr: nil, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "error - performing pgdump", |
| 69 | + snapshot: &snapshot.Snapshot{ |
| 70 | + SchemaName: testSchema, |
| 71 | + TableNames: []string{testTable}, |
| 72 | + }, |
| 73 | + conn: &mocks.Querier{ |
| 74 | + ExecFn: func(ctx context.Context, i uint, query string, args ...any) (pglib.CommandTag, error) { |
| 75 | + return pglib.CommandTag{}, errors.New("ExecFn: should not be called") |
| 76 | + }, |
| 77 | + }, |
| 78 | + pgdumpFn: func(po pglib.PGDumpOptions) ([]byte, error) { |
| 79 | + return nil, errTest |
| 80 | + }, |
| 81 | + pgrestoreFn: func(po pglib.PGRestoreOptions, dump []byte) (string, error) { |
| 82 | + return "", errors.New("pgrestoreFn: should not be called") |
| 83 | + }, |
| 84 | + |
| 85 | + wantErr: errTest, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "error - performing pgrestore", |
| 89 | + snapshot: &snapshot.Snapshot{ |
| 90 | + SchemaName: publicSchema, |
| 91 | + TableNames: []string{testTable}, |
| 92 | + }, |
| 93 | + conn: &mocks.Querier{ |
| 94 | + ExecFn: func(ctx context.Context, i uint, query string, args ...any) (pglib.CommandTag, error) { |
| 95 | + return pglib.CommandTag{}, errors.New("ExecFn: should not be called") |
| 96 | + }, |
| 97 | + }, |
| 98 | + pgdumpFn: func(po pglib.PGDumpOptions) ([]byte, error) { |
| 99 | + return testDump, nil |
| 100 | + }, |
| 101 | + pgrestoreFn: func(po pglib.PGRestoreOptions, dump []byte) (string, error) { |
| 102 | + return "", errTest |
| 103 | + }, |
| 104 | + |
| 105 | + wantErr: errTest, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "error - creating schema", |
| 109 | + snapshot: &snapshot.Snapshot{ |
| 110 | + SchemaName: testSchema, |
| 111 | + TableNames: []string{testTable}, |
| 112 | + }, |
| 113 | + conn: &mocks.Querier{ |
| 114 | + ExecFn: func(ctx context.Context, i uint, query string, args ...any) (pglib.CommandTag, error) { |
| 115 | + return pglib.CommandTag{}, errTest |
| 116 | + }, |
| 117 | + }, |
| 118 | + pgdumpFn: func(po pglib.PGDumpOptions) ([]byte, error) { |
| 119 | + return testDump, nil |
| 120 | + }, |
| 121 | + pgrestoreFn: func(po pglib.PGRestoreOptions, dump []byte) (string, error) { |
| 122 | + return "", errors.New("pgrestoreFn: should not be called") |
| 123 | + }, |
| 124 | + |
| 125 | + wantErr: errTest, |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + for _, tc := range tests { |
| 130 | + t.Run(tc.name, func(t *testing.T) { |
| 131 | + t.Parallel() |
| 132 | + |
| 133 | + sg := SnapshotGenerator{ |
| 134 | + sourceURL: "source-url", |
| 135 | + targetURL: "target-url", |
| 136 | + targetConn: tc.conn, |
| 137 | + pgDumpFn: tc.pgdumpFn, |
| 138 | + pgRestoreFn: tc.pgrestoreFn, |
| 139 | + logger: log.NewNoopLogger(), |
| 140 | + } |
| 141 | + |
| 142 | + err := sg.CreateSnapshot(context.Background(), tc.snapshot) |
| 143 | + require.ErrorIs(t, err, tc.wantErr) |
| 144 | + }) |
| 145 | + } |
| 146 | +} |
0 commit comments