def test_plans(self): self.service.plans().get(per_page=3) self.expect('GET', '/plans', {'per_page': 3}) self.service.plan('basic').get() self.expect('GET', '/plans/basic') self.service.plans().create({'plan': {'x': 'x'}}) self.expect('POST', '/plans', xml.dict_to_xml({'plan': {'x': 'x'}})) self.service.plan('basic').update({'plan': {'x': 'x'}}) self.expect('PUT', '/plans/basic', xml.dict_to_xml({'plan': {'x': 'x'}})) self.service.plan('basic').delete() self.expect('DELETE', '/plans/basic') self.service.plan('basic').addons().get(per_page=3) self.expect('GET', '/plans/basic/add_ons', {'per_page': 3}) self.service.plan('basic').addon('item').get() self.expect('GET', '/plans/basic/add_ons/item') self.service.plan('basic').addons().create({'add_on': {'x': 'x'}}) self.expect('POST', '/plans/basic/add_ons', xml.dict_to_xml({'add_on': {'x': 'x'}})) self.service.plan('basic').addon('item').update({'add_on': {'x': 'x'}}) self.expect('PUT', '/plans/basic/add_ons/item', xml.dict_to_xml({'add_on': {'x': 'x'}})) self.service.plan('basic').addon('item').delete() self.expect('DELETE', '/plans/basic/add_ons/item')
def use_xml(self, request): request.headers['Content-Type'] = 'application/xml' request.headers['Accept'] = 'application/xml' if request.method.upper() not in http.URLENCODE_METHODS: request.params = xml.dict_to_xml(request.params)
def test_encoding(self): u_test = (b'<?xml version="1.0" encoding="latin1"?>' b'<team><boolean>True</boolean>' b'<name>Bar\xe7a</name>' b'<nil />' b'<number>1234</number></team>') resp = xml.parse_xml(u_test, 200, None) self.assertEqual(resp['team']['name'], b'Bar\xe7a'.decode('latin1')) u_test = (b'<?xml version="1.0" encoding="UTF-8"?>' b'<team><boolean>True</boolean>' b'<name>Bar\xc3\xa7a</name>' b'<nil />' b'<number>1234</number></team>') resp = xml.parse_xml(u_test, 200, None) self.assertEqual(resp['team']['name'], b'Bar\xc3\xa7a'.decode('utf-8')) resp = xml.dict_to_xml({ 'team': { 'name': b'Bar\xc3\xa7a'.decode('utf-8'), 'boolean': True, 'number': 1234, 'nil': None } }) self.assertEqual(resp, u_test)
def test_cells(self): # XXX this fails with lxml compiled against some libxml versions with: # ValueError: Invalid tag name u'gs:colCount' # so disable the test for now if hasattr(xml.etree, 'LXML_VERSION'): # unittest doesn't have skip on Python 2.6... return with port.assertRaises(MethodNotSupported): (self.service.spreadsheet('key').worksheet('id', 'v', 'p').cells() .create({})) (self.service.spreadsheet('key').worksheet('id', 'v', 'p') .cell('cell').delete('v1')) cell = { 'entry': { '@xmlns': 'http://www.w3.org/2005/Atom', '@xmlns:gs': 'http://schemas.google.com/spreadsheets/2006', 'gs:cell': { '@inputValue': '=SUM(A2:A3)', '@row': '5', '@col': '1' } } } (self.service.spreadsheet('key').worksheet('id', 'v', 'p').cell('R5C1') .update('v1', cell.copy())) self.expect('PUT', '/feeds/cells/key/id/v/p/R5C1/v1', params=xml.dict_to_xml(cell))
def test_transactions(self): self.service.transactions().get() self.expect('GET', '/transactions') self.service.transactions().get(state='failed') self.expect('GET', '/transactions', {'state': 'failed'}) self.service.transactions().get(per_page=23) self.expect('GET', '/transactions', {'per_page': 23}) self.service.transactions().get(type='refund', per_page=23) self.expect('GET', '/transactions', {'type': 'refund', 'per_page': 23}) self.service.transactions().create({'transaction': {'x': 'x'}}) self.expect('POST', '/transactions', xml.dict_to_xml({'transaction': { 'x': 'x' }})) self.service.transaction('uuid').get() self.expect('GET', '/transactions/uuid') self.service.transaction('uuid').refund() self.expect('DELETE', '/transactions/uuid') self.service.transaction('uuid').refund(amount_in_cents=1000) self.expect('DELETE', '/transactions/uuid?amount_in_cents=1000')
def test_transactions(self): self.service.transactions().get() self.expect('GET', '/transactions') self.service.transactions().get(state='failed') self.expect('GET', '/transactions', {'state': 'failed'}) self.service.transactions().get(per_page=23) self.expect('GET', '/transactions', {'per_page': 23}) self.service.transactions().get(type='refund', per_page=23) self.expect('GET', '/transactions', {'type': 'refund', 'per_page': 23}) self.service.transactions().create({'transaction': {'x': 'x'}}) self.expect('POST', '/transactions', xml.dict_to_xml({'transaction': {'x': 'x'}})) self.service.transaction('uuid').get() self.expect('GET', '/transactions/uuid') self.service.transaction('uuid').refund() self.expect('DELETE', '/transactions/uuid') self.service.transaction('uuid').refund(amount_in_cents=1000) self.expect('DELETE', '/transactions/uuid?amount_in_cents=1000')
def test_subscriptions(self): self.service.subscriptions().get() self.expect('GET', '/subscriptions', {'state': 'live'}) self.service.subscriptions().get(state='future') self.expect('GET', '/subscriptions', {'state': 'future'}) self.service.subscriptions().get(per_page=23) self.expect('GET', '/subscriptions', {'state': 'live', 'per_page': 23}) self.service.subscriptions().create({'subscription': {'x': 'x'}}) self.expect('POST', '/subscriptions', xml.dict_to_xml({'subscription': { 'x': 'x' }})) self.service.subscription('uuid').get() self.expect('GET', '/subscriptions/uuid') self.service.subscription('uuid').update({'subscription': {'x': 'x'}}) self.expect('PUT', '/subscriptions/uuid', xml.dict_to_xml({'subscription': { 'x': 'x' }})) self.service.subscription('uuid').cancel() self.expect('PUT', '/subscriptions/uuid/cancel') self.service.subscription('uuid').reactivate() self.expect('PUT', '/subscriptions/uuid/reactivate') self.service.subscription('uuid').terminate() self.expect('PUT', '/subscriptions/uuid/terminate?refund=none') self.service.subscription('uuid').terminate(refund='partial') self.expect('PUT', '/subscriptions/uuid/terminate?refund=partial') today = '{0}'.format(date.today()) self.service.subscription('uuid').postpone(today) self.expect( 'PUT', '/subscriptions/uuid/postpone?next_renewal_date={0}'.format(today))
def test_worksheets(self): # XXX this fails with lxml compiled against some libxml versions with: # ValueError: Invalid tag name u'gs:colCount' # so disable the test for now if hasattr(xml.etree, 'LXML_VERSION'): # unittest doesn't have skip on Python 2.6... return create = { 'entry': { '@xmlns': 'http://www.w3.org/2005/Atom', '@xmlns:gs': 'http://schemas.google.com/spreadsheets/2006', 'title': 'testing', 'gs:rowCount': 50, 'gs:colCount': 10 } } (self.service.spreadsheet('key').worksheets('v', 'p') .create(create.copy())) self.expect('POST', '/feeds/worksheets/key/v/p', params=xml.dict_to_xml(create)) update = { 'entry': { '@xmlns': 'http://www.w3.org/2005/Atom', '@xmlns:gs': 'http://schemas.google.com/spreadsheets/2006', 'title': 'updated', 'gs:rowCount': 40, 'gs:colCount': 15 } } (self.service.spreadsheet('key').worksheet('id', 'v', 'p') .update('v1', update.copy())) self.expect('PUT', '/feeds/worksheets/key/v/p/id/v1', params=xml.dict_to_xml(update)) (self.service.spreadsheet('key').worksheet('id', 'v', 'p') .delete('v2')) self.expect('DELETE', '/feeds/worksheets/key/v/p/id/v2', None)
def test_subscriptions(self): self.service.subscriptions().get() self.expect('GET', '/subscriptions', {'state': 'live'}) self.service.subscriptions().get(state='future') self.expect('GET', '/subscriptions', {'state': 'future'}) self.service.subscriptions().get(per_page=23) self.expect('GET', '/subscriptions', {'state': 'live', 'per_page': 23}) self.service.subscriptions().create({'subscription': {'x': 'x'}}) self.expect('POST', '/subscriptions', xml.dict_to_xml({'subscription': {'x': 'x'}})) self.service.subscription('uuid').get() self.expect('GET', '/subscriptions/uuid') self.service.subscription('uuid').update({'subscription': {'x': 'x'}}) self.expect('PUT', '/subscriptions/uuid', xml.dict_to_xml({'subscription': {'x': 'x'}})) self.service.subscription('uuid').cancel() self.expect('PUT', '/subscriptions/uuid/cancel') self.service.subscription('uuid').reactivate() self.expect('PUT', '/subscriptions/uuid/reactivate') self.service.subscription('uuid').terminate() self.expect('PUT', '/subscriptions/uuid/terminate?refund=none') self.service.subscription('uuid').terminate(refund='partial') self.expect('PUT', '/subscriptions/uuid/terminate?refund=partial') today = '{0}'.format(date.today()) self.service.subscription('uuid').postpone(today) self.expect('PUT', '/subscriptions/uuid/postpone?next_renewal_date={0}'.format(today))
def test_coupons(self): self.service.coupons().get(per_page=3) self.expect('GET', '/coupons', {'per_page': 3}) self.service.coupons().get(state='expired') self.expect('GET', '/coupons', {'state': 'expired'}) self.service.coupon('discount').get() self.expect('GET', '/coupons/discount') self.service.coupons().create({'coupon': {'coupon_code': 'x'}}) self.expect('POST', '/coupons', xml.dict_to_xml({'coupon': {'coupon_code': 'x'}})) self.service.coupon('discount').delete() self.expect('DELETE', '/coupons/discount')
def test_basic_usage(self): resp1 = xml.parse_xml(self.xml_header + self.xml_duck, 200, None) duck1 = resp1['duck'] self.check_duck(duck1) # From generated xml: generate a dict; then, parse it again # It must be exactly the same than the very first dict resp2 = xml.parse_xml(xml.dict_to_xml(resp1), 200, None) duck2 = resp2['duck'] self.check_duck(duck2) # Now, let's test an array... xml_doc = self.xml_header xml_doc += b'<ducks>' xml_doc += self.xml_duck xml_doc += self.xml_duck xml_doc += b'</ducks>' resp3 = xml.parse_xml(xml_doc, 200, None) ducks = resp3['ducks']['duck'] for duck in ducks: self.check_duck(duck)
def test_encoding(self): u_test = (b'<?xml version="1.0" encoding="latin1"?>' b'<team><boolean>True</boolean>' b'<name>Bar\xe7a</name>' b'<nil />' b'<number>1234</number></team>') resp = xml.parse_xml(u_test, 200, None) self.assertEqual(resp['team']['name'], b'Bar\xe7a'.decode('latin1')) u_test_part1 = (b'<?xml version="1.0" encoding="UTF-8"?>' b'<team><boolean>True</boolean>' b'<name>Bar\xc3\xa7a</name>') u_test_part2 = b'<number>1234</number></team>' # some versions of libxml2 produce <nil /> for empty tags, others # produce <nil></nil> - we don't care when serializing and we'll just # check for both u_test = u_test_part1 + b'<nil />' + u_test_part2 u_test_alt = u_test_part1 + b'<nil></nil>' + u_test_part2 resp = xml.parse_xml(u_test, 200, None) self.assertEqual(resp['team']['name'], b'Bar\xc3\xa7a'.decode('utf-8')) resp = xml.dict_to_xml({ 'team': { 'boolean': True, 'name': b'Bar\xc3\xa7a'.decode('utf-8'), 'nil': None, 'number': 1234 } }) # accept both <nil /> and <nil></nil>; can't use assertIn, as it only # got added in Python 2.7 try: self.assertEqual(resp, u_test) except AssertionError: self.assertEqual(resp, u_test_alt)
def test_encoding(self): u_test = (b'<?xml version="1.0" encoding="latin1"?>' b'<team><boolean>True</boolean>' b'<name>Bar\xe7a</name>' b'<nil />' b'<number>1234</number></team>') resp = xml.parse_xml(u_test, 200, None) self.assertEqual(resp['team']['name'], b'Bar\xe7a'.decode('latin1')) u_test = (b'<?xml version="1.0" encoding="UTF-8"?>' b'<team><boolean>True</boolean>' b'<name>Bar\xc3\xa7a</name>' b'<nil />' b'<number>1234</number></team>') resp = xml.parse_xml(u_test, 200, None) self.assertEqual(resp['team']['name'], b'Bar\xc3\xa7a'.decode('utf-8')) resp = xml.dict_to_xml({'team': { 'name': b'Bar\xc3\xa7a'.decode('utf-8'), 'boolean': True, 'number': 1234, 'nil': None }}) self.assertEqual(resp, u_test)
def test_encoding(self): u_test = (b'<?xml version="1.0" encoding="latin1"?>' b'<team><boolean>True</boolean>' b'<name>Bar\xe7a</name>' b'<nil />' b'<number>1234</number></team>') resp = xml.parse_xml(u_test, 200, None) self.assertEqual(resp['team']['name'], b'Bar\xe7a'.decode('latin1')) u_test_part1 = (b'<?xml version="1.0" encoding="UTF-8"?>' b'<team><boolean>True</boolean>' b'<name>Bar\xc3\xa7a</name>') u_test_part2 = b'<number>1234</number></team>' # some versions of libxml2 produce <nil /> for empty tags, others # produce <nil></nil> - we don't care when serializing and we'll just # check for both u_test = u_test_part1 + b'<nil />' + u_test_part2 u_test_alt = u_test_part1 + b'<nil></nil>' + u_test_part2 resp = xml.parse_xml(u_test, 200, None) self.assertEqual(resp['team']['name'], b'Bar\xc3\xa7a'.decode('utf-8')) resp = xml.dict_to_xml({'team': { 'name': b'Bar\xc3\xa7a'.decode('utf-8'), 'boolean': True, 'number': 1234, 'nil': None }}) # accept both <nil /> and <nil></nil>; can't use assertIn, as it only # got added in Python 2.7 try: self.assertEqual(resp, u_test) except AssertionError: self.assertEqual(resp, u_test_alt)
def set_format(self, request): if request.method.upper() in http.URLENCODE_METHODS: request.params['alt'] = 'json' else: request.headers['Content-Type'] = 'application/atom+xml' request.params = xml.dict_to_xml(request.params)
def test_accounts(self): self.service.accounts().get(per_page=3) self.expect('GET', '/accounts', {'state': 'active', 'per_page': 3}) self.service.accounts().get(state='closed') self.expect('GET', '/accounts', {'state': 'closed'}) self.service.account(3).get() self.expect('GET', '/accounts/3') self.service.account(3).adjustments().get() self.expect('GET', '/accounts/3/adjustments') self.service.account(3).adjustments().get(state='pending') self.expect('GET', '/accounts/3/adjustments', {'state': 'pending'}) self.service.account(3).adjustments().get(type='credit') self.expect('GET', '/accounts/3/adjustments', {'type': 'credit'}) self.service.account(3).invoices().get() self.expect('GET', '/accounts/3/invoices') self.service.account(3).invoices().get(per_page=23) self.expect('GET', '/accounts/3/invoices', {'per_page': 23}) self.service.account(3).subscriptions().get() self.expect('GET', '/accounts/3/subscriptions') self.service.account(3).subscriptions().get(per_page=23) self.expect('GET', '/accounts/3/subscriptions', {'per_page': 23}) self.service.account(3).billing_info().get() self.expect('GET', '/accounts/3/billing_info') self.service.account(3).redemption().get() self.expect('GET', '/accounts/3/redemption') self.service.accounts().create({'account': {'account_code': 'x'}}) self.expect('POST', '/accounts', xml.dict_to_xml({'account': {'account_code': 'x'}})) self.service.account(23).update({'account': {'username': '******'}}) self.expect('PUT', '/accounts/23', xml.dict_to_xml({'account': {'username': '******'}})) self.service.account(23).adjustments().create({ 'adjustment': {'x': 'x'} }) self.expect('POST', '/accounts/23/adjustments', xml.dict_to_xml({'adjustment': {'x': 'x'}})) self.service.account(23).invoices().create({ 'invoice': {'line_items': [{ 'adjustment': {'x': 'a'}}, {'adjustment': {'x': 'b'} }] }}) self.expect('POST', '/accounts/23/invoices', xml.dict_to_xml({ 'invoice': {'line_items': [{ 'adjustment': {'x': 'a'}}, {'adjustment': {'x': 'b'}} ]} })) self.service.account(23).billing_info().update({ 'billing_info': {'x': 'x'} }) self.expect('PUT', '/accounts/23/billing_info', xml.dict_to_xml({'billing_info': {'x': 'x'}})) self.service.account(23).billing_info().delete() self.expect('DELETE', '/accounts/23/billing_info') self.service.account(23).redemption().create({ 'redemption': {'coupon_code': 'x'} }) self.expect('POST', '/accounts/23/redemption', xml.dict_to_xml({'redemption': {'coupon_code': 'x'}})) self.service.account(23).redemption().delete() self.expect('DELETE', '/accounts/23/redemption') self.service.account(23).delete() self.expect('DELETE', '/accounts/23')
def test_accounts(self): self.service.accounts().get(per_page=3) self.expect('GET', '/accounts', {'state': 'active', 'per_page': 3}) self.service.accounts().get(state='closed') self.expect('GET', '/accounts', {'state': 'closed'}) self.service.account(3).get() self.expect('GET', '/accounts/3') self.service.account(3).adjustments().get() self.expect('GET', '/accounts/3/adjustments') self.service.account(3).adjustments().get(state='pending') self.expect('GET', '/accounts/3/adjustments', {'state': 'pending'}) self.service.account(3).adjustments().get(type='credit') self.expect('GET', '/accounts/3/adjustments', {'type': 'credit'}) self.service.account(3).invoices().get() self.expect('GET', '/accounts/3/invoices') self.service.account(3).invoices().get(per_page=23) self.expect('GET', '/accounts/3/invoices', {'per_page': 23}) self.service.account(3).subscriptions().get() self.expect('GET', '/accounts/3/subscriptions') self.service.account(3).subscriptions().get(per_page=23) self.expect('GET', '/accounts/3/subscriptions', {'per_page': 23}) self.service.account(3).billing_info().get() self.expect('GET', '/accounts/3/billing_info') self.service.account(3).redemption().get() self.expect('GET', '/accounts/3/redemption') self.service.accounts().create({'account': {'account_code': 'x'}}) self.expect('POST', '/accounts', xml.dict_to_xml({'account': { 'account_code': 'x' }})) self.service.account(23).update({'account': {'username': '******'}}) self.expect('PUT', '/accounts/23', xml.dict_to_xml({'account': { 'username': '******' }})) self.service.account(23).adjustments().create( {'adjustment': { 'x': 'x' }}) self.expect('POST', '/accounts/23/adjustments', xml.dict_to_xml({'adjustment': { 'x': 'x' }})) self.service.account(23).invoices().create({ 'invoice': { 'line_items': [{ 'adjustment': { 'x': 'a' } }, { 'adjustment': { 'x': 'b' } }] } }) self.expect( 'POST', '/accounts/23/invoices', xml.dict_to_xml({ 'invoice': { 'line_items': [{ 'adjustment': { 'x': 'a' } }, { 'adjustment': { 'x': 'b' } }] } })) self.service.account(23).billing_info().update( {'billing_info': { 'x': 'x' }}) self.expect('PUT', '/accounts/23/billing_info', xml.dict_to_xml({'billing_info': { 'x': 'x' }})) self.service.account(23).billing_info().delete() self.expect('DELETE', '/accounts/23/billing_info') self.service.account(23).redemption().create( {'redemption': { 'coupon_code': 'x' }}) self.expect('POST', '/accounts/23/redemption', xml.dict_to_xml({'redemption': { 'coupon_code': 'x' }})) self.service.account(23).redemption().delete() self.expect('DELETE', '/accounts/23/redemption') self.service.account(23).delete() self.expect('DELETE', '/accounts/23')