8000 Merge pull request #82 from skyl/refactor_resources · Stackdriver/twilio-python@1a64fca · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 1, 2018. It is now read-only.

Commit 1a64fca

Browse files
committed
Merge pull request twilio#82 from skyl/refactor_resources
Refactor resources into multiple files
2 parents 84fc539 + 4c7cace commit 1a64fca

31 files changed

+1664
-1557
lines changed

tests/resources/process.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
#!/usr/bin/env python
22
from __future__ import with_statement
33
import re
4-
import sys
5-
import argparse
6-
import json
7-
import random
84
import os
95

10-
sid = "ed59a5733d2c1c1c69a83a28"
6+
sid = "ed59a5733d2c1c1c69a83a28"
7+
118

129
def twilio_clean(contents):
1310
contents = re.sub(r"([A-Z]{2}\w{8})\w{24}", r"\1%s" % sid, contents)
@@ -16,6 +13,7 @@ def twilio_clean(contents):
1613
contents = re.sub(r"[0-9\- \(\)]{14}", r"(415) 867-5309", contents)
1714
return contents
1815

16+
1917
def main():
2018
for f in os.listdir(os.path.abspath(os.path.dirname(__file__))):
2119
path, ext = os.path.splitext(f)
@@ -27,5 +25,6 @@ def main():
2725
g.write(contents.strip())
2826
g.write("\n")
2927

28+
3029
if __name__ == "__main__":
3130
main()

tests/test_applications.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import unittest2 as unittest
44
else:
55
import unittest
6+
67
from mock import Mock
7-
from twilio import TwilioException
8-
from twilio.rest.resources import Application
8+
#from twilio import TwilioException
9+
#from twilio.rest.resources import Application
910
from twilio.rest.resources import Applications
1011

1112

@@ -45,7 +46,7 @@ def test_update(self):
4546
self.resource.update("123", voice_url="hey")
4647

4748
uri = "http://api.twilio.com/Applications/123"
48-
request.assert_called_with("POST", uri, data={"VoiceUrl":"hey"})
49+
request.assert_called_with("POST", uri, data={"VoiceUrl": "hey"})
4950

5051
def test_create(self):
5152
request = Mock()
@@ -57,4 +58,4 @@ def test_create(self):
5758
self.resource.create(friendly_name="hey")
5859

5960
uri = "http://api.twilio.com/Applications"
60-
request.assert_called_with("POST", uri, data={"FriendlyName":"hey"})
61+
request.assert_called_with("POST", uri, data={"FriendlyName": "hey"})

tests/test_authorized_connect_apps.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import unittest2 as unittest
55
else:
66
import unittest
7+
78
from mock import Mock, patch
8-
from twilio import TwilioException
99
from twilio.rest.resources import AuthorizedConnectApps
1010
from twilio.rest.resources import AuthorizedConnectApp
1111

12+
1213
class AuthorizedConnectAppTest(unittest.TestCase):
1314

1415
def setUp(self):
@@ -17,7 +18,7 @@ def setUp(self):
1718
self.auth = ("AC123", "token")
1819
self.resource = AuthorizedConnectApps(self.uri, self.auth)
1920

20-
@patch("twilio.rest.resources.make_twilio_request")
21+
@patch("twilio.rest.resources.base.make_twilio_request")
2122
def test_get(self, mock):
2223
mock.return_value = Mock()
2324
mock.return_value.content = '{"connect_app_sid": "SID"}'
@@ -26,7 +27,7 @@ def test_get(self, mock):
2627
mock.assert_called_with("GET", "/base/AuthorizedConnectApps/SID",
2728
auth=self.auth)
2829

