Skip to content

Commit f3e224c

Browse files
committed
Using HTTPretty to mock the API for the doctests.
1 parent 69f8cc4 commit f3e224c

File tree

8 files changed

+148
-43
lines changed

8 files changed

+148
-43
lines changed

intercom/intercom.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def get_user(cls, email=None, user_id=None):
149149
150150
>>> user = Intercom.get_user(user_id='123')
151151
>>> user['name']
152-
u'Bob'
152+
u'Somebody'
153153
154154
"""
155155

@@ -188,16 +188,13 @@ def create_user(cls, **kwargs):
188188
unsubscribed status.
189189
190190
191-
>>> from datetime import datetime
192-
>>> import time
193-
>>> now = time.mktime(datetime.now().timetuple())
194191
>>> user = Intercom.create_user(user_id='7902', email='ben@intercom.io',
195-
... name='Ben McRedmond', created_at=now, last_seen_ip='1.2.3.4',
196-
... custom_data={ 'plan': 'pro'}, last_request_at=1300000000)
192+
... name='Somebody', created_at=1270000000, last_seen_ip='1.2.3.4',
193+
... custom_data={ 'app_name': 'Genesis'}, last_request_at=1300000000)
197194
>>> user['name']
198-
u'Ben McRedmond'
199-
>>> user['custom_data']['plan']
200-
u'pro'
195+
u'Somebody'
196+
>>> user['custom_data']['app_name']
197+
u'Genesis'
201198
>>> user['last_impression_at']
202199
1300000000
203200
@@ -210,10 +207,10 @@ def update_user(cls, **kwargs):
210207
211208
>>> user = Intercom.get_user(user_id='123')
212209
>>> user['name']
213-
u'Bob'
214-
>>> user = Intercom.update_user(user_id='123', name='Han')
210+
u'Somebody'
211+
>>> user = Intercom.update_user(user_id='123', name='Guido')
215212
>>> user['name']
216-
u'Han'
213+
u'Guido'
217214
218215
"""
219216
return Intercom._create_or_update_user('PUT', **kwargs)
@@ -222,9 +219,9 @@ def update_user(cls, **kwargs):
222219
def delete_user(cls, user_id=None, email=None):
223220
""" Delete a user.
224221
225-
>>> user = Intercom.get_user(user_id='7902')
222+
>>> user = Intercom.get_user(user_id='123')
226223
>>> user['email']
227-
u'bob@example.com'
224+
u'somebody@example.com'
228225
229226
"""
230227
params = {
@@ -263,9 +260,9 @@ def create_note(cls, user_id=None, email=None, body=None):
263260
""" Create a note.
264261
265262
>>> result = Intercom.create_note(email="somebody@example.com",
266-
... body="This is the text of my note.")
263+
... body="This is a note")
267264
>>> result['html']
268-
u'<p>This is the text of my note.</p>'
265+
u'<p>This is a note</p>'
269266
>>> result['user']['email']
270267
u'somebody@example.com'
271268
@@ -311,13 +308,13 @@ def create_message_thread(cls, user_id=None, email=None, body=None):
311308
312309
>>> message_thread = Intercom.create_message_thread(
313310
... email="somebody@example.com",
314-
... body="Uh, everything's under control. Situation normal.")
311+
... body="Hey Intercom, What is up?")
315312
>>> message_thread['thread_id']
316313
5591
317314
>>> len(message_thread['messages'])
318-
1
315+
3
319316
>>> message_thread['messages'][0]['html']
320-
u"<p>Uh, everything's under control. Situation normal.</p>"
317+
u'<p>Hey Intercom, What is up?</p>\n\n<p></p>'
321318
322319
"""
323320
params = {

intercom/message_thread.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def create(cls, user_id=None, email=None, body=None):
6666
>>> message_thread.thread_id
6767
5591
6868
>>> len(message_thread.messages)
69-
1
69+
3
7070
7171
"""
7272
resp = Intercom.create_message_thread(

intercom/note.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from . import Intercom
1717
from . import from_timestamp_property
18+
from . import to_timestamp_property
1819
from .user import User
1920
from .user import UserId
2021

@@ -27,10 +28,11 @@ def create(cls, user_id=None, email=None, body=None):
2728
""" Create a Note.
2829
2930
>>> note = Note.create(email="somebody@example.com",
30-
... body="This is the text of my note.")
31-
>>> note['created_at']
31+
... body="This is a note.")
32+
>>> note.created_at.year
33+
2011
3234
>>> note.html
33-
u'<p>This is the text of my note.</p>'
35+
u'<p>This is a note</p>'
3436
3537
"""
3638
resp = Intercom.create_note(user_id=user_id, email=email, body=body)
@@ -41,10 +43,10 @@ def save(self):
4143
4244
>>> note = Note()
4345
>>> note.email = "somebody@example.com"
44-
>>> note.body = "This is the text of my note."
46+
>>> note.body = "This is a note."
4547
>>> note.save()
4648
>>> note.html
47-
u'<p>This is the text of my note.</p>'
49+
u'<p>This is a note</p>'
4850
4951
"""
5052
resp = Intercom.create_note(
@@ -73,6 +75,12 @@ def created_at(self):
7375
API response. """
7476
return dict.get(self, 'created_at', None)
7577

78+
@created_at.setter
79+
@to_timestamp_property
80+
def created_at(self, created_at):
81+
""" Set the datetime this note was created. """
82+
self['created_at'] = created_at
83+
7684
@property
7785
def user(self):
7886
""" Get the noted user – set by an API response. """

intercom/user.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
""" User module.
88
99
>>> from intercom import Intercom
10-
>>> Intercom.app_id = 'dummy-app-id'
11-
>>> Intercom.api_key = 'dummy-api-key'
1210
>>> from intercom import User
1311
1412
"""
@@ -63,9 +61,9 @@ def find(cls, user_id=None, email=None):
6361
u'Somebody'
6462
>>> user = User.find(user_id=123)
6563
>>> user.email
66-
u'bob@example.com'
64+
u'somebody@example.com'
6765
>>> user.name
68-
u'Bob'
66+
u'Somebody'
6967
7068
"""
7169
resp = Intercom.get_user(user_id=user_id, email=email)
@@ -91,9 +89,9 @@ def find_by_user_id(cls, user_id):
9189
9290
>>> user = User.find(user_id=123)
9391
>>> user.email
94-
u'bob@example.com'
92+
u'somebody@example.com'
9593
>>> user.name
96-
u'Bob'
94+
u'Somebody'
9795
9896
"""
9997
resp = Intercom.get_user(user_id=user_id)
@@ -107,7 +105,7 @@ def create(cls, **kwargs):
107105
>>> user.name
108106
u'Somebody'
109107
>>> user.last_impression_at.year
110-
2014
108+
2011
111109
112110
"""
113111
resp = Intercom.create_user(**kwargs)
@@ -122,7 +120,7 @@ def delete(cls, user_id=None, email=None):
122120
u'123'
123121
>>> user = User.delete(user_id="123")
124122
>>> user.email
125-
u'bob@example.com'
123+
u'somebody@example.com'
126124
127125
"""
128126
resp = Intercom.delete_user(user_id=user_id, email=email)
@@ -381,11 +379,7 @@ def custom_data(self, custom_data):
381379
<class 'intercom.user.CustomData'>
382380
>>> user.save()
383381
>>> len(user.custom_data)
384-
1
385-
>>> user.custom_data['avg_monthly_spend'] = 102
386-
>>> user.save()
387-
>>> len(user.custom_data)
388-
2
382+
3
389383
390384
"""
391385
if not isinstance(custom_data, CustomData):
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"id": "52322b4150233908a800013c",
3+
"name": "Free Trial",
4+
"segment": false,
5+
"tagged_user_count": 2
6+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"intercom_id": "528f291df8d4a41d8c000088",
3+
"email": "somebody@example.com",
4+
"user_id": "123",
5+
"name": "Guido",
6+
"created_at": 1270000000,
7+
"last_impression_at": 1300000000,
8+
"custom_data": {
9+
"app_name": "Genesis",
10+
"monthly_spend": 155.5,
11+
"team_mates": 7
12+
},
13+
"social_profiles": [
14+
{
15+
"type": "twitter",
16+
"url": "http://twitter.com/abc",
17+
"username": "abc"
18+
},
19+
{
20+
"type": "facebook",
21+
"url": "http://facebook.com/vanity",
22+
"username": "vanity",
23+
"id": "13241141441141413"
24+
}
25+
],
26+
"location_data": {
27+
"city_name": "Santiago",
28+
"continent_code": "SA",
29+
"country_name": "Chile",
30+
"latitude": -33.44999999999999,
31+
"longitude": -70.6667,
32+
"postal_code": "",
33+
"region_name": "12",
34+
"timezone": "Chile/Continental",
35+
"country_code": "CHL"
36+
},
37+
"session_count": 0,
38+
"last_seen_ip": "127.0.0.1",
39+
"last_seen_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
40+
"unsubscribed_from_emails": false,
41+
"avatar_url": "https://graph.facebook.com/13241141441141413/picture?width=24&height=24",
42+
"company_ids": [
43+
44+
]
45+
}

tests/integration/fixtures/v1-users-note.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"html": "<p>This is a note</p>",
3-
"created_at": null,
3+
"created_at": 1300000000,
44
"user": {
55
"intercom_id": "528f2cbb1b393a6e470000e6",
66
"email": "somebody@example.com",

tests/integration/test.py

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
get = httpretty.GET
2828
post = httpretty.POST
29+
put = httpretty.PUT
30+
delete = httpretty.DELETE
2931
r = re.compile
3032

3133
def fixture(fixture):
@@ -34,10 +36,7 @@ def fixture(fixture):
3436

3537
@httpretty.activate
3638
def test_users():
37-
httpretty.register_uri(
38-
httpretty.GET,
39-
re.compile(r"/v1/users"),
40-
body=fixture('v1-users'))
39+
httpretty.register_uri(get, r(r"/v1/users"), body=fixture('v1-users'))
4140
expect(len(User.all())).should.be.greater_than(0)
4241

4342
@httpretty.activate
@@ -122,3 +121,59 @@ class BadGatewayError(Exception):
122121
User.find.when.called_with(email='somebody@example.com').should.throw(BadGatewayError)
123122
Intercom.endpoints = ("http://example.com", "http://api.example.com")
124123
User.find.when.called_with(email='not-found@example.com').should.throw(BadGatewayError)
124+
125+
@httpretty.activate
126+
def test_doctest():
127+
import doctest
128+
import intercom
129+
130+
def request_callback(method, uri, headers):
131+
parsed_body = httpretty.last_request().parsed_body
132+
# handle the user updates
133+
if 'name' in parsed_body:
134+
return (200, headers, fixture('v1-user-updated'))
135+
return (200, headers, fixture('v1-user'))
136+
137+
httpretty.register_uri(get, r(r'/v1/users$'), body=fixture('v1-users'), match_querystring=True)
138+
httpretty.register_uri(get, r(r'/v1/users\?page=1'), body=fixture('v1-users'), match_querystring=True)
139+
httpretty.register_uri(post, r(r'/v1/users$'), body=fixture('v1-user'), match_querystring=True)
140+
httpretty.register_uri(put, r(r'/v1/users$'), body=request_callback, match_querystring=True)
141+
httpretty.register_uri(delete, r(r'/v1/users$'), body=fixture('v1-user'), match_querystring=True)
142+
httpretty.register_uri(get, r(r"/v1/users\?email=somebody"), body=fixture('v1-user'), match_querystring=True)
143+
httpretty.register_uri(get, r(r"/v1/users\?user_id=123"), body=fixture('v1-user'), match_querystring=True)
144+
httpretty.register_uri(get, r(r"/v1/users\?email=not-found"), status=404, match_querystring=True)
145+
httpretty.register_uri(get, r(r"/v1/users\?email=server-error"), status=500, match_querystring=True)
146+
httpretty.register_uri(get, r(r"/v1/users\?email=authentication-error"), status=401, match_querystring=True)
147+
148+
httpretty.register_uri(get, r(r"/v1/tags\?name=Free"), body=fixture('v1-tag'))
149+
httpretty.register_uri(get, r(r"/v1/tags"), body=fixture('v1-tag'))
150+
httpretty.register_uri(post, r(r"/v1/tags"), body=fixture('v1-tag'))
151+
httpretty.register_uri(put, r(r"/v1/tags"), body=fixture('v1-tag'))
152+
153+
httpretty.register_uri(post, r(r"/v1/users/notes"), body=fixture('v1-users-note'))
154+
155+
httpretty.register_uri(get, r(r'/v1/users/message_threads$'), body=fixture('v1-users-message_threads'), match_querystring=True)
156+
httpretty.register_uri(post, r(r'/v1/users/message_threads$'), body=fixture('v1-users-message_thread'), match_querystring=True)
157+
httpretty.register_uri(put, r(r'/v1/users/message_threads$'), body=fixture('v1-users-message_thread'), match_querystring=True)
158+
httpretty.register_uri(get, r(r"/v1/users/message_threads\?thread_id=5591"), body=fixture('v1-users-message_thread'), match_querystring=True)
159+
httpretty.register_uri(get, r(r"/v1/users/message_threads\?email=somebody"), body=fixture('v1-users-message_threads'), match_querystring=True)
160+
161+
httpretty.register_uri(post, r(r"/v1/users/impressions"), body=fixture('v1-users-impressions'))
162+
163+
(failure_count, test_count) = doctest.testfile("../../intercom/user.py")
164+
expect(failure_count).to.equal(0)
165+
166+
(failure_count, test_count) = doctest.testfile("../../intercom/tag.py")
167+
expect(failure_count).to.equal(0)
168+
169+
(failure_count, test_count) = doctest.testfile("../../intercom/note.py")
170+
expect(failure_count).to.equal(0)
171+
172+
(failure_count, test_count) = doctest.testfile("../../intercom/message_thread.py")
173+
expect(failure_count).to.equal(0)
174+
175+
(failure_count, test_count) = doctest.testfile("../../intercom/impression.py")
176+
expect(failure_count).to.equal(0)
177+
178+
(failure_count, test_count) = doctest.testfile("../../intercom/intercom.py")
179+
expect(failure_count).to.equal(0)

0 commit comments

Comments
 (0)