Skip to content

Commit 37f06b1

Browse files
committed
Merge pull request intercom#76 from jkeyes/issue-74
Issue 74
2 parents 8bcbad0 + f012c55 commit 37f06b1

File tree

11 files changed

+283
-300
lines changed

11 files changed

+283
-300
lines changed

dev-requirements.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@
22
# Development dependencies.
33
#
44
nose==1.3.4
5-
httpretty==0.8.6
65
mock==1.0.1
7-
coverage==3.7.1
8-
Sphinx==1.3.1

intercom/traits/api_resource.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,17 @@ def to_datetime_value(value):
3333

3434

3535
class Resource(object):
36+
changed_attributes = []
3637

3738
def __init__(_self, **params): # noqa
3839
# intercom includes a 'self' field in the JSON, to avoid the naming
3940
# conflict we go with _self here
40-
_self.changed_attributes = []
4141
_self.from_dict(params)
4242

4343
if hasattr(_self, 'flat_store_attributes'):
4444
for attr in _self.flat_store_attributes:
4545
if not hasattr(_self, attr):
4646
setattr(_self, attr, FlatStore())
47-
_self.changed_attributes = []
4847

4948
def _flat_store_attribute(self, attribute):
5049
if hasattr(self, 'flat_store_attributes'):
@@ -59,14 +58,16 @@ def from_api(cls, response):
5958

6059
def from_response(self, response):
6160
self.from_dict(response)
62-
self.changed_attributes = []
6361
return self
6462

6563
def from_dict(self, dict):
6664
for attribute, value in list(dict.items()):
6765
if type_field(attribute):
6866
continue
6967
setattr(self, attribute, value)
68+
if hasattr(self, 'id'):
69+
# already exists in Intercom
70+
self.changed_attributes = []
7071

7172
@property
7273
def attributes(self):
@@ -77,7 +78,7 @@ def attributes(self):
7778
return res
7879

7980
def submittable_attribute(self, name, value):
80-
return name in self.changed_attributes or isinstance(value, FlatStore)
81+
return name in self.changed_attributes or (isinstance(value, FlatStore) and name in self.flat_store_attributes) # noqa
8182

8283
def __getattribute__(self, attribute):
8384
value = super(Resource, self).__getattribute__(attribute)
@@ -96,5 +97,5 @@ def __setattr__(self, attribute, value):
9697
else:
9798
value_to_set = value
9899
if attribute != 'changed_attributes':
99-
self.__dict__['changed_attributes'].append(attribute)
100+
self.changed_attributes.append(attribute)
100101
super(Resource, self).__setattr__(attribute, value_to_set)

tests/unit/test_collection_proxy.py

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,48 @@
11
# -*- coding: utf-8 -*-
22

3-
import httpretty
4-
import json
5-
import re
63
import unittest
74

5+
from intercom import Intercom
86
from intercom import User
7+
from mock import call
8+
from mock import patch
99
from nose.tools import eq_
1010
from nose.tools import istest
1111
from tests.unit import page_of_users
1212

13-
get = httpretty.GET
14-
r = re.compile
15-
1613

1714
class CollectionProxyTest(unittest.TestCase):
1815

1916
@istest
20-
@httpretty.activate
2117
def it_stops_iterating_if_no_next_link(self):
22-
body = json.dumps(page_of_users(include_next_link=False))
23-
httpretty.register_uri(get, r(r"/users"), body=body)
24-
emails = [user.email for user in User.all()]
25-
eq_(emails, ['user1@example.com', 'user2@example.com', 'user3@example.com']) # noqa
18+
body = page_of_users(include_next_link=False)
19+
with patch.object(Intercom, 'get', return_value=body) as mock_method:
20+
emails = [user.email for user in User.all()]
21+
mock_method.assert_called_once_with('/users')
22+
eq_(emails, ['user1@example.com', 'user2@example.com', 'user3@example.com']) # noqa
2623

