Skip to content

Instantly share code, notes, and snippets.

@SunKing2
Last active May 27, 2017 21:20
Show Gist options
  • Save SunKing2/b080d44ce75cf56a4945786f11ac1d7f to your computer and use it in GitHub Desktop.
Save SunKing2/b080d44ce75cf56a4945786f11ac1d7f to your computer and use it in GitHub Desktop.
python mongodb create and retrieve Ubuntu 17.04 continuous testing with unittest
sudo apt-get install -y python3.6 python3-pip virtualenv mongodb
sudo systemctl start mongodb
virtualenv -p /usr/bin/python3.6 --distribute myproject
source myproject/bin/activate
pip install --upgrade pip
python -m pip install pymongo
pip install PyZen
cat > test_mongo.py <<EOL
import unittest
from pymongo import MongoClient
from pprint import pprint
class TestMongo(unittest.TestCase):
def test_insert(self):
self.longMessage = True
db = MongoClient(port=27017).pythonemps
db.employees.drop()
self.assertEqual(0, db.employees.count(), "employees collection should be empty")
db.employees.insert_one(
{
'name': 'Bob',
'salary': 44000
}
)
self.assertEqual(1, db.employees.count(), "employees collection should have one")
def test_find(self):
self.longMessage = True
db = MongoClient(port=27017).pythonemps
myrec = db.employees.find_one(
{
'name': 'Bob',
}
)
print("Here it is:")
pprint(myrec)
print("The end.")
self.assertEqual("Bob", myrec["name"], "retrieved record should have name Bob")
self.assertEqual(44000, myrec["salary"], "retrieved record should have correct salary")
if __name__ == '__main__':
unittest.main()
EOL
python test_mongo.py
# use pyzen to rerun tests automatically:
#pyzen test_mongo.py
# now change code in another terminal window, the tests magically rerun :)
# the end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment