当前位置: 首页>>代码示例>>Python>>正文


Python SubscriptionModel.create方法代码示例

本文整理汇总了Python中billy.models.subscription.SubscriptionModel.create方法的典型用法代码示例。如果您正苦于以下问题:Python SubscriptionModel.create方法的具体用法?Python SubscriptionModel.create怎么用?Python SubscriptionModel.create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在billy.models.subscription.SubscriptionModel的用法示例。


在下文中一共展示了SubscriptionModel.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_transaction_list_by_subscription

# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import create [as 别名]
    def test_transaction_list_by_subscription(self):
        from billy.models.transaction import TransactionModel
        from billy.models.subscription import SubscriptionModel
        subscription_model = SubscriptionModel(self.testapp.session)
        transaction_model = TransactionModel(self.testapp.session)
        with db_transaction.manager:
            subscription_guid1 = subscription_model.create(
                customer_guid=self.customer_guid,
                plan_guid=self.plan_guid,
            )
            subscription_guid2 = subscription_model.create(
                customer_guid=self.customer_guid,
                plan_guid=self.plan_guid,
            )
        guids1 = []
        guids2 = []
        with db_transaction.manager:
            for i in range(10):
                with freeze_time('2013-08-16 00:00:{:02}'.format(i + 1)):
                    guid = transaction_model.create(
                        subscription_guid=subscription_guid1,
                        transaction_type=transaction_model.TYPE_CHARGE,
                        amount=10 * i,
                        payment_uri='/v1/cards/tester',
                        scheduled_at=datetime.datetime.utcnow(),
                    )
                    guids1.append(guid)
            for i in range(20):
                with freeze_time('2013-08-16 00:00:{:02}'.format(i + 1)):
                    guid = transaction_model.create(
                        subscription_guid=subscription_guid2,
                        transaction_type=transaction_model.TYPE_CHARGE,
                        amount=10 * i,
                        payment_uri='/v1/cards/tester',
                        scheduled_at=datetime.datetime.utcnow(),
                    )
                    guids2.append(guid)
        guids1 = list(reversed(guids1))
        guids2 = list(reversed(guids2))

        res = self.testapp.get(
            '/v1/subscriptions/{}/transactions'.format(subscription_guid1),
            extra_environ=dict(REMOTE_USER=self.api_key), 
            status=200,
        )
        items = res.json['items']
        result_guids = [item['guid'] for item in items]
        self.assertEqual(result_guids, guids1)

        res = self.testapp.get(
            '/v1/subscriptions/{}/transactions'.format(subscription_guid2),
            extra_environ=dict(REMOTE_USER=self.api_key), 
            status=200,
        )
        items = res.json['items']
        result_guids = [item['guid'] for item in items]
        self.assertEqual(result_guids, guids2)
开发者ID:mjallday,项目名称:billy,代码行数:59,代码来源:test_subscription.py

示例2: test_cancel_subscription

# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import create [as 别名]
    def test_cancel_subscription(self):
        from billy.models.subscription import SubscriptionModel
        from billy.models.transaction import TransactionModel

        subscription_model = SubscriptionModel(self.testapp.session)
        tx_model = TransactionModel(self.testapp.session)
        now = datetime.datetime.utcnow()

        with db_transaction.manager:
            subscription_guid = subscription_model.create(
                customer_guid=self.customer_guid,
                plan_guid=self.plan_guid,
            )
            tx_model.create(
                subscription_guid=subscription_guid,
                transaction_type=tx_model.TYPE_CHARGE,
                amount=100, 
                scheduled_at=now,
            )

        with freeze_time('2013-08-16 07:00:00'):
            canceled_at = datetime.datetime.utcnow()
            res = self.testapp.post(
                '/v1/subscriptions/{}/cancel'.format(subscription_guid), 
                extra_environ=dict(REMOTE_USER=self.api_key), 
                status=200,
            )

        subscription = res.json
        self.assertEqual(subscription['canceled'], True)
        self.assertEqual(subscription['canceled_at'], canceled_at.isoformat())
开发者ID:mjallday,项目名称:billy,代码行数:33,代码来源:test_subscription.py

