Skip to content

Commit

Permalink
Add library_exists implementation (pandas-dev#661)
Browse files Browse the repository at this point in the history
* add library_exists implementation

* updated library_exists() to work robustly without requiring auth

* fix typo for method name test_library_doesnt_exist
  • Loading branch information
dimosped authored Nov 15, 2018
1 parent 60011d0 commit 883455c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
28 changes: 28 additions & 0 deletions arctic/arctic.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,34 @@ def list_libraries(self):
libs.append(coll[:-1 * len(self.METADATA_COLL) - 1])
return libs

def library_exists(self, library):
"""
Check whether a given library exists.
Parameters
----------
library : `str`
The name of the library. e.g. 'library' or 'user.library'
Returns
-------
`bool`
True if the library with the given name already exists, False otherwise
"""
exists = False
try:
# This forces auth errors, and to fall back to the slower "list_collections"
ArcticLibraryBinding(self, library).get_library_type()
# This will obtain the library, if no exception thrown we have verified its existence
self.get_library(library)
exists = True
except OperationFailure:
exists = library in self.list_libraries()
except LibraryNotFoundException:
pass
return exists


@mongo_retry
def initialize_library(self, library, lib_type=VERSION_STORE, **kwargs):
"""
Expand Down
19 changes: 18 additions & 1 deletion tests/integration/test_arctic.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import pytest
import time
from datetime import datetime as dt
from mock import patch
from mock import patch, MagicMock
from pandas.util.testing import assert_frame_equal
from pymongo.errors import OperationFailure

from arctic.arctic import Arctic, VERSION_STORE
from arctic.exceptions import LibraryNotFoundException, QuotaExceededException
Expand Down Expand Up @@ -211,3 +212,19 @@ def test_lib_rename_namespace(arctic):
def test_lib_type(arctic):
arctic.initialize_library('test')
assert(arctic.get_library_type('test') == VERSION_STORE)


def test_library_exists(arctic):
arctic.initialize_library('test')
assert arctic.library_exists('test')
assert not arctic.library_exists('nonexistentlib')


def test_library_exists_no_auth(arctic):
arctic.initialize_library('test')
with patch('arctic.arctic.ArcticLibraryBinding') as AB:
AB.return_value = MagicMock(
get_library_type=MagicMock(side_effect=OperationFailure("not authorized on arctic to execute command")))
assert arctic.library_exists('test')
assert AB.return_value.get_library_type.called
assert not arctic.library_exists('nonexistentlib')
14 changes: 13 additions & 1 deletion tests/unit/test_arctic.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def test_initialize_library_too_many_ns():
assert 'Too many namespaces 5001, not creating: sentinel.lib_name' in str(e)


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


def test_library_exists():
self = create_autospec(Arctic)
self.get_library.return_value = 'not an exception'
assert Arctic.library_exists(self, 'mylib')


def test_library_doesnt_exist():
self = create_autospec(Arctic)
self.get_library.side_effect = LibraryNotFoundException('not found')
assert not Arctic.library_exists(self, 'mylib')


def test_get_library():
self = create_autospec(Arctic)
self._library_cache = {}
Expand Down

0 comments on commit 883455c

Please sign in to comment.