From 98a77c66ad9382cdd6ea86bacdd8c4b6842bcc0c Mon Sep 17 00:00:00 2001 From: Isaiah Date: Wed, 12 May 2021 09:25:44 -0500 Subject: [PATCH] #1 got the db creation done. Looks like will continually need to update the basic_mock_pyarango to neuter any functionality for basic init tests as we go --- lowball_arangodb_authdb/authdb.py | 7 +++++++ tests/conftest.py | 27 +++++++++++++++++++++++---- tests/test_authdb.py | 24 +++++++++++++++++++----- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/lowball_arangodb_authdb/authdb.py b/lowball_arangodb_authdb/authdb.py index 2200ef8..def128d 100644 --- a/lowball_arangodb_authdb/authdb.py +++ b/lowball_arangodb_authdb/authdb.py @@ -50,6 +50,13 @@ def __init__(self, verify=self.verify ) + try: + self.database = self.connection[self.database_name] + except KeyError: + self.database = self.connection.createDatabase(name=self.database_name) + + + @property def url(self): return self._url diff --git a/tests/conftest.py b/tests/conftest.py index 88eaf4d..726bc20 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -223,6 +223,9 @@ def init_calls_expected_connection(request): verify=verify ) +@pytest.fixture +def basic_db_name(): + return 'test' class TestMockConnection(Connection): @@ -242,7 +245,7 @@ class TestMockDocument(Document): pass @pytest.fixture -def basic_mock_pyarango(basic_mock_connection_init): +def basic_mock_pyarango(basic_mock_connection, basic_mock_database): pass @@ -255,16 +258,32 @@ def mock_pyarango(monkeypatch): monkeypatch.setattr(lowball_arangodb_authdb.authdb, "Document", TestMockDocument) @pytest.fixture -def basic_mock_connection_init(monkeypatch): +def basic_mock_connection(monkeypatch): monkeypatch.setattr(TestMockConnection, "__init__", Mock(return_value=None)) + monkeypatch.setattr(TestMockConnection, "createDatabase", Mock(return_value=Mock())) + monkeypatch.setattr(TestMockConnection, "__getitem__", Mock(return_value=None)) + +@pytest.fixture +def basic_mock_database(monkeypatch): + + monkeypatch.setattr(TestMockDatabase, "__init__", Mock(return_value=None)) @pytest.fixture -def basic_mock_connection_get_item_db_not_present(monkeypatch): +def basic_mock_connection_get_item_db_not_present(monkeypatch, basic_db_name): mock = Mock() mock.side_effect = KeyError() + TestMockDatabase.name = basic_db_name monkeypatch.setattr(TestMockDatabase, "__init__", Mock(return_value=None)) + monkeypatch.setattr(TestMockConnection, "__init__", Mock(return_value=None)) monkeypatch.setattr(TestMockConnection, "__getitem__", mock) - monkeypatch.setattr(TestMockConnection, "createDatabase", Mock(return_value=TestMockDatabase(connection=Mock(), name="test"))) + monkeypatch.setattr(TestMockConnection, "createDatabase", Mock(return_value=TestMockDatabase(connection=Mock(), name=basic_db_name))) + +@pytest.fixture +def mock_connection_get_item_db_present(monkeypatch, basic_db_name): + TestMockDatabase.name = basic_db_name + monkeypatch.setattr(TestMockDatabase, "__init__", Mock(return_value=None)) + monkeypatch.setattr(TestMockConnection, "__init__", Mock(return_value=None)) + monkeypatch.setattr(TestMockConnection, "__getitem__", Mock(return_value=TestMockDatabase(connection=Mock(), name=basic_db_name))) diff --git a/tests/test_authdb.py b/tests/test_authdb.py index 1c5b3af..a2b4d26 100644 --- a/tests/test_authdb.py +++ b/tests/test_authdb.py @@ -4,6 +4,9 @@ from unittest.mock import call import pyArango from pyArango.connection import Connection +from pyArango.collection import Collection +from pyArango.database import Database +from pyArango.document import Document class TestAuthDBInit: """Tests: @@ -235,7 +238,7 @@ def test_validation_of_index_client_id_if_bool(self, def test_arango_connection_created_correctly(self, mock_pyarango, init_calls_expected_connection, basic_mock_pyarango, - basic_mock_connection_init): + basic_mock_connection): params, expected_call = init_calls_expected_connection @@ -246,13 +249,24 @@ def test_arango_connection_created_correctly(self, mock_pyarango, init_calls_exp def test_authentication_database_created_if_not_present(self, mock_pyarango, basic_mock_connection_get_item_db_not_present, - basic_mock_connection_init): + basic_db_name): - pass + authdb = AuthDB(database_name=basic_db_name) + authdb.connection.__getitem__.assert_called_once_with(basic_db_name) + authdb.connection.createDatabase.assert_called_once_with(name=basic_db_name) - def test_authentication_database_accessed_if_present(self): + assert isinstance(authdb.database, Database) + assert authdb.database.name == basic_db_name - pass + def test_authentication_database_accessed_if_present(self, + mock_pyarango, + basic_db_name, + mock_connection_get_item_db_present): + authdb = AuthDB(database_name=basic_db_name) + authdb.connection.__getitem__.assert_called_once_with(basic_db_name) + + assert isinstance(authdb.database, Database) + assert authdb.database.name == basic_db_name def test_authentication_collection_created_if_not_present(self):