|
| 1 | +package helpers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/ydb-platform/ydb-go-sdk/v3/scheme" |
| 12 | +) |
| 13 | + |
| 14 | +type isDirectoryExistsSchemeClient struct { |
| 15 | + dbName string |
| 16 | + existingPath string |
| 17 | +} |
| 18 | + |
| 19 | +func (c isDirectoryExistsSchemeClient) Database() string { |
| 20 | + return c.dbName |
| 21 | +} |
| 22 | + |
| 23 | +func (c isDirectoryExistsSchemeClient) ListDirectory(ctx context.Context, path string) ( |
| 24 | + d scheme.Directory, err error, |
| 25 | +) { |
| 26 | + if c.existingPath == path { |
| 27 | + return scheme.Directory{ |
| 28 | + Entry: scheme.Entry{ |
| 29 | + Name: path, |
| 30 | + Type: scheme.EntryDirectory, |
| 31 | + }, |
| 32 | + }, nil |
| 33 | + } |
| 34 | + if strings.HasPrefix(c.existingPath, path) { |
| 35 | + children := strings.Split(strings.TrimLeft(c.existingPath, path), "/") |
| 36 | + return scheme.Directory{ |
| 37 | + Entry: scheme.Entry{ |
| 38 | + Name: path, |
| 39 | + Type: scheme.EntryDirectory, |
| 40 | + }, |
| 41 | + Children: []scheme.Entry{ |
| 42 | + { |
| 43 | + Name: children[0], |
| 44 | + Type: scheme.EntryDirectory, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, nil |
| 48 | + } |
| 49 | + return d, fmt.Errorf("path '%s' not found in '%s'", path, c) |
| 50 | +} |
| 51 | + |
| 52 | +func TestIsDirectoryExists(t *testing.T) { |
| 53 | + for _, tt := range []struct { |
| 54 | + checkPath string |
| 55 | + client isDirectoryExistsSchemeClient |
| 56 | + exists bool |
| 57 | + err bool |
| 58 | + }{ |
| 59 | + { |
| 60 | + checkPath: "/a/b/c/d", |
| 61 | + client: isDirectoryExistsSchemeClient{"/a", "/a/"}, |
| 62 | + exists: false, |
| 63 | + err: false, |
| 64 | + }, |
| 65 | + { |
| 66 | + checkPath: "/a/b/c/d", |
| 67 | + client: isDirectoryExistsSchemeClient{"/a", "/a/b/"}, |
| 68 | + exists: false, |
| 69 | + err: false, |
| 70 | + }, |
| 71 | + { |
| 72 | + checkPath: "/a/b/c/d", |
| 73 | + client: isDirectoryExistsSchemeClient{"/a", "/a/b/c/"}, |
| 74 | + exists: false, |
| 75 | + err: false, |
| 76 | + }, |
| 77 | + { |
| 78 | + checkPath: "/a/b/c/d", |
| 79 | + client: isDirectoryExistsSchemeClient{"/a", "/a/b/c/d/"}, |
| 80 | + exists: true, |
| 81 | + err: false, |
| 82 | + }, |
| 83 | + { |
| 84 | + checkPath: "/a/b/c/d", |
| 85 | + client: isDirectoryExistsSchemeClient{"/a", "/a/b/c/d/e/"}, |
| 86 | + exists: true, |
| 87 | + err: false, |
| 88 | + }, |
| 89 | + } { |
| 90 | + t.Run("", func(t *testing.T) { |
| 91 | + exists, err := IsDirectoryExists(context.Background(), tt.client, tt.checkPath) |
| 92 | + if tt.err { |
| 93 | + require.Error(t, err) |
| 94 | + } else { |
| 95 | + require.NoError(t, err) |
| 96 | + } |
| 97 | + require.Equal(t, tt.exists, exists) |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +type isTableExistsSchemeClient struct { |
| 103 | + dbName string |
| 104 | + tablePath string |
| 105 | +} |
| 106 | + |
| 107 | +func (c isTableExistsSchemeClient) Database() string { |
| 108 | + return c.dbName |
| 109 | +} |
| 110 | + |
| 111 | +func (c isTableExistsSchemeClient) ListDirectory(ctx context.Context, path string) ( |
| 112 | + d scheme.Directory, err error, |
| 113 | +) { |
| 114 | + if strings.HasPrefix(c.tablePath, path) { |
| 115 | + children := strings.Split(strings.TrimLeft(c.tablePath, path), "/") |
| 116 | + switch { |
| 117 | + case len(children) == 1: |
| 118 | + return scheme.Directory{ |
| 119 | + Entry: scheme.Entry{ |
| 120 | + Name: path, |
| 121 | + Type: scheme.EntryDirectory, |
| 122 | + }, |
| 123 | + Children: []scheme.Entry{ |
| 124 | + { |
| 125 | + Name: children[0], |
| 126 | + Type: scheme.EntryTable, |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, nil |
| 130 | + case len(children) > 1: |
| 131 | + return scheme.Directory{ |
| 132 | + Entry: scheme.Entry{ |
| 133 | + Name: path, |
| 134 | + Type: scheme.EntryDirectory, |
| 135 | + }, |
| 136 | + Children: []scheme.Entry{ |
| 137 | + { |
| 138 | + Name: children[0], |
| 139 | + Type: scheme.EntryDirectory, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, nil |
| 143 | + default: |
| 144 | + return scheme.Directory{ |
| 145 | + Entry: scheme.Entry{ |
| 146 | + Name: "", |
| 147 | + Type: scheme.EntryDirectory, |
| 148 | + }, |
| 149 | + }, nil |
| 150 | + } |
| 151 | + } |
| 152 | + return d, fmt.Errorf("path '%s' not found in '%s'", path, c) |
| 153 | +} |
| 154 | + |
| 155 | +func TestIsTableExists(t *testing.T) { |
| 156 | + for _, tt := range []struct { |
| 157 | + checkPath string |
| 158 | + client isTableExistsSchemeClient |
| 159 | + exists bool |
| 160 | + err bool |
| 161 | + }{ |
| 162 | + { |
| 163 | + checkPath: "/a/b/c/d", |
| 164 | + client: isTableExistsSchemeClient{"/b", "/a/b"}, |
| 165 | + exists: false, |
| 166 | + err: true, |
| 167 | + }, |
| 168 | + { |
| 169 | + checkPath: "/a/b/c/d", |
| 170 | + client: isTableExistsSchemeClient{"/a", "/a/b"}, |
| 171 | + exists: false, |
| 172 | + err: true, |
| 173 | + }, |
| 174 | + { |
| 175 | + checkPath: "/a/b/c/d", |
| 176 | + client: isTableExistsSchemeClient{"/a", "/a/b/c"}, |
| 177 | + exists: false, |
| 178 | + err: true, |
| 179 | + }, |
| 180 | + { |
| 181 | + checkPath: "/a/b/c/d", |
| 182 | + client: isTableExistsSchemeClient{"/a", "/a/b/c/d"}, |
| 183 | + exists: true, |
| 184 | + err: false, |
| 185 | + }, |
| 186 | + { |
| 187 | + checkPath: "/a/b/c/d", |
| 188 | + client: isTableExistsSchemeClient{"/a", "/a/b/c/d/e"}, |
| 189 | + exists: false, |
| 190 | + err: true, |
| 191 | + }, |
| 192 | + } { |
| 193 | + t.Run("", func(t *testing.T) { |
| 194 | + exists, err := IsTableExists(context.Background(), tt.client, tt.checkPath) |
| 195 | + if tt.err { |
| 196 | + require.Error(t, err) |
| 197 | + } else { |
| 198 | + require.NoError(t, err) |
| 199 | + } |
| 200 | + require.Equal(t, tt.exists, exists) |
| 201 | + }) |
| 202 | + } |
| 203 | +} |
0 commit comments