Skip to content

Commit

Permalink
touching up contextmanager to use config object rather than keys dire…
Browse files Browse the repository at this point in the history
…ctly
  • Loading branch information
lockefox committed Apr 12, 2018
1 parent 597b12f commit 655f4d7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 17 deletions.
2 changes: 1 addition & 1 deletion prosper/test_utils/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
INSTALLED = False

HERE = path.abspath(path.dirname(__file__))

__library_name__ = 'test_utils'
def get_version():
"""find current version information
Expand Down
14 changes: 14 additions & 0 deletions prosper/test_utils/root_schema.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"type": "object",
"properties": {
"schema_group": {"type": "string"},
"schema_name": {"type": "string"},
"update": {"type": "string", "format": "date-time"},
"version": {"type": "string"},
"schema": {"type": "object"}
},
"required": [
"schema_group", "schema_name", "update", "version", "schema"
],
"additionalProperties": false
}
30 changes: 16 additions & 14 deletions prosper/test_utils/schema_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import logging
import pathlib


import genson
import pymongo
import semantic_version


from . import _version
ROOT_SCHEMA = pathlib.Path(__file__).parent / 'root_schema.schema'

class MongoContextManager:
Expand All @@ -18,19 +17,22 @@ class MongoContextManager:
connection_str requires {username}, {password} format strings
Args:
username (str): MongoDB Username
password (str): MongoDB Password
connection_str (str): connection string to platform
config (:obj:`prosper.common.prosper_config.ProsperConfig`): configparser-like object
# username (str): MongoDB Username
# password (str): MongoDB Password
database (str): which database to connect to
# connection_str (str): connection string to platform
"""
logger = logging.getLogger('PROSPER__test_helper')
def __init__(self, username, password, connection_str):
self.username = username
self.password = password
self.connection_str = connection_str

def __init__(self, config, database, config_key='MONGODB', log_key=_version.__library_name__):
self.username = config.get_option(config_key, 'username')
self.password = config.get_option(config_key, 'password')
self.database = database
self.connection_string = config.get_option(config_key, 'connection_string')
self._testmode = False
self._testmode_filepath = pathlib.Path(__file__).parent / 'testdb.json'

self.logger = logging.getLogger('PROSPER__test_helper')
# TODO: validate {} in connection_str

def __get_connector(self):
Expand All @@ -45,7 +47,7 @@ def __get_connector(self):
return tinymongo.TinyMongoClient(self._testmode_filepath)

return pymongo.MongoClient(
self.connection_str.format(
self.connection_string.format(
username=self.username,
password=self.password,
)
Expand All @@ -54,8 +56,8 @@ def __get_connector(self):
def __enter__(self):
"""with MongoContextManager() entrypoint"""
self.connection = self.__get_connector()
return self.connection
return self.connection[self.database]

def __exit__(self, *exc):
"""with MongoContextManager() exitpoint"""
self.connection_str.close()
self.connection.close()
26 changes: 24 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from setuptools import setup, find_packages
from setuptools.command.test import test as TestCommand

__package_name__ = 'ProsperTestUtils'
__library_name__ = 'test_utils'

def get_version(*args):
"""find __version__ for making package
Expand All @@ -26,6 +24,26 @@ def get_version(*args):

return version

def get_library_name(*args):
"""find __library_name__ for making package
TODO:
Fix in 3.7: https://stackoverflow.com/a/48916205
Args:
(str): python path to project
Returns:
str: __library_name__ value
"""

module = '.'.join(args) + '._version'
package = importlib.import_module(module)

library_name = package.__library_name__

return library_name

def hack_find_packages(include_str):
"""patches setuptools.find_packages issue
Expand All @@ -41,6 +59,10 @@ def hack_find_packages(include_str):

return new_list

__package_name__ = 'ProsperTestUtils'
__library_name__ = 'test_utils'


class PyTest(TestCommand):
"""PyTest cmdclass hook for test-at-buildtime functionality
Expand Down

0 comments on commit 655f4d7

Please sign in to comment.