This repository was archived by the owner on May 28, 2018. It is now read-only.
forked from matze/python-phant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
47 lines (34 loc) · 1.36 KB
/
tests.py
File metadata and controls
47 lines (34 loc) · 1.36 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
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(200, data, {'Content-Type': 'application/json'})
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, 'field', private_key=PRIVATE)
remaining, used = p.remaining_bytes, p.used_bytes
p.log(123)
self.assertLess(p.remaining_bytes, remaining)
self.assertGreater(p.used_bytes, used)