1
0
mirror of https://github.com/bspeice/metrik synced 2024-11-23 07:38:09 -05:00

Initial tests for merging script

This commit is contained in:
Bradlee Speice 2016-08-29 23:05:35 -04:00
parent e88547c47d
commit 905fb6092c
3 changed files with 44 additions and 7 deletions

View File

@ -54,7 +54,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_3" default="false" assert-keyword="false" jdk-15="false" project-jdk-name="Python 3.5.1 (C:\Users\Bradlee Speice\Anaconda3\python.exe)" project-jdk-type="Python SDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_3" default="false" assert-keyword="false" jdk-15="false" project-jdk-name="Python 2.7.12 virtualenv at ~/Development/metrik/py2-virt" project-jdk-type="Python SDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="PythonCompatibilityInspectionAdvertiser">

View File

@ -9,9 +9,9 @@ def open_connection(host, port):
return MongoClient(host=host, port=port)
def merge(con1, con2, database_name='metrik'):
database1 = con1[database_name]
database2 = con2[database_name]
def merge(con1, con2, db1, db2):
database1 = con1[db1]
database2 = con2[db2]
collections = database1.collection_names(include_system_collections=False)
for collection_name in collections:
collection1 = database1[collection_name]
@ -37,14 +37,16 @@ def main():
help='The port number of the `left` database')
parser.add_argument('-o', '--port-2', default=27017, dest='port2', type=int,
help='The port number of the `right` database')
parser.add_argument('-d', '--database', default='metrik',
help='The database to merge from one host to the other')
parser.add_argument('-d', '--database-1', default='metrik', dest='db1',
help='The database on the `left` host we are merging from')
parser.add_argument('-s', '--database-2', default='metrik', dest='db2',
help='The database on the `right` host we are merging into')
parser.add_argument('-v', '--version', action='version', version=__version__)
args = parser.parse_args()
con1 = open_connection(args.host1, args.port1)
con2 = open_connection(args.host2, args.port2)
merge(con1, con2, args.database)
merge(con1, con2, args.db1, args.db2)
con1.close()
con2.close()

35
test/test_merge.py Normal file
View File

@ -0,0 +1,35 @@
import random, string
from metrik.merge import merge, open_connection
from metrik.conf import get_config
from test.mongo_test import MongoTest
class MergeTest(MongoTest):
db2_name = 'metrik_test_2'
collection_name = 'merge_test'
def setUp(self):
super(MergeTest, self).setUp()
self.client2 = self.client
self.db2 = self.client2[self.db2_name]
def tearDown(self):
super(MergeTest, self).tearDown()
self.client2.drop_database(self.db2_name)
def test_left_right_merge(self):
item_string = ''.join(random.choice(string.lowercase) for i in range(10))
item = {'string': item_string}
item_id = self.db[self.collection_name].save(item)
merge(self.client, self.client2,
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())) == 1
item_retrieved = self.db2[self.collection_name].find_one({'_id': item_id})
assert item_retrieved is not None
assert item_retrieved['string'] == item_string