-
Notifications
You must be signed in to change notification settings - Fork 9
/
test.py
66 lines (53 loc) · 2.17 KB
/
test.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import os
import sys
import bottle
from bottle.ext.mongo import MongoPlugin
import pymongo
class RedisTest(unittest.TestCase):
def setUp(self):
self.app = bottle.Bottle(catchall=False)
plugin = MongoPlugin(uri="mongodb://127.0.0.1",
db="bottle", json_mongo=True)
self.plugin = self.app.install(plugin)
def test_with_keyword(self):
@self.app.get('/')
def test(mongodb):
self.assertEqual(type(mongodb), pymongo.database.Database)
self.assertTrue(mongodb)
self.app({'PATH_INFO':'/', 'REQUEST_METHOD':'GET'}, lambda x, y: None)
def test_save(self):
@self.app.get('/')
def test(mongodb):
mongodb.drop_collection("bottle")
insert = {"lang": "python", "framework": "bottle"}
collection = mongodb['bottle'].insert(insert)
self.assertTrue(collection)
self.assertEqual(mongodb['bottle'].count(), 1)
self.app({'PATH_INFO':'/', 'REQUEST_METHOD':'GET'}, lambda x,y: None)
def test_get(self):
@self.app.get('/')
def test(mongodb):
mongodb.drop_collection("bottle")
insert = {"lang": "python", "framework": "bottle"}
collection = mongodb['bottle'].insert(insert)
try:
connection = pymongo.MongoClient()
except AttributeError:
connection = pymongo.Connection()
db = connection.bottle
get = db.bottle.find_one({})
self.assertTrue(get)
self.assertEqual(get, mongodb['bottle'].find_one())
self.app({'PATH_INFO':'/', 'REQUEST_METHOD':'GET'}, lambda x,y: None)
class ReplicaSetTest(RedisTest):
def setUp(self):
self.app = bottle.Bottle(catchall=False)
read_pref = pymongo.ReadPreference.SECONDARY
plugin = MongoPlugin(uri="mongodb://127.0.0.1:27017,127.0.0.1:27018/?replicaSet=testReplSet",
db="bottle", json_mongo=True, read_preference=read_pref, w=2)
self.plugin = self.app.install(plugin)
if __name__ == '__main__':
unittest.main()