29-
@patch("twilio.rest.resources.make_twilio_request")
30+
@patch("twilio.rest.resources.base.make_twilio_request")
3031
def test_list(self, mock):
3132
mock.return_value = Mock()
3233
mock.return_value.content = '{"authorized_connect_apps": []}'
@@ -38,14 +39,14 @@ def test_list(self, mock):
3839
def test_load(self):
3940
instance = AuthorizedConnectApp(Mock(), "sid")
4041
instance.load({
41-
"connect_app_sid":"SID",
42-
"account_sid":"AC8dfe2f2358cf421cb6134cf6f217c6a3",
43-
"permissions":["get-all"],
44-
"connect_app_friendly_name":"foo",
45-
"connect_app_description":"bat",
46-
"connect_app_company_name":"bar",
47-
"connect_app_homepage_url":"http://www.google.com",
48-
"uri":"/2010-04-01/Accounts/",
42+
"connect_app_sid": "SID",
43+
"account_sid": "AC8dfe2f2358cf421cb6134cf6f217c6a3",
44+
"permissions": ["get-all"],
45+
"connect_app_friendly_name": "foo",
46+
"connect_app_description": "bat",
47+
"connect_app_company_name": "bar",
48+
"connect_app_homepage_url": "http://www.google.com",
49+
"uri": "/2010-04-01/Accounts/",
4950
})
5051

5152
self.assertEquals(instance.permissions, ["get-all"])
@@ -66,4 +67,3 @@ def test_create(self):
6667
def test_update(self):
6768
with self.assertRaises(AttributeError):
6869
self.resource.update()
69-

tests/test_calls.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import date
2-
from mock import patch, Mock
3-
from nose.tools import raises, assert_equals, assert_true
2+
from mock import patch
3+
from nose.tools import raises, assert_true
44
from twilio.rest.resources import Calls
55
from tools import create_mock_json
66

@@ -11,7 +11,8 @@
1111

1212
list_resource = Calls(BASE_URI, AUTH)
1313

14-
@patch("twilio.rest.resources.make_twilio_request")
14+
15+
@patch("twilio.rest.resources.base.make_twilio_request")
1516
def test_create_call(mock):
1617
resp = create_mock_json("tests/resources/calls_instance.json")
1718
resp.status_code = 201
@@ -30,28 +31,30 @@ def test_create_call(mock):
3031
mock.assert_called_with("POST", uri, data=exp_params, auth=AUTH)
3132

3233

33-
@patch("twilio.rest.resources.make_twilio_request")
34+
@patch("twilio.rest.resources.base.make_twilio_request")
3435
def test_paging(mock):
3536
resp = create_mock_json("tests/resources/calls_list.json")
3637
mock.return_value = resp
3738

3839
uri = "%s/Calls" % (BASE_URI)
39-
list_resource.list(started_before=date(2010,12,5))
40+
list_resource.list(started_before=date(2010, 12, 5))
4041
exp_params = {'StartTime<': '2010-12-05'}
4142

4243
mock.assert_called_with("GET", uri, params=exp_params, auth=AUTH)
4344

44-
@patch("twilio.rest.resources.make_twilio_request")
45+
46+
@patch("twilio.rest.resources.base.make_twilio_request")
4547
def test_get(mock):
4648
resp = create_mock_json("tests/resources/calls_instance.json")
4749
mock.return_value = resp
4850

4951
uri = "%s/Calls/%s" % (BASE_URI, CALL_SID)
50-
r = list_resource.get(CALL_SID)
52+
list_resource.get(CALL_SID)
5153

5254
mock.assert_called_with("GET", uri, auth=AUTH)
5355

54-
@patch("twilio.rest.resources.make_twilio_request")
56+
57+
@patch("twilio.rest.resources.base.make_twilio_request")
5558
def test_hangup(mock):
5659
resp = create_mock_json("tests/resources/calls_instance.json")
5760
resp.status_code = 204
@@ -64,7 +67,8 @@ def test_hangup(mock):
6467
mock.assert_called_with("POST", uri, data=exp_data, auth=AUTH)
6568
assert_true(r)
6669

67-
@patch("twilio.rest.resources.make_twilio_request")
70+
71+
@patch("twilio.rest.resources.base.make_twilio_request")
6872
def test_cancel(mock):
6973
resp = create_mock_json("tests/resources/calls_instance.json")
7074
resp.status_code = 204
@@ -77,6 +81,7 @@ def test_cancel(mock):
7781
mock.assert_called_with("POST", uri, data=exp_data, auth=AUTH)
7882
assert_true(r)
7983

