forked from ask/python-github2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.py
More file actions
73 lines (54 loc) · 2.03 KB
/
unit.py
File metadata and controls
73 lines (54 loc) · 2.03 KB
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
# -*- coding: latin-1 -*-
import os
import unittest
from email import message_from_file
import httplib2
from github2.issues import Issue
from github2.client import Github
HTTP_DATA_DIR = "tests/data/"
class HttpMock(object):
"""Simple Http mock that returns saved entries
Implementation tests should never span network boundaries
"""
def __init__(self, cache=None, timeout=None, proxy_info=None):
pass
def request(self, uri, method='GET', body=None, headers=None,
redirections=5, connection_type=None):
file = os.path.join(HTTP_DATA_DIR, httplib2.safename(uri))
print file
if os.path.exists(file):
response = message_from_file(open(file))
body = response.get_payload()
headers = httplib2.Response(response)
return (headers, body)
else:
return (httplib2.Response({"status": "404"}), "")
class ReprTests(unittest.TestCase):
"""__repr__ must return strings, not unicode objects."""
def test_issue(self):
"""Issues can have non-ASCII characters in the title."""
i = Issue(title=u'abcdé')
self.assertEqual(str, type(repr(i)))
class RateLimits(unittest.TestCase):
"""Test API rate-limitting"""
def setUp(self):
self.old_httplib2 = httplib2.Http
httplib2.Http = HttpMock
def tearDown(self):
httplib2.Http = self.old_httplib2
def test_delays(self):
"""Test call delay is at least one second"""
import datetime
USERNAME = ''
API_KEY = ''
client = Github(username=USERNAME, api_token=API_KEY,
requests_per_second=.5)
client.users.show('defunkt')
start = datetime.datetime.now()
client.users.show('mojombo')
end = datetime.datetime.now()
delta = end - start
delta_seconds = delta.days * 24 * 60 * 60 + delta.seconds
self.assertTrue(delta_seconds >= 2,
"Expected .5 reqs per second to require a 2 second delay between "
"calls.")