diff --git a/hexonet/apiconnector/util.py b/hexonet/apiconnector/util.py index bc231b8..ea96b5b 100755 --- a/hexonet/apiconnector/util.py +++ b/hexonet/apiconnector/util.py @@ -108,8 +108,8 @@ def response_to_list_hash(response): if prop in ['FIRST', 'LAST', 'LIMIT', 'COUNT', 'TOTAL']: list_hash[prop] = int(response['PROPERTY'][prop][0]) else: - if columns and not columns[prop]: - continue + # if columns and not columns[prop]: + # continue list_hash['COLUMNS'].append(prop) index = 0 for v in values: diff --git a/requirements.txt b/requirements.txt index aa98afd..f9d51d4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,7 @@ guzzle-sphinx-theme==0.7.11 -e git://github.com/miyakogi/m2r@dev#egg=m2r pep8==1.7.1 pycodestyle==2.4.0 +pytestcov==2.5.1 six==1.11.0 Sphinx==1.7.5 sphinxcontrib-log-cabinet==1.0.0 diff --git a/scripts/coverage.sh b/scripts/coverage.sh new file mode 100755 index 0000000..f9955c3 --- /dev/null +++ b/scripts/coverage.sh @@ -0,0 +1,3 @@ +#!/bin/bash +rm -rf .pytest_cache htmlcov tests/__pycache___ +py.test --cov-report html --cov=hexonet.apiconnector tests/ diff --git a/tests/test_connection.py b/tests/test_connection.py new file mode 100644 index 0000000..c615d6f --- /dev/null +++ b/tests/test_connection.py @@ -0,0 +1,28 @@ +from hexonet.apiconnector import connect +from hexonet.apiconnector.connection import Connection + +def test_connectcommon(): + api = connect( + "test.user", + "test.password", + "https://coreapi.1api.net/call/call.cgi", + "1234" + ) + assert isinstance(api, Connection) + api.call({ + "COMMAND": "GetUserIndex" + }) + +def test_connectuserandrole(): + api = connect( + "test.user", + "test.password", + "https://coreapi.1api.net/call/call.cgi", + "1234", + "hexotestman.com", + "testrole" + ) + assert isinstance(api, Connection) + api.call({ + "COMMAND": "GetUserIndex" + }) \ No newline at end of file diff --git a/tests/test_response.py b/tests/test_response.py new file mode 100644 index 0000000..b9a90bb --- /dev/null +++ b/tests/test_response.py @@ -0,0 +1,73 @@ +from hexonet.apiconnector import connect +from hexonet.apiconnector.response import Response + +def test_response(): + api = connect( + "test.user", + # wrong password + "test.password", + "https://coreapi.1api.net/api/call.cgi", + "1234" + ) + response = api.call({ + "COMMAND": "GetUserIndex" + }) + assert isinstance(response, Response) + assert response.description() == "Authentication failed" + assert response.code() == 530 + assert type(response.as_string()) is str + assert type(response.as_list_hash()) is dict + assert isinstance(response.as_list(), list) + assert len(response) == 0 + assert response["CODE"] == 530 + assert response.is_success() == False + +def test_listresponse(): + api = connect( + "test.user", + "test.passw0rd", + "https://coreapi.1api.net/api/call.cgi", + "1234" + ) + response = api.call({ + "COMMAND": "QueryDomainList", + "VERSION": 2, + "NOTOTAL": 1,#TOTAL to have value from total to equal to count + "LIMIT": 10, + "FIRST": 0 + }) + assert isinstance(response, Response) + assert response.description() == "Command completed successfully" + assert response.code() == 200 + assert len(response) == 10 + assert type(response[0]) is dict + assert type(response.runtime()) is float + assert type(response.queuetime()) is float + assert type(response.properties()) is dict + assert response.property("DOMAIN") is None + assert isinstance(response.property("OBJECTID"), list) + assert type(response.property()) is dict + assert response.property() == response.properties() + assert response.is_success() == True + assert response.is_tmp_error() == False + assert isinstance(response.columns(), list) + assert type(response.first()) is int + assert response.first() == 0 + assert type(response.last()) is int + assert response.last() == 9 + assert type(response.count()) is int + assert response.count() == 10 + assert type(response.limit()) is int + assert response.limit() == 10 + assert type(response.total()) is int + assert response.total() == 10 + assert type(response.pages()) is float #TODO int makes more sense + assert response.pages() == 1.9 # doesn't make sense, should be 1 in this case + assert type(response.page()) is int + assert response.page() == 1 + assert response.prevpage() == None + assert response.prevpagefirst() == None + assert response.nextpage() == None + assert response.nextpagefirst() == None + assert type(response.lastpagefirst()) is float + assert response.lastpagefirst() == 9.0 \ No newline at end of file diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..bb55b14 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,43 @@ +from hexonet.apiconnector import connect +from hexonet.apiconnector.util import sqltime, timesql, url_encode, url_decode, base64_encode, base64_decode + +def test_utilmethods(): + # cover COLUMN specific code in response_to_list_hash + # LINE 108-109 deprecated? + api = connect( + "test.user", + "test.passw0rd", + "https://coreapi.1api.net/api/call.cgi", + "1234" + ) + response = api.call({ + "COMMAND": "QueryDomainPendingDeleteList", + "LIMIT": 10, + "FIRST": 20 + }) + assert response.code() == 200 + + # sqltime() + ts = sqltime() # now() + assert type(ts) is str + + uxorg = 1531479459 + ts = sqltime(uxorg) + assert type(ts) is str + assert ts == "2018-07-13 12:57:39" + + # timesql() + ux = timesql(ts) + assert ux == uxorg + + # url_encode / url_decode + enc = url_encode("+") + assert enc == "%2B" + + dec = url_decode("%2B") + assert dec == "+" + + # base64_encode / base64_decode + key = "das stinkt zum Himmel" + enc = base64_encode(key) + assert enc == "wirklich" \ No newline at end of file