Skip to content

Commit

Permalink
Merge pull request #90 from recurly/preview_associations
Browse files Browse the repository at this point in the history
Create resources based on the element if no href links to the object
  • Loading branch information
bhelx committed Oct 31, 2014
2 parents fcd62db + 7cc02a7 commit ed039df
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 29 deletions.
8 changes: 7 additions & 1 deletion recurly/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,13 @@ def relatitator(**kwargs):
return Page.page_for_value(resp, value)
return value
return relatitator
return make_relatitator(elem.attrib['href'])

url = elem.attrib['href']

if url is '':
return Resource.value_for_element(elem)
else:
return make_relatitator(url)

return self.value_for_element(elem)

Expand Down
61 changes: 61 additions & 0 deletions tests/fixtures/subscription/change-preview.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
POST https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab/preview HTTP/1.1
Accept: application/xml
Authorization: Basic YXBpa2V5Og==
User-Agent: recurly-python/{version}
Content-Type: application/xml; charset=utf-8

<?xml version='1.0' encoding='UTF-8'?>
<subscription>
<quantity type="integer">2</quantity>
</subscription>

HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<subscription
href="https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab">
<uuid>123456789012345678901234567890ab</uuid>
<account href="https://api.recurly.com/v2/accounts/subscribemock"/>
<plan href="https://api.recurly.com/v2/plans/basicplan">
<plan_code>basicplan</plan_code>
<name>Basic Plan</name>
</plan>
<state>active</state>
<quantity type="integer">1</quantity>
<currency>EUR</currency>
<unit_amount_in_cents type="integer">1000</unit_amount_in_cents>
<activated_at type="datetime">2011-05-27T07:00:00Z</activated_at>
<canceled_at nil="nil"></canceled_at>
<expires_at nil="nil"></expires_at>
<current_period_started_at type="datetime">2011-06-27T07:00:00Z</current_period_started_at>
<current_period_ends_at type="datetime">2010-07-27T07:00:00Z</current_period_ends_at>
<trial_started_at nil="nil"></trial_started_at>
<trial_ends_at nil="nil"></trial_ends_at>
<tax_in_cents type="integer">0</tax_in_cents>
<tax_type>usst</tax_type>
<subscription_add_ons type="array">
</subscription_add_ons>
<invoice href="">
<account href="https://api.recurly.com/v2/accounts/subscribemock"/>
<subscription href="https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab"/>
<uuid>4ba1531325014b4f969cd13676f514d8</uuid>
<account_code>subscribemock</account_code>
<total_in_cents>2000</total_in_cents>
<created_at type="nil"></created_at>
<line_items type="array">
<adjustment>
<uuid>4ba1531325014b4f969cd13676f51438</uuid>
<account_code>chargemock</account_code>
<unit_amount_in_cents type="integer">1000</unit_amount_in_cents>
<amount_in_cents type="integer">2000</amount_in_cents>
<quantity>2</quantity>
<currency>USD</currency>
<start_date type="datetime">2011-06-27T07:00:00Z</start_date>
<end_date type="datetime">2010-07-27T07:00:00Z</end_date>
<description>Basic Plan charge</description>
<created_at type="nil"></created_at>
</adjustment>
</line_items>
</invoice>
</subscription>
52 changes: 25 additions & 27 deletions tests/fixtures/subscription/show.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
GET https://api.recurly.com/v2/accounts/subscribemock/subscriptions HTTP/1.1
GET https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab HTTP/1.1
Accept: application/xml
Authorization: Basic YXBpa2V5Og==
User-Agent: recurly-python/{version}
Expand All @@ -9,29 +9,27 @@ X-Records: 1
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<subscriptions type="array">
<subscription
href="https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab">
<uuid>123456789012345678901234567890ab</uuid>
<account href="https://api.recurly.com/v2/accounts/subscribemock"/>
<plan href="https://api.recurly.com/v2/plans/basicplan">
<plan_code>basicplan</plan_code>
<name>Basic Plan</name>
</plan>
<state>active</state>
<quantity type="integer">1</quantity>
<currency>EUR</currency>
<unit_amount_in_cents type="integer">1000</unit_amount_in_cents>
<activated_at type="datetime">2011-05-27T07:00:00Z</activated_at>
<canceled_at nil="nil"></canceled_at>
<expires_at nil="nil"></expires_at>
<current_period_started_at type="datetime">2011-06-27T07:00:00Z</current_period_started_at>
<current_period_ends_at type="datetime">2010-07-27T07:00:00Z</current_period_ends_at>
<trial_started_at nil="nil"></trial_started_at>
<trial_ends_at nil="nil"></trial_ends_at>
<tax_in_cents type="integer">0</tax_in_cents>
<tax_type>usst</tax_type>
<subscription_add_ons type="array">
</subscription_add_ons>
</subscription>
</subscriptions>
<subscription
href="https://api.recurly.com/v2/subscriptions/123456789012345678901234567890ab">
<uuid>123456789012345678901234567890ab</uuid>
<account href="https://api.recurly.com/v2/accounts/subscribemock"/>
<plan href="https://api.recurly.com/v2/plans/basicplan">
<plan_code>basicplan</plan_code>
<name>Basic Plan</name>
</plan>
<state>active</state>
<quantity type="integer">1</quantity>
<currency>EUR</currency>
<unit_amount_in_cents type="integer">1000</unit_amount_in_cents>
<activated_at type="datetime">2011-05-27T07:00:00Z</activated_at>
<canceled_at nil="nil"></canceled_at>
<expires_at nil="nil"></expires_at>
<current_period_started_at type="datetime">2011-06-27T07:00:00Z</current_period_started_at>
<current_period_ends_at type="datetime">2010-07-27T07:00:00Z</current_period_ends_at>
<trial_started_at nil="nil"></trial_started_at>
<trial_ends_at nil="nil"></trial_ends_at>
<tax_in_cents type="integer">0</tax_in_cents>
<tax_type>usst</tax_type>
<subscription_add_ons type="array">
</subscription_add_ons>
</subscription>
11 changes: 10 additions & 1 deletion tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,15 @@ def test_plan(self):
plan = Plan.get(plan_code)
self.assertTrue(plan.tax_exempt)

def test_preview_subscription_change(self):
with self.mock_request('subscription/show.xml'):
sub = Subscription.get('123456789012345678901234567890ab')

with self.mock_request('subscription/change-preview.xml'):
sub.quantity = 2
sub.preview()
self.assertTrue(sub.invoice.line_items[0].amount_in_cents, 2000)

def test_subscribe(self):
logging.basicConfig(level=logging.DEBUG) # make sure it's init'ed
logger = logging.getLogger('recurly.http.request')
Expand Down Expand Up @@ -818,7 +827,7 @@ def test_subscribe(self):
plan.delete()

with self.mock_request('subscription/show.xml'):
sub = account.subscriptions()[0]
sub = Subscription.get('123456789012345678901234567890ab')
self.assertEqual(sub.tax_in_cents, 0)
self.assertEqual(sub.tax_type, 'usst')

Expand Down

0 comments on commit ed039df

Please sign in to comment.