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
Expand file tree
/
Copy pathtest_transitions.py
More file actions
83 lines (59 loc) · 2.55 KB
/
test_transitions.py
File metadata and controls
83 lines (59 loc) · 2.55 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
74
75
76
77
78
79
80
81
82
83
# coding: utf-8
from coreapi import Document, Link, Client
from coreapi.transports import HTTPTransport
from coreapi.transports.http import _handle_inplace_replacements
import pytest
class MockTransport(HTTPTransport):
schemes = ['mock']
def transition(self, link, decoders, params=None, link_ancestors=None, stream=True):
if link.action == 'get':
document = Document(title='new', content={'new': 123})
elif link.action in ('put', 'post'):
if params is None:
params = {}
document = Document(title='new', content={'new': 123, 'foo': params.get('foo')})
else:
document = None
return _handle_inplace_replacements(document, link, link_ancestors)
client = Client(transports=[MockTransport()])
@pytest.fixture
def doc():
return Document(title='original', content={
'nested': Document(content={
'follow': Link(url='mock://example.com', action='get'),
'action': Link(url='mock://example.com', action='post', transform='inplace', fields=['foo']),
'create': Link(url='mock://example.com', action='post', fields=['foo']),
'update': Link(url='mock://example.com', action='put', fields=['foo']),
'delete': Link(url='mock://example.com', action='delete')
})
})
# Test valid transitions.
def test_get(doc):
new = client.action(doc, ['nested', 'follow'])
assert new == {'new': 123}
assert new.title == 'new'
def test_inline_post(doc):
new = client.action(doc, ['nested', 'action'], params={'foo': 123})
assert new == {'nested': {'new': 123, 'foo': 123}}
assert new.title == 'original'
def test_post(doc):
new = client.action(doc, ['nested', 'create'], params={'foo': 456})
assert new == {'new': 123, 'foo': 456}
assert new.title == 'new'
def test_put(doc):
new = client.action(doc, ['nested', 'update'], params={'foo': 789})
assert new == {'nested': {'new': 123, 'foo': 789}}
assert new.title == 'original'
def test_delete(doc):
new = client.action(doc, ['nested', 'delete'])
assert new == {}
assert new.title == 'original'
# Test overrides
def test_override_action(doc):
new = client.action(doc, ['nested', 'follow'], overrides={'action': 'put'})
assert new == {'nested': {'new': 123, 'foo': None}}
assert new.title == 'original'
def test_override_transform(doc):
new = client.action(doc, ['nested', 'update'], params={'foo': 456}, overrides={'transform': 'new'})
assert new == {'new': 123, 'foo': 456}
assert new.title == 'new'