-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
178 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,27 @@ | ||
from unittest.mock import MagicMock, Mock | ||
import pytest | ||
import json | ||
import os | ||
|
||
# Helper method for getting JSONs from tests/data folder | ||
|
||
@pytest.fixture() | ||
def load_data(): | ||
"""Helper method for getting JSONs from tests/data folder""" | ||
def load(file_name): | ||
with open(os.path.join('tests/data/', file_name)) as f: | ||
return json.load(f) | ||
|
||
return load | ||
|
||
# Helper method for returning mock responses via requests module | ||
|
||
@pytest.fixture() | ||
def mock_request(): | ||
"""Helper method for returning mock responses via requests module""" | ||
def mock(data): | ||
class Response: | ||
def __init__(self, data): | ||
self.status_code = 200 | ||
self.text = json.dumps(data) | ||
|
||
return lambda *args, **kwargs: Response(data) | ||
return mock | ||
|
||
return mock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,53 @@ | ||
from exponea_python_sdk.client import Exponea | ||
from pytest_mock import mocker | ||
|
||
|
||
def test_get_funnel(mocker, load_data, mock_request): | ||
exponea = Exponea("test") | ||
mock_exponea_response = load_data("test_funnel.json") | ||
mocker.patch("requests.request", mock_request(mock_exponea_response)) | ||
funnel = exponea.analyses.get_funnel("test") | ||
assert funnel["name"] == "test_funnel" | ||
assert funnel["data"][0] == { | ||
"serie": "Total", | ||
"step 1 first_session count": 2, | ||
"step 2 session_start count": 1, | ||
"step 2 session_start duration from previous": 435764.1615576744 | ||
exponea = Exponea('test') | ||
mock_exponea_response = load_data('test_funnel.json') | ||
mocker.patch('requests.request', mock_request(mock_exponea_response)) | ||
funnel = exponea.analyses.get_funnel('test') | ||
assert funnel['name'] == 'test_funnel' | ||
assert funnel['data'][0] == { | ||
'serie': 'Total', | ||
'step 1 first_session count': 2, | ||
'step 2 session_start count': 1, | ||
'step 2 session_start duration from previous': 435764.1615576744 | ||
} | ||
assert funnel["data"][1] == { | ||
"serie": "Foo", | ||
"step 1 first_session count": 1, | ||
"step 2 session_start count": 1, | ||
"step 2 session_start duration from previous": 435764.1615576744 | ||
assert funnel['data'][1] == { | ||
'serie': 'Foo', | ||
'step 1 first_session count': 1, | ||
'step 2 session_start count': 1, | ||
'step 2 session_start duration from previous': 435764.1615576744 | ||
} | ||
|
||
|
||
def test_get_report(mocker, load_data, mock_request): | ||
exponea = Exponea("test") | ||
mock_exponea_response = load_data("test_report.json") | ||
mocker.patch("requests.request", mock_request(mock_exponea_response)) | ||
report = exponea.analyses.get_report("test") | ||
assert report["name"] == "test_report" | ||
assert report["data"][0] == { | ||
"cookie id": "test1", | ||
"count(customer)": 1 | ||
exponea = Exponea('test') | ||
mock_exponea_response = load_data('test_report.json') | ||
mocker.patch('requests.request', mock_request(mock_exponea_response)) | ||
report = exponea.analyses.get_report('test') | ||
assert report['name'] == 'test_report' | ||
assert report['data'][0] == { | ||
'cookie id': 'test1', | ||
'count(customer)': 1 | ||
} | ||
assert report["data"][1] == { | ||
"cookie id": "test2", | ||
"count(customer)": 2 | ||
assert report['data'][1] == { | ||
'cookie id': 'test2', | ||
'count(customer)': 2 | ||
} | ||
|
||
|
||
def test_get_segmentation(mocker, load_data, mock_request): | ||
exponea = Exponea("test") | ||
mock_exponea_response = load_data("test_segmentation.json") | ||
mocker.patch("requests.request", mock_request(mock_exponea_response)) | ||
segmentation = exponea.analyses.get_segmentation("test") | ||
assert segmentation["name"] == "test_segmentation" | ||
assert segmentation["data"][0] == { | ||
"segment": "already_bought", | ||
"#": 0 | ||
exponea = Exponea('test') | ||
mock_exponea_response = load_data('test_segmentation.json') | ||
mocker.patch('requests.request', mock_request(mock_exponea_response)) | ||
segmentation = exponea.analyses.get_segmentation('test') | ||
assert segmentation['name'] == 'test_segmentation' | ||
assert segmentation['data'][0] == { | ||
'segment': 'already_bought', | ||
'#': 0 | ||
} | ||
assert segmentation["data"][1] == { | ||
"segment": "not_bought", | ||
"#": 4 | ||
assert segmentation['data'][1] == { | ||
'segment': 'not_bought', | ||
'#': 4 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
from exponea_python_sdk.client import Exponea | ||
from pytest_mock import mocker | ||
|
||
|
||
def test_create_catalog(mocker, load_data, mock_request): | ||
exponea = Exponea("test") | ||
mock_exponea_response = load_data("test_create_catalog.json") | ||
mocker.patch("requests.request", mock_request(mock_exponea_response)) | ||
catalog_id = exponea.catalog.create_catalog("test", ["field_one", "field_two"]) | ||
assert catalog_id == "5bfawefds3a0015e7f0b5" | ||
exponea = Exponea('test') | ||
mock_exponea_response = load_data('test_create_catalog.json') | ||
mocker.patch('requests.request', mock_request(mock_exponea_response)) | ||
catalog_id = exponea.catalog.create_catalog('test', ['field_one', 'field_two']) | ||
assert catalog_id == '5bfawefds3a0015e7f0b5' | ||
|
||
|
||
def test_get_catalog_name(mocker, load_data, mock_request): | ||
exponea = Exponea("test") | ||
mock_exponea_response = load_data("test_get_catalog_name.json") | ||
mocker.patch("requests.request", mock_request(mock_exponea_response)) | ||
catalog_name = exponea.catalog.get_catalog_name("catalog_id") | ||
assert catalog_name == "test_catalog" | ||
exponea = Exponea('test') | ||
mock_exponea_response = load_data('test_get_catalog_name.json') | ||
mocker.patch('requests.request', mock_request(mock_exponea_response)) | ||
catalog_name = exponea.catalog.get_catalog_name('catalog_id') | ||
assert catalog_name == 'test_catalog' | ||
|
||
|
||
def test_get_catalog_items(mocker, load_data, mock_request): | ||
exponea = Exponea("test") | ||
mock_exponea_response = load_data("test_catalog_items.json") | ||
mocker.patch("requests.request", mock_request(mock_exponea_response)) | ||
catalog = exponea.catalog.get_catalog_items("catalog_id") | ||
assert catalog["matched"] == 1 | ||
assert catalog["matched_limited"] == False | ||
assert catalog["total"] == 1 | ||
assert catalog["limit"] == 20 | ||
assert catalog["skip"] == 0 | ||
assert catalog["data"][0] == { | ||
"item_id": "1", | ||
"properties": { | ||
"one": "foo", | ||
"two": "bar" | ||
exponea = Exponea('test') | ||
mock_exponea_response = load_data('test_catalog_items.json') | ||
mocker.patch('requests.request', mock_request(mock_exponea_response)) | ||
catalog = exponea.catalog.get_catalog_items('catalog_id') | ||
assert catalog['matched'] == 1 | ||
assert catalog['matched_limited'] is False | ||
assert catalog['total'] == 1 | ||
assert catalog['limit'] == 20 | ||
assert catalog['skip'] == 0 | ||
assert catalog['data'][0] == { | ||
'item_id': '1', | ||
'properties': { | ||
'one': 'foo', | ||
'two': 'bar' | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,68 @@ | ||
from exponea_python_sdk.client import Exponea | ||
from pytest_mock import mocker | ||
|
||
|
||
def test_get_customer(mocker, load_data, mock_request): | ||
exponea = Exponea("test") | ||
mock_exponea_response = load_data("test_customer.json") | ||
mocker.patch("requests.request", mock_request(mock_exponea_response)) | ||
customer = exponea.customer.get_customer({ "registered": "test" }) | ||
assert customer["properties"]["first_name"] == "Lukas" | ||
assert customer["ids"]["registered"] == "test" | ||
assert customer["events"][0] == { | ||
"properties": { | ||
"foo": "bar" | ||
}, | ||
"timestamp":1533495544.343536, | ||
"type":"test" | ||
exponea = Exponea('test') | ||
mock_exponea_response = load_data('test_customer.json') | ||
mocker.patch('requests.request', mock_request(mock_exponea_response)) | ||
customer = exponea.customer.get_customer({'registered': 'test'}) | ||
assert customer['properties']['first_name'] == 'Lukas' | ||
assert customer['ids']['registered'] == 'test' | ||
assert customer['events'][0] == { | ||
'properties': { | ||
'foo': 'bar' | ||
}, | ||
'timestamp': 1533495544.343536, | ||
'type': 'test' | ||
} | ||
|
||
|
||
def test_get_customer_consents(mocker, load_data, mock_request): | ||
exponea = Exponea("test") | ||
mock_exponea_response = load_data("test_customer_consents.json") | ||
mocker.patch("requests.request", mock_request(mock_exponea_response)) | ||
consents = exponea.customer.get_customer_consents({ "registered": "test" }, [ "newsletter" ]) | ||
assert consents["newsletter"] == False | ||
exponea = Exponea('test') | ||
mock_exponea_response = load_data('test_customer_consents.json') | ||
mocker.patch('requests.request', mock_request(mock_exponea_response)) | ||
consents = exponea.customer.get_customer_consents({'registered': 'test'}, ['newsletter']) | ||
assert consents['newsletter'] is False | ||
|
||
|
||
def test_get_customer_attributes(mocker, load_data, mock_request): | ||
exponea = Exponea("test") | ||
mock_exponea_response = load_data("test_customer_attributes.json") | ||
mocker.patch("requests.request", mock_request(mock_exponea_response)) | ||
customer = exponea.customer.get_customer_attributes({ "registered": "test" }, ids=["id"], segmentations=["segm"], aggregations=["aggr"], properties=["prop"]) | ||
assert customer["properties"]["prop"] == "Lukas" | ||
assert customer["ids"]["id"] == ["123"] | ||
assert customer["aggregations"]["aggr"] == 0 | ||
assert customer["segmentations"]["segm"] == "not_bought" | ||
exponea = Exponea('test') | ||
mock_exponea_response = load_data('test_customer_attributes.json') | ||
mocker.patch('requests.request', mock_request(mock_exponea_response)) | ||
customer = exponea.customer.get_customer_attributes({'registered': 'test'}, ids=['id'], segmentations=['segm'], | ||
aggregations=['aggr'], properties=['prop']) | ||
assert customer['properties']['prop'] == 'Lukas' | ||
assert customer['ids']['id'] == ['123'] | ||
assert customer['aggregations']['aggr'] == 0 | ||
assert customer['segmentations']['segm'] == 'not_bought' | ||
|
||
|
||
def test_get_customers(mocker, load_data, mock_request): | ||
exponea = Exponea("test") | ||
mock_exponea_response = load_data("test_customers.json") | ||
mocker.patch("requests.request", mock_request(mock_exponea_response)) | ||
exponea = Exponea('test') | ||
mock_exponea_response = load_data('test_customers.json') | ||
mocker.patch('requests.request', mock_request(mock_exponea_response)) | ||
customers = exponea.customer.get_customers() | ||
assert customers[0] == { | ||
"ids": { | ||
"registered": "test", | ||
"cookie": [ "cookie" ] | ||
'ids': { | ||
'registered': 'test', | ||
'cookie': ['cookie'] | ||
}, | ||
"properties": { | ||
"first_name": "Lukas", | ||
"last_name": "Cerny" | ||
'properties': { | ||
'first_name': 'Lukas', | ||
'last_name': 'Cerny' | ||
} | ||
} | ||
|
||
|
||
def test_get_events(mocker, load_data, mock_request): | ||
exponea = Exponea("test") | ||
mock_exponea_response = load_data("test_events.json") | ||
mocker.patch("requests.request", mock_request(mock_exponea_response)) | ||
events = exponea.customer.get_events({ "registered": "test"}, [ "test" ]) | ||
exponea = Exponea('test') | ||
mock_exponea_response = load_data('test_events.json') | ||
mocker.patch('requests.request', mock_request(mock_exponea_response)) | ||
events = exponea.customer.get_events({'registered': 'test'}, ['test']) | ||
assert events[0] == { | ||
"properties":{ | ||
"test": "foo" | ||
'properties': { | ||
'test': 'foo' | ||
}, | ||
"timestamp":1533495529.9268496, | ||
"type":"test" | ||
'timestamp': 1533495529.9268496, | ||
'type': 'test' | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,55 @@ | ||
import pytest | ||
|
||
from exponea_python_sdk.client import Exponea | ||
from exponea_python_sdk.exceptions import APIException | ||
from pytest_mock import mocker | ||
|
||
|
||
def test_access_key_not_found_exception(mocker, mock_request): | ||
exponea = Exponea("test") | ||
exponea = Exponea('test') | ||
response = { | ||
"error": "access key not found", | ||
"success": False | ||
'error': 'access key not found', | ||
'success': False | ||
} | ||
mocker.patch("requests.request", mock_request(response)) | ||
mocker.patch('requests.request', mock_request(response)) | ||
with pytest.raises(APIException) as exception: | ||
exponea.analyses.get_report("test") | ||
assert "access key not found" in str(exception.value) | ||
exponea.analyses.get_report('test') | ||
assert 'access key not found' in str(exception.value) | ||
|
||
|
||
def test_not_authorized_exception(mocker, mock_request): | ||
exponea = Exponea("test") | ||
exponea = Exponea('test') | ||
response = { | ||
"errors": ["not authorized to update specified customer properties"], | ||
"success": False | ||
'errors': ['not authorized to update specified customer properties'], | ||
'success': False | ||
} | ||
mocker.patch("requests.request", mock_request(response)) | ||
mocker.patch('requests.request', mock_request(response)) | ||
with pytest.raises(APIException) as exception: | ||
exponea.analyses.get_report("test") | ||
assert "not authorized to update specified customer properties" in str(exception.value) | ||
exponea.analyses.get_report('test') | ||
assert 'not authorized to update specified customer properties' in str(exception.value) | ||
|
||
|
||
def test_errors_global_exception(mocker, mock_request): | ||
exponea = Exponea("test") | ||
exponea = Exponea('test') | ||
response = { | ||
"errors": {"_global": ["Customer does not exist"]}, | ||
"success": False | ||
'errors': {'_global': ['Customer does not exist']}, | ||
'success': False | ||
} | ||
mocker.patch("requests.request", mock_request(response)) | ||
mocker.patch('requests.request', mock_request(response)) | ||
with pytest.raises(APIException) as exception: | ||
exponea.customer.get_customer({"registered": "test"}) | ||
assert "Customer does not exist" in str(exception.value) | ||
exponea.customer.get_customer({'registered': 'test'}) | ||
assert 'Customer does not exist' in str(exception.value) | ||
|
||
|
||
def test_no_permission_to_retrieve_attribute(mocker, mock_request): | ||
exponea = Exponea("test") | ||
exponea = Exponea('test') | ||
response = { | ||
'results': [{'value': 'Lukas', 'success': True}, {'error': 'No permission', 'success': False}, {'value': 'not bought', 'success': True}], | ||
'results': [{'value': 'Lukas', 'success': True}, {'error': 'No permission', 'success': False}, | ||
{'value': 'not bought', 'success': True}], | ||
'success': True | ||
} | ||
mocker.patch("requests.request", mock_request(response)) | ||
attributes = exponea.customer.get_customer_attributes({"registered": "test"}, ids=["test"], properties=["name"], expressions=["test"]) | ||
assert attributes["ids"]["test"] == None | ||
assert attributes["properties"]["name"] == "Lukas" | ||
assert attributes["expressions"]["test"] == "not bought" | ||
mocker.patch('requests.request', mock_request(response)) | ||
attributes = exponea.customer.get_customer_attributes({'registered': 'test'}, ids=['test'], properties=['name'], | ||
expressions=['test']) | ||
assert attributes['ids']['test'] is None | ||
assert attributes['properties']['name'] == 'Lukas' | ||
assert attributes['expressions']['test'] == 'not bought' |
Oops, something went wrong.