Skip to content

Commit

Permalink
Merge branch 'master' of github.com:EVEprosper/ProsperTestUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
lockefox committed Apr 12, 2018
2 parents f216d95 + 4e44079 commit f77023b
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 9 deletions.
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include prosper/test_utils/version.txt
include prosper/test_utils/version.txt
include prosper/test_utils/root_schema.schema
7 changes: 5 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ProsperTestUtils
================

|Build Status| |Coverage Status| |PyPI Badge| |Docs|
|Build Status| |Coverage Status| |PyPI Badge| |Docs| |Gitter|

Collection of test helpers for Prosper python projects

Expand All @@ -20,4 +20,7 @@ Collection of test helpers for Prosper python projects
:target: https://badge.fury.io/py/ProsperTestUtils
.. |Docs| image:: https://readthedocs.org/projects/prospertestutils/badge/?version=latest
:target: http://prospertestutils.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status
:alt: Documentation Status
.. |Gitter| image:: https://badges.gitter.im/Join%20Chat.svg
:alt: Join the chat at https://gitter.im/EVEProsper/Lobby
:target: https://gitter.im/EVEProsper/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge
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
}
68 changes: 68 additions & 0 deletions prosper/test_utils/schema_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""schema testers"""
import json
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:
"""context manager for mongo connections
Notes:
connection_str requires {username}, {password} format strings
Args:
config (:obj:`prosper.common.prosper_config.ProsperConfig`): configparser-like object
database (str): which database to connect to
config_key (str): section name for ConfigParser
log_key (str): name of logger to attach to
"""

def __init__(
self,
config,
database,
config_key='MONGODB',
log_key='PROSPER__' + _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(log_key)
# TODO: validate {} in connection_str

def __get_connector(self):
"""switches between testmode/prod connectors
Returns:
pymongo.MongoCollection: connection to mongodb
"""
if self._testmode:
import tinymongo
return tinymongo.TinyMongoClient(self._testmode_filepath)

return pymongo.MongoClient(
self.connection_string.format(
username=self.username,
password=self.password,
)
)

def __enter__(self):
"""with MongoContextManager() entrypoint"""
self.connection = self.__get_connector()
return self.connection[self.database]

def __exit__(self, *exc):
"""with MongoContextManager() exitpoint"""
self.connection.close()
43 changes: 38 additions & 5 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,13 +59,20 @@ 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
http://doc.pytest.org/en/latest/goodpractices.html#manual-integration
"""
user_options = [('pytest-args=', 'a', 'Arguments to pass to pytest')]
user_options = [
('pytest-args=', 'a', 'Arguments to pass to pytest')

]

def initialize_options(self):
TestCommand.initialize_options(self)
Expand Down Expand Up @@ -99,24 +124,32 @@ def run_tests(self):
packages=hack_find_packages('prosper'),
include_package_data=True,
package_data={
'': ['LICENSE', 'README.rst'],
'': ['LICENSE', 'README.rst', ],
'prosper': [
'test_utils/root_schema.schema',
'test_utils/version.txt',
],
},
python_requires='>=3.5',
install_requires=[
'prospercommon',
'requests',
'semantic_version',
'plumbum',
'docker',
'genson',
'pymongo[tls]',
],
tests_require=[
'pytest',
'pytest_cov',
'tinymongo',
],
extras_require={
'dev':[
'sphinx',
'sphinxcontrib-napoleon',
]
],
},
cmdclass={
'test':PyTest,
Expand Down

0 comments on commit f77023b

Please sign in to comment.