From 529375e10bd0a4a0bf03c372f648224b872aabec Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 30 Aug 2016 08:32:23 -0400 Subject: [PATCH] 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) --- metrik/conf.py | 4 ++-- test/mongo_test.py | 4 ++-- test/test_conf.py | 12 +++++++++++- test/test_merge.py | 11 +++++++++++ 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/metrik/conf.py b/metrik/conf.py index db76628..15422f1 100644 --- a/metrik/conf.py +++ b/metrik/conf.py @@ -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 diff --git a/test/mongo_test.py b/test/mongo_test.py index d70feb2..24ebfff 100644 --- a/test/mongo_test.py +++ b/test/mongo_test.py @@ -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')) diff --git a/test/test_conf.py b/test/test_conf.py index 2d7596f..5871514 100644 --- a/test/test_conf.py +++ b/test/test_conf.py @@ -7,4 +7,14 @@ class ConfigurationTest(TestCase): def test_config_returns_values(self): config = get_config([]) - assert config is not None \ No newline at end of file + 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') \ No newline at end of file diff --git a/test/test_merge.py b/test/test_merge.py index 38881e4..7541bc0 100644 --- a/test/test_merge.py +++ b/test/test_merge.py @@ -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 \ No newline at end of file