84+
8085
@raises(AttributeError)
8186
def test_create():
8287
list_resource.delete

tests/test_client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
import unittest2 as unittest
44
else:
55
import unittest
6-
from twilio.rest import TwilioRestClient
7-
from twilio.rest import resources
6+
7+
from twilio.rest import TwilioRestClient, resources
88
from mock import patch, Mock
99

10+
1011
class RestClientTest(unittest.TestCase):
1112

1213
def setUp(self):
1314
self.client = TwilioRestClient("ACCOUNT_SID", "AUTH_TOKEN")
1415

15-
1616
@patch("twilio.rest.make_request")
1717
def test_request(self, mock):
1818
self.client.request("2010-04-01", method="GET")
@@ -21,14 +21,14 @@ def test_request(self, mock):
2121
auth=("ACCOUNT_SID", "AUTH_TOKEN"), data=None)
2222

10000 2323
def test_connect_apps(self):
24-
self.assertIsInstance(self.client.connect_apps,
24+
self.assertIsInstance(self.client.connect_apps,
2525
resources.ConnectApps)
2626

2727
def test_authorized_apps(self):
28-
self.assertIsInstance(self.client.authorized_connect_apps,
28+
self.assertIsInstance(self.client.authorized_connect_apps,
2929
resources.AuthorizedConnectApps)
3030

31-
@patch("twilio.rest.resources.make_request")
31+
@patch("twilio.rest.resources.base.make_request")
3232
def test_conferences(self, mock):
3333
mock.return_value = Mock()
3434
mock.return_value.ok = True

tests/test_connect_apps.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
import unittest2 as unittest
55
else:
66
import unittest
7+
78
from mock import Mock, patch
8-
from twilio import TwilioException
99
from twilio.rest.resources import ConnectApps
10-
from twilio.rest.resources import ConnectApp
1110

1211

1312
class ConnectAppTest(unittest.TestCase):
@@ -18,16 +17,16 @@ def setUp(self):
1817
self.auth = ("AC123", "token")
1918
self.resource = ConnectApps(self.uri, self.auth)
2019

21-
@patch("twilio.rest.resources.make_twilio_request")
20+
@patch("twilio.rest.resources.base.make_twilio_request")
2221
def test_get(self, mock):
2322
mock.return_value = Mock()
24-
mock.return_value.content = '{"sid": "SID"}'
23+
mock.return_value.content = '{"sid": "SID"}'
2524

2625
self.resource.get("SID")
2726
mock.assert_called_with("GET", "/base/ConnectApps/SID",
2827
auth=self.auth)
2928

30-
@patch("twilio.rest.resources.make_twilio_request")
29+
@patch("twilio.rest.resources.base.make_twilio_request")
3130
def test_list_with_paging(self, mock):
3231
mock.return_value = Mock()
3332
mock.return_value.content = '{"connect_apps": []}'
@@ -36,7 +35,7 @@ def test_list_with_paging(self, mock):
3635
mock.assert_called_with("GET", "/base/ConnectApps",
3736
params={"Page": 1, "PageSize": 50}, auth=self.auth)
3837

39-
@patch("twilio.rest.resources.make_twilio_request")
38+
@patch("twilio.rest.resources.base.make_twilio_request")
4039
def test_list(self, mock):
4140
mock.return_value = Mock()
4241
mock.return_value.content = '{"connect_apps": []}'
@@ -56,4 +55,3 @@ def test_delete(self):
5655
def test_update(self):
5756
with self.assertRaises(AttributeError):
5857
self.resource.update()
59-

tests/test_make_request.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99
import simplejson as json
1010

1111
import twilio
12-
from nose.tools import assert_equals
1312
from nose.tools import raises
14-
from mock import patch
15-
from mock import Mock
13+
from mock import patch, Mock
1614
from twilio import TwilioRestException
17-
from twilio.rest.resources import make_request
18-
from twilio.rest.resources import make_twilio_request
15+
from twilio.rest.resources.base import make_request, make_twilio_request
1916

2017
get_headers = {
2118
"User-Agent": "twilio-python/%s" % (twilio.__version__),
@@ -26,7 +23,7 @@
2623
post_headers["Content-Type"] = "application/x-www-form-urlencoded"
2724

2825

29-
@patch('twilio.rest.resources.Response')
26+
@patch('twilio.rest.resources.base.Response')
3027
@patch('httplib2.Http')
3128
def test_get_params(http_mock, response_mock):
3229
http = Mock()
@@ -37,7 +34,7 @@ def test_get_params(http_mock, response_mock):
3734
body=None, headers=None)
3835

3936

40-
@patch('twilio.rest.resources.Response')
37+
@patch('twilio.rest.resources.base.Response')
4138
@patch('httplib2.Http')
4239
def test_get_extra_paranms(http_mock, response_mock):
4340
http = Mock()
@@ -48,7 +45,7 @@ def test_get_extra_paranms(http_mock, response_mock):
4845
body=None, headers=None)
4946

5047

51-
@patch('twilio.rest.resources.Response')
48+
@patch('twilio.rest.resources.base.Response')
5249
@patch('httplib2.Http')
5350
def test_resp_uri(http_mock, response_mock):
5451
http = Mock()
@@ -59,15 +56,16 @@ def test_resp_uri(http_mock, response_mock):
5956
body=None, headers=None)
6057

