test_mongodb.py 1.07 KB
Newer Older
1 2
from __future__ import absolute_import

3
from mock import patch
4 5 6 7 8 9 10 11

from django.test import TestCase

from track.backends.mongodb import MongoBackend


class TestMongoBackend(TestCase):
    def setUp(self):
12
        super(TestMongoBackend, self).setUp()
13 14
        self.mongo_patcher = patch('track.backends.mongodb.MongoClient')
        self.mongo_patcher.start()
15
        self.addCleanup(self.mongo_patcher.stop)
16

17
        self.backend = MongoBackend()
18

19 20
    def test_mongo_backend(self):
        events = [{'test': 1}, {'test': 2}]
21

22 23
        self.backend.send(events[0])
        self.backend.send(events[1])
24

25 26 27 28 29
        # Check if we inserted events into the database

        calls = self.backend.collection.insert.mock_calls

        self.assertEqual(len(calls), 2)
30

31 32
        # Unpack the arguments and check if the events were used
        # as the first argument to collection.insert
33

34 35 36
        def first_argument(call):
            _, args, _ = call
            return args[0]
37

38 39
        self.assertEqual(events[0], first_argument(calls[0]))
        self.assertEqual(events[1], first_argument(calls[1]))