This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app_tests.py
82 lines (63 loc) · 2.13 KB
/
app_tests.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import app as a
import json
import unittest
from mockredis import MockRedis
class MockRedisWrapper(MockRedis):
'''A wrapper to add the `from_url` classmethod'''
@classmethod
def from_url(cls, *args, **kwargs):
return cls()
class AppTestCase(unittest.TestCase):
def setUp(self):
a.app.config.update(dict(
TESTING=True,
REDIS_PROVIDER=MockRedisWrapper,
))
self.app = a.app.test_client()
def test_pad_key(self):
self.assertEqual(a.pad_key('bar'), 'pad:bar')
self.assertEqual(a.pad_key('bar', 'foo'), 'foo:bar')
def test_pad_get(self):
with a.app.app_context():
rs = a.get_redis()
rs.set('pad:foo', 'bar')
self.assertEqual(a.pad_get('foo'), 'bar')
self.assertEqual(a.pad_get('foobar'), '')
def test_pad_set(self):
with a.app.app_context():
rs = a.get_redis()
a.pad_set('foo', 'bar')
a.pad_set('foobar', 'foobar')
self.assertEqual(rs.get('pad:foo'), 'bar')
self.assertEqual(rs.get('pad:foobar'), 'foobar')
def test_route_get(self):
with a.app.app_context():
a.pad_set('foo', '#foobar context#')
rv = self.app.get('/foo')
self.assertTrue('#foobar context#' in rv.data)
def test_route_set(self):
rv = self.app.post('/foo', data=dict(
t='foobar'
))
data = self.load_json(rv.data)
self.assertEqual(data['message'], 'ok')
self.assertEqual(data['padname'], 'foo')
def test_route_set_fail(self):
rv = self.app.post('/foo')
self.assertEqual(rv.status_code, 400)
def test_route_main(self):
rv = self.app.get('/')
self.assertEqual(rv.status_code, 302)
rv = self.app.get('/', follow_redirects=True)
self.assertEqual(rv.status_code, 200)
@staticmethod
def load_json(string):
try:
data = json.loads(string)
except ValueError:
return False
return data
if __name__ == '__main__':
unittest.main()