示例3: test_cancel_a_canceled_subscription

# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import create [as 别名]
    def test_cancel_a_canceled_subscription(self):
        from billy.models.subscription import SubscriptionModel
        from billy.models.transaction import TransactionModel

        subscription_model = SubscriptionModel(self.testapp.session)
        tx_model = TransactionModel(self.testapp.session)
        now = datetime.datetime.utcnow()

        with db_transaction.manager:
            subscription_guid = subscription_model.create(
                customer_guid=self.customer_guid,
                plan_guid=self.plan_guid,
            )
            tx_model.create(
                subscription_guid=subscription_guid,
                transaction_type=tx_model.TYPE_CHARGE,
                amount=100, 
                scheduled_at=now,
            )

        self.testapp.post(
            '/v1/subscriptions/{}/cancel'.format(subscription_guid), 
            extra_environ=dict(REMOTE_USER=self.api_key), 
            status=200,
        )
        self.testapp.post(
            '/v1/subscriptions/{}/cancel'.format(subscription_guid), 
            extra_environ=dict(REMOTE_USER=self.api_key), 
            status=400,
        )
开发者ID:MDrollette,项目名称:billy,代码行数:32,代码来源:test_subscription.py

示例4: subscription_list_post

# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import create [as 别名]
def subscription_list_post(request):
    """Create a new subscription 

    """
    company = auth_api_key(request)
    form = validate_form(SubscriptionCreateForm, request)

    customer_guid = form.data['customer_guid']
    plan_guid = form.data['plan_guid']
    amount = form.data.get('amount')
    payment_uri = form.data.get('payment_uri')
    if not payment_uri:
        payment_uri = None
    started_at = form.data.get('started_at')
    maximum_retry = int(request.registry.settings.get(
        'billy.transaction.maximum_retry', 
        TransactionModel.DEFAULT_MAXIMUM_RETRY,
    ))

    model = SubscriptionModel(request.session)
    plan_model = PlanModel(request.session)
    customer_model = CustomerModel(request.session)
    tx_model = TransactionModel(request.session)

    customer = customer_model.get(customer_guid)
    if customer.company_guid != company.guid:
        return HTTPForbidden('Can only subscribe to your own customer')
    if customer.deleted:
        return HTTPBadRequest('Cannot subscript to a deleted customer')
    plan = plan_model.get(plan_guid)
    if plan.company_guid != company.guid:
        return HTTPForbidden('Can only subscribe to your own plan')
    if plan.deleted:
        return HTTPBadRequest('Cannot subscript to a deleted plan')

    # create subscription and yield transactions
    with db_transaction.manager:
        guid = model.create(
            customer_guid=customer_guid, 
            plan_guid=plan_guid, 
            amount=amount, 
            payment_uri=payment_uri,
            started_at=started_at, 
        )
        tx_guids = model.yield_transactions([guid])
    # this is not a deferred subscription, just process transactions right away
    if started_at is None:
        with db_transaction.manager:
            tx_model.process_transactions(
                processor=request.processor, 
                guids=tx_guids,
                maximum_retry=maximum_retry,
            )

    subscription = model.get(guid)
    return subscription
开发者ID:MDrollette,项目名称:billy,代码行数:58,代码来源:views.py

示例5: test_transaction_list_by_subscription_with_bad_api_key

# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import create [as 别名]
    def test_transaction_list_by_subscription_with_bad_api_key(self):
        from billy.models.subscription import SubscriptionModel

        subscription_model = SubscriptionModel(self.testapp.session)
        with db_transaction.manager:
            subscription_guid = subscription_model.create(customer_guid=self.customer_guid, plan_guid=self.plan_guid)

        self.testapp.get(
            "/v1/subscriptions/{}/transactions".format(subscription_guid),
            extra_environ=dict(REMOTE_USER=b"BAD_API_KEY"),
            status=403,
        )
开发者ID:JeffersonK,项目名称:billy,代码行数:14,代码来源:test_subscription.py

示例6: test_cancel_subscription_with_refund_amount

# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import create [as 别名]
    def test_cancel_subscription_with_refund_amount(self):
        from billy.models.subscription import SubscriptionModel
        from billy.models.transaction import TransactionModel

        subscription_model = SubscriptionModel(self.testapp.session)
        tx_model = TransactionModel(self.testapp.session)
        now = datetime.datetime.utcnow()

        with db_transaction.manager:
            subscription_guid = subscription_model.create(customer_guid=self.customer_guid, plan_guid=self.plan_guid)
            tx_guid = tx_model.create(
                subscription_guid=subscription_guid,
                transaction_type=tx_model.TYPE_CHARGE,
                amount=1000,
                scheduled_at=now,
            )
            subscription = subscription_model.get(subscription_guid)
            subscription.period = 1
            subscription.next_transaction_at = datetime.datetime(2013, 8, 23)
            self.testapp.session.add(subscription)

            transaction = tx_model.get(tx_guid)
            transaction.status = tx_model.STATUS_DONE
            transaction.external_id = "MOCK_BALANCED_DEBIT_URI"
            self.testapp.session.add(transaction)

        refund_called = []

        def mock_refund(transaction):
            refund_called.append(transaction)
            return "MOCK_PROCESSOR_REFUND_URI"

        mock_processor = flexmock(DummyProcessor)
        (mock_processor.should_receive("refund").replace_with(mock_refund).once())

        res = self.testapp.post(
            "/v1/subscriptions/{}/cancel".format(subscription_guid),
            dict(refund_amount=234),
            extra_environ=dict(REMOTE_USER=self.api_key),
            status=200,
        )
        subscription = res.json

        transaction = refund_called[0]
        self.testapp.session.add(transaction)
        self.assertEqual(transaction.refund_to.guid, tx_guid)
        self.assertEqual(transaction.subscription_guid, subscription_guid)
        self.assertEqual(transaction.amount, 234)
        self.assertEqual(transaction.status, tx_model.STATUS_DONE)

        res = self.testapp.get("/v1/transactions", extra_environ=dict(REMOTE_USER=self.api_key), status=200)
        guids = [item["guid"] for item in res.json["items"]]
        self.assertEqual(set(guids), set([tx_guid, transaction.guid]))
开发者ID:JeffersonK,项目名称:billy,代码行数:55,代码来源:test_subscription.py

示例7: test_subscription_list

# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import create [as 别名]
    def test_subscription_list(self):
        from billy.models.subscription import SubscriptionModel

        subscription_model = SubscriptionModel(self.testapp.session)
        with db_transaction.manager:
            guids = []
            for i in range(4):
                with freeze_time("2013-08-16 00:00:{:02}".format(i + 1)):
                    guid = subscription_model.create(customer_guid=self.customer_guid, plan_guid=self.plan_guid)
                    guids.append(guid)
        guids = list(reversed(guids))

        res = self.testapp.get("/v1/subscriptions", extra_environ=dict(REMOTE_USER=self.api_key), status=200)
        items = res.json["items"]
        result_guids = [item["guid"] for item in items]
        self.assertEqual(result_guids, guids)
开发者ID:JeffersonK,项目名称:billy,代码行数:18,代码来源:test_subscription.py

示例8: test_cancel_subscription_to_other_company

# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import create [as 别名]
    def test_cancel_subscription_to_other_company(self):
        from billy.models.subscription import SubscriptionModel
        from billy.models.company import CompanyModel

        subscription_model = SubscriptionModel(self.testapp.session)
        company_model = CompanyModel(self.testapp.session)

        with db_transaction.manager:
            subscription_guid = subscription_model.create(customer_guid=self.customer_guid, plan_guid=self.plan_guid)
            other_company_guid = company_model.create(processor_key="MOCK_PROCESSOR_KEY")
            other_company = company_model.get(other_company_guid)
            other_api_key = str(other_company.api_key)

        self.testapp.post(
            "/v1/subscriptions/{}/cancel".format(subscription_guid),
            extra_environ=dict(REMOTE_USER=other_api_key),
            status=403,
        )
开发者ID:JeffersonK,项目名称:billy,代码行数:20,代码来源:test_subscription.py

