Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feture/tests #31

Open
wants to merge 6 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down
2 changes: 1 addition & 1 deletion nexusadspy/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
51 changes: 51 additions & 0 deletions nexusadspy/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os

import pytest
from mock import patch

from nexusadspy import AppnexusClient
from nexusadspy.exceptions import NexusadspyConfigurationError
Expand All @@ -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".'
29 changes: 29 additions & 0 deletions nexusadspy/tests/test_report.py
Original file line number Diff line number Diff line change
@@ -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'}