File tree Expand file tree Collapse file tree 3 files changed +59
-2
lines changed Expand file tree Collapse file tree 3 files changed +59
-2
lines changed Original file line number Diff line number Diff line change @@ -202,6 +202,34 @@ def list_libraries(self):
202
202
libs .append (coll [:- 1 * len (self .METADATA_COLL ) - 1 ])
203
203
return libs
204
204
205
+ def library_exists (self , library ):
206
+ """
207
+ Check whether a given library exists.
208
+
209
+ Parameters
210
+ ----------
211
+ library : `str`
212
+ The name of the library. e.g. 'library' or 'user.library'
213
+
214
+ Returns
215
+ -------
216
+ `bool`
217
+ True if the library with the given name already exists, False otherwise
218
+ """
219
+ exists = False
220
+ try :
221
+ # This forces auth errors, and to fall back to the slower "list_collections"
222
+ ArcticLibraryBinding (self , library ).get_library_type ()
223
+ # This will obtain the library, if no exception thrown we have verified its existence
224
+ self .get_library (library )
225
+ exists = True
226
+ except OperationFailure :
227
+ exists = library in self .list_libraries ()
228
+ except LibraryNotFoundException :
229
+ pass
230
+ return exists
231
+
232
+
205
233
@mongo_retry
206
234
def initialize_library (self , library , lib_type = VERSION_STORE , ** kwargs ):
207
235
"""
Original file line number Diff line number Diff line change 1
1
import pytest
2
2
import time
3
3
from datetime import datetime as dt
4
- from mock import patch
4
+ from mock import patch , MagicMock
5
5
from pandas .util .testing import assert_frame_equal
6
+ from pymongo .errors import OperationFailure
6
7
7
8
from arctic .arctic import Arctic , VERSION_STORE
8
9
from arctic .exceptions import LibraryNotFoundException , QuotaExceededException
@@ -211,3 +212,19 @@ def test_lib_rename_namespace(arctic):
211
212
def test_lib_type (arctic ):
212
213
arctic .initialize_library ('test' )
213
214
assert (arctic .get_library_type ('test' ) == VERSION_STORE )
215
+
216
+
217
+ def test_library_exists (arctic ):
218
+ arctic .initialize_library ('test' )
219
+ assert arctic .library_exists ('test' )
220
+ assert not arctic .library_exists ('nonexistentlib' )
221
+
222
+
223
+ def test_library_exists_no_auth (arctic ):
224
+ arctic .initialize_library ('test' )
225
+ with patch ('arctic.arctic.ArcticLibraryBinding' ) as AB :
226
+ AB .return_value = MagicMock (
227
+ get_library_type = MagicMock (side_effect = OperationFailure ("not authorized on arctic to execute command" )))
228
+ assert arctic .library_exists ('test' )
229
+ assert AB .return_value .get_library_type .called
230
+ assert not arctic .library_exists ('nonexistentlib' )
Original file line number Diff line number Diff line change @@ -329,7 +329,7 @@ def test_initialize_library_too_many_ns():
329
329
assert 'Too many namespaces 5001, not creating: sentinel.lib_name' in str (e )
330
330
331
331
332
- def test_initialize_library ():
332
+ def test_initialize_library_with_list_coll_names ():
333
333
self = create_autospec (Arctic )
334
334
self ._conn = create_autospec (MongoClient )
335
335
lib = create_autospec (ArcticLibraryBinding )
@@ -346,6 +346,18 @@ def test_initialize_library():
346
346
assert lib_type .initialize_library .call_args_list == [call (ML .return_value , thing = sentinel .thing )]
347
347
348
348
349
+ def test_library_exists ():
350
+ self = create_autospec (Arctic )
351
+ self .get_library .return_value = 'not an exception'
352
+ assert Arctic .library_exists (self , 'mylib' )
353
+
354
+
355
+ def test_library_doesnt_exist ():
356
+ self = create_autospec (Arctic )
357
+ self .get_library .side_effect = LibraryNotFoundException ('not found' )
358
+ assert not Arctic .library_exists (self , 'mylib' )
359
+
360
+
349
361
def test_get_library ():
350
362
self = create_autospec (Arctic )
351
363
self ._library_cache = {}
You can’t perform that action at this time.
0 commit comments