示例9: test_server_info_with_transaction

# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import create [as 别名]
    def test_server_info_with_transaction(self):
        from billy.models.company import CompanyModel
        from billy.models.customer import CustomerModel
        from billy.models.plan import PlanModel
        from billy.models.subscription import SubscriptionModel
        from billy.models.transaction import TransactionModel

        company_model = CompanyModel(self.testapp.session)
        customer_model = CustomerModel(self.testapp.session)
        plan_model = PlanModel(self.testapp.session)
        subscription_model = SubscriptionModel(self.testapp.session)
        transaction_model = TransactionModel(self.testapp.session)

        with db_transaction.manager:
            company_guid = company_model.create(
                processor_key='MOCK_PROCESSOR_KEY',
            )
            customer_guid = customer_model.create(
                company_guid=company_guid
            )
            plan_guid = plan_model.create(
                company_guid=company_guid,
                frequency=plan_model.FREQ_WEEKLY,
                plan_type=plan_model.TYPE_CHARGE,
                amount=10,
            )
            subscription_guid = subscription_model.create(
                customer_guid=customer_guid,
                plan_guid=plan_guid,
            )
            transaction_guid = transaction_model.create(
                subscription_guid=subscription_guid,
                transaction_type=transaction_model.TYPE_CHARGE,
                amount=10,
                payment_uri='/v1/cards/tester',
                scheduled_at=datetime.datetime.utcnow(),
            )

        transaction = transaction_model.get(transaction_guid)

        res = self.testapp.get('/', status=200)
        self.assertEqual(res.json['last_transaction_created_at'], 
                         transaction.created_at.isoformat())
开发者ID:JeffersonK,项目名称:billy,代码行数:45,代码来源:test_server_info.py

示例10: test_get_transaction_of_other_company

# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import create [as 别名]
 def test_get_transaction_of_other_company(self):
     from billy.models.company import CompanyModel
     from billy.models.customer import CustomerModel
     from billy.models.plan import PlanModel
     from billy.models.subscription import SubscriptionModel
     from billy.models.transaction import TransactionModel
     company_model = CompanyModel(self.testapp.session)
     customer_model = CustomerModel(self.testapp.session)
     plan_model = PlanModel(self.testapp.session)
     subscription_model = SubscriptionModel(self.testapp.session)
     transaction_model = TransactionModel(self.testapp.session)
     with db_transaction.manager:
         other_company_guid = company_model.create(
             processor_key='MOCK_PROCESSOR_KEY',
         )
         other_customer_guid = customer_model.create(
             company_guid=other_company_guid
         )
         other_plan_guid = plan_model.create(
             company_guid=other_company_guid,
             frequency=plan_model.FREQ_WEEKLY,
             plan_type=plan_model.TYPE_CHARGE,
             amount=10,
         )
         other_subscription_guid = subscription_model.create(
             customer_guid=other_customer_guid,
             plan_guid=other_plan_guid,
         )
         other_transaction_guid = transaction_model.create(
             subscription_guid=other_subscription_guid,
             transaction_type=transaction_model.TYPE_CHARGE,
             amount=10,
             payment_uri='/v1/cards/tester',
             scheduled_at=datetime.datetime.utcnow(),
         )
     self.testapp.get(
         '/v1/transactions/{}'.format(other_transaction_guid), 
         extra_environ=dict(REMOTE_USER=self.api_key), 
         status=403,
     )
开发者ID:mjallday,项目名称:billy,代码行数:42,代码来源:test_transaction.py

示例11: test_cancel_subscription_with_bad_arguments

# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import create [as 别名]
    def test_cancel_subscription_with_bad_arguments(self):
        from billy.models.subscription import SubscriptionModel
        from billy.models.transaction import TransactionModel

        subscription_model = SubscriptionModel(self.testapp.session)
        tx_model = TransactionModel(self.testapp.session)
        now = datetime.datetime.utcnow()

        with db_transaction.manager:
            subscription_guid = subscription_model.create(
                customer_guid=self.customer_guid,
                plan_guid=self.plan_guid,
                amount=100,
            )
            tx_guid = tx_model.create(
                subscription_guid=subscription_guid,
                transaction_type=tx_model.TYPE_CHARGE,
                amount=100, 
                scheduled_at=now,
            )
            subscription = subscription_model.get(subscription_guid)
            subscription.period = 1
            subscription.next_transaction_at = datetime.datetime(2013, 8, 23)
            self.testapp.session.add(subscription)

            transaction = tx_model.get(tx_guid)
            transaction.status = tx_model.STATUS_DONE
            transaction.external_id = 'MOCK_BALANCED_DEBIT_URI'
            self.testapp.session.add(transaction)

        def assert_bad_parameters(kwargs):
            self.testapp.post(
                '/v1/subscriptions/{}/cancel'.format(subscription_guid), 
                kwargs,
                extra_environ=dict(REMOTE_USER=self.api_key), 
                status=400,
            )
        assert_bad_parameters(dict(prorated_refund=True, refund_amount=10))
        assert_bad_parameters(dict(refund_amount='100.01'))
开发者ID:mjallday,项目名称:billy,代码行数:41,代码来源:test_subscription.py

示例12: setUp

# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import create [as 别名]
 def setUp(self):
     from pyramid.testing import DummyRequest
     from billy.models.company import CompanyModel
     from billy.models.customer import CustomerModel
     from billy.models.plan import PlanModel
     from billy.models.subscription import SubscriptionModel
     from billy.models.transaction import TransactionModel
     super(TestRenderer, self).setUp()
     company_model = CompanyModel(self.testapp.session)
     customer_model = CustomerModel(self.testapp.session)
     plan_model = PlanModel(self.testapp.session)
     subscription_model = SubscriptionModel(self.testapp.session)
     transaction_model = TransactionModel(self.testapp.session)
     with db_transaction.manager:
         self.company_guid = company_model.create(
             processor_key='MOCK_PROCESSOR_KEY',
         )
         self.customer_guid = customer_model.create(
             company_guid=self.company_guid
         )
         self.plan_guid = plan_model.create(
             company_guid=self.company_guid,
             frequency=plan_model.FREQ_WEEKLY,
             plan_type=plan_model.TYPE_CHARGE,
             amount=10,
         )
         self.subscription_guid = subscription_model.create(
             customer_guid=self.customer_guid,
             plan_guid=self.plan_guid,
         )
         self.transaction_guid = transaction_model.create(
             subscription_guid=self.subscription_guid,
             transaction_type=transaction_model.TYPE_CHARGE,
             amount=10,
             payment_uri='/v1/cards/tester',
             scheduled_at=datetime.datetime.utcnow(),
         )
     self.dummy_request = DummyRequest()
开发者ID:JeffersonK,项目名称:billy,代码行数:40,代码来源:test_renderer.py

示例13: test_cancel_subscription_with_prorated_refund

# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import create [as 别名]
    def test_cancel_subscription_with_prorated_refund(self):
        from billy.models.subscription import SubscriptionModel
        from billy.models.transaction import TransactionModel

        subscription_model = SubscriptionModel(self.testapp.session)
        tx_model = TransactionModel(self.testapp.session)
        now = datetime.datetime.utcnow()

        with db_transaction.manager:
            subscription_guid = subscription_model.create(
                customer_guid=self.customer_guid,
                plan_guid=self.plan_guid,
                amount=100,
            )
            tx_guid = tx_model.create(
                subscription_guid=subscription_guid,
                transaction_type=tx_model.TYPE_CHARGE,
                amount=100, 
                scheduled_at=now,
            )
            subscription = subscription_model.get(subscription_guid)
            subscription.period = 1
            subscription.next_transaction_at = datetime.datetime(2013, 8, 23)
            self.testapp.session.add(subscription)

            transaction = tx_model.get(tx_guid)
            transaction.status = tx_model.STATUS_DONE
            transaction.external_id = 'MOCK_BALANCED_DEBIT_URI'
            self.testapp.session.add(transaction)

        refund_called = []

        def mock_refund(transaction):
            refund_called.append(transaction)
            return 'MOCK_PROCESSOR_REFUND_URI'

        mock_processor = flexmock(DummyProcessor)
        (
            mock_processor
            .should_receive('refund')
            .replace_with(mock_refund)
            .once()
        )

        with freeze_time('2013-08-17'):
            canceled_at = datetime.datetime.utcnow()
            res = self.testapp.post(
                '/v1/subscriptions/{}/cancel'.format(subscription_guid), 
                dict(prorated_refund=True),
                extra_environ=dict(REMOTE_USER=self.api_key), 
                status=200,
            )

        subscription = res.json
        self.assertEqual(subscription['canceled'], True)
        self.assertEqual(subscription['canceled_at'], canceled_at.isoformat())

        transaction = refund_called[0]
        self.testapp.session.add(transaction)
        self.assertEqual(transaction.refund_to.guid, tx_guid)
        self.assertEqual(transaction.subscription_guid, subscription_guid)
        # only one day is elapsed, and it is a weekly plan, so
        # it should be 100 - (100 / 7) and round to cent, 85.71
        self.assertEqual(transaction.amount, decimal.Decimal('85.71'))
        self.assertEqual(transaction.status, tx_model.STATUS_DONE)

        res = self.testapp.get(
            '/v1/transactions', 
            extra_environ=dict(REMOTE_USER=self.api_key), 
            status=200,
        )
        guids = [item['guid'] for item in res.json['items']]
        self.assertEqual(set(guids), set([tx_guid, transaction.guid]))
开发者ID:mjallday,项目名称:billy,代码行数:75,代码来源:test_subscription.py

示例14: test_main_with_crash

# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import create [as 别名]
    def test_main_with_crash(self):
        from pyramid.paster import get_appsettings
        from billy.models import setup_database
        from billy.models.company import CompanyModel
        from billy.models.customer import CustomerModel
        from billy.models.plan import PlanModel
        from billy.models.subscription import SubscriptionModel
        from billy.scripts import initializedb
        from billy.scripts import process_transactions

        class MockProcessor(object):

            def __init__(self):
                self.charges = {}
                self.tx_sn = 0
                self.called_times = 0

            def create_customer(self, customer):
                return 'MOCK_PROCESSOR_CUSTOMER_ID'

            def prepare_customer(self, customer, payment_uri=None):
                pass

            def charge(self, transaction):
                self.called_times += 1
                if self.called_times == 2:
                    raise KeyboardInterrupt
                guid = transaction.guid
                if guid in self.charges:
                    return self.charges[guid]
                self.charges[guid] = self.tx_sn
                self.tx_sn += 1

        mock_processor = MockProcessor()
        
        cfg_path = os.path.join(self.temp_dir, 'config.ini')
        with open(cfg_path, 'wt') as f:
            f.write(textwrap.dedent("""\
            [app:main]
            use = egg:billy

            sqlalchemy.url = sqlite:///%(here)s/billy.sqlite
            """))
        initializedb.main([initializedb.__file__, cfg_path])

        settings = get_appsettings(cfg_path)
        settings = setup_database({}, **settings)
        session = settings['session']
        company_model = CompanyModel(session)
        customer_model = CustomerModel(session)
        plan_model = PlanModel(session)
        subscription_model = SubscriptionModel(session)

        with db_transaction.manager:
            company_guid = company_model.create('my_secret_key')
            plan_guid = plan_model.create(
                company_guid=company_guid,
                plan_type=plan_model.TYPE_CHARGE,
                amount=10,
                frequency=plan_model.FREQ_MONTHLY,
            )
            customer_guid = customer_model.create(
                company_guid=company_guid,
            )
            subscription_model.create(
                customer_guid=customer_guid,
                plan_guid=plan_guid,
                payment_uri='/v1/cards/tester',
            )
            subscription_model.create(
                customer_guid=customer_guid,
                plan_guid=plan_guid,
                payment_uri='/v1/cards/tester',
            )

        with self.assertRaises(KeyboardInterrupt):
            process_transactions.main([process_transactions.__file__, cfg_path], 
                                      processor=mock_processor)

        process_transactions.main([process_transactions.__file__, cfg_path], 
                                  processor=mock_processor)

        # here is the story, we have two subscriptions here
        #   
        #   Subscription1
        #   Subscription2
        #
        # And the time is not advanced, so we should only have two transactions
        # to be yielded and processed. However, we assume bad thing happens
        # durring the process. We let the second call to charge of processor 
        # raises a KeyboardInterrupt error. So, it would look like this
        #
        #   charge for transaction from Subscription1
        #   charge for transaction from Subscription2 (Crash)
        #
        # Then, we perform the process_transactions again, if it works 
        # correctly, the first transaction is already yield and processed. 
        # 
        #   charge for transaction from Subscription2
        #
