Commit 2677598a by Will Daly

Merge pull request #938 from rocha/mock-mongo-on-event-tracking-tests

Mock mongo event tracking backend during tests
parents 2b5fa799 6a64b404
...@@ -2,7 +2,7 @@ from __future__ import absolute_import ...@@ -2,7 +2,7 @@ from __future__ import absolute_import
from uuid import uuid4 from uuid import uuid4
import pymongo from mock import patch
from django.test import TestCase from django.test import TestCase
...@@ -11,33 +11,30 @@ from track.backends.mongodb import MongoBackend ...@@ -11,33 +11,30 @@ from track.backends.mongodb import MongoBackend
class TestMongoBackend(TestCase): class TestMongoBackend(TestCase):
def setUp(self): def setUp(self):
# Use a random database name to prevent problems with tests running self.mongo_patcher = patch('track.backends.mongodb.MongoClient')
# simultenousely against the same mongo instance self.addCleanup(self.mongo_patcher.stop)
database = '_track_backends_mongodb_{0}'.format(uuid4().hex) self.mongo_patcher.start()
collection = '_test'
self.connection = pymongo.MongoClient() self.backend = MongoBackend()
self.database = self.connection[database]
self.collection = self.database[collection]
# During tests, wait until mongo acknowledged the write def test_mongo_backend(self):
write_concern = 1 events = [{'test': 1}, {'test': 2}]
self.backend = MongoBackend( self.backend.send(events[0])
database=database, self.backend.send(events[1])
collection=collection,
w=write_concern
)
def test_mongo_backend(self): # Check if we inserted events into the database
self.backend.send({'test': 1})
self.backend.send({'test': 2}) calls = self.backend.collection.insert.mock_calls
self.assertEqual(len(calls), 2)
# Get all the objects in the db ignoring _id # Unpack the arguments and check if the events were used
results = list(self.collection.find({}, {'_id': False})) # as the first argument to collection.insert
self.assertEqual(len(results), 2) def first_argument(call):
self.assertEqual(results, [{'test': 1}, {'test': 2}]) _, args, _ = call
return args[0]
def tearDown(self): self.assertEqual(events[0], first_argument(calls[0]))
self.connection.drop_database(self.database) self.assertEqual(events[1], first_argument(calls[1]))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment