Skip to content

Commit

Permalink
add tests for decode part
Browse files Browse the repository at this point in the history
  • Loading branch information
agrausem committed Mar 22, 2017
1 parent bb3a4e1 commit 00e715f
Show file tree
Hide file tree
Showing 2 changed files with 276 additions and 4 deletions.
10 changes: 6 additions & 4 deletions spore_codec/decode.py
Expand Up @@ -14,7 +14,7 @@ def parse_spore_description(data, base_url=None):
content = {}

for method in spore_methods.items():
keys, action, link = _get_link_from_method(method)
action, keys, link = _get_link_from_method(method)

base_dict = content
for key in keys:
Expand All @@ -23,7 +23,7 @@ def parse_spore_description(data, base_url=None):

return Document(
title=data.get('name'),
base_url=schema_url,
url=schema_url,
description=description,
content=content,
media_type='application/sporeapi+json'
Expand All @@ -36,10 +36,12 @@ def _get_link_from_method(method):
link = Link(
url=spore_method.get('path'),
action=spore_method.get('method').lower(),
title=spore_name,
title=spore_method.get('description',
spore_name.replace('_', ' ').capitalize()),
authentication=spore_method.get('authentication', False),
formats=spore_method.get('formats', []),
fields=_get_fields_from_method(spore_method)
fields=_get_fields_from_method(spore_method),
description=spore_method.get('documentation', '')
)
return action, keys, link

Expand Down
270 changes: 270 additions & 0 deletions tests/test_decode.py
@@ -1,4 +1,6 @@
from coreapi import Document
from spore_codec import decode
from spore_codec.document import Link

class TestFieldFromMethod:

Expand Down Expand Up @@ -50,3 +52,271 @@ def test_all_together(self):
assert len(result) == 3
assert len([r for r in result if r.required]) == 2
assert len([r for r in result if r.location == 'query']) == 2


class TestActionAndKeyFromMethod:

def test_return_type_from_getting_link_method(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET'
})
result = decode._get_link_from_method(method)
assert isinstance(result, tuple)


def test_number_of_elements_in_tuple_simple_method(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET'
})
result = decode._get_link_from_method(method)
assert len(result) == 3

def test_number_of_elements_in_tuple_complex_method(self):
method = ('list_users_groups', {
'path': '/users/{}/groups/',
'method': 'GET'
})
result = decode._get_link_from_method(method)
assert len(result) == 3

def test_action_element(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET'
})
result = decode._get_link_from_method(method)
assert result[0] == 'list'

def test_keys_element_simple_method(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET'
})
result = decode._get_link_from_method(method)
assert result[1] == ['users']

def test_keys_element_complex_method(self):
method = ('list_users_groups', {
'path': '/users/{}/groups/',
'method': 'GET'
})
result = decode._get_link_from_method(method)
assert result[1] == ['users', 'groups']

def test_link_element(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET'
})
result = decode._get_link_from_method(method)
assert isinstance(result[-1], Link)


class TestLinkFromMethod:

def test_link_url(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET'
})
link = decode._get_link_from_method(method)[-1]
assert link.url == '/users/'

def test_link_action(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET'
})
link = decode._get_link_from_method(method)[-1]
assert link.action == 'get'

def test_link_title(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET',
'description': 'List users from app'
})
link = decode._get_link_from_method(method)[-1]
assert link.title == 'List users from app'

def test_link_default_title(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET',
})
link = decode._get_link_from_method(method)[-1]
assert link.title == 'List users'

def test_link_authentication(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET',
'authentication': True
})
link = decode._get_link_from_method(method)[-1]
assert link.authentication

def test_link_default_authentication(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET',
})
link = decode._get_link_from_method(method)[-1]
assert not link.authentication

def test_link_formats(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET',
'formats': ['xml', 'json']
})
link = decode._get_link_from_method(method)[-1]
assert link.formats == ['xml', 'json']

def test_link_default_formats(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET',
})
link = decode._get_link_from_method(method)[-1]
assert link.formats == []

def test_link_fields(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET',
})
link = decode._get_link_from_method(method)[-1]
assert len(link.fields) == 0

def test_link_description(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET',
'documentation': 'Blabla'
})
link = decode._get_link_from_method(method)[-1]
assert link.description == 'Blabla'

def test_link_default_description(self):
method = ('list_users', {
'path': '/users/',
'method': 'GET',
})
link = decode._get_link_from_method(method)[-1]
assert link.description == ''


class TestDocumentFromSpore:

def test_returning_document(self):
spore_data = {
'name': 'API',
'methods': {}
}
result = decode.parse_spore_description(spore_data, 'http://test/')
assert isinstance(result, Document)

def test_document_title(self):
spore_data = {
'name': 'API',
'methods': {}
}
result = decode.parse_spore_description(spore_data, 'http://test/')
assert result.title == 'API'

def test_document_url_from_signature(self):
spore_data = {
'name': 'API',
'methods': {}
}
result = decode.parse_spore_description(spore_data, 'http://test/')
assert result.url == 'http://test/'

def test_document_url_from_data(self):
spore_data = {
'name': 'API',
'base_url': 'http://other-test/',
'methods': {}
}
result = decode.parse_spore_description(spore_data)
assert result.url == 'http://other-test/'

def test_document_url_precedence(self):
spore_data = {
'name': 'API',
'base_url': 'http://other-test/',
'methods': {}
}
result = decode.parse_spore_description(spore_data, 'http://test/')
assert result.url == 'http://test/'

def test_document_description(self):
spore_data = {
'name': 'API',
'methods': {},
'meta': {
'documentation': 'doc'
}
}
result = decode.parse_spore_description(spore_data, 'http://test/')
assert result.description == 'doc'

def test_document_default_description(self):
spore_data = {
'name': 'API',
'methods': {},
'meta': {
}
}
result = decode.parse_spore_description(spore_data, 'http://test/')
assert result.description == ''

def test_document_media_type(self):
spore_data = {
'name': 'API',
'methods': {},
}
result = decode.parse_spore_description(spore_data, 'http://test/')
assert result.media_type == 'application/sporeapi+json'

def test_document_simple_content(self):
spore_data = {
'name': 'API',
'methods': {
'list_users': {
'path': '/users/',
'method': 'GET'
},
'create_users': {
'path': '/users/',
'method': 'POST'
}
},
}
result = decode.parse_spore_description(spore_data, 'http://test/')
assert 'users' in result.data
assert 'list' in result.data['users']
assert 'create' in result.data['users']

def test_document_complex_content(self):
spore_data = {
'name': 'API',
'methods': {
'list_users': {
'path': '/users/',
'method': 'GET'
},
'list_users_groups': {
'path': '/users/{pk}/groups/',
'method': 'GET',
'required_params': ['pk']
}
},
}
result = decode.parse_spore_description(spore_data, 'http://test/')
assert 'list' in result.data['users']
assert 'groups' in result.data['users']
assert 'list' in result.data['users']['groups']

0 comments on commit 00e715f

Please sign in to comment.