-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtests.py
More file actions
51 lines (38 loc) · 1.51 KB
/
tests.py
File metadata and controls
51 lines (38 loc) · 1.51 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
from httmock import urlmatch, HTTMock, all_requests, response
import phant
import unittest
BASE_URL = r'data.sparkfun.com'
PUBLIC = 'foo'
PRIVATE = 'bar'
def json_response(data):
return response(status_code=200,
content=data,
headers={'Content-Type': 'application/json',
'x-rate-limit-remaining': '1'},
)
class Server(object):
def __init__(self):
self.remaining_requests = 100
self.remaining_bytes = 50 * 1024 * 1024
self.used_bytes = 0
@urlmatch(scheme=r'http', netloc=BASE_URL, path=r'/input/foo.json')
def mock_input(self, url, request):
self.remaining_requests -= 1
self.remaining_bytes -= 1
self.used_bytes += 1
return json_response({'success': True})
@urlmatch(scheme='http', netloc=BASE_URL, path='/output/' + PUBLIC + '/stats.json')
def mock_stats(self, url, request):
stats = dict(remaining=self.remaining_bytes,
used=self.used_bytes)
return json_response(stats)
class RequestTests(unittest.TestCase):
def setUp(self):
self.server = Server()
def test_stats(self):
with HTTMock(self.server.mock_input, self.server.mock_stats):
p = phant.Phant(PUBLIC, fields='field', privateKey=PRIVATE)
p.log(123)
remaining, used = p.remaining_bytes, p.used_bytes
self.assertEqual(p.remaining_bytes, remaining)
self.assertEqual(p.used_bytes, used)