2724
@istest
28-
@httpretty.activate
2925
def it_keeps_iterating_if_next_link(self):
30-
page1 = json.dumps(page_of_users(include_next_link=True))
31-
page2 = json.dumps(page_of_users(include_next_link=False))
32-
httpretty.register_uri(
33-
get, r(r"https://api.intercom.io/users$"), body=page1,
34-
match_querystring=True)
35-
httpretty.register_uri(
36-
get, r(r"https://api.intercom.io/users\?per_page=50&page=2"),
37-
body=page2, match_querystring=True)
38-
emails = [user.email for user in User.all()]
39-
eq_(emails, ['user1@example.com', 'user2@example.com', 'user3@example.com'] * 2) # noqa
26+
page1 = page_of_users(include_next_link=True)
27+
page2 = page_of_users(include_next_link=False)
28+
side_effect = [page1, page2]
29+
with patch.object(Intercom, 'get', side_effect=side_effect) as mock_method: # noqa
30+
emails = [user.email for user in User.all()]
31+
eq_([call('/users'), call('https://api.intercom.io/users?per_page=50&page=2')], # noqa
32+
mock_method.mock_calls)
33+
eq_(emails, ['user1@example.com', 'user2@example.com', 'user3@example.com'] * 2) # noqa
4034

4135
@istest
42-
@httpretty.activate
4336
def it_supports_indexed_array_access(self):
44-
body = json.dumps(page_of_users(include_next_link=False))
45-
httpretty.register_uri(get, r(r"/users$"), body=body)
46-
eq_(User.all()[0].email, 'user1@example.com')
37+
body = page_of_users(include_next_link=False)
38+
with patch.object(Intercom, 'get', return_value=body) as mock_method:
39+
eq_(User.all()[0].email, 'user1@example.com')
40+
mock_method.assert_called_once_with('/users')
4741

4842
@istest
49-
@httpretty.activate
5043
def it_supports_querying(self):
51-
body = json.dumps(page_of_users(include_next_link=False))
52-
httpretty.register_uri(get, r(r"/users$"), body=body)
53-
emails = [user.email for user in User.find_all(tag_name='Taggart J')]
54-
eq_(emails, ['user1@example.com', 'user2@example.com', 'user3@example.com']) # noqa
44+
body = page_of_users(include_next_link=False)
45+
with patch.object(Intercom, 'get', return_value=body) as mock_method:
46+
emails = [user.email for user in User.find_all(tag_name='Taggart J')] # noqa
47+
eq_(emails, ['user1@example.com', 'user2@example.com', 'user3@example.com']) # noqa
48+
mock_method.assert_called_once_with('/users', tag_name='Taggart J')

tests/unit/test_company.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,45 @@
11
# -*- coding: utf-8 -*-
22

3-
import httpretty
4-
import json
5-
import re
63
import intercom
74
import unittest
85

96
from intercom import Company
7+
from intercom import Intercom
8+
from mock import call
9+
from mock import patch
1010
from nose.tools import assert_raises
11+
from nose.tools import eq_
1112
from nose.tools import istest
1213

13-
get = httpretty.GET
14-
r = re.compile
15-
1614

1715
class CompanyTest(unittest.TestCase):
1816

1917
@istest
20-
@httpretty.activate
2118
def it_raises_error_if_no_response_on_find(self):
22-
httpretty.register_uri(
23-
get, r(r'/companies$'), body=None, status=200)
24-
with assert_raises(intercom.HttpError):
25-
Company.find(company_id='4')
19+
with patch.object(Intercom, 'get', return_value=None) as mock_method:
20+
with assert_raises(intercom.HttpError):
21+
Company.find(company_id='4')
22+
mock_method.assert_called_once_with('/companies', company_id='4')
2623

2724
@istest
28-
@httpretty.activate
2925
def it_raises_error_if_no_response_on_find_all(self):
30-
httpretty.register_uri(
31-
get, r(r'/companies$'), body=None, status=200)
32-
with assert_raises(intercom.HttpError):
33-
[x for x in Company.all()]
26+
with patch.object(Intercom, 'get', return_value=None) as mock_method:
27+
with assert_raises(intercom.HttpError):
28+
[x for x in Company.all()]
29+
mock_method.assert_called_once_with('/companies')
3430

3531
@istest
36-
@httpretty.activate
3732
def it_raises_error_on_load(self):
3833
data = {
3934
'type': 'user',
4035
'id': 'aaaaaaaaaaaaaaaaaaaaaaaa',
4136
'company_id': '4',
4237
'name': 'MyCo'
4338
}
44-
httpretty.register_uri(
45-
get, r(r'/companies$'), body=json.dumps(data), status=200)
46-
company = Company.find(company_id='4')
47-
httpretty.register_uri(
48-
get, r(r'/companies/aaaaaaaaaaaaaaaaaaaaaaaa$'), body=None, status=200) # noqa
49-
with assert_raises(intercom.HttpError):
50-
company.load()
39+
side_effect = [data, None]
40+
with patch.object(Intercom, 'get', side_effect=side_effect) as mock_method: # noqa
41+
company = Company.find(company_id='4')
42+
with assert_raises(intercom.HttpError):
43+
company.load()
44+
eq_([call('/companies', company_id='4'), call('/companies/aaaaaaaaaaaaaaaaaaaaaaaa')], # noqa
45+
mock_method.mock_calls)

