Skip to content

Commit

Permalink
add test frameowrk to test framework (to test framework...)
Browse files Browse the repository at this point in the history
  • Loading branch information
lockefox committed Apr 15, 2018
1 parent 8b0bb4b commit edfe8ab
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 10 deletions.
16 changes: 16 additions & 0 deletions prosper/test_utils/app.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[LOGGING]
log_level = INFO
log_path = logs
log_freq = midnight
log_total = 30
discord_webhook = #SECRET
discord_level = ERROR
slack_webhook = #SECRET
slack_level = ERROR
hipchat_webhook = #SECRET
hipchat_level = ERROR

[MONGO]
username = #SECRET
password = #SECRET
connection_string = #SECRET
12 changes: 12 additions & 0 deletions prosper/test_utils/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""exceptions for test_utils cases"""
class TestUtilsException(Exception):
"""general exception for prosper.test_utils modules"""
pass

class DockerUtilsException(TestUtilsException):
"""general exception for docker_utils libraries"""
pass

class DockerNotFound(DockerUtilsException):
"""cannot find/connect to Docker in environment"""
pass
8 changes: 4 additions & 4 deletions prosper/test_utils/schema_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ def __init__(
self,
config,
database,
config_key='MONGODB',
log_key='PROSPER__' + _version.__library_name__
config_key='MONGO',
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._testmode_filepath = pathlib.Path(__file__).parent
self.logger = logging.getLogger(log_key)
# TODO: validate {} in connection_str

Expand All @@ -49,7 +49,7 @@ def __get_connector(self):
"""
if self._testmode:
import tinymongo
return tinymongo.TinyMongoClient(self._testmode_filepath)
return tinymongo.TinyMongoClient(foldername=str(self._testmode_filepath))

return pymongo.MongoClient(
self.connection_string.format(
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ def run_tests(self):
'prosper': [
'test_utils/root_schema.schema',
'test_utils/version.txt',
'test_utils/app.cfg',
],
},
python_requires='>=3.5',
Expand Down
13 changes: 13 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""common test stuff"""

import os

import prosper.common.prosper_config as p_config

HERE = os.path.abspath(os.path.dirname(__file__))
ROOT = os.path.join(
os.path.dirname(HERE), 'prosper', 'test_utils'
)

TEST_CONFIG = p_config.ProsperConfig(os.path.join(HERE, 'test.cfg'))
ROOT_CONFIG = p_config.ProsperConfig(os.path.join(ROOT, 'app.cfg'))
Empty file added tests/test.cfg
Empty file.
12 changes: 6 additions & 6 deletions tests/test_docker_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
import pytest
import _pytest.outcomes

import prosper.common.test_helpers as p_helpers
import prosper.common.exceptions as exceptions
import prosper.test_utils.docker_utils as docker_utils
import prosper.test_utils.exceptions as exceptions

def test_assert_docker_regular():
"""make sure p_helpers.assert_docker() raises expected error"""
p_helpers.DOCKER_OK = False
docker_utils.DOCKER_OK = False
with pytest.raises(exceptions.DockerNotFound):
p_helpers.assert_docker()
docker_utils.assert_docker()

def test_assert_docker_xfail():
"""make sure p_helpers.assert_docker() raises xfail in mode"""
p_helpers.DOCKER_OK = False
docker_utils.DOCKER_OK = False
with pytest.raises(_pytest.outcomes.XFailed):
p_helpers.assert_docker(xfail=True)
docker_utils.assert_docker(xfail=True)
30 changes: 30 additions & 0 deletions tests/test_schema_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""validate prosper.test_utils.schema_utils"""
import os

import pytest
import helpers

import prosper.test_utils.schema_utils as schema_utils

class TestMongoContextManager:
"""validate expected behavior for MongoContextManager"""
database_name = 'mongo_test'
demo_data = [
{'butts': True, 'many': 10},
{'butts': False, 'many': 100},
]
def test_mongo_context_testmode(self, tmpdir):
"""test with _testmode enabled"""
mongo_context = schema_utils.MongoContextManager(
helpers.TEST_CONFIG,
self.database_name,
)
mongo_context._testmode = True
mongo_context._testmode_filepath = tmpdir
with mongo_context as _:
_['test_collection'].insert(self.demo_data)

with mongo_context as _:
data = _['test_collection'].find_one({'butts': True})

assert data['many'] == 10

0 comments on commit edfe8ab

Please sign in to comment.