This repository was archived by the owner on Mar 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathtest_integration.py
93 lines (64 loc) · 2.38 KB
/
test_integration.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
# coding: utf-8
import coreapi
import requests
import pytest
encoded = (
b'{"_type":"document","_meta":{"url":"http://example.org"},'
b'"a":123,"next":{"_type":"link"}}'
)
@pytest.fixture
def document():
codec = coreapi.codecs.CoreJSONCodec()
return codec.decode(encoded)
class MockResponse(object):
def __init__(self, content):
self.content = content
self.headers = {}
self.url = 'http://example.org'
self.status_code = 200
def iter_content(self, *args, **kwargs):
n = 2
list_of_chunks = list(self.content[i:i + n] for i in range(0, len(self.content), n))
return list_of_chunks
def close(self):
return
# Basic integration tests.
def test_load():
codec = coreapi.codecs.CoreJSONCodec()
assert codec.decode(encoded) == {
"a": 123,
"next": coreapi.Link(url='http://example.org')
}
def test_dump(document):
codec = coreapi.codecs.CoreJSONCodec()
content = codec.encode(document)
assert content == encoded
def test_get(monkeypatch):
def mockreturn(self, request, *args, **kwargs):
return MockResponse(b'{"_type": "document", "example": 123}')
monkeypatch.setattr(requests.Session, 'send', mockreturn)
client = coreapi.Client()
doc = client.get('http://example.org')
assert doc == {'example': 123}
def test_follow(monkeypatch, document):
def mockreturn(self, request, *args, **kwargs):
return MockResponse(b'{"_type": "document", "example": 123}')
monkeypatch.setattr(requests.Session, 'send', mockreturn)
client = coreapi.Client()
doc = client.action(document, ['next'])
assert doc == {'example': 123}
def test_reload(monkeypatch):
def mockreturn(self, request, *args, **kwargs):
return MockResponse(b'{"_type": "document", "example": 123}')
monkeypatch.setattr(requests.Session, 'send', mockreturn)
client = coreapi.Client()
doc = coreapi.Document(url='http://example.org')
doc = client.reload(doc)
assert doc == {'example': 123}
def test_error(monkeypatch, document):
def mockreturn(self, request, *args, **kwargs):
return MockResponse(b'{"_type": "error", "message": ["failed"]}')
monkeypatch.setattr(requests.Session, 'send', mockreturn)
client = coreapi.Client()
with pytest.raises(coreapi.exceptions.ErrorMessage):
client.action(document, ['next'])