tests/unit/test_event.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22

3-
import httpretty
43
import time
54
import unittest
65

@@ -41,7 +40,6 @@ def it_creates_an_event_with_metadata(self):
4140
mock_method.assert_called_once_with('/events/', **data)
4241

4342
@istest
44-
@httpretty.activate
4543
def it_creates_an_event_without_metadata(self):
4644
data = {
4745
'event_name': 'sale of item',

tests/unit/test_message.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
# -*- coding: utf-8 -*-
22

3-
import httpretty
4-
import json
5-
import re
63
import time
74
import unittest
85

96
from datetime import datetime
7+
from intercom import Intercom
108
from intercom import User
119
from intercom import Message
10+
from mock import patch
1211
from nose.tools import eq_
1312
from nose.tools import istest
1413

15-
post = httpretty.POST
16-
r = re.compile
17-
1814

1915
class MessageTest(unittest.TestCase):
2016

@@ -28,7 +24,6 @@ def setUp(self): # noqa
2824
self.created_time = now - 300
2925

3026
@istest
31-
@httpretty.activate
3227
def it_creates_a_user_message_with_string_keys(self):
3328
data = {
3429
'from': {
@@ -37,13 +32,12 @@ def it_creates_a_user_message_with_string_keys(self):
3732
},
3833
'body': 'halp'
3934
}
40-
httpretty.register_uri(
41-
post, r(r'/messages/$'), body=json.dumps(data), status=200)
42-
message = Message.create(**data)
43-
eq_('halp', message.body)
35+
with patch.object(Intercom, 'post', return_value=data) as mock_method:
36+
message = Message.create(**data)
37+
mock_method.assert_called_once_with('/messages/', **data)
38+
eq_('halp', message.body)
4439

4540
@istest
46-
@httpretty.activate
4741
def it_creates_an_admin_message(self):
4842
data = {
4943
'from': {
@@ -57,8 +51,9 @@ def it_creates_an_admin_message(self):
5751
'body': 'halp',
5852
'message_type': 'inapp'
5953
}
60-
httpretty.register_uri(
61-
post, r(r'/messages/$'), body=json.dumps(data), status=200)
62-
message = Message.create(**data)
63-
eq_('halp', message.body)
64-
eq_('inapp', message.message_type)
54+
55+
with patch.object(Intercom, 'post', return_value=data) as mock_method:
56+
message = Message.create(**data)
57+
mock_method.assert_called_once_with('/messages/', **data)
58+
eq_('halp', message.body)
59+
eq_('inapp', message.message_type)

tests/unit/test_note.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
11
# -*- coding: utf-8 -*-
22

3-
import httpretty
4-
import json
5-
import re
63
import unittest
74

5+
from intercom import Intercom
86
from intercom import Note
7+
from mock import patch
98
from nose.tools import eq_
109
from nose.tools import istest
1110

12-
post = httpretty.POST
13-
r = re.compile
14-
1511

1612
class NoteTest(unittest.TestCase):
1713

1814
@istest
19-
@httpretty.activate
2015
def it_creates_a_note(self):
2116
data = {
2217
'body': '<p>Note to leave on user</p>',
2318
'created_at': 1234567890
2419
}
25-
httpretty.register_uri(
26-
post, r(r'/notes/$'), body=json.dumps(data))
27-
note = Note.create(body="Note to leave on user")
28-
eq_(note.body, "<p>Note to leave on user</p>")
20+
with patch.object(Intercom, 'post', return_value=data) as mock_method:
21+
note = Note.create(body="Note to leave on user")
22+
mock_method.assert_called_once_with('/notes/', body="Note to leave on user") # noqa
23+
eq_(note.body, "<p>Note to leave on user</p>")
2924

3025
@istest
31-
@httpretty.activate
3226
def it_sets_gets_allowed_keys(self):
3327
params = {
3428
'body': 'Note body',

0 commit comments

Comments
 (0)