summaryrefslogtreecommitdiffstats
path: root/utils/test/result_collection_api/tests/unit/test_fake_pymongo.py
blob: 6920fcad8f92d652d40a5baabfda763dca9d90ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import unittest
from tornado.web import Application
from tornado import gen
from tornado.testing import AsyncHTTPTestCase, gen_test

import fake_pymongo


class MyTest(AsyncHTTPTestCase):
    def setUp(self):
        super(MyTest, self).setUp()
        self.db = fake_pymongo
        self.addCleanup(self._clear)
        self.io_loop.run_sync(self.fixture_setup)

    def get_app(self):
        return Application()

    @gen.coroutine
    def fixture_setup(self):
        self.test1 = {'_id': '1', 'name': 'test1'}
        self.test2 = {'name': 'test2'}
        yield self.db.pods.insert({'_id': '1', 'name': 'test1'})
        yield self.db.pods.insert({'name': 'test2'})

    @gen_test
    def test_find_one(self):
        user = yield self.db.pods.find_one({'name': 'test1'})
        self.assertEqual(user, self.test1)
        self.db.pods.remove()

    @gen_test
    def test_find(self):
        cursor = self.db.pods.find()
        names = []
        while (yield cursor.fetch_next):
            ob = cursor.next_object()
            names.append(ob.get('name'))
        self.assertItemsEqual(names, ['test1', 'test2'])

    @gen_test
    def test_update(self):
        yield self.db.pods.update({'_id': '1'}, {'name': 'new_test1'})
        user = yield self.db.pods.find_one({'_id': '1'})
        self.assertEqual(user.get('name', None), 'new_test1')

    @gen_test
    def test_remove(self):
        yield self.db.pods.remove({'_id': '1'})
        user = yield self.db.pods.find_one({'_id': '1'})
        self.assertIsNone(user)

    @gen_test
    def test_insert_check_keys(self):
        yield self.db.pods.insert({'_id': '1', 'name': 'test1'},
                                  check_keys=False)
        cursor = self.db.pods.find({'_id': '1'})
        names = []
        while (yield cursor.fetch_next):
            ob = cursor.next_object()
            names.append(ob.get('name'))
        self.assertItemsEqual(names, ['test1', 'test1'])

    def _clear(self):
        self.db.pods.clear()

if __name__ == '__main__':
    unittest.main()