-
Notifications
You must be signed in to change notification settings - Fork 8
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
Python client notes and call logs API support #86
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from marshmallow import Schema, fields, post_load, EXCLUDE | ||
from ..resource import Resource | ||
from collections import namedtuple | ||
|
||
|
||
class CustomerNote(Resource): | ||
""" | ||
https://dev.chartmogul.com/v1.0/reference#customer-notes | ||
""" | ||
|
||
_path = "/customer_notes{/uuid}" | ||
_root_key = "entries" | ||
_many = namedtuple("CustomerNotes", [_root_key, "has_more", "cursor"]) | ||
|
||
class _Schema(Schema): | ||
uuid = fields.String() | ||
customer_uuid = fields.String(allow_none=True) | ||
type = fields.String(allow_none=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if we should set |
||
text = fields.String(allow_none=True) | ||
author_email = fields.String(allow_none=True) | ||
call_duration = fields.Int(allow_none=True) | ||
created_at = fields.DateTime(allow_none=True) | ||
updated_at = fields.DateTime(allow_none=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for |
||
|
||
@post_load | ||
def make(self, data, **kwargs): | ||
return CustomerNote(**data) | ||
|
||
_schema = _Schema(unknown=EXCLUDE) |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,5 +1,5 @@ | ||||
import unittest | ||||
from chartmogul import Customer, Contact, Config | ||||
from chartmogul import Customer, Contact, Config, CustomerNote | ||||
from chartmogul.api.customer import Attributes, Address | ||||
from datetime import datetime | ||||
from chartmogul import APIError | ||||
|
@@ -279,6 +279,35 @@ | |||
], | ||||
} | ||||
|
||||
note = { | ||||
"customer_uuid": "cus_00000000-0000-0000-0000-000000000000", | ||||
"type": "note", | ||||
"text": "This is a note", | ||||
"call_duration": 0, | ||||
"author_email": "john@example.com", | ||||
"created_at": "2015-06-09T13:16:00-04:00", | ||||
"updated_at": "2015-06-09T13:16:00-04:00" | ||||
} | ||||
|
||||
createNote = { | ||||
"customer_uuid": "cus_00000000-0000-0000-0000-000000000000", | ||||
"type": "note", | ||||
"text": "This is a note", | ||||
"author_email": "john@xample.com" | ||||
} | ||||
|
||||
noteEntry = { | ||||
"uuid": "cus_00000000-0000-0000-0000-000000000000", | ||||
"customer_uuid": "cus_00000000-0000-0000-0000-000000000000", | ||||
"type": "note", | ||||
"text": "This is a note", | ||||
"call_duration": 0, | ||||
"author_email": "john@example.com", | ||||
"created_at": "2015-06-09T13:16:00-04:00", | ||||
"updated_at": "2015-06-09T13:16:00-04:00" | ||||
} | ||||
|
||||
allNotes = {"entries": [noteEntry], "cursor": "cursor==", "has_more": True} | ||||
|
||||
class CustomerTestCase(unittest.TestCase): | ||||
""" | ||||
|
@@ -480,3 +509,43 @@ def test_createContact(self, mock_requests): | |||
self.assertEqual(mock_requests.last_request.qs, {}) | ||||
self.assertEqual(mock_requests.last_request.json(), createContact) | ||||
self.assertTrue(isinstance(expected, Contact)) | ||||
|
||||
@requests_mock.mock() | ||||
def test_notes(self, mock_requests): | ||||
mock_requests.register_uri( | ||||
"GET", | ||||
"https://api.chartmogul.com/v1/customer_notes?customer_uuid=cus_00000000-0000-0000-0000-000000000000", | ||||
status_code=200, | ||||
json=allNotes, | ||||
) | ||||
|
||||
config = Config("token") | ||||
notes = Customer.notes(config, uuid="cus_00000000-0000-0000-0000-000000000000").get() | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also test with other params? e.g.
and the |
||||
expected = Customer._many(**allNotes) | ||||
|
||||
self.assertEqual(mock_requests.call_count, 1, "expected call") | ||||
self.assertEqual(mock_requests.last_request.qs, {'customer_uuid': ['cus_00000000-0000-0000-0000-000000000000']}) | ||||
self.assertEqual(mock_requests.last_request.text, None) | ||||
self.assertEqual(sorted(dir(notes)), sorted(dir(expected))) | ||||
self.assertTrue(isinstance(notes.entries[0], CustomerNote)) | ||||
self.assertEqual(notes.cursor, "cursor==") | ||||
self.assertTrue(notes.has_more) | ||||
|
||||
@requests_mock.mock() | ||||
def test_createNote(self, mock_requests): | ||||
mock_requests.register_uri( | ||||
"POST", | ||||
"https://api.chartmogul.com/v1/customer_notes", | ||||
status_code=200, | ||||
json=note, | ||||
) | ||||
|
||||
config = Config("token") | ||||
expected = Customer.createNote( | ||||
config, data=createNote | ||||
).get() | ||||
|
||||
self.assertEqual(mock_requests.call_count, 1, "expected call") | ||||
self.assertEqual(mock_requests.last_request.qs, {}) | ||||
self.assertEqual(mock_requests.last_request.json(), createNote) | ||||
self.assertTrue(isinstance(expected, CustomerNote)) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,127 @@ | ||||||
import unittest | ||||||
from chartmogul import CustomerNote, Config | ||||||
import requests_mock | ||||||
|
||||||
from pprint import pprint | ||||||
|
||||||
note = { | ||||||
"uuid": "note_00000000-0000-0000-0000-000000000000", | ||||||
"customer_uuid": "cus_00000000-0000-0000-0000-000000000000", | ||||||
"type": "note", | ||||||
"text": "This is a note", | ||||||
"call_duration": 0, | ||||||
"author": "John Doe (john@example.com)", | ||||||
"created_at": "2015-06-09T13:16:00-04:00", | ||||||
"updated_at": "2015-06-09T13:16:00-04:00" | ||||||
} | ||||||
|
||||||
createNote = { | ||||||
"customer_uuid": "cus_00000000-0000-0000-0000-000000000000", | ||||||
"type": "note", | ||||||
"text": "This is a note", | ||||||
"authoer_email": "john@xample.com" | ||||||
} | ||||||
|
||||||
allNotes = {"entries": [note], "cursor": "cursor==", "has_more": False} | ||||||
|
||||||
|
||||||
class CustomerNoteTestCase(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/customer_notes?cursor=Ym9veWFo&per_page=1&customer_uuid=cus_00000000-0000-0000-0000-000000000000", | ||||||
status_code=200, | ||||||
json=allNotes, | ||||||
) | ||||||
|
||||||
config = Config("token") | ||||||
notes = CustomerNote.all( | ||||||
config, | ||||||
customer_uuid="cus_00000000-0000-0000-0000-000000000000", | ||||||
cursor="Ym9veWFo", | ||||||
per_page=1, | ||||||
).get() | ||||||
expected = CustomerNote._many(**allNotes) | ||||||
|
||||||
self.assertEqual(mock_requests.call_count, 1, "expected call") | ||||||
self.assertEqual( | ||||||
mock_requests.last_request.qs, | ||||||
{ | ||||||
"cursor": ["ym9vewfo"], | ||||||
"per_page": ["1"], | ||||||
"customer_uuid": ["cus_00000000-0000-0000-0000-000000000000"], | ||||||
}, | ||||||
) | ||||||
self.assertEqual(mock_requests.last_request.text, None) | ||||||
self.assertEqual(dir(notes), dir(expected)) | ||||||
self.assertTrue(isinstance(notes.entries[0], CustomerNote)) | ||||||
self.assertFalse(notes.has_more) | ||||||
self.assertEqual(notes.cursor, "cursor==") | ||||||
|
||||||
@requests_mock.mock() | ||||||
def test_create(self, mock_requests): | ||||||
mock_requests.register_uri( | ||||||
"POST", "https://api.chartmogul.com/v1/customer_notes", status_code=200, json=note | ||||||
) | ||||||
|
||||||
config = Config("token") | ||||||
CustomerNote.create(config, data=createNote).get() | ||||||
self.assertEqual(mock_requests.call_count, 1, "expected call") | ||||||
self.assertEqual(mock_requests.last_request.qs, {}) | ||||||
self.assertEqual(mock_requests.last_request.json(), createNote) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also IMO it's better to assert the return value of |
||||||
|
||||||
@requests_mock.mock() | ||||||
def test_patch(self, mock_requests): | ||||||
mock_requests.register_uri( | ||||||
"PATCH", | ||||||
"https://api.chartmogul.com/v1/customer_notes/note_00000000-0000-0000-0000-000000000000", | ||||||
status_code=200, | ||||||
json=note, | ||||||
) | ||||||
|
||||||
config = Config("token") | ||||||
CustomerNote.patch( | ||||||
config, uuid="note_00000000-0000-0000-0000-000000000000", data=createNote | ||||||
).get() | ||||||
self.assertEqual(mock_requests.call_count, 1, "expected call") | ||||||
self.assertEqual(mock_requests.last_request.qs, {}) | ||||||
self.assertEqual(mock_requests.last_request.json(), createNote) | ||||||
|
||||||
@requests_mock.mock() | ||||||
def test_retrieve(self, mock_requests): | ||||||
mock_requests.register_uri( | ||||||
"GET", | ||||||
"https://api.chartmogul.com/v1/customer_notes/note_00000000-0000-0000-0000-000000000000", | ||||||
status_code=200, | ||||||
json=note, | ||||||
) | ||||||
|
||||||
config = Config("token") | ||||||
expected = CustomerNote.retrieve( | ||||||
config, uuid="note_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, CustomerNote)) | ||||||
|
||||||
@requests_mock.mock() | ||||||
def test_destroy(self, mock_requests): | ||||||
mock_requests.register_uri( | ||||||
"DELETE", | ||||||
"https://api.chartmogul.com/v1/customer_notes/note_00000000-0000-0000-0000-000000000000", | ||||||
status_code=200, | ||||||
json=note, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't look right; when the note is deleted, it returns
Suggested change
|
||||||
) | ||||||
|
||||||
config = Config("token") | ||||||
CustomerNote.destroy( | ||||||
config, uuid="note_00000000-0000-0000-0000-000000000000" | ||||||
).get() | ||||||
self.assertEqual(mock_requests.call_count, 1, "expected call") | ||||||
self.assertEqual(mock_requests.last_request.qs, {}) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's better to assert the return value of the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.