Skip to content

Commit 2562c40

Browse files
committed
Writing api Resource spec.
1 parent bd0ef77 commit 2562c40

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

intercom/traits/api_resource.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def from_api(cls, response):
5353

5454
def from_response(self, response):
5555
self.from_dict(response)
56+
return self
5657

5758
def from_dict(self, dict):
5859
for attribute, value in dict.items():

tests/unit/traits/__init__.py

Whitespace-only changes.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from datetime import datetime
2+
from describe import expect
3+
from intercom.traits.api_resource import Resource
4+
5+
6+
class DescribeIntercomTraitsApiResource:
7+
8+
def before_each(self, context):
9+
self.object_json = {
10+
"type": "company",
11+
"id": "aaaaaaaaaaaaaaaaaaaaaaaa",
12+
"app_id": "some-app-id",
13+
"name": "SuperSuite",
14+
"plan_id": 1,
15+
"remote_company_id": "8",
16+
"remote_created_at": 103201,
17+
"created_at": 1374056196,
18+
"user_count": 1,
19+
"custom_attributes": {}
20+
}
21+
self.api_resource = Resource().from_response(self.object_json)
22+
self.api_resource_obj = super(Resource, self.api_resource)
23+
24+
def it_does_not_set_type_on_parsing_json(self):
25+
expect(self.api_resource).to_not.have_attr('type')
26+
27+
def it_coerces_time_on_parsing_json(self):
28+
expect(datetime.fromtimestamp(1374056196)) == self.api_resource.created_at
29+
30+
def it_dynamically_defines_accessors_for_non_existent_properties(self):
31+
expect(self.api_resource).to_not.have_attr('spiders')
32+
self.api_resource.spiders = 4
33+
expect(self.api_resource).to.have_attr('spiders')
34+
35+
def it_calls_dynamically_defined_getter_when_asked(self):
36+
self.api_resource.foo = 4
37+
expect(4) == self.api_resource.foo
38+
39+
def it_accepts_unix_timestamps_into_dynamically_defined_date_setters(self):
40+
self.api_resource.foo_at = 1401200468
41+
expect(1401200468) == self.api_resource_obj.__getattribute__('foo_at')
42+
43+
def it_accepts_unix_timestamps_into_dynamically_defined_data_getters(self):
44+
self.api_resource.foo_at = 1401200468
45+
expect(datetime.fromtimestamp(1401200468)) == self.api_resource.foo_at
46+
47+
# def it_throws_regular_error_when_non_existant_getter_is_called_that_is_backed_by_an_instance_variable(self):
48+
# super(Resource, self.api_resource).__setattr__('bar', 'you cant see me')
49+
# print self.api_resource.bar
50+
51+
def it_throws_attribute_error_when_non_existent_attribute_is_called(self):
52+
with expect.to_raise_error(AttributeError):
53+
self.api_resource.flubber
54+
55+
def it_throws_attribute_error_when_non_existent_method_is_called(self):
56+
with expect.to_raise_error(AttributeError):
57+
self.api_resource.flubber()
58+
59+
def it_throws_attribute_error_when_non_existent_setter_is_called(self):
60+
with expect.to_raise_error(AttributeError):
61+
self.api_resource.flubber('a', 'b')
62+
63+
def it_create_an_initialized_resource_equal_to_a_from_response_resource(self):
64+
initialized_api_resource = Resource(**self.object_json)
65+
for key in self.object_json.keys():
66+
if key == "type":
67+
continue
68+
expect(getattr(initialized_api_resource, key)) == getattr(self.api_resource, key)

0 commit comments

Comments
 (0)