<commit_before>import decimal

from django.test import TestCase

from django.contrib.auth import get_user_model

from mock import patch, PropertyMock, Mock

from ..actions import refunds, customers, syncs, charges
from ..proxies import CustomerProxy, ChargeProxy, SubscriptionProxy


class TestCustomer(TestCase):
    def setUp(self):
        self.User = get_user_model()
        self.user = self.User.objects.create_user(
            username="patrick",
            email="<EMAIL>"
        )
        self.customer = CustomerProxy.objects.create(
            user=self.user,
            stripe_id="cus_xxxxxxxxxxxxxxx"
        )
        self.subscription = SubscriptionProxy.objects.create(
            customer=self.customer,
            plan="foo",
            stripe_id="su_123",
            quantity=1,
            start="2015-01-01"
        )

    def test_model_table_name(self):
        self.assertEquals(CustomerProxy()._meta.db_table, "pinax_stripe_customer")

    @patch("stripe.Customer.retrieve")
    @patch("stripe.Customer.create")
    def test_customer_create_user_only(self, CreateMock, RetrieveMock):
        self.customer.delete()
        cu = CreateMock()
        cu.account_balance = 0
        cu.delinquent = False
        cu.default_source = "card_178Zqj2eZvKYlo2Cr2fUZZz7"
        cu.currency = "usd"
        cu.id = "cus_XXXXX"
        customer = customers.create(self.user)
        self.assertEqual(customer.user, self.user)
        self.assertEqual(customer.stripe_id, "cus_XXXXX")
        _, kwargs = CreateMock.call_args
        self.assertEqual(kwargs["email"], self.user.email)
        self.assertIsNone(kwargs["source"])
        self.assertIsNone(kwargs["plan"])
        self.assertIsNone(kwargs["trial_end"])

    @patch("stripe.Invoice.create")
    @patch("stripe.Customer.retrieve")
    @patch("stripe.Customer.create")
    def test_customer_create_user_with_plan(self, CreateMock, RetrieveMock, InvoiceMock):
        self.customer.delete()
        type(InvoiceMock()).amount_due = PropertyMock(return_value=3)
        cu = CreateMock()
        cu.account_balance = 0
        cu.delinquent = False
        cu.default_source = "card_178Zqj2eZvKYlo2Cr2fUZZz7"
        cu.currency = "usd"
        cu.id = "cus_YYYYYYYYYYYYY"
        subscription = Mock()
        subscription.plan.id = "pro-monthly"
        subscription.current_period_start = 1348876800
        subscription.current_period_end = 1349876800
        subscription.canceled_at = 1349876800
        subscription.ended_at = 1349876800
        subscription.application_fee_percent = 0
        subscription.plan.amount = 9999
        subscription.plan.currency = "usd"
        subscription.status = "active"
        subscription.cancel_at_period_end = False
        subscription.start = 1348876800
        subscription.quantity = 1
        subscription.trial_start = 1348876800
        subscription.trial_end = 1349876800
        subscription.id = "su_YYYYYYYYYYYYY"
        cu.subscriptions.data = [subscription]
        rm = RetrieveMock()
        rm.id = "cus_YYYYYYYYYYYYY"
        rm.account_balance = 0
        rm.delinquent = False
        rm.default_source = "card_178Zqj2eZvKYlo2Cr2fUZZz7"
        rm.currency = "usd"
        rm.subscription.plan.id = "pro-monthly"
        rm.subscriptions.data = [subscription]
        customer = customers.create(self.user, card="token232323", plan="pro")
        self.assertEqual(customer.user, self.user)
        self.assertEqual(customer.stripe_id, "cus_YYYYYYYYYYYYY")
        _, kwargs = CreateMock.call_args
        self.assertEqual(kwargs["email"], self.user.email)
        self.assertEqual(kwargs["source"], "token232323")
        self.assertEqual(kwargs["plan"], "pro-monthly")
        self.assertIsNotNone(kwargs["trial_end"])
        self.assertTrue(InvoiceMock.called)
        self.assertTrue(customer.subscription_set.all()[0].plan, "pro")
    #
    # @patch("stripe.Customer.retrieve")
    # def test_customer_subscribe_with_specified_quantity(self, CustomerRetrieveMock):
    #     cu = CustomerRetrieveMock()
    #     cu.account_balance = 0
    #     cu.delinquent = False<commit_msg>Update customer creation test for new plan handling<commit_after>import decimal

from django.test import TestCase

from django.contrib.auth import get_user_model

from mock import patch, PropertyMock, Mock

from ..actions import refunds, customers, syncs, charges
from ..proxies import CustomerProxy, ChargeProxy, SubscriptionProxy, PlanProxy


