Skip to content
This repository has been archived by the owner on Aug 29, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Schadel committed Aug 26, 2011
2 parents df4caa9 + 52a1bf5 commit 318d9d3
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.svn
*.egg-info
*.swp
build/
6 changes: 4 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
Changelog
---------

2011-08-25: v1.1.0
2011-08-26: v1.1.0
* Modified OAuthAdapter to raise an APIException when any API errors occur.
* Added get_activity() method to return subscriber analytics activity.
* Modified create() method to return the instance of the newly created Resource instead of true.
* Fixed bugs in find and findSubscribers methods when no matches are found.
* Modified create() method tests to be more generic.
* Fixed bug in POST and GET where dictionaries and lists were not properly json serialized.
* Fixed bug in find and findSubscribers methods when no matches are found by the api.
4 changes: 4 additions & 0 deletions aweber_api/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def _prepare_request_body(self, method, url, data):
if method not in ['POST', 'GET', 'PATCH'] or len(data.keys()) == 0:
return ''
if method in ['POST', 'GET']:
# WARNING: non-primative items in data must be json serialized.
for key in data:
if type(data[key]) in [dict, list]:
data[key] = json.dumps(data[key])
return urlencode(data)
if method == 'PATCH':
return json.dumps(data)
1 change: 1 addition & 0 deletions tests/data/any_collection/1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name": "Favorite Color", "is_subscriber_updateable": true, "https_etag": "\"356a192b7913b04c54574d18c28d46e6395428ab-cf46a99b7a9ec9059c21044110a3528be7b0a5de\"", "self_link": "https://api.aweber.com/1.0/accounts/1/lists/303449/custom_fields/1", "resource_type_link": "https://api.aweber.com/1.0/#custom_field", "id": 1}
1 change: 1 addition & 0 deletions tests/data/any_collection/page1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"start": 0, "total_size": 0, "entries": [], "resource_type_link": "https://api.aweber.com/1.0/#any_collection"}
2 changes: 1 addition & 1 deletion tests/data/error.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"error": {"message": "Simulated Exception", "type": "BadRequestError"}}
{"error": {"status": 400, "message": "Simulated Exception", "type": "BadRequestError", "documentation_url": "https://labs.aweber.com/docs/troubleshooting#BadRequestError"}}
35 changes: 20 additions & 15 deletions tests/mock_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,23 @@
'/accounts/1?ws.op=findSubscribers&' \
'email=joe%40example.com&' \
'ws.show=total_size': ({}, 'accounts/findSubscribers_ts'),
'/accounts/1?ws.op=getWebForms': ({}, 'accounts/webForms'),
'/accounts/1?ws.op=getWebFormSplitTests': ({}, 'accounts/webFormSplitTests'),
'/accounts/1/lists': ({}, 'lists/page1'),
'/accounts/1/lists?ws.start=20&ws.size=20': ({}, 'lists/page2'),
'/accounts/1/lists/303449': ({}, 'lists/303449'),
'/accounts/1/lists/505454': ({}, 'lists/505454'),
'/accounts/1/lists/303449/campaigns': ({}, 'campaigns/303449'),
'/accounts/1/lists/303449/custom_fields': ({}, 'custom_fields/303449'),
'/accounts/1/lists/505454/custom_fields': ({}, 'custom_fields/505454'),
'/accounts/1/lists/303449/custom_fields/1': ({}, 'custom_fields/1'),
'/accounts/1/lists/303449/custom_fields/2': ({}, 'custom_fields/2'),
'/accounts/1/lists/303449/subscribers': ({}, 'subscribers/page1'),
'/accounts/1/lists/303449/subscribers/1': ({}, 'subscribers/1'),
'/accounts/1/lists/303449/subscribers/2': ({}, 'subscribers/2'),
'/accounts/1/lists/505454/subscribers/3': ({}, 'subscribers/3'),
'/accounts/1?ws.op=getWebForms': ({}, 'accounts/webForms'),
'/accounts/1?ws.op=getWebFormSplitTests': ({}, 'accounts/webFormSplitTests'),
'/accounts/1/lists': ({}, 'lists/page1'),
'/accounts/1/lists?ws.start=20&ws.size=20': ({}, 'lists/page2'),
'/accounts/1/lists/303449': ({}, 'lists/303449'),
'/accounts/1/lists/505454': ({}, 'lists/505454'),
'/accounts/1/lists/303449/any_collection': ({}, 'any_collection/page1'),
'/accounts/1/lists/303449/any_collection/1': ({}, 'any_collection/1'),
'/accounts/1/lists/303449/campaigns': ({}, 'campaigns/303449'),
'/accounts/1/lists/303449/custom_fields': ({}, 'custom_fields/303449'),
'/accounts/1/lists/505454/custom_fields': ({}, 'custom_fields/505454'),
'/accounts/1/lists/303449/custom_fields/1': ({}, 'custom_fields/1'),
'/accounts/1/lists/303449/custom_fields/2': ({}, 'custom_fields/2'),
'/accounts/1/lists/303449/subscribers': ({}, 'subscribers/page1'),
'/accounts/1/lists/303449/subscribers/1': ({}, 'subscribers/1'),
'/accounts/1/lists/303449/subscribers/2': ({}, 'subscribers/2'),
'/accounts/1/lists/505454/subscribers/3': ({}, 'subscribers/3'),
'/accounts/1/lists/303449/subscribers/1?ws.op=getActivity': (
{}, 'subscribers/get_activity'),
'/accounts/1/lists/303449/subscribers/1?ws.op=getActivity&ws.show=total_size': (
Expand All @@ -48,6 +50,9 @@
'ws.show=total_size': ({}, 'subscribers/find_ts'),
},
'POST' : {
'/accounts/1/lists/303449/any_collection': ({
'status': '201',
'location': '/accounts/1/lists/303449/any_collection/1'}, None),
'/accounts/1/lists/303449/custom_fields': ({
'status': '201',
'location': '/accounts/1/lists/303449/custom_fields/2'}, None),
Expand Down
36 changes: 28 additions & 8 deletions tests/test_aweber_collection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
from unittest import TestCase

from aweber_api import AWeberAPI, AWeberCollection, AWeberEntry
from aweber_api.base import API_BASE, APIException
from mock_adapter import MockAdapter
Expand Down Expand Up @@ -99,23 +101,41 @@ def test_should_return_new_resource_entry_object(self):
assert self.resp.id == 2
assert self.resp.url == '/accounts/1/lists/303449/custom_fields/2'

def test_should_have_requested_create_with_post(self):
self.assertEqual(self.create_req['method'], 'POST')

def test_should_have_requested_create_on_cf(self):
self.assertEqual(self.create_req['url'] , self.cf.url)
class TestCreateMethod(TestCase):

def setUp(self):
self.aweber = AWeberAPI('1', '2')
self.aweber.adapter = MockAdapter()
url = '/accounts/1/lists/303449/any_collection'
self.any_collection = self.aweber.load_from_url(url)

self.aweber.adapter.requests = []
self.resp = self.any_collection.create(
a_string='Bob', a_dict={'Color': 'blue'}, a_list=['apple'])
self.create_req = self.aweber.adapter.requests[0]
self.get_req = self.aweber.adapter.requests[1]

def test_should_make_request_with_correct_parameters(self):
expected_params = {'ws.op': 'create', 'a_string': 'Bob',
'a_dict': json.dumps({'Color': 'blue'}),
'a_list': json.dumps(['apple'])}

def test_should_have_requested_move_with_correct_parameters(self):
expected_params = {'ws.op': 'create', 'name': 'Wedding Song'}
self.assertEqual(self.create_req['data'], expected_params)

def test_should_make_two_requests(self):
self.assertEqual(len(self.aweber.adapter.requests), 2)

def test_should_refresh_cf_resource(self):
def test_should_have_requested_create_on_cf(self):
self.assertEqual(self.create_req['url'] , self.any_collection.url)

def test_should_have_requested_create_with_post(self):
self.assertEqual(self.create_req['method'], 'POST')

def test_should_refresh_created_resource(self):
self.assertEqual(self.get_req['method'], 'GET')
self.assertEqual(self.get_req['url'] ,
'/accounts/1/lists/303449/custom_fields/2')
'/accounts/1/lists/303449/any_collection/1')


class TestGettingParentEntry(TestCase):
Expand Down

0 comments on commit 318d9d3

Please sign in to comment.