|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 2 | +// use this file except in compliance with the License. You may obtain a copy of |
| 3 | +// the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +// License for the specific language governing permissions and limitations under |
| 11 | +// the License. |
| 12 | + |
| 13 | +package pg |
| 14 | + |
| 15 | +import ( |
| 16 | + "net/http" |
| 17 | + "testing" |
| 18 | + |
| 19 | + "github.com/google/go-cmp/cmp" |
| 20 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 21 | + "gitlab.com/flimzy/testy/v2" |
| 22 | + |
| 23 | + "github.com/go-kivik/kivik/v4" |
| 24 | + "github.com/go-kivik/kivik/v4/driver" |
| 25 | +) |
| 26 | + |
| 27 | +func TestAllDBs(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + |
| 30 | + type test struct { |
| 31 | + client *client |
| 32 | + options driver.Options |
| 33 | + want []string |
| 34 | + wantErr string |
| 35 | + wantStatus int |
| 36 | + } |
| 37 | + |
| 38 | + tests := testy.NewTable[test]() |
| 39 | + tests.Add("no databases found", test{ |
| 40 | + client: testClient(t), |
| 41 | + want: []string{}, |
| 42 | + }) |
| 43 | + tests.AddFunc("some dbs exist", func(t *testing.T) test { |
| 44 | + client := testClient(t) |
| 45 | + |
| 46 | + const dbName1, dbName2 = "testdb1", "testdb2" |
| 47 | + if err := client.CreateDB(t.Context(), dbName1, nil); err != nil { |
| 48 | + t.Fatalf("Failed to create test db: %s", err) |
| 49 | + } |
| 50 | + if err := client.CreateDB(t.Context(), dbName2, nil); err != nil { |
| 51 | + t.Fatalf("Failed to create test db: %s", err) |
| 52 | + } |
| 53 | + |
| 54 | + return test{ |
| 55 | + client: client, |
| 56 | + want: []string{dbName1, dbName2}, |
| 57 | + } |
| 58 | + }) |
| 59 | + tests.AddFunc("exclude non-kivik tables", func(t *testing.T) test { |
| 60 | + client := testClient(t) |
| 61 | + |
| 62 | + const dbName = "testdb" |
| 63 | + if err := client.CreateDB(t.Context(), dbName, nil); err != nil { |
| 64 | + t.Fatalf("Failed to create test db: %s", err) |
| 65 | + } |
| 66 | + if _, err := client.pool.Exec(t.Context(), "CREATE TABLE foo (id TEXT)"); err != nil { |
| 67 | + t.Fatalf("Failed to create non-kivik table: %s", err) |
| 68 | + } |
| 69 | + |
| 70 | + return test{ |
| 71 | + client: client, |
| 72 | + want: []string{dbName}, |
| 73 | + } |
| 74 | + }) |
| 75 | + tests.AddFunc("db connection error", func(t *testing.T) test { |
| 76 | + client := testClient(t) |
| 77 | + |
| 78 | + client.pool.Close() |
| 79 | + |
| 80 | + return test{ |
| 81 | + client: client, |
| 82 | + wantErr: "closed pool", |
| 83 | + wantStatus: http.StatusInternalServerError, |
| 84 | + } |
| 85 | + }) |
| 86 | + tests.AddFunc("option: descending=true", func(t *testing.T) test { |
| 87 | + client := testClient(t) |
| 88 | + |
| 89 | + const dbName1, dbName2 = "testdb1", "testdb2" |
| 90 | + if err := client.CreateDB(t.Context(), dbName1, nil); err != nil { |
| 91 | + t.Fatalf("Failed to create test db: %s", err) |
| 92 | + } |
| 93 | + if err := client.CreateDB(t.Context(), dbName2, nil); err != nil { |
| 94 | + t.Fatalf("Failed to create test db: %s", err) |
| 95 | + } |
| 96 | + |
| 97 | + return test{ |
| 98 | + client: client, |
| 99 | + options: kivik.Param("descending", "true"), |
| 100 | + want: []string{dbName2, dbName1}, |
| 101 | + } |
| 102 | + }) |
| 103 | + tests.AddFunc("end key", func(t *testing.T) test { |
| 104 | + client := testClient(t) |
| 105 | + |
| 106 | + const dbName1, dbName2 = "testdb1", "testdb2" |
| 107 | + if err := client.CreateDB(t.Context(), dbName1, nil); err != nil { |
| 108 | + t.Fatalf("Failed to create test db: %s", err) |
| 109 | + } |
| 110 | + if err := client.CreateDB(t.Context(), dbName2, nil); err != nil { |
| 111 | + t.Fatalf("Failed to create test db: %s", err) |
| 112 | + } |
| 113 | + |
| 114 | + return test{ |
| 115 | + client: client, |
| 116 | + options: kivik.Param("endkey", dbName1), |
| 117 | + want: []string{dbName1}, |
| 118 | + } |
| 119 | + }) |
| 120 | + |
| 121 | + /* |
| 122 | + TODO: |
| 123 | + - options: |
| 124 | + - limit (number) – Limit the number of the returned databases to the specified number. |
| 125 | + - skip (number) – Skip this number of databases before starting to return the results. Default is 0. |
| 126 | + - startkey (json) – Return databases starting with the specified key. |
| 127 | + - start_key (json) – Alias for startkey. |
| 128 | + - inclusive_end (boolean) – If true, the specified endkey will be included in the result. Default is true. |
| 129 | + */ |
| 130 | + |
| 131 | + tests.Run(t, func(t *testing.T, tt test) { |
| 132 | + t.Parallel() |
| 133 | + |
| 134 | + got, err := tt.client.AllDBs(t.Context(), tt.options) |
| 135 | + if !testy.ErrorMatchesRE(tt.wantErr, err) { |
| 136 | + t.Errorf("Unexpected error: %s", err) |
| 137 | + } |
| 138 | + if status := kivik.HTTPStatus(err); status != tt.wantStatus { |
| 139 | + t.Errorf("Unexpected status: %d", status) |
| 140 | + } |
| 141 | + if d := cmp.Diff(tt.want, got, cmpopts.EquateEmpty()); d != "" { |
| 142 | + t.Errorf("Unexpected result:\n%s", d) |
| 143 | + } |
| 144 | + }) |
| 145 | +} |
0 commit comments