Skip to content

Commit 883455c

Browse files
authored
Add library_exists implementation (pandas-dev#661)
* add library_exists implementation * updated library_exists() to work robustly without requiring auth * fix typo for method name test_library_doesnt_exist
1 parent 60011d0 commit 883455c

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

arctic/arctic.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,34 @@ def list_libraries(self):
202202
libs.append(coll[:-1 * len(self.METADATA_COLL) - 1])
203203
return libs
204204

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+
205233
@mongo_retry
206234
def initialize_library(self, library, lib_type=VERSION_STORE, **kwargs):
207235
"""

tests/integration/test_arctic.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import pytest
22
import time
33
from datetime import datetime as dt
4-
from mock import patch
4+
from mock import patch, MagicMock
55
from pandas.util.testing import assert_frame_equal
6+
from pymongo.errors import OperationFailure
67

78
from arctic.arctic import Arctic, VERSION_STORE
89
from arctic.exceptions import LibraryNotFoundException, QuotaExceededException
@@ -211,3 +212,19 @@ def test_lib_rename_namespace(arctic):
211212
def test_lib_type(arctic):
212213
arctic.initialize_library('test')
213214
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')

tests/unit/test_arctic.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def test_initialize_library_too_many_ns():
329329
assert 'Too many namespaces 5001, not creating: sentinel.lib_name' in str(e)
330330

331331

332-
def test_initialize_library():
332+
def test_initialize_library_with_list_coll_names():
333333
self = create_autospec(Arctic)
334334
self._conn = create_autospec(MongoClient)
335335
lib = create_autospec(ArcticLibraryBinding)
@@ -346,6 +346,18 @@ def test_initialize_library():
346346
assert lib_type.initialize_library.call_args_list == [call(ML.return_value, thing=sentinel.thing)]
347347

348348

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+
349361
def test_get_library():
350362
self = create_autospec(Arctic)
351363
self._library_cache = {}

0 commit comments

Comments
 (0)