Skip to content
Browse files

Merge branch 'gweeds' of github.com:balanced/balanced-python into gweeds

  • Loading branch information...
2 parents 583f979 + da9e3d0 commit d60856f8a7edd91a642e1cbfb65059d24e378429 Andrew committed May 23, 2012
Showing with 150 additions and 70 deletions.
  1. +3 −2 balanced/__init__.py
  2. +40 −2 balanced/resources.py
  3. +107 −66 tests/suite.py
View
5 balanced/__init__.py
@@ -1,4 +1,5 @@
-__version__ = '0.8.4'
+__version__ = '0.8.5'
+from collections import defaultdict
import contextlib
@@ -30,7 +31,7 @@
config = http_client.config
-CACHE = dict()
+CACHE = defaultdict(dict)
def bust_cache():
View
42 balanced/resources.py
@@ -407,10 +407,10 @@ def cacher(f):
@functools.wraps(f)
def wrapped(*args, **kwargs):
from balanced import config, CACHE
- cached = CACHE.get(config.api_key_secret)
+ cached = CACHE[config.api_key_secret].get(f.__name__)
if bust_cache or not cached:
cached = f(*args, **kwargs)
- CACHE[config.api_key_secret] = cached
+ CACHE[config.api_key_secret][f.__name__] = cached
return cached
return wrapped
@@ -437,6 +437,44 @@ class Marketplace(Resource):
collection='marketplaces',
resides_under_marketplace=False)
+ def create_card(self,
+ name,
+ card_number,
+ expiration_month,
+ expiration_year,
+ security_code=None,
+ street_address=None,
+ city=None,
+ region=None,
+ postal_code=None,
+ country_code=None,
+ phone_number=None,
+ ):
+ return Card(
+ card_number=card_number,
+ expiration_month=expiration_month,
+ expiration_year=expiration_year,
+ name=name,
+ security_code=security_code,
+ street_address=street_address,
+ postal_code=postal_code,
+ city=city,
+ region=region,
+ country_code=country_code,
+ phone_number=phone_number,
+ ).save()
+
+ def create_bank_account(self,
+ name,
+ account_number,
+ bank_code,
+ ):
+ return BankAccount(
+ name=name,
+ account_number=account_number,
+ bank_code=bank_code,
+ ).save()
+
def create_buyer(self, email_address, card_uri, name=None, meta=None):
meta = meta or {}
return Account(
View
173 tests/suite.py
@@ -5,8 +5,11 @@
import requests
import balanced
+from balanced.exc import NoResultFound
+# fixtures
+
TEST_CARDS = {
'visa': [
'4112344112344113',
@@ -29,11 +32,59 @@
'6559906559906557',
]}
+PERSON_MERCHANT = {
+ 'type': 'person',
+ 'name': 'William James',
+ 'tax_id': '393483992',
+ 'street_address': '167 West 74th Street',
+ 'postal_code': '10023',
+ 'dob': '1842-01-01',
+ 'phone_number': '+16505551234',
+ 'country_code': 'USA',
+ }
+
+BUSINESS_PRINCIPAL = {
+ 'name': 'William James',
+ 'tax_id': '393483992',
+ 'street_address': '167 West 74th Street',
+ 'postal_code': '10023',
+ 'dob': '1842-01-01',
+ 'phone_number': '+16505551234',
+ 'country_code': 'USA',
+ }
+
+BUSINESS_MERCHANT = {
+ 'type': 'business',
+ 'name': 'Levain Bakery',
+ 'tax_id': '253912384',
+ 'street_address': '167 West 74th Street',
+ 'postal_code': '10023',
+ 'phone_number': '+16505551234',
+ 'country_code': 'USA',
+ 'person': BUSINESS_PRINCIPAL,
+ }
+
+CARD = {
+ 'street_address': '123 Fake Street',
+ 'city': 'Jollywood',
+ 'region': 'CA',
+ 'postal_code': '90210',
+ 'name': 'Johnny Fresh',
+ 'card_number': '4' + '1' * 15,
+ 'expiration_month': 12,
+ 'expiration_year': 2013,
+ }
+
+BANK_ACCOUNT = {
+ 'name': 'Homer Jay',
+ 'account_number': '112233a',
+ 'bank_code': '121042882',
+ }
+
+
+# tests
class BasicUseCases(unittest.TestCase):
- """
- FIXME: fix all id issues
- """
@classmethod
def setUpClass(cls):
@@ -43,35 +94,35 @@ def setUpClass(cls):
balanced.configure(api_key.secret)
cls.merchant = api_key.merchant
- def test_1_merchant_expectations(self):
+ def test_00_merchant_expectations(self):
self.assertFalse(hasattr(self.merchant, 'principal'))
self.assertFalse(hasattr(self.merchant, 'payout_method'))
- #self.assertTrue(self.merchant.id.startswith('TEST-MR'))
+ self.assertTrue(self.merchant.id.startswith('TEST-MR'))
- def test_a_create_marketplace(self):
+ def test_01_create_marketplace(self):
self.assertTrue(self.merchant.accounts_uri.endswith('/accounts'))
self.assertIsNotNone(balanced.config.api_key_secret)
marketplace = balanced.Marketplace().save()
- #self.assertTrue(marketplace.id.startswith('TEST-MP'))
+ self.assertTrue(marketplace.id.startswith('TEST-MP'))
self.merchant = balanced.Merchant.find(self.merchant.uri)
self.assertEqual(marketplace.in_escrow, 0)
- def test_b_create_a_second_marketplace_should_fail(self):
+ def test_02_create_a_second_marketplace_should_fail(self):
self.assertIsNotNone(balanced.config.api_key_secret)
with self.assertRaises(requests.HTTPError) as exc:
balanced.Marketplace().save()
the_exception = exc.exception
self.assertEqual(the_exception.status_code, 409)
- def test_c_index_the_marketplaces(self):
+ def test_03_index_the_marketplaces(self):
self.assertIsNotNone(balanced.config.api_key_secret)
mps = balanced.Marketplace.query.all()
self.assertEqual(len(mps), 1)
def _find_marketplace(self):
return balanced.Marketplace.query.one()
- def test_d_create_a_buyer(self):
+ def test_04_create_a_buyer(self):
self.assertIsNotNone(balanced.config.api_key_secret)
card_number = TEST_CARDS['visa'][0]
@@ -94,7 +145,7 @@ def test_d_create_a_buyer(self):
card_uri=card_uri,
meta={'test#': 'test_d'}
)
- #self.assertTrue(buyer.id.startswith('AC'), buyer.id)
+ self.assertTrue(buyer.id.startswith('AC'), buyer.id)
self.assertEqual(buyer.name, 'khalkhalash onastick')
self.assertEqual(buyer.roles, ['buyer'])
self.assertIsNotNone(buyer.created_at)
@@ -119,7 +170,7 @@ def _find_account(self, role, owner=False, all_accounts=False):
return accounts[0]
- def test_e_index_accounts(self):
+ def test_05_index_accounts(self):
accounts = self._find_account(None, all_accounts=True)
self.assertEqual(len(accounts), 2)
account = self._find_account('buyer')
@@ -129,13 +180,13 @@ def test_e_index_accounts(self):
self.assertDictEqual(account.meta, {'test#': 'test_d'})
self.assertIsNotNone(account.uri)
- def test_f_debit_buyer_account_and_refund(self):
+ def test_06_debit_buyer_account_and_refund(self):
account = self._find_account('buyer')
debit = account.debit(
amount=1000,
appears_on_statement_as='atest',
meta={'fraud': 'yes'})
- #self.assertTrue(debit.id.startswith('W'))
+ self.assertTrue(debit.id.startswith('W'))
self.assertIsInstance(debit.account, balanced.Account)
self.assertIsInstance(debit.hold, balanced.Hold)
self.assertEqual(debit.fee, (1000 * 0.035))
@@ -155,7 +206,7 @@ def test_f_debit_buyer_account_and_refund(self):
another_refund = another_debit.refund()
self.assertEqual(another_refund.fee + another_debit.fee, 0)
- def test_g_create_hold_and_void_it(self):
+ def test_07_create_hold_and_void_it(self):
account = self._find_account('buyer')
hold = account.hold(amount=1500)
self.assertEqual(hold.fee, 35)
@@ -165,77 +216,46 @@ def test_g_create_hold_and_void_it(self):
self.assertTrue(hold.is_void)
self.assertEqual(hold.fee, 35) # fee still the same
- def test_g_create_hold_and_debit_it(self):
+ def test_08_create_hold_and_debit_it(self):
account = self._find_account('buyer')
hold = account.hold(amount=1500)
- #self.assertTrue(hold.id.startswith('HL'))
+ self.assertTrue(hold.id.startswith('HL'))
debit = hold.capture()
self.assertEqual(debit.fee, int((1500 * 0.035)))
- def test_h_create_a_person_merchant(self):
+ def test_09_create_a_person_merchant(self):
mp = self._find_marketplace()
- merchant = mp.create_merchant('mahmoud@poundpay.com', merchant={
- "type": "person",
- "name": "William James",
- "tax_id": "393483992",
- "street_address": "167 West 74th Street",
- "postal_code": "10023",
- "dob": "1842-01-01",
- "phone_number": "+16505551234",
- "country_code": "USA",
- })
+ merchant = mp.create_merchant('mahmoud@poundpay.com',
+ merchant=PERSON_MERCHANT)
self.assertEqual(merchant.roles, ['merchant'])
- def test_i_create_a_business_merchant(self):
+ def test_10_create_a_business_merchant(self):
mp = self._find_marketplace()
payload = {
"name": "Levain Bakery LLC",
"account_number": "28304871049",
"bank_code": "121042882",
- }
+ }
bank_account = balanced.BankAccount(**payload).save()
merchant = mp.create_merchant(
- 'mahmoud+khalkhalash@poundpay.com', merchant={
- "type": "business",
- "name": "Levain Bakery",
- "tax_id": "253912384",
- "street_address": "167 West 74th Street",
- "postal_code": "10023",
- "phone_number": "+16505551234",
- "country_code": "USA",
- "person": {
- "name": "William James",
- "tax_id": "393483992",
- "street_address": "167 West 74th Street",
- "postal_code": "10023",
- "dob": "1842-01-01",
- "phone_number": "+16505551234",
- "country_code": "USA",
- }},
+ 'mahmoud+khalkhalash@poundpay.com',
+ merchant=BUSINESS_MERCHANT,
bank_account_uri=bank_account.uri,
)
self.assertItemsEqual(merchant.roles, ['buyer', 'merchant'])
- def test_j_create_a_business_merchant_with_existing_email_addr(self):
+ def test_11_create_a_business_merchant_with_existing_email_addr(self):
mp = self._find_marketplace()
with self.assertRaises(requests.HTTPError) as exc:
- mp.create_merchant('mahmoud@poundpay.com', merchant={
- "type": "person",
- "name": "William James",
- "tax_id": "393483992",
- "street_address": "167 West 74th Street",
- "postal_code": "10023",
- "dob": "1842-01-01",
- "phone_number": "+16505551234",
- "country_code": "USA",
- })
+ mp.create_merchant('mahmoud@poundpay.com',
+ merchant=PERSON_MERCHANT)
the_exception = exc.exception
self.assertEqual(the_exception.status_code, 409)
self.assertIn(
"Account with email address 'mahmoud@poundpay.com' already exists",
the_exception.description)
- def test_k_get_business_merchant_for_crediting(self):
+ def test_12_get_business_merchant_for_crediting(self):
buyer = self._find_account('buyer')
buyer.debit(amount=10000)
self.merchant = self.merchant.find(self.merchant.uri)
@@ -246,14 +266,14 @@ def test_k_get_business_merchant_for_crediting(self):
))
merchant = merchants[0]
credit = merchant.credit(amount=1000)
- #self.assertTrue(credit.id.startswith('CR'))
+ self.assertTrue(credit.id.startswith('CR'))
self.assertEqual(credit.amount, 1000)
marketplace = marketplace.find(marketplace.uri)
self.assertEqual(
marketplace.in_escrow,
original_balance - credit.amount)
- def test_l_credit_more_than_the_escrow_balance_should_fail(self):
+ def test_13_credit_more_than_the_escrow_balance_should_fail(self):
buyer = self._find_account('buyer')
buyer.debit(amount=10000)
self.merchant = self.merchant.find(self.merchant.uri)
@@ -266,30 +286,29 @@ def test_l_credit_more_than_the_escrow_balance_should_fail(self):
self.assertEqual(the_exception.status_code, 409)
print the_exception
- def test_m_appends_marketplace_for_creating_account(self):
+ def test_14_appends_marketplace_for_creating_account(self):
with self.assertRaises(requests.HTTPError) as exc:
balanced.Account().save()
the_exception = exc.exception
self.assertEqual(the_exception.status_code, 400)
print the_exception
- def test_n_debits_without_an_account(self):
+ def test_15_debits_without_an_account(self):
with self.assertRaises(requests.HTTPError) as exc:
balanced.Debit().save()
the_exception = exc.exception
self.assertEqual(the_exception.status_code, 400)
print the_exception
- def test_o_slice_syntax(self):
+ def test_16_slice_syntax(self):
total_debit = balanced.Debit.query.count()
self.assertNotEqual(total_debit, 2)
sliced_debits = balanced.Debit.query[:2]
self.assertEqual(len(sliced_debits), 2)
for debit in sliced_debits:
self.assertIsInstance(debit, balanced.Debit)
- @unittest.skip('fix this')
- def test_p_test_merchant_cache_busting(self):
+ def test_17_test_merchant_cache_busting(self):
# cache it.
a_merchant = self.merchant.me
a_merchant.bank_account = {
@@ -300,3 +319,25 @@ def test_p_test_merchant_cache_busting(self):
self.assertTrue(hasattr(self.merchant.me, 'bank_account'))
a_merchant.save()
self.assertFalse(hasattr(a_merchant, 'bank_account'))
+
+ def test_18_create_and_associate_card(self):
+ try:
+ mp = balanced.Marketplace.query.one()
+ except NoResultFound:
+ mp = balanced.Marketplace().save()
+ card = mp.create_card(**CARD)
+ self.assertTrue(card.id.startswith('CC'))
+ account = mp.create_merchant('randy@pandy.com',
+ merchant=PERSON_MERCHANT)
+ account.add_card(card.uri)
+
+ def test_19_create_and_associate_bank_account(self):
+ try:
+ mp = balanced.Marketplace.query.one()
+ except NoResultFound:
+ mp = balanced.Marketplace().save()
+ bank_account = mp.create_bank_account(**BANK_ACCOUNT)
+ self.assertTrue(bank_account.id.startswith('BA'))
+ account = mp.create_merchant('free@example.com',
+ merchant=PERSON_MERCHANT)
+ account.add_bank_account(bank_account.uri)

0 comments on commit d60856f

Please sign in to comment.
Something went wrong with that request. Please try again.