6158

62-
@patch('twilio.rest.resources.make_request')
59+
@patch('twilio.rest.resources.base.make_request')
6360
def test_make_twilio_request_headers(mock):
6461
url = "http://random/url"
6562
make_twilio_request("POST", url)
6663
mock.assert_called_with("POST", "http://random/url.json",
6764
headers=post_headers)
6865

66+
6967
@raises(TwilioRestException)
70-
@patch('twilio.rest.resources.make_request')
68+
@patch('twilio.rest.resources.base.make_request')
7169
def test_make_twilio_request_bad_data(mock):
7270
resp = Mock()
7371
resp.ok = False

tests/test_members.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
from m 10000 ock import patch, Mock
2-
from datetime import date
3-
from mock import patch, Mock
4-
from nose.tools import raises, assert_equals, assert_true
1+
from mock import patch
52
from tools import create_mock_json
6-
from twilio.rest.resources import Member, Members
3+
from twilio.rest.resources import Members
74

85
QUEUE_SID = "QU1b9faddec3d54ec18488f86c83019bf0"
96
ACCOUNT_SID = "AC123"
@@ -15,7 +12,8 @@
1512

1613
list_resource = Members(BASE_URI, AUTH)
1714

18-
@patch("twilio.rest.resources.make_twilio_request")
15+
16+
@patch("twilio.rest.resources.base.make_twilio_request")
1917
def test_members_list(mock):
2018
resp = create_mock_json("tests/resources/members_list.json")
2119
mock.return_value = resp
@@ -25,7 +23,8 @@ def test_members_list(mock):
2523

2624
mock.assert_called_with("GET", uri, params={}, auth=AUTH)
2725

28-
@patch("twilio.rest.resources.make_twilio_request")
26+
27+
@patch("twilio.rest.resources.base.make_twilio_request")
2928
def test_members_dequeue_front(mock):
3029
resp = create_mock_json("tests/resources/members_instance.json")
3130
mock.return_value = resp
@@ -35,7 +34,8 @@ def test_members_dequeue_front(mock):
3534

3635
mock.assert_called_with("POST", uri, data={"Url": TWIML_URL}, auth=AUTH)
3736

38-
@patch("twilio.rest.resources.make_twilio_request")
37+
38+
@patch("twilio.rest.resources.base.make_twilio_request")
3939
def test_members_dequeue_call(mock):
4040
resp = create_mock_json("tests/resources/members_instance.json")
4141
mock.return_value = resp

0 commit comments

Comments
 (0)
0