Skip to content

Commit 3f35f9c

Browse files
committed
新增mongo测试
1 parent 0af7f30 commit 3f35f9c

1 file changed

Lines changed: 73 additions & 21 deletions

File tree

test/test_mongo.py

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,84 @@
22
__author__ = 'zhanghe'
33

44

5-
from pymongo import MongoClient
5+
import sys
6+
sys.path.append('..')
7+
from tools.mongo import Mongodb
68

79

8-
client = MongoClient("localhost", 27017)
10+
db_config = {
11+
'host': 'localhost',
12+
'port': 27017,
13+
'database': 'test_user'
14+
}
915

10-
# 显示数据库名称列表
11-
print client.database_names()
1216

13-
# 获取数据库对象
14-
db = client.get_database('local')
15-
# db = client.local
16-
# db = client['local']
17-
print db
17+
test_date = [
18+
{
19+
'_id': 1,
20+
'id': 1,
21+
'name': 'Lily',
22+
'sex': 'F',
23+
'age': 20,
24+
'city': 'shanghai',
25+
},
26+
{
27+
'_id': 2,
28+
'id': 2,
29+
'name': 'Tom',
30+
'sex': 'M',
31+
'age': 22,
32+
'city': 'shanghai',
33+
},
34+
{
35+
'_id': 3,
36+
'id': 3,
37+
'name': 'Jerry',
38+
'sex': 'M',
39+
'age': 22,
40+
'city': 'beijing',
41+
}
42+
]
1843

19-
# 显示local库下的所有文档集合(数据表)的名称列表
20-
print db.collection_names()
21-
print db.collection_names(include_system_collections=False)
2244

23-
# 获得文档集合的对象
24-
collection = db.get_collection('startup_log')
25-
# collection = db.startup_log
26-
# collection = db['startup_log']
27-
print collection
45+
def test():
46+
try:
47+
conn = Mongodb(db_config)
48+
print conn.db
49+
print conn.find_one('user')
50+
print conn.insert('user', test_date)
51+
conn.output_rows('user')
52+
except Exception, e:
53+
print e
2854

29-
# 查询一条记录
30-
print collection.find_one()
3155

32-
# 查询集合记录的总数
33-
print collection.count()
56+
if __name__ == '__main__':
57+
test()
58+
59+
60+
"""
61+
Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), u'test_user')
62+
None
63+
[1, 2, 3]
64+
********** 表名[user] [1/3] **********
65+
city : shanghai
66+
name : Lily
67+
sex : F
68+
age : 20
69+
id : 1
70+
_id : 1
71+
********** 表名[user] [2/3] **********
72+
city : shanghai
73+
name : Tom
74+
sex : M
75+
age : 22
76+
id : 2
77+
_id : 2
78+
********** 表名[user] [3/3] **********
79+
city : beijing
80+
name : Jerry
81+
sex : M
82+
age : 22
83+
id : 3
84+
_id : 3
85+
"""

0 commit comments

Comments
 (0)