Skip to content

Commit

Permalink
Merge pull request #3 from aaltat/master
Browse files Browse the repository at this point in the history
Initial version of unit test
  • Loading branch information
jerry57 committed Apr 8, 2013
2 parents 222e812 + cf71442 commit 5a7a347
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ downloads/
report.html
output.xml
log.html
*.sublime-project
*.sublime-workspace

File renamed without changes.
123 changes: 123 additions & 0 deletions test/unit/test_MongoQuery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import pymongo
import subprocess
import tempfile
import shutil
import unittest
import os
import time
import sys

# Get src directory and put it in path
# To get import forking for MongoDBLibrary

ROOT_DIR = os.path.dirname(os.path.abspath('..'))
SRC_DIR = os.path.join(ROOT_DIR, "src")
sys.path.insert(0, SRC_DIR)

# Suppress logging from MongoDBLibrary
#f = open(os.devnull, 'w')
#sys.stdout = f

# settings for unit test
test_mongo_connection_host = 'localhost'
test_mongo_connection_port = 51000
test_database_name = 'test_database'
test_collection_name = 'test_collection'
data1 = {"firstName": "John","lastName": "Smith","age": 25,"address": {"streetAddress": "21 2nd Street","city": "New York","state": "NY","postalCode": 10021},"phoneNumbers": [{"type": "home","number": "212 555-1234"},{"type": "fax","number": "646 555-4567"}]}

from MongoDBLibrary import MongoDBLibrary


class TestMongoDBLibrary(unittest.TestCase):

def setUp(self):
self._tmpdir = tempfile.mkdtemp()
self._process = subprocess.Popen(['mongod', '--bind_ip', test_mongo_connection_host,
'--port', str(test_mongo_connection_port),
'--dbpath', self._tmpdir,
'--nojournal', '--nohttpinterface',
'--noauth', '--smallfiles',
'--syncdelay', '0',
'--maxConns', '10',
'--nssize', '1', ],
stdout=open(os.devnull, 'wb'),
stderr=subprocess.STDOUT)
# Mongo is super fast but
# Wait for database connection
for i in range(5):
time.sleep(0.1)
try:
self._conn = pymongo.MongoClient(test_mongo_connection_host, test_mongo_connection_port)
except pymongo.errors.ConnectionFailure:
continue
else:
break
else:
self.tearDown()
assert False, 'Cannot connect to the mongodb test instance'

def test_get_mongodb_databases(self):
self.mongo_create_db()
self.mongo_inser_data(data1)

a = MongoDBLibrary()
a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port)
db = a.get_mongodb_databases()
a.disconnect_from_mongodb()

self.mongo_drop_database()

expected = ['test_database', 'local']
self.assertEqual(db, expected)

def test_get_mongodb_collections(self):
self.mongo_create_db()
self.mongo_inser_data(data1)

a = MongoDBLibrary()
a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port)
collection_names = a.get_mongodb_collections(test_database_name)
a.disconnect_from_mongodb()

self.mongo_drop_database()

expected = ['system.indexes', 'test_collection']
self.assertEqual(collection_names, expected)

def test_drop_mongodb_database(self):
self.mongo_create_db()
self.mongo_inser_data(data1)

a = MongoDBLibrary()
a.connect_to_mongodb(dbHost=test_mongo_connection_host, dbPort=test_mongo_connection_port)
a.drop_mongodb_database(test_database_name)
a.disconnect_from_mongodb()

database_names = self.mongo_database_names()
expected = ['local']
self.assertEqual(database_names, expected)

def tearDown(self):
# Terminate Mongodb process
if self._process:
self._process.terminate()
self._process.wait()
self._process = None
shutil.rmtree(self._tmpdir, ignore_errors=True)

def mongo_create_db(self, db=test_database_name, collection=test_collection_name):
test_db = self._conn[db]
self._collection = test_db[collection]

def mongo_drop_database(self, db=test_database_name):
self._conn.drop_database(db)

def mongo_inser_data(self, data):
self._collection.insert(data)

def mongo_database_names(self, db=test_database_name, collection=test_collection_name):
return self._conn.database_names()


if __name__ == '__main__':
unittest.main()
21 changes: 21 additions & 0 deletions test/unit_test_readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

- Unit test are defined in the src/unit/test_*.py files
- Currently each test suite spans a subprocess of the mongo db in the setUp
- It is assumed that each unit test will do independently:
* Set up all required database(s), collections and documents in collection
* Clean all that is done is test setup
* There are mongo_* functions for these purposes

- Unit test are run from src/unit folder by executing command: python -m unittest discover
- For more details hot to run different unit test (suites), see python unit test documentation [1].


TODO:
Unit test setUp good benefit some refactoring:
1) All of the def mongo_* functions could be placed in some other file, when this one unit test file is divided
2) Also starting up the mongo database could be a separate class all together
3) Also some cored could take a look and see if unit test written by tester make any sense at all.
4) Currently running unit test requires python 2.7 or in older version unittest2 [2] is required. Some automatic detection would be nice

[1] http://docs.python.org/2/library/unittest.html#command-line-interface
[2] https://pypi.python.org/pypi/unittest2

0 comments on commit 5a7a347

Please sign in to comment.