def test_save(self): key = 'SOME_TEST_KEY' marketplace = 'SOME_MARKETPLACE' txbalanced.configure(key, marketplace) ret = self._create_deferred(content=TEST_DATA['customer']) with mock.patch('treq.request', return_value=ret): customer = yield txbalanced.Customer(name='TEST_PERSON').save() the_method, headers = treq.request.call_args self.assertEqual(the_method[0], 'POST') self.assertIn('data', headers) self.assertEqual(json.dumps({'name': 'TEST_PERSON'}), headers['data']) ret = self._create_deferred(content=TEST_DATA['customer']) with mock.patch('treq.request', return_value=ret): uri = '/v1/customers/TESTESTESTESTEST' customer = yield txbalanced.Customer(name='TEST_PERSON') customer._data['uri'] = uri yield customer.save() the_method, headers = treq.request.call_args self.assertTrue(the_method[1].endswith(uri)) self.assertEqual(the_method[0], 'PUT') self.assertIn('data', headers) self.assertEqual(json.dumps({'name': 'TEST_PERSON'}), headers['data'])
def test_resource_uri(self): key = 'SOME_TEST_KEY' marketplace = 'SOME_MARKETPLACE' txbalanced.configure(key, marketplace) customer = txbalanced.Customer() self.assertEqual(customer.resource(), 'customers') uri = '/customers' self.assertEqual(customer.resource_uri(), uri)
def test_remove(self): key = 'SOME_TEST_KEY' txbalanced.configure(key, 'MPTESTTEST') ret = self._create_deferred(content='') with mock.patch('treq.request', return_value=ret): uri = '/v1/customers/TESTESTESTESTEST' customer = yield txbalanced.Customer(uri=uri) yield customer.remove() the_method, headers = treq.request.call_args self.assertEqual(the_method[0], 'DELETE')
def test_customer(self): key = 'SOME_TEST_KEY' txbalanced.configure(key, 'MPTESTTEST') ret = self._create_deferred(content=TEST_DATA['customer']) with mock.patch('treq.request', return_value=ret): uri = '/v1/customers/TESTESTESTESTEST' customer = yield txbalanced.Customer.find(uri) self.assertEqual(customer.name, 'Test Name') self.assertEqual(customer.get('name'), 'Test Name') self.assertIdentical(customer.get('nothere'), None) self.assertEqual(customer.get('nothere', 'blah'), 'blah')
def test_last_page(self): key = 'SOME_TEST_KEY' txbalanced.configure(key, 'MPTESTTEST') ret = self._create_deferred(content=TEST_DATA['customers']) with mock.patch('treq.request', return_value=ret): customers = yield txbalanced.Customer.query() next_page = json.loads(TEST_DATA['customers']) next_page['last_uri'] = None next_page = json.dumps(next_page) ret = self._create_deferred(content=next_page) with mock.patch('treq.request', return_value=ret): more = yield customers.last_page() self.assertIsInstance(more, txbalanced.Page) self.assertIdentical(more.last_uri, None) npage = yield more.last_page() self.assertIdentical(npage, None)
def test_debit(self): key = 'SOME_TEST_KEY' txbalanced.configure(key, 'MPTESTTEST') ret = self._create_deferred(content=TEST_DATA['debit']) with mock.patch('treq.request', return_value=ret): uri = '/v1/marketplaces/MKTESTMARKETPLACEURI/debits/WDTESTDEBITURI' debit = yield txbalanced.Debit.find(uri) self.assertEqual(debit.amount, 1200) self.assertIsInstance(debit.customer, txbalanced.Customer) ret = self._create_deferred(content=TEST_DATA['refund']) with mock.patch('treq.request', return_value=ret): r = yield debit.refund() url, data = treq.request.call_args self.assertEqual(url[0], 'POST') l = len(debit.refunds_uri) * -1 self.assertEqual(url[1][l:], debit.refunds_uri) self.assertIsInstance(r, txbalanced.Refund)
def test_page(self): key = 'SOME_TEST_KEY' txbalanced.configure(key, 'MPTESTTEST') ret = self._create_deferred(content=TEST_DATA['customers']) with mock.patch('treq.request', return_value=ret): customers = yield txbalanced.Customer.query() self.assertIsInstance(customers, txbalanced.Page) self.assertEqual(len(customers), 10) self.assertIsInstance(customers[0], txbalanced.Customer) # Test iteration tmp = list(customers) self.assertEqual(len(customers), 10) tmp = reversed(tmp) self.assertEqual(list(tmp), list(reversed(customers))) self.assertIn('_type', dir(customers)) self.assertEqual(customers._type, 'page') self.assertRaises(AttributeError, getattr, customers, 'notintehre')
def test_hold(self): key = 'SOME_TEST_KEY' txbalanced.configure(key, 'MPTESTTEST') ret = self._create_deferred(content=TEST_DATA['hold']) with mock.patch('treq.request', return_value=ret): uri = '/v1/marketplaces/MKTESTMARKETPLACEURI/holds/HLTESTHOLDURI' hold = yield txbalanced.Hold.find(uri) self.assertEqual(hold.amount, 1000) self.assertIsInstance(hold.customer, txbalanced.Customer) self.assertIsInstance(hold.expires_at, datetime.datetime) ret = self._create_deferred(content=TEST_DATA['hold']) with mock.patch('treq.request', return_value=ret): r = yield hold.cancel() url, data = treq.request.call_args self.assertEqual(url[0], 'PUT') self.assertIn('data', data) self.assertEqual({"is_void": True}, json.loads(data['data'])) ret = self._create_deferred(content=TEST_DATA['debit']) with mock.patch('treq.request', return_value=ret): d = yield hold.capture() url, data = treq.request.call_args self.assertEqual(url[0], 'POST') self.assertIn('data', data) data = json.loads(data['data']) self.assertEqual(data['hold_uri'], hold.uri) self.assertEqual(data['amount'], hold.amount) bad_date = json.loads(TEST_DATA['hold']) bad_date['expires_at'] = 'XXXXNOTADATEXXXX' bad_date = json.dumps(bad_date) ret = self._create_deferred(content=bad_date) with mock.patch('treq.request', return_value=ret): uri = '/v1/marketplaces/MKTESTMARKETPLACEURI/holds/HLTESTHOLDURI' d = yield txbalanced.Hold.find(uri) self.assertEqual(d.expires_at, 'XXXXNOTADATEXXXX')
def test_config(self): self.assertIdentical(txbalanced.HTTPClient.config.api_key_secret, None) key = 'SOME_TEST_KEY' txbalanced.configure(key, 'MPTESTTEST') self.assertEqual(txbalanced.HTTPClient.config.api_key_secret, key)