diff --git a/.travis.yml b/.travis.yml index a1f13d6..d53d141 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ python: install: - pip install -r requirements.txt - - pip install pytest pytest-cov flake8 coveralls + - pip install pytest pytest-cov flake8 coveralls mock before_script: - flake8 --show-source . diff --git a/nexusadspy/segment.py b/nexusadspy/segment.py index d892f0b..08123a0 100644 --- a/nexusadspy/segment.py +++ b/nexusadspy/segment.py @@ -83,7 +83,7 @@ def _get_buffer_for_upload(self): self._logger.debug("Attempting to upload \n" + upload_string) compressed_buffer = BytesIO() with GzipFile(fileobj=compressed_buffer, mode='wb') as compressor: - compressor.write(upload_string) + compressor.write(upload_string.encode('UTF-8')) compressed_buffer.seek(0) return compressed_buffer diff --git a/nexusadspy/tests/test_client.py b/nexusadspy/tests/test_client.py index e668f1a..703cb49 100644 --- a/nexusadspy/tests/test_client.py +++ b/nexusadspy/tests/test_client.py @@ -8,6 +8,7 @@ import os import pytest +from mock import patch from nexusadspy import AppnexusClient from nexusadspy.exceptions import NexusadspyConfigurationError @@ -23,3 +24,53 @@ def test_failure_no_credentials(): client._get_new_auth_token() assert 'set environment variables' in str(excinfo.value.lower()) + + +def test_http_methods(): + with patch.object(AppnexusClient, "_do_paged_get", autospec=True) as mock_paged: + with patch.object(AppnexusClient, "_do_authenticated_request", autospec=True) as mock_auth: + mock_auth.return_value = (200, {}) + mock_paged.return_value = (200, {}) + clnt = AppnexusClient("foo") + clnt.request("bar", "get") + assert mock_auth.call_count == 0 + mock_paged.assert_called_once_with(clnt, clnt.endpoint + "/bar", "get", + data={}, get_field=None, headers=None, params={}) + + with patch.object(AppnexusClient, "_do_paged_get", autospec=True) as mock_paged: + with patch.object(AppnexusClient, "_do_authenticated_request", autospec=True) as mock_auth: + mock_auth.return_value = (200, {}) + mock_paged.return_value = (200, {}) + clnt = AppnexusClient("bar") + clnt.request("foo", "post") + mock_auth.assert_called_once_with(clnt, clnt.endpoint + "/foo", "post", + data={}, headers=None, params={}) + assert mock_paged.call_count == 0 + + with patch.object(AppnexusClient, "_do_paged_get", autospec=True) as mock_paged: + with patch.object(AppnexusClient, "_do_authenticated_request", autospec=True) as mock_auth: + mock_auth.return_value = (200, {}) + mock_paged.return_value = (200, {}) + clnt = AppnexusClient("pfoo") + clnt.request("pbar", "put") + mock_auth.assert_called_once_with(clnt, clnt.endpoint + "/pbar", "put", + data={}, headers=None, params={}) + assert mock_paged.call_count == 0 + + with patch.object(AppnexusClient, "_do_paged_get", autospec=True) as mock_paged: + with patch.object(AppnexusClient, "_do_authenticated_request", autospec=True) as mock_auth: + mock_auth.return_value = (200, {}) + mock_paged.return_value = (200, {}) + clnt = AppnexusClient("dfoo") + clnt.request("dbar", "delete") + mock_auth.assert_called_once_with(clnt, clnt.endpoint + "/dbar", "delete", + data={}, headers=None, params={}) + assert mock_paged.call_count == 0 + + +def test_wrong_http_methods(): + clnt = AppnexusClient("foo") + with pytest.raises(ValueError) as excinfo: + clnt.request("bar", "wget") + assert excinfo.value.lower() == 'Argument "method" must be one of ' \ + '["get", "post", "put", "delete"]. You supplied: "wget".' diff --git a/nexusadspy/tests/test_report.py b/nexusadspy/tests/test_report.py new file mode 100644 index 0000000..bf966e5 --- /dev/null +++ b/nexusadspy/tests/test_report.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- + +from __future__ import ( + print_function, division, generators, + absolute_import, unicode_literals +) + +import pytest + +from nexusadspy import AppnexusReport + + +def test_init_report(): + with pytest.raises(ValueError) as excinfo: + AppnexusReport("foo", "one") + assert excinfo.value.lower() == '"columns" is expected as a list, you provided "one"' + + rep = AppnexusReport("foo", ["one"]) + assert rep.endpoint == "report" + assert rep.request == { + 'report': { + 'columns': ['one'], + 'filters': [], + 'groups': [], + 'report_type': 'foo', + 'special_pixel_reporting': False, + 'timezone': 'CET' + }, + 'report_interval': 'lifetime'}