forked from alexvassel/adnow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
153 lines (114 loc) · 5.78 KB
/
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
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
# -*- coding: utf-8 -*-
from datetime import datetime
from http import client
import io
import json
from urllib.parse import urlencode
import os
import unittest
import mock
from tornado.httpclient import HTTPRequest, HTTPResponse
from tornado.concurrent import Future
from tornado.testing import AsyncHTTPTestCase
from app import application
from helpers import get_next_page, get_commit_from_json, create_tables
from models import DB, Repo, Commit
class ApplicationTestCase(AsyncHTTPTestCase):
TEST_REPO = dict(href='https://github.com/alexvassel/adnow', name='test_name',
owner_name='test_owner_name', next_page='test_next_page')
TEST_COMMIT = dict(author='test_author', message='test_message', sha='test_sha',
date_added=datetime.now())
MESSAGES = dict(get_more='Подгрузить еще', no_records='записей нет')
TEST_COUNT = 3
data = open('fixtures/commits.json')
def get_app(self):
return application
def setUp(self):
create_tables()
super().setUp()
def tearDown(self):
os.remove(DB)
super().tearDown()
def test_redirect(self):
response = self.fetch(self.get_app().reverse_url('index'), follow_redirects=False)
self.assertEqual(response.code, client.MOVED_PERMANENTLY)
def test_index(self):
response = self.fetch(self.get_app().reverse_url('index'))
self.assertEqual(response.code, client.OK)
def test_create_repo(self):
response = self.fetch(self.get_app().reverse_url('create'))
self.assertNotIn('errors', response.body.decode())
response = self.fetch(self.get_app().reverse_url('create'), method='POST', body='')
self.assertIn('errors', response.body.decode())
data = {'href': self.TEST_REPO['href'] + '/'}
response = self.fetch(self.get_app().reverse_url('create'), method='POST',
body=urlencode(data).encode())
self.assertIn('errors', response.body.decode())
def test_repo_commits(self):
repo = Repo.create(**self.TEST_REPO)
repo.save()
response = self.fetch(self.get_app().reverse_url('view', repo.id))
self.assertIn(self.MESSAGES['no_records'], response.body.decode())
self.assertIn(self.MESSAGES['get_more'], response.body.decode())
for commit in range(self.TEST_COUNT):
commit_data = self.TEST_COMMIT
commit_data.update({'repo': repo})
c = Commit(**commit_data)
c.save()
response = self.fetch(self.get_app().reverse_url('view', repo.id))
self.assertEqual(response.body.decode().count(self.TEST_COMMIT['message']),
self.TEST_COUNT)
self.assertIn(self.MESSAGES['get_more'], response.body.decode())
repo.next_page = None
repo.save()
response = self.fetch(self.get_app().reverse_url('view', repo.id))
self.assertNotIn(self.MESSAGES['get_more'], response.body.decode())
@mock.patch('helpers.BaseHandler.get_commits')
def test_commits_create(self, get_commits):
fixtures = self.data.buffer.read()
jsn = json.loads(fixtures.decode())
request = HTTPRequest(self.TEST_REPO['href'])
response = HTTPResponse(request, client.OK, buffer=io.BytesIO(fixtures))
future = Future()
future.set_result(response)
get_commits.return_value = future
body = {'href': self.TEST_REPO['href']}
response = self.fetch(self.get_app().reverse_url('create'), method='POST',
body=urlencode(body).encode())
self.assertIn('всего {}'.format(len(jsn)), response.body.decode())
self.assertEqual(len(jsn), Commit.select().count())
self.assertEqual(1, Repo.select().count())
class HeaderParsingTest(unittest.TestCase):
data = '''<https://api.github.com/repositories/4164482/commits?page=782>; rel="next", <https://api.github.com/repositories/4164482/commits?page=782>; rel="last", <https://api.github.com/repositories/4164482/commits?page=1>; rel="first", <https://api.github.com/repositories/4164482/commits?page=780>; rel="prev"'''
def test_header_parsing(self):
expect = {'rel="next"': '<https://api.github.com/repositories/4164482/commits?page=782>',
'rel="prev"': '<https://api.github.com/repositories/4164482/commits?page=780>',
'rel="last"': '<https://api.github.com/repositories/4164482/commits?page=782>',
'rel="first"': '<https://api.github.com/repositories/4164482/commits?page=1>'}
r = get_next_page(self.data)
self.assertDictEqual(r, expect)
def test_common(self):
for kind in (str, list, dict, set, int, float):
r = get_next_page(kind())
self.assertIsInstance(r, dict)
self.assertDictEqual(r, {})
for obj in ([1], (1,), dict(a=1), 1, 1.0):
self.assertRaises(AttributeError, get_next_page, obj)
self.assertRaises(TypeError, get_next_page)
self.assertRaises(ValueError, get_next_page, ' ')
class JsonParsingTest(unittest.TestCase):
EXPECT = {'id': 42, 'keys': ['author', 'sha', 'date_added', 'message', 'repo']}
data = open('fixtures/commits.json')
def test_common(self):
self.assertRaises(TypeError, get_commit_from_json)
self.assertRaises(TypeError, get_commit_from_json, '')
for d in get_commit_from_json('', ''):
self.assertIs(d, None)
def test_json_parsing(self):
jsn = json.load(self.data)
for r in get_commit_from_json(jsn, self.EXPECT['id']):
self.assertIsInstance(r, dict)
self.assertEqual(r['repo'], self.EXPECT['id'])
self.assertListEqual(sorted(r), sorted(self.EXPECT['keys']))
if __name__ == '__main__':
unittest.main()