summaryrefslogtreecommitdiffstats
path: root/utils/test/result_collection_api/tests/unit/test_fake_pymongo.py
blob: 5ddbf28d98b21dbfcd47c92d972688004abf6e56 (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
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.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.pod.insert({'_id': '1', 'name': 'test1'})
        yield self.db.pod.insert({'name': 'test2'})

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

    @gen_test
    def test_find(self):
        cursor = self.db.pod.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.pod.update({'_id': '1'}, {'name': 'new_test1'})
        user = yield self.db.pod.find_one({'_id': '1'})
        self.assertEqual(user.get('name', None), 'new_test1')

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

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