class TestCustomer(TestCase):
    def setUp(self):
        self.User = get_user_model()
        self.user = self.User.objects.create_user(
            username="patrick",
            email="<EMAIL>"
        )
        self.customer = CustomerProxy.objects.create(
            user=self.user,
            stripe_id="cus_xxxxxxxxxxxxxxx"
        )
        self.subscription = SubscriptionProxy.objects.create(
            customer=self.customer,
            plan="foo",
            stripe_id="su_123",
            quantity=1,
            start="2015-01-01"
        )

    def test_model_table_name(self):
        self.assertEquals(CustomerProxy()._meta.db_table, "pinax_stripe_customer")

    @patch("stripe.Customer.retrieve")
    @patch("stripe.Customer.create")
    def test_customer_create_user_only(self, CreateMock, RetrieveMock):
        self.customer.delete()
        cu = CreateMock()
        cu.account_balance = 0
        cu.delinquent = False
        cu.default_source = "card_178Zqj2eZvKYlo2Cr2fUZZz7"
        cu.currency = "usd"
        cu.id = "cus_XXXXX"
        customer = customers.create(self.user)
        self.assertEqual(customer.user, self.user)
        self.assertEqual(customer.stripe_id, "cus_XXXXX")
        _, kwargs = CreateMock.call_args
        self.assertEqual(kwargs["email"], self.user.email)
        self.assertIsNone(kwargs["source"])
        self.assertIsNone(kwargs["plan"])
        self.assertIsNone(kwargs["trial_end"])

    @patch("stripe.Invoice.create")
    @patch("stripe.Customer.retrieve")
    @patch("stripe.Customer.create")
    def test_customer_create_user_with_plan(self, CreateMock, RetrieveMock, InvoiceMock):
        self.customer.delete()
        PlanProxy.objects.create(
            stripe_id="pro-monthly",
            name="Pro ($19.99/month)",
            amount=19.99,
            interval="monthly",
            interval_count=1,
            currency="usd"
        )
        type(InvoiceMock()).amount_due = PropertyMock(return_value=3)
        cu = CreateMock()
        cu.account_balance = 0
        cu.delinquent = False
        cu.default_source = "card_178Zqj2eZvKYlo2Cr2fUZZz7"
        cu.currency = "usd"
        cu.id = "cus_YYYYYYYYYYYYY"
        subscription = Mock()
        subscription.plan.id = "pro-monthly"
        subscription.current_period_start = 1348876800
        subscription.current_period_end = 1349876800
        subscription.canceled_at = 1349876800
        subscription.ended_at = 1349876800
        subscription.application_fee_percent = 0
        subscription.plan.amount = 9999
        subscription.plan.currency = "usd"
        subscription.status = "active"
        subscription.cancel_at_period_end = False
        subscription.start = 1348876800
        subscription.quantity = 1
        subscription.trial_start = 1348876800
        subscription.trial_end = 1349876800
        subscription.id = "su_YYYYYYYYYYYYY"
        cu.subscriptions.data = [subscription]
        rm = RetrieveMock()
        rm.id = "cus_YYYYYYYYYYYYY"
        rm.account_balance = 0
        rm.delinquent = False
        rm.default_source = "card_178Zqj2eZvKYlo2Cr2fUZZz7"
        rm.currency = "usd"
        rm.subscription.plan.id = "pro-monthly"
        rm.subscriptions.data = [subscription]
        customer = customers.create(self.user, card="token232323", plan="pro-monthly")
        self.assertEqual(customer.user, self.user)
        self.assertEqual(customer.stripe_id, "cus_YYYYYYYYYYYYY")
        _, kwargs = CreateMock.call_args
        self.assertEqual(kwargs["email"], self.user.email)
        self.assertEqual(kwargs["source"], "token232323")
        self.assertEqual(kwargs["plan"], "pro-monthly")
        self.assertIsNotNone(kwargs["trial_end"])
        self.assertTrue(InvoiceMock.called)
        self.assertTrue(customer.subscription_set.all()[0].plan, "pro")
    #
    # @patch("stripe.Customer.retrieve")
    # def test_customer_subscribe_with_specified_quantity(self, CustomerRetrieveMock):
    #     cu = CustomerRetrieveMock()
    #     cu.account_balance = 0
    #     cu.delinquent = False
{'fraction_non_alphanumeric': 0.10506654214335746, 'fraction_numerical': 0.03070277842633668, 'mean_word_length': 2.749234135667396, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 10, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1768056', 'n_tokens_mistral': 2774, 'n_tokens_neox': 2601, 'n_words': 393}