|
1 | 1 | import unittest
|
| 2 | +from datetime import datetime |
| 3 | +import flask |
2 | 4 | from flaskext import sqlalchemy
|
3 | 5 |
|
4 | 6 |
|
| 7 | +class BasicAppTestCase(unittest.TestCase): |
| 8 | + |
| 9 | + def setUp(self): |
| 10 | + app = flask.Flask(__name__) |
| 11 | + app.config['SQLALCHEMY_ENGINE'] = 'sqlite://' |
| 12 | + app.config['TESTING'] = True |
| 13 | + db = sqlalchemy.SQLAlchemy(app) |
| 14 | + |
| 15 | + class Todo(db.Model): |
| 16 | + __tablename__ = 'todos' |
| 17 | + id = db.Column('todo_id', db.Integer, primary_key=True) |
| 18 | + title = db.Column(db.String(60)) |
| 19 | + text = db.Column(db.String) |
| 20 | + done = db.Column(db.Boolean) |
| 21 | + pub_date = db.Column(db.DateTime) |
| 22 | + |
| 23 | + def __init__(self, title, text): |
| 24 | + self.title = title |
| 25 | + self.text = text |
| 26 | + self.done = False |
| 27 | + self.pub_date = datetime.utcnow() |
| 28 | + |
| 29 | + @app.route('/') |
| 30 | + def index(): |
| 31 | + return '\n'.join(x.title for x in Todo.query.all()) |
| 32 | + |
| 33 | + @app.route('/add', methods=['POST']) |
| 34 | + def add(): |
| 35 | + form = flask.request.form |
| 36 | + todo = Todo(form['title'], form['text']) |
| 37 | + db.session.add(todo) |
| 38 | + db.session.commit() |
| 39 | + return 'added' |
| 40 | + |
| 41 | + db.create_all() |
| 42 | + |
| 43 | + self.app = app |
| 44 | + self.db = db |
| 45 | + self.Todo = Todo |
| 46 | + |
| 47 | + def tearDown(self): |
| 48 | + self.db.drop_all() |
| 49 | + |
| 50 | + def test_basic_insert(self): |
| 51 | + c = self.app.test_client() |
| 52 | + c.post('/add', data=dict(title='First Item', text='The text')) |
| 53 | + c.post('/add', data=dict(title='2nd Item', text='The text')) |
| 54 | + rv = c.get('/') |
| 55 | + assert rv.data == 'First Item\n2nd Item' |
| 56 | + |
| 57 | + def test_request_context(self): |
| 58 | + self.assertEqual(self.Todo.query, None) |
| 59 | + with self.app.test_request_context(): |
| 60 | + todo = self.Todo('Test', 'test') |
| 61 | + self.db.session.add(todo) |
| 62 | + self.db.session.commit() |
| 63 | + self.assertEqual(len(self.Todo.query.all()), 1) |
| 64 | + |
| 65 | + def test_query_recording(self): |
| 66 | + with self.app.test_request_context(): |
| 67 | + todo = self.Todo('Test 1', 'test') |
| 68 | + self.db.session.add(todo) |
| 69 | + self.db.session.commit() |
| 70 | + |
| 71 | + queries = sqlalchemy.get_debug_queries() |
| 72 | + self.assertEqual(len(queries), 1) |
| 73 | + query = queries[0] |
| 74 | + self.assert_('insert into' in query.statement.lower()) |
| 75 | + self.assertEqual(query.parameters[0], 'Test 1') |
| 76 | + self.assertEqual(query.parameters[1], 'test') |
| 77 | + self.assert_('test_sqlalchemy.py' in query.context) |
| 78 | + self.assert_('test_query_recording' in query.context) |
| 79 | + |
| 80 | + def test_helper_api(self): |
| 81 | + self.assertEqual(self.db.metadata, self.db.Model.metadata) |
| 82 | + |
| 83 | + |
5 | 84 | class PaginationTestCase(unittest.TestCase):
|
6 | 85 |
|
7 | 86 | def test_basic_pagination(self):
|
8 | 87 | p = sqlalchemy.Pagination(None, 1, 20, 500, [])
|
9 |
| - assert p.page == 1 |
10 |
| - assert not p.has_prev |
11 |
| - assert p.has_next |
12 |
| - assert p.total == 500 |
13 |
| - assert p.pages == 25 |
14 |
| - assert p.next_num == 2 |
15 |
| - assert list(p.iter_pages()) == [1, 2, 3, 4, 5, None, 24, 25] |
| 88 | + self.assertEqual(p.page, 1) |
| 89 | + self.assertFalse(p.has_prev) |
| 90 | + self.assert_(p.has_next) |
| 91 | + self.assertEqual(p.total, 500) |
| 92 | + self.assertEqual(p.pages, 25) |
| 93 | + self.assertEqual(p.next_num, 2) |
| 94 | + self.assertEqual(list(p.iter_pages()), |
| 95 | + [1, 2, 3, 4, 5, None, 24, 25]) |
16 | 96 | p.page = 10
|
17 |
| - assert list(p.iter_pages()) == [1, 2, None, 8, 9, 10, 11, |
18 |
| - 12, 13, 14, None, 24, 25] |
| 97 | + self.assertEqual(list(p.iter_pages()), |
| 98 | + [1, 2, None, 8, 9, 10, 11, 12, 13, 14, None, 24, 25]) |
19 | 99 |
|
20 | 100 |
|
21 | 101 | if __name__ == '__main__':
|
|
0 commit comments