-
Notifications
You must be signed in to change notification settings - Fork 8
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
9 changed files
with
306 additions
and
6 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,3 +1,3 @@ | ||
[flake8] | ||
ignore = E501 | ||
ignore = E501,W503 | ||
exclude = __init__.py |
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
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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from marshmallow import Schema, fields, post_load, EXCLUDE | ||
from ..resource import Resource | ||
from collections import namedtuple | ||
|
||
|
||
class Contact(Resource): | ||
""" | ||
https://dev.chartmogul.com/v1.0/reference#contacts | ||
""" | ||
_path = "/contacts{/uuid}" | ||
_root_key = "entries" | ||
_many = namedtuple("Contacts", | ||
[_root_key, "has_more", "cursor"]) | ||
|
||
class _Schema(Schema): | ||
customer_uuid = fields.String(allow_none=True) | ||
data_source_uuid = fields.String(allow_none=True) | ||
customer_external_id = fields.String(allow_none=True) | ||
first_name = fields.String(allow_none=True) | ||
last_name = fields.String(allow_none=True) | ||
position = fields.Int(allow_none=True) | ||
email = fields.String(allow_none=True) | ||
title = fields.String(allow_none=True) | ||
notes = fields.String(allow_none=True) | ||
phone = fields.String(allow_none=True) | ||
linked_in = fields.String(allow_none=True) | ||
twitter = fields.String(allow_none=True) | ||
custom = fields.Dict(allow_none=True) | ||
|
||
@post_load | ||
def make(self, data, **kwargs): | ||
return Contact(**data) | ||
|
||
_schema = _Schema(unknown=EXCLUDE) | ||
|
||
|
||
Contact.merge = Contact._method("merge", "post", "/contacts/{into_uuid}/merge/{from_uuid}") |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import unittest | ||
from chartmogul import Contact, Config | ||
import requests_mock | ||
|
||
from pprint import pprint | ||
|
||
contact = { | ||
"uuid": "con_00000000-0000-0000-0000-000000000000", | ||
"customer_uuid": "cus_00000000-0000-0000-0000-000000000000", | ||
"data_source_uuid": "ds_00000000-0000-0000-0000-000000000000", | ||
"customer_external_id": "external_001", | ||
"first_name": "First name", | ||
"last_name": "Last name", | ||
"position": 9, | ||
"title": "Title", | ||
"email": "test@example.com", | ||
"phone": "+1234567890", | ||
"linked_in": "https://linkedin.com/not_found", | ||
"twitter": "https://twitter.com/not_found", | ||
"notes": "Heading\nBody\nFooter", | ||
"custom": { | ||
"MyStringAttribute": "Test", | ||
"MyIntegerAttribute": 123 | ||
} | ||
} | ||
|
||
createContact = { | ||
"customer_uuid": "cus_00000000-0000-0000-0000-000000000000", | ||
"data_source_uuid": "ds_00000000-0000-0000-0000-000000000000", | ||
"first_name": "First name", | ||
"last_name": "Last name", | ||
"position": 9, | ||
"title": "Title", | ||
"email": "test@example.com", | ||
"phone": "+1234567890", | ||
"linked_in": "https://linkedin.com/not_found", | ||
"twitter": "https://twitter.com/not_found", | ||
"notes": "Heading\nBody\nFooter", | ||
"custom": [ | ||
{ "key": "MyStringAttribute", "value": "Test" }, | ||
{ "key": "MyIntegerAttribute", "value": 123 } | ||
] | ||
} | ||
|
||
allContacts = { | ||
"entries": [contact], | ||
"cursor": "MjAyMy0wMy0xMFQwMzo1MzoxNS44MTg1MjUwMDArMDA6MDAmY29uXzE2NDcwZjk4LWJlZjctMTFlZC05MjA4LTdiMDhhNDBmMzA0OQ==", | ||
"has_more": False | ||
} | ||
|
||
|
||
class ContactTestCase(unittest.TestCase): | ||
""" | ||
Tests complex nested structure & assymetric create/retrieve schema. | ||
""" | ||
|
||
@requests_mock.mock() | ||
def test_all(self, mock_requests): | ||
mock_requests.register_uri( | ||
"GET", | ||
"https://api.chartmogul.com/v1/contacts?cursor=Ym9veWFo&per_page=1&data_source_uuid=ds_00000000-0000-0000-0000-000000000000", | ||
status_code=200, | ||
json=allContacts | ||
) | ||
|
||
config = Config("token") | ||
contacts = Contact.all(config, data_source_uuid="ds_00000000-0000-0000-0000-000000000000", cursor="Ym9veWFo", per_page=1).get() | ||
expected = Contact._many(**allContacts) | ||
|
||
self.assertEqual(mock_requests.call_count, 1, "expected call") | ||
self.assertEqual(mock_requests.last_request.qs, { | ||
"cursor": ["ym9vewfo"], | ||
"per_page": ["1"], | ||
"data_source_uuid": ["ds_00000000-0000-0000-0000-000000000000"] | ||
}) | ||
self.assertEqual(mock_requests.last_request.text, None) | ||
self.assertEqual(dir(contacts), dir(expected)) | ||
self.assertTrue(isinstance(contacts.entries[0], Contact)) | ||
|
||
@requests_mock.mock() | ||
def test_create(self, mock_requests): | ||
mock_requests.register_uri( | ||
"POST", | ||
"https://api.chartmogul.com/v1/contacts", | ||
status_code=200, | ||
json=contact | ||
) | ||
|
||
config = Config("token") | ||
Contact.create(config, data=createContact).get() | ||
self.assertEqual(mock_requests.call_count, 1, "expected call") | ||
self.assertEqual(mock_requests.last_request.qs, {}) | ||
self.assertEqual(mock_requests.last_request.json(), createContact) | ||
|
||
@requests_mock.mock() | ||
def test_merge(self, mock_requests): | ||
mock_requests.register_uri( | ||
"POST", | ||
"https://api.chartmogul.com/v1/contacts/con_00000000-0000-0000-0000-000000000000/merge/con_00000000-0000-0000-0000-000000000001", | ||
status_code=200, | ||
json=contact | ||
) | ||
|
||
config = Config("token") | ||
expected = Contact.merge(config, into_uuid="con_00000000-0000-0000-0000-000000000000", from_uuid="con_00000000-0000-0000-0000-000000000001").get() | ||
|
||
self.assertEqual(mock_requests.call_count, 1, "expected call") | ||
self.assertEqual(mock_requests.last_request.qs, {}) | ||
self.assertTrue(isinstance(expected, Contact)) | ||
|
||
@requests_mock.mock() | ||
def test_modify(self, mock_requests): | ||
mock_requests.register_uri( | ||
"PATCH", | ||
"https://api.chartmogul.com/v1/contacts/con_00000000-0000-0000-0000-000000000000", | ||
status_code=200, | ||
json=contact | ||
) | ||
|
||
jsonRequest = { | ||
"email": "test2@example.com" | ||
} | ||
config = Config("token") | ||
expected = Contact.modify(config, uuid="con_00000000-0000-0000-0000-000000000000", data=jsonRequest).get() | ||
|
||
self.assertEqual(mock_requests.call_count, 1, "expected call") | ||
self.assertEqual(mock_requests.last_request.qs, {}) | ||
self.assertEqual(mock_requests.last_request.json(), jsonRequest) | ||
self.assertTrue(isinstance(expected, Contact)) | ||
|
||
@requests_mock.mock() | ||
def test_retrieve(self, mock_requests): | ||
mock_requests.register_uri( | ||
"GET", | ||
"https://api.chartmogul.com/v1/contacts/con_00000000-0000-0000-0000-000000000000", | ||
status_code=200, | ||
json=contact | ||
) | ||
|
||
config = Config("token") | ||
expected = Contact.retrieve(config, uuid="con_00000000-0000-0000-0000-000000000000").get() | ||
|
||
self.assertEqual(mock_requests.call_count, 1, "expected call") | ||
self.assertEqual(mock_requests.last_request.qs, {}) | ||
self.assertTrue(isinstance(expected, Contact)) | ||
|
||
@requests_mock.mock() | ||
def test_destroy(self, mock_requests): | ||
mock_requests.register_uri( | ||
"DELETE", | ||
"https://api.chartmogul.com/v1/contacts/con_00000000-0000-0000-0000-000000000000", | ||
status_code=200, | ||
json={} | ||
) | ||
|
||
config = Config("token") | ||
expected = Contact.destroy(config, uuid="con_00000000-0000-0000-0000-000000000000").get() | ||
|
||
self.assertEqual(mock_requests.call_count, 1, "expected call") | ||
self.assertEqual(mock_requests.last_request.qs, {}) | ||
self.assertTrue(expected, {}) |
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