github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/scheme/helpers/check_exists_test.go (about)

     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  
    37  		return scheme.Directory{
    38  			Entry: scheme.Entry{
    39  				Name: path,
    40  				Type: scheme.EntryDirectory,
    41  			},
    42  			Children: []scheme.Entry{
    43  				{
    44  					Name: children[0],
    45  					Type: scheme.EntryDirectory,
    46  				},
    47  			},
    48  		}, nil
    49  	}
    50  
    51  	return d, fmt.Errorf("path '%s' not found in '%s'", path, c)
    52  }
    53  
    54  func TestIsDirectoryExists(t *testing.T) {
    55  	for _, tt := range []struct {
    56  		checkPath string
    57  		client    isDirectoryExistsSchemeClient
    58  		exists    bool
    59  		err       bool
    60  	}{
    61  		{
    62  			checkPath: "/c/d",
    63  			client:    isDirectoryExistsSchemeClient{"/a", "/a/b/"},
    64  			exists:    false,
    65  			err:       true,
    66  		},
    67  		{
    68  			checkPath: "/a",
    69  			client:    isDirectoryExistsSchemeClient{"/a", "/a/b/"},
    70  			exists:    true,
    71  			err:       false,
    72  		},
    73  		{
    74  			checkPath: "/a/b/c",
    75  			client:    isDirectoryExistsSchemeClient{"/a/b/c", "/a/b/c/d/"},
    76  			exists:    true,
    77  			err:       false,
    78  		},
    79  		{
    80  			checkPath: "/a/b",
    81  			client:    isDirectoryExistsSchemeClient{"/a", "/a/b/"},
    82  			exists:    true,
    83  			err:       false,
    84  		},
    85  		{
    86  			checkPath: "/a/b/c/d",
    87  			client:    isDirectoryExistsSchemeClient{"/a", "/a/"},
    88  			exists:    false,
    89  			err:       false,
    90  		},
    91  		{
    92  			checkPath: "/a/b/c/d",
    93  			client:    isDirectoryExistsSchemeClient{"/a", "/a/b/"},
    94  			exists:    false,
    95  			err:       false,
    96  		},
    97  		{
    98  			checkPath: "/a/b/c/d",
    99  			client:    isDirectoryExistsSchemeClient{"/a", "/a/b/c/"},
   100  			exists:    false,
   101  			err:       false,
   102  		},
   103  		{
   104  			checkPath: "/a/b/c/d",
   105  			client:    isDirectoryExistsSchemeClient{"/a", "/a/b/c/d/"},
   106  			exists:    true,
   107  			err:       false,
   108  		},
   109  		{
   110  			checkPath: "/a/b/c/d",
   111  			client:    isDirectoryExistsSchemeClient{"/a", "/a/b/c/d/e/"},
   112  			exists:    true,
   113  			err:       false,
   114  		},
   115  	} {
   116  		t.Run("", func(t *testing.T) {
   117  			exists, err := IsDirectoryExists(context.Background(), tt.client, tt.checkPath)
   118  			if tt.err {
   119  				require.Error(t, err)
   120  			} else {
   121  				require.NoError(t, err)
   122  			}
   123  			require.Equal(t, tt.exists, exists)
   124  		})
   125  	}
   126  }
   127  
   128  type isTableExistsSchemeClient struct {
   129  	dbName    string
   130  	tablePath string
   131  }
   132  
   133  func (c isTableExistsSchemeClient) Database() string {
   134  	return c.dbName
   135  }
   136  
   137  func (c isTableExistsSchemeClient) ListDirectory(ctx context.Context, path string) (
   138  	d scheme.Directory, err error,
   139  ) {
   140  	if strings.HasPrefix(c.tablePath, path) {
   141  		children := strings.Split(strings.TrimLeft(c.tablePath, path), "/")
   142  		switch {
   143  		case len(children) == 1:
   144  			return scheme.Directory{
   145  				Entry: scheme.Entry{
   146  					Name: path,
   147  					Type: scheme.EntryDirectory,
   148  				},
   149  				Children: []scheme.Entry{
   150  					{
   151  						Name: children[0],
   152  						Type: scheme.EntryTable,
   153  					},
   154  				},
   155  			}, nil
   156  		case len(children) > 1:
   157  			return scheme.Directory{
   158  				Entry: scheme.Entry{
   159  					Name: path,
   160  					Type: scheme.EntryDirectory,
   161  				},
   162  				Children: []scheme.Entry{
   163  					{
   164  						Name: children[0],
   165  						Type: scheme.EntryDirectory,
   166  					},
   167  				},
   168  			}, nil
   169  		default:
   170  			return scheme.Directory{
   171  				Entry: scheme.Entry{
   172  					Name: "",
   173  					Type: scheme.EntryDirectory,
   174  				},
   175  			}, nil
   176  		}
   177  	}
   178  
   179  	return d, fmt.Errorf("path '%s' not found in '%s'", path, c)
   180  }
   181  
   182  func TestIsTableExists(t *testing.T) {
   183  	for _, tt := range []struct {
   184  		checkPath string
   185  		client    isTableExistsSchemeClient
   186  		exists    bool
   187  		err       bool
   188  	}{
   189  		{
   190  			checkPath: "/a/b/c/d",
   191  			client:    isTableExistsSchemeClient{"/b", "/a/b"},
   192  			exists:    false,
   193  			err:       true,
   194  		},
   195  		{
   196  			checkPath: "/a/b/c/d",
   197  			client:    isTableExistsSchemeClient{"/a", "/a/b"},
   198  			exists:    false,
   199  			err:       true,
   200  		},
   201  		{
   202  			checkPath: "/a/b/c/d",
   203  			client:    isTableExistsSchemeClient{"/a", "/a/b/c"},
   204  			exists:    false,
   205  			err:       true,
   206  		},
   207  		{
   208  			checkPath: "/a/b/c/d",
   209  			client:    isTableExistsSchemeClient{"/a", "/a/b/c/d"},
   210  			exists:    true,
   211  			err:       false,
   212  		},
   213  		{
   214  			checkPath: "/a/b/c/d",
   215  			client:    isTableExistsSchemeClient{"/a", "/a/b/c/d/e"},
   216  			exists:    false,
   217  			err:       true,
   218  		},
   219  	} {
   220  		t.Run("", func(t *testing.T) {
   221  			exists, err := IsEntryExists(context.Background(),
   222  				tt.client, tt.checkPath,
   223  				scheme.EntryTable, scheme.EntryColumnTable,
   224  			)
   225  			if tt.err {
   226  				require.Error(t, err)
   227  			} else {
   228  				require.NoError(t, err)
   229  			}
   230  			require.Equal(t, tt.exists, exists)
   231  		})
   232  	}
   233  }
   234  
   235  func TestEntryTypePrintf(t *testing.T) {
   236  	require.Equal(t,
   237  		"[Table ColumnTable]",
   238  		fmt.Sprintf("%v", []scheme.EntryType{scheme.EntryTable, scheme.EntryColumnTable}),
   239  	)
   240  }