Skip to content

Commit 545f5eb

Browse files
committed
Modifying test_user_obj to a test_user function.
1 parent 0a1df41 commit 545f5eb

File tree

3 files changed

+166
-91
lines changed

3 files changed

+166
-91
lines changed

tests/unit/__init__.py

Lines changed: 107 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -39,95 +39,115 @@ def _call(*args, **kwargs):
3939
return _call
4040

4141

42-
test_user_obj = {
43-
"type": "user",
44-
"id": "aaaaaaaaaaaaaaaaaaaaaaaa",
45-
"user_id": 'id-from-customers-app',
46-
"email": 'bob@example.com',
47-
"name": "Joe Schmoe",
48-
"avatar": {
49-
"type": "avatar",
50-
"image_url": "https://graph.facebook.com/1/picture?width=24&height=24"
51-
},
52-
"app_id": "the-app-id",
53-
"created_at": 1323422442,
54-
"custom_attributes": {"a": "b", "b": 2},
55-
"companies": {
56-
"type": "company.list",
57-
"companies": [
58-
{
59-
"type": "company",
60-
"company_id": "123",
61-
"id": "bbbbbbbbbbbbbbbbbbbbbbbb",
62-
"app_id": "the-app-id",
63-
"name": "Company 1",
64-
"remote_created_at": 1390936440,
65-
"created_at": 1401970114,
66-
"updated_at": 1401970114,
67-
"last_request_at": 1401970113,
68-
"monthly_spend": 0,
69-
"session_count": 0,
70-
"user_count": 1,
71-
"tag_ids": [],
72-
"custom_attributes": {
73-
"category": "Tech"
42+
def test_user(email="bob@example.com"):
43+
return {
44+
"type": "user",
45+
"id": "aaaaaaaaaaaaaaaaaaaaaaaa",
46+
"user_id": 'id-from-customers-app',
47+
"email": email,
48+
"name": "Joe Schmoe",
49+
"avatar": {
50+
"type": "avatar",
51+
"image_url": "https://graph.facebook.com/1/picture?width=24&height=24"
52+
},
53+
"app_id": "the-app-id",
54+
"created_at": 1323422442,
55+
"custom_attributes": {"a": "b", "b": 2},
56+
"companies": {
57+
"type": "company.list",
58+
"companies": [
59+
{
60+
"type": "company",
61+
"company_id": "123",
62+
"id": "bbbbbbbbbbbbbbbbbbbbbbbb",
63+
"app_id": "the-app-id",
64+
"name": "Company 1",
65+
"remote_created_at": 1390936440,
66+
"created_at": 1401970114,
67+
"updated_at": 1401970114,
68+
"last_request_at": 1401970113,
69+
"monthly_spend": 0,
70+
"session_count": 0,
71+
"user_count": 1,
72+
"tag_ids": [],
73+
"custom_attributes": {
74+
"category": "Tech"
75+
}
7476
}
75-
}
76-
]
77-
},
78-
"session_count": 123,
79-
"unsubscribed_from_emails": True,
80-
"last_request_at": 1401970113,
81-
"created_at": 1401970114,
82-
"remote_created_at": 1393613864,
83-
"updated_at": 1401970114,
84-
"user_agent_data": "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",
85-
"social_profiles": {
86-
"type": "social_profile.list",
87-
"social_profiles": [
88-
{
89-
"type": "social_profile",
90-
"name": "twitter",
91-
"url": "http://twitter.com/abc",
92-
"username": "abc",
93-
"id": None
94-
},
95-
{
96-
"type": "social_profile",
97-
"name": "twitter",
98-
"username": "abc2",
99-
"url": "http://twitter.com/abc2",
100-
"id": None
101-
},
102-
{
103-
"type": "social_profile",
104-
"name": "facebook",
105-
"url": "http://facebook.com/abc",
106-
"username": "abc",
107-
"id": "1234242"
108-
},
109-
{
110-
"type": "social_profile",
111-
"name": "quora",
112-
"url": "http://facebook.com/abc",
113-
"username": "abc",
114-
"id": "1234242"
115-
}
116-
]
117-
},
118-
"location_data": {
119-
"type": "location_data",
120-
"city_name": 'Dublin',
121-
"continent_code": 'EU',
122-
"country_name": 'Ireland',
123-
"latitude": '90',
124-
"longitude": '10',
125-
"postal_code": 'IE',
126-
"region_name": 'Europe',
127-
"timezone": '+1000',
128-
"country_code": "IRL"
77+
]
78+
},
79+
"session_count": 123,
80+
"unsubscribed_from_emails": True,
81+
"last_request_at": 1401970113,
82+
"created_at": 1401970114,
83+
"remote_created_at": 1393613864,
84+
"updated_at": 1401970114,
85+
"user_agent_data": "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",
86+
"social_profiles": {
87+
"type": "social_profile.list",
88+
"social_profiles": [
89+
{
90+
"type": "social_profile",
91+
"name": "twitter",
92+
"url": "http://twitter.com/abc",
93+
"username": "abc",
94+
"id": None
95+
},
96+
{
97+
"type": "social_profile",
98+
"name": "twitter",
99+
"username": "abc2",
100+
"url": "http://twitter.com/abc2",
101+
"id": None
102+
},
103+
{
104+
"type": "social_profile",
105+
"name": "facebook",
106+
"url": "http://facebook.com/abc",
107+
"username": "abc",
108+
"id": "1234242"
109+
},
110+
{
111+
"type": "social_profile",
112+
"name": "quora",
113+
"url": "http://facebook.com/abc",
114+
"username": "abc",
115+
"id": "1234242"
116+
}
117+
]
118+
},
119+
"location_data": {
120+
"type": "location_data",
121+
"city_name": 'Dublin',
122+
"continent_code": 'EU',
123+
"country_name": 'Ireland',
124+
"latitude": '90',
125+
"longitude": '10',
126+
"postal_code": 'IE',
127+
"region_name": 'Europe',
128+
"timezone": '+1000',
129+
"country_code": "IRL"
130+
}
129131
}
130-
}
132+
133+
134+
def page_of_users(include_next_link=False):
135+
page = {
136+
"type": "user.list",
137+
"pages": {
138+
"type": "pages",
139+
"page": 1,
140+
"next": None,
141+
"per_page": 50,
142+
"total_pages": 7
143+
},
144+
"users": [test_user("user1@example.com"), test_user("user2@example.com"), test_user("user3@example.com")],
145+
"total_count": 314
146+
}
147+
if include_next_link:
148+
page["pages"]["next"] = "https://api.intercom.io/users?per_page=50&page=2"
149+
return page
150+
131151

132152
# def test_messages
133153
# [test_message, test_message]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import httpretty
2+
import json
3+
import re
4+
from describe import expect
5+
from intercom.admin import Admin
6+
from intercom.collection_proxy import CollectionProxy
7+
8+
get = httpretty.GET
9+
r = re.compile
10+
11+
12+
class DescribeIntercomCollectionProxy:
13+
14+
@httpretty.activate
15+
def it_stops_iterating_if_no_next_link(self):
16+
body = json.dumps({})
17+
httpretty.register_uri(get, r(r"/users"), body=body)
18+
all = Admin.all()
19+
expect(all).to.be_instance_of(CollectionProxy)
20+
21+
22+
# require "spec_helper"
23+
24+
# describe Intercom::CollectionProxy do
25+
26+
# it "stops iterating if no next link" do
27+
# Intercom.expects(:get).with("/users", {}).returns(page_of_users(include_next_link:false))
28+
# emails = []
29+
# Intercom::User.all.each { |user| emails << user.email }
30+
# emails.must_equal %W(user1@example.com user2@example.com user3@example.com)
31+
# end
32+
33+
# it "keeps iterating if next link" do
34+
# Intercom.expects(:get).with("/users", {}).returns(page_of_users(include_next_link:true))
35+
# Intercom.expects(:get).with('https://api.intercom.io/users?per_page=50&page=2', {}).returns(page_of_users(include_next_link:false))
36+
# emails = []
37+
# Intercom::User.all.each { |user| emails << user.email }
38+
# end
39+
40+
# it "supports indexed array access" do
41+
# Intercom.expects(:get).with("/users", {}).returns(page_of_users(include_next_link:false))
42+
# Intercom::User.all[0].email.must_equal 'user1@example.com'
43+
# end
44+
45+
# it "supports map" do
46+
# Intercom.expects(:get).with("/users", {}).returns(page_of_users(include_next_link:false))
47+
# emails = Intercom::User.all.map { |user| user.email }
48+
# emails.must_equal %W(user1@example.com user2@example.com user3@example.com)
49+
# end
50+
51+
# it "supports querying" do
52+
# Intercom.expects(:get).with("/users", {:tag_name => 'Taggart J'}).returns(page_of_users(include_next_link:false))
53+
# Intercom::User.find_all(:tag_name => 'Taggart J').map(&:email).must_equal %W(user1@example.com user2@example.com user3@example.com)
54+
# end
55+
# end

tests/unit/user_spec.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from intercom.lib.flat_store import FlatStore
1111
from intercom.user import User
1212
from intercom.utils import create_class_instance
13-
from tests.unit import test_user_obj
13+
from tests.unit import test_user
1414

1515

1616
get = httpretty.GET
@@ -47,7 +47,7 @@ def it_throws_an_attribute_error_on_trying_to_access_an_attribute_that_has_not_b
4747
user.foo_property
4848

4949
def it_presents_a_complete_user_record_correctly(self):
50-
user = User.from_api(test_user_obj)
50+
user = User.from_api(test_user())
5151
expect('id-from-customers-app') == user.user_id
5252
expect('bob@example.com') == user.email
5353
expect('Joe Schmoe') == user.name
@@ -133,13 +133,13 @@ def it_rejects_nested_data_structures_in_custom_attributes(self):
133133
with expect.to_raise_error(ValueError):
134134
user.custom_attributes = { 1: { 2: 3}}
135135

136-
user = User.from_api(test_user_obj)
136+
user = User.from_api(test_user())
137137
with expect.to_raise_error(ValueError):
138138
user.custom_attributes["thing"] = [1]
139139

140140
@httpretty.activate
141141
def it_fetches_a_user(self):
142-
body = json.dumps(test_user_obj)
142+
body = json.dumps(test_user())
143143

144144
httpretty.register_uri(
145145
get, r(r"https://api.intercom.io/users\?email="),

0 commit comments

Comments
 (0)