1
0
mirror of https://github.com/bspeice/metrik synced 2024-11-04 22:48:11 -05:00

Guard against improper drops

I have destroyed a good bit of data so far because unit tests (correctly) dropped databases when they were done, but the config told it to drop the live database (incorrectly)
This commit is contained in:
Bradlee Speice 2016-08-30 08:32:23 -04:00
parent 2232283ec5
commit 529375e10b
4 changed files with 26 additions and 5 deletions

View File

@ -15,7 +15,7 @@ def get_default_conf():
# Normally it's terrible practice to put static calls into the signature,
# but this is safe (for now) since the get_config_locations() won't change
# during a run. I.e. it starts up and that's the only time it's ever needed.
def get_config(extra_locations=get_config_locations()):
def get_config(extra_locations=get_config_locations(), is_test=False):
config = RawConfigParser()
config.readfp(get_default_conf())
@ -28,7 +28,7 @@ def get_config(extra_locations=get_config_locations()):
# end-users, but I'm calling it a special case for testing
is_travis = 'TRAVIS_BUILD_NUMBER' in os.environ
is_pytest = hasattr(sys, '_called_from_test')
if is_pytest or is_travis:
if is_pytest or is_travis or is_test:
config.set('metrik', 'mongo_database', 'metrik-test')
return config

View File

@ -7,7 +7,7 @@ from metrik.targets.mongo import MongoTarget
class MongoTest(TestCase):
def setUp(self):
config = get_config()
config = get_config(is_test=True)
self.client = MongoClient(
host=config.get('metrik', 'mongo_host'),
port=config.getint('metrik', 'mongo_port'))
@ -15,7 +15,7 @@ class MongoTest(TestCase):
def tearDown(self):
super(MongoTest, self).tearDown()
config = get_config()
config = get_config(is_test=True)
self.client.drop_database(config.get('metrik', 'mongo_database'))

View File

@ -7,4 +7,14 @@ class ConfigurationTest(TestCase):
def test_config_returns_values(self):
config = get_config([])
assert config is not None
assert config is not None
def test_config_manual_test_instruction(self):
config = get_config()
self.assertEqual(config.get('metrik', 'mongo_database'), 'metrik')
config = get_config(is_test=False)
self.assertEqual(config.get('metrik', 'mongo_database'), 'metrik')
config = get_config(is_test=True)
self.assertEqual(config.get('metrik', 'mongo_database'), 'metrik-test')

View File

@ -33,3 +33,14 @@ class MergeTest(MongoTest):
assert item_retrieved is not None
assert item_retrieved['string'] == item_string
def test_merge_is_nondestructive(self):
item1_id = self.db2[self.collection_name].save({})
item2_id = self.db[self.collection_name].save({})
assert len(list(self.db[self.collection_name].find())) == 1
assert len(list(self.db2[self.collection_name].find())) == 1
merge(self.client, self.client,
self.db.name, self.db2.name)
assert len(list(self.db[self.collection_name].find())) == 0
assert len(list(self.db2[self.collection_name].find())) == 2