This repository has been archived by the owner on Mar 12, 2018. It is now read-only.
/
test_couchdbkit.py
156 lines (118 loc) · 4.82 KB
/
test_couchdbkit.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from __future__ import with_statement
import unittest
from datetime import datetime
import flask
from flask.ext.couchdbkit import CouchDBKit
from couchdbkit.exceptions import BadValueError
def make_todo_model(couchdb):
class Todo(couchdb.Document):
title = couchdb.StringProperty()
text = couchdb.StringProperty()
done = couchdb.BooleanProperty(default=False)
pub_date = couchdb.DateTimeProperty(default=datetime.utcnow)
return Todo
class SchemaTestCase(unittest.TestCase):
def setUp(self):
app = flask.Flask(__name__)
app.config['COUCHDB_DATABASE'] = 'test_db_1'
app.config['TESTING'] = True
couchdb = CouchDBKit(app)
self.Todo = make_todo_model(couchdb)
self.app = app
self.couchdb = couchdb
with app.app_context():
self.db = self.couchdb.db
def tearDown(self):
del self.db.server[self.db.dbname]
def test_create(self):
with self.app.app_context():
todo = self.Todo(title='First Todo', text='Some text.')
todo.save()
self.assertTrue(self.db.doc_exist(todo._id))
def test_field_validation(self):
self.assertRaises(BadValueError, self.Todo, title=1, text='More text.')
def test_retrieve(self):
with self.app.app_context():
todo = self.Todo(title='2nd Todo', text='Some text.')
todo.save()
retrieved = self.Todo.get(todo._id)
self.assertEqual(retrieved.title, '2nd Todo')
class InitializationTestCase(unittest.TestCase):
def setUp(self):
app = flask.Flask(__name__)
app.config['COUCHDB_DATABASE'] = 'test_db_2'
app.config['TESTING'] = True
self.app = app
def tearDown(self):
with self.app.app_context():
del self.couchdb.db.server[self.app.config['COUCHDB_DATABASE']]
def test_init_db(self):
self.couchdb = CouchDBKit(self.app)
with self.app.app_context():
# the jump over db is needed, since get_or_create is done at first usage
self.assertTrue(self.couchdb.db.server.all_dbs().index('test_db_2'))
def test_late_initialization(self):
self.couchdb = CouchDBKit()
self.couchdb.init_app(self.app)
with self.app.app_context():
self.assertTrue(self.couchdb.db.server.all_dbs().index('test_db_2'))
class DocLoaderTestCase(unittest.TestCase):
def setUp(self):
app = flask.Flask(__name__)
app.config['COUCHDB_DATABASE'] = 'test_db_3'
app.config['TESTING'] = True
couchdb = CouchDBKit(app)
with app.app_context():
self.Todo = make_todo_model(couchdb)
self.Todo(title='First Todo', text='Some text.').save()
self.Todo(title='Second Todo', text='More text.', done=True).save()
self.Todo(title='Third Todo', text='Even more text.').save()
self.app = app
self.couchdb = couchdb
def tearDown(self):
with self.app.app_context():
del self.couchdb.db.server[self.app.config['COUCHDB_DATABASE']]
def test_doc_loader(self):
with self.app.app_context():
self.couchdb.sync()
results = self.Todo.view('todos/status_count', group=True).all()
self.assertFalse(results[0]['key'])
self.assertEqual(results[0]['value'], 2)
class BasicAppTestCase(unittest.TestCase):
def setUp(self):
app = flask.Flask(__name__)
app.config['COUCHDB_DATABASE'] = 'test_db_4'
app.config['TESTING'] = True
couchdb = CouchDBKit(app)
self.Todo = make_todo_model(couchdb)
@app.route('/')
def index():
return '\n'.join(row['key'] for row in
self.Todo.view('todos/all'))
@app.route('/add', methods=['POST'])
def add():
form = flask.request.form
todo = self.Todo(title=form['title'], text=form['text'])
todo.save()
return 'added'
self.app = app
self.couchdb = couchdb
self.couchdb.sync()
def tearDown(self):
with self.app.app_context():
del self.couchdb.server[self.app.config['COUCHDB_DATABASE']]
def test_basic_insert(self):
c = self.app.test_client()
c.post('/add', data=dict(title='First Item', text='The text'))
c.post('/add', data=dict(title='Second Item', text='More text'))
rv = c.get('/')
self.assertEqual(rv.data, 'First Item\nSecond Item')
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(SchemaTestCase))
suite.addTest(unittest.makeSuite(InitializationTestCase))
suite.addTest(unittest.makeSuite(DocLoaderTestCase))
suite.addTest(unittest.makeSuite(BasicAppTestCase))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')