Skip to content

Commit

Permalink
adding table-clear prestart check
Browse files Browse the repository at this point in the history
  • Loading branch information
lockefox committed Apr 16, 2018
1 parent 1b5df5c commit efacbf7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""conftest: pytest configuration"""
import helpers

helpers.clear_mongo_test_db()
55 changes: 54 additions & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""common test stuff"""

import os
import warnings

import pymongo

import prosper.common.prosper_config as p_config

Expand All @@ -11,3 +13,54 @@

TEST_CONFIG = p_config.ProsperConfig(os.path.join(HERE, 'test.cfg'))
ROOT_CONFIG = p_config.ProsperConfig(os.path.join(ROOT, 'app.cfg'))

DATABASE_NAME = 'mongo_test'

def can_connect_to_mongo(config):
"""returns true/false whether mongo is available
Args:
config (:obj:`prosper_config.ProsperConfig`): configparser with `MONGO` keys
Returns:
bool: can connect to mongo
"""
if not any([
config.get_option('MONGO', 'username'),
config.get_option('MONGO', 'password'),
config.get_option('MONGO', 'connection_string'),
]):
return False

# TODO: connect to prod mongo with keys, raise if keys are bad
return True

def clear_mongo_test_db(
database_name=DATABASE_NAME,
config=TEST_CONFIG,
):
"""clear out test/dummy data
WARNING: DELETES TABLES!!!
Args:
database_name (str): name of database to clear out
config (:obj:`prosper_config.ProsperConfig`): configpaser with `MONGO` keys
"""
if not can_connect_to_mongo(config):
return

conn = pymongo.MongoClient(
config.get_option('MONGO', 'connection_string').format(
username=config.get_option('MONGO', 'username'),
password=config.get_option('MONGO', 'password'),
)
)
tables = conn[database_name].collection_names()
print(tables)
for table in tables:
conn[database_name][table].remove()

conn.close()
11 changes: 3 additions & 8 deletions tests/test_schema_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class TestMongoContextManager:
"""validate expected behavior for MongoContextManager"""
database_name = 'mongo_test'
demo_data = [
{'butts': True, 'many': 10},
{'butts': False, 'many': 100},
Expand All @@ -17,7 +16,7 @@ def test_mongo_context_testmode(self, tmpdir):
"""test with _testmode enabled"""
mongo_context = schema_utils.MongoContextManager(
helpers.TEST_CONFIG,
self.database_name,
helpers.DATABASE_NAME,
)
mongo_context._testmode = True
mongo_context._testmode_filepath = tmpdir
Expand All @@ -31,16 +30,12 @@ def test_mongo_context_testmode(self, tmpdir):

def test_mongo_context_prodmode(self):
"""test against real mongo"""
if not any([
helpers.TEST_CONFIG.get_option('MONGO', 'username'),
helpers.TEST_CONFIG.get_option('MONGO', 'password'),
helpers.TEST_CONFIG.get_option('MONGO', 'connection_string'),
]):
if not helpers.can_connect_to_mongo(helpers.TEST_CONFIG):
pytest.xfail('no mongo credentials')

mongo_context = schema_utils.MongoContextManager(
helpers.TEST_CONFIG,
self.database_name,
helpers.DATABASE_NAME,
)

with mongo_context as _:
Expand Down

0 comments on commit efacbf7

Please sign in to comment.