diff --git a/VERSION b/VERSION index d917d3e..17e51c3 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.2 +0.1.1 diff --git a/flask_arango_orm/arango.py b/flask_arango_orm/arango.py index cce8908..3beef93 100644 --- a/flask_arango_orm/arango.py +++ b/flask_arango_orm/arango.py @@ -3,7 +3,7 @@ from arango import ArangoClient from arango_orm import ConnectionPool, Database -from flask import current_app, Flask +from flask import current_app, _app_ctx_stack ARANGODB_CLUSTER = False ARANGODB_HOST = ('http', '127.0.0.1', 8529) @@ -21,7 +21,7 @@ def __init__(self, app=None): :param app: Flask app instance :type app: flask.Flask """ - #self.app = app + self.app = app if app is not None: self.init_app(app) @@ -60,40 +60,29 @@ def connect(self): :returns: Connection to arangodb :rtype: arango_orm.Database """ - with current_app.app_context(): - db_name = current_app.config['ARANGODB_DATABASE'] - username = current_app.config['ARANGODB_USER'] - password = current_app.config['ARANGODB_PASSWORD'] - - if current_app.config['ARANGODB_CLUSTER'] == True: - hosts = [] - host_pool = current_app.config['ARANGODB_HOST_POOL'] - for protocol, host, port in host_pool: - hosts.append( - ArangoClient( - hosts="{protocol}://{host}:{port}".format( - protocol = protocol, - host = host, - port = port - ) - ) - ) - return ConnectionPool(hosts, - dbname=db_name, - password=password, - username=username) - else: - protocol, host, port = current_app.config['ARANGODB_HOST'] - client = ArangoClient( - hosts="{protocol}://{host}:{port}".format( - protocol = protocol, - host = host, - port = port - ) - ) - return Database(client.db(name=db_name, - username=username, - password=password)) + db_name = current_app.config['ARANGODB_DATABASE'] + username = current_app.config['ARANGODB_USER'] + password = current_app.config['ARANGODB_PASSWORD'] + + if current_app.config['ARANGODB_CLUSTER'] == True: + hosts = [] + host_pool = current_app.config['ARANGODB_HOST_POOL'] + for protocol, host, port in host_pool: + hosts.append(ArangoClient(protocol=protocol, + host=host, + port=port)) + return ConnectionPool(hosts, + dbname=db_name, + password=password, + username=username) + else: + protocol, host, port = current_app.config['ARANGODB_HOST'] + client = ArangoClient(protocol=protocol, + host=host, + port=port) + return Database(client.db(name=db_name, + username=username, + password=password)) @property def connection(self): @@ -107,9 +96,9 @@ def connection(self): :returns: ArangoDB connection :rtype: arango_orm.Database """ - with current_app.app_context(): - if not hasattr(current_app, 'arangodb'): - setattr(current_app, 'arangodb', self.connect()) + ctx = _app_ctx_stack.top + if ctx is not None: + if not hasattr(ctx, 'arangodb'): + setattr(ctx, 'arangodb', self.connect()) # ctx.arangodb = self.connect() - return current_app.arangodb - + return ctx.arangodb diff --git a/requirements.txt b/requirements.txt index 3de5837..6d68f9e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ # Generated file, do not edit -arango-orm>=0.7.2 -python-arango>=7 -Flask>=2.0.0 +arango-orm>=0.5.3 +python-arango<5,>4 +Flask>=1.0.0 setuptools pytest-runner Sphinx<2,>=1.8.0 @@ -9,4 +9,4 @@ sphinx_rtd_theme pytest>=4.3.0 pytest-cov pytest-flake8 -mock;python_version>"3.3" +mock;python_version<"3.3" diff --git a/setup.py b/setup.py index e9f7249..f6e99bc 100644 --- a/setup.py +++ b/setup.py @@ -10,19 +10,19 @@ PROJECT_NAME = 'Flask-arango-orm' INSTALL_REQUIRES = [ - 'arango-orm>=0.7.2', - 'python-arango>=7', - 'Flask>=2.0.0', ] + 'arango-orm>=0.5.3', + 'python-arango<5,>4', + 'Flask>=1.0.0', ] SETUP_REQUIRES = [ 'setuptools', 'pytest-runner', - 'Sphinx<7', + 'Sphinx<2,>=1.8.0', 'sphinx_rtd_theme', ] TESTS_REQUIRES = [ 'pytest>=4.3.0', 'pytest-cov', 'pytest-flake8', - 'mock;python_version>"3.3"', ] + 'mock;python_version<"3.3"', ] def get_version(): with open('VERSION') as f: diff --git a/tests/test_arango.py b/tests/test_arango.py index 7e8b2df..50ea832 100644 --- a/tests/test_arango.py +++ b/tests/test_arango.py @@ -3,19 +3,12 @@ except ImportError: import mock -from flask import Flask from flask_arango_orm import ArangoORM -from flask_arango_orm.arango import ARANGODB_HOST -from arango_orm import Collection - - -class Car(Collection): - __collection__ = "cars" +from flask_arango_orm.arango import ARANGODB_CLUSTER, ARANGODB_HOST class TestArangoORM: - ''' @mock.patch('flask.Flask') def test_init(self, mock_flask): app = mock_flask(__name__) @@ -23,86 +16,59 @@ def test_init(self, mock_flask): calls = [mock.call('ARANGODB_CLUSTER', ARANGODB_CLUSTER), mock.call('ARANGODB_HOST', ARANGODB_HOST)] app.config.setdefault.assert_has_calls(calls) - ''' - ''' + def test_init_no_app(self): arango = ArangoORM() assert arango.app is None - ''' - # @mock.patch('flask_arango_orm.arango.Database') - # @mock.patch('flask_arango_orm.arango.ArangoClient') - # @mock.patch('flask_arango_orm.arango.ArangoClient.app') - def test_connect_single(self): - app = Flask(__name__) + @mock.patch('flask_arango_orm.arango.Database') + @mock.patch('flask_arango_orm.arango.ArangoClient') + @mock.patch('flask_arango_orm.arango.current_app') + def test_connect_single(self, mock_current_app, mock_client, mock_db): arango_db = 'test_db' arango_user = 'test_user' arango_passwd = 'test_password' - # app.config = {} - app.config['SERVER_NAME'] = "localhost" - app.config['ARANGODB_DATABASE'] = arango_db - app.config['ARANGODB_USER'] = arango_user - app.config['ARANGODB_PASSWORD'] = arango_passwd - app.config['ARANGODB_CLUSTER'] = False - app.config['ARANGODB_HOST'] = ARANGODB_HOST - arango = ArangoORM(app) - with app.app_context(): - try: - db = arango.connect() - if db.has_collection(Car) is False: - db.create_collection(Car) - db.has_collection(Car) - except Exception as e: - assert False, e - ''' + mock_current_app.config = {} + mock_current_app.config['ARANGODB_DATABASE'] = arango_db + mock_current_app.config['ARANGODB_USER'] = arango_user + mock_current_app.config['ARANGODB_PASSWORD'] = arango_passwd + mock_current_app.config['ARANGODB_CLUSTER'] = False + mock_current_app.config['ARANGODB_HOST'] = ARANGODB_HOST + arango = ArangoORM() + arango.connect() protocol, host, port = ARANGODB_HOST - mock_client.assert_called_with( - hosts="{protocol}://{host}:{port}".format( - protocol=protocol, - host=host, - port=port - ) - ) - ''' - - ''' + mock_client.assert_called_with(protocol=protocol, + host=host, + port=port) mock_db.assert_called_with( mock_client.return_value.db(name=arango_db, username=arango_user, password=arango_passwd)) - ''' - # @mock.patch('flask_arango_orm.arango.ConnectionPool') - # @mock.patch('flask_arango_orm.arango.ArangoClient') - # @mock.patch('flask_arango_orm.arango.current_app') - def test_connect_cluster(self): - app = Flask(__name__) + @mock.patch('flask_arango_orm.arango.ConnectionPool') + @mock.patch('flask_arango_orm.arango.ArangoClient') + @mock.patch('flask_arango_orm.arango.current_app') + def test_connect_cluster(self, mock_current_app, mock_client, mock_pool): arango_db = 'test_db' arango_user = 'test_user' arango_passwd = 'test_password' host_pool = [('http', '127.0.0.1', 8529), ('http', '127.0.0.2', 8530), ('https', '127.0.0.3', 8529)] - - app.config['ARANGODB_DATABASE'] = arango_db - app.config['ARANGODB_USER'] = arango_user - app.config['ARANGODB_PASSWORD'] = arango_passwd - app.config['ARANGODB_CLUSTER'] = True - app.config['ARANGODB_HOST_POOL'] = host_pool - arango = ArangoORM(app) - with app.app_context(): - try: - db = arango.connect() - if db.has_collection(Car) is False: - db.create_collection(Car) - db.has_collection(Car) - except Exception as e: - assert False, e - - # calls = [mock.call(hosts=h) for h in hosts] - # mock_client.assert_has_calls(calls) + mock_current_app.config = {} + mock_current_app.config['ARANGODB_DATABASE'] = arango_db + mock_current_app.config['ARANGODB_USER'] = arango_user + mock_current_app.config['ARANGODB_PASSWORD'] = arango_passwd + mock_current_app.config['ARANGODB_CLUSTER'] = True + mock_current_app.config['ARANGODB_HOST_POOL'] = host_pool + arango = ArangoORM() + arango.connect() + calls = [mock.call(protocol=protocol, + host=host, + port=port) for protocol, host, port in host_pool] + mock_client.assert_has_calls(calls) # TODO: Need to figure out ConnectionPool arguments - # mock_pool.assert_called_once() + mock_pool.assert_called_once() @mock.patch.object(ArangoORM, 'connect') def test_connection_attribute(self, mock_connect): @@ -112,8 +78,6 @@ def test_connection_attribute(self, mock_connect): import flask app = flask.Flask(__name__) arango = ArangoORM(app) - with app.app_context(): - try: - arango.connect() - except Exception as e: - assert False, e + with app.test_request_context(): + db_conn = arango.connection + assert db_conn == test_conn