#.........这里部分代码省略.........
开发者ID:JeffersonK,项目名称:billy,代码行数:103,代码来源:test_process_transactions.py

示例15: TestBalancedProcessorModel

# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import create [as 别名]
class TestBalancedProcessorModel(ModelTestCase):

    def setUp(self):
        from billy.models.company import CompanyModel
        from billy.models.customer import CustomerModel
        from billy.models.plan import PlanModel
        from billy.models.subscription import SubscriptionModel
        from billy.models.transaction import TransactionModel
        super(TestBalancedProcessorModel, self).setUp()
        # build the basic scenario for transaction model
        self.company_model = CompanyModel(self.session)
        self.customer_model = CustomerModel(self.session)
        self.plan_model = PlanModel(self.session)
        self.subscription_model = SubscriptionModel(self.session)
        self.transaction_model = TransactionModel(self.session)
        with db_transaction.manager:
            self.company_guid = self.company_model.create('my_secret_key')
            self.plan_guid = self.plan_model.create(
                company_guid=self.company_guid,
                plan_type=self.plan_model.TYPE_CHARGE,
                amount=10,
                frequency=self.plan_model.FREQ_MONTHLY,
            )
            self.customer_guid = self.customer_model.create(
                company_guid=self.company_guid,
            )
            self.subscription_guid = self.subscription_model.create(
                customer_guid=self.customer_guid,
                plan_guid=self.plan_guid,
                payment_uri='/v1/credit_card/tester',
            )

    def make_one(self, *args, **kwargs):
        from billy.models.processors.balanced_payments import BalancedProcessor
        return BalancedProcessor(*args, **kwargs)

    def test_create_customer(self):
        import balanced

        customer = self.customer_model.get(self.customer_guid)

        # make sure API key is set correctly
        (
            flexmock(balanced)
            .should_receive('configure')
            .with_args('my_secret_key')
            .once()
        )

        # mock balanced customer instance
        mock_balanced_customer = (
            flexmock(uri='MOCK_BALANCED_CUSTOMER_URI')
            .should_receive('save')
            .replace_with(lambda: mock_balanced_customer)
            .once()
            .mock()
        )

        class BalancedCustomer(object):
            pass
        flexmock(BalancedCustomer).new_instances(mock_balanced_customer) 

        processor = self.make_one(customer_cls=BalancedCustomer)
        customer_id = processor.create_customer(customer)
        self.assertEqual(customer_id, 'MOCK_BALANCED_CUSTOMER_URI')

    def test_prepare_customer_with_card(self):
        import balanced

        with db_transaction.manager:
            self.customer_model.update(
                guid=self.customer_guid,
                external_id='MOCK_BALANCED_CUSTOMER_URI',
            )
        customer = self.customer_model.get(self.customer_guid)

        # make sure API key is set correctly
        (
            flexmock(balanced)
            .should_receive('configure')
            .with_args('my_secret_key')
            .once()
        )

        # mock balanced.Customer instance
        mock_balanced_customer = (
            flexmock()
            .should_receive('add_card')
            .with_args('/v1/cards/my_card')
            .once()
            .mock()
        )

        # mock balanced.Customer class
        class BalancedCustomer(object): 
            def find(self, uri):
                pass
        (
            flexmock(BalancedCustomer)
            .should_receive('find')
#.........这里部分代码省略.........
开发者ID:JeffersonK,项目名称:billy,代码行数:103,代码来源:test_processors.py


注:本文中的billy.models.subscription.SubscriptionModel.create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。