def test_create_plan(self): url = reverse('plan-list') ProductCodeFactory.create_batch(2) feature1_pc = ProductCode.objects.get(id=1).value plan_pc = ProductCode.objects.get(id=2).value provider = ProviderFactory.create() provider_url = reverse('provider-detail', kwargs={'pk': provider.pk}) response = self.client.post(url, json.dumps({ "name": "Hydrogen", "interval": "month", "interval_count": 1, "amount": 149.99, "currency": "USD", "trial_period_days": 15, "generate_after": 86400, "enabled": True, "private": False, "product_code": plan_pc, 'metered_features': [{ 'name': 'Page Views', 'unit': '100k', 'price_per_unit': 0.01, 'included_units': 0, 'product_code': feature1_pc }, { 'name': 'VIP Support', 'price_per_unit': 49.99, 'unit': 1, 'included_units': 1, 'product_code': "1234" }], 'provider': provider_url }), content_type='application/json') assert response.status_code == status.HTTP_201_CREATED
def setUp(self): admin_user = AdminUserFactory.create() self.client.force_authenticate(user=admin_user) ProductCodeFactory.reset_sequence(1) self.product_code = ProductCodeFactory.create() self.complete_data = { "name": "Page Views", "unit": "100k", "price_per_unit": '0.0500', "included_units": '0.0000', "product_code": self.product_code.value }
def create_basic_plan(self, start_date, end_date, interval=Plan.INTERVALS.YEAR): self.provider = ProviderFactory.create(flow=Provider.FLOWS.INVOICE) self.customer = CustomerFactory.create( consolidated_billing=False, sales_tax_percent=Decimal('0.00')) self.currency = 'USD' self.plan = PlanFactory.create( interval=interval, interval_count=1, generate_after=0, enabled=True, provider=self.provider, product_code=ProductCodeFactory(value="yearly-seat-plan"), amount=Decimal('10.00'), prebill_plan=False, currency=self.currency, trial_period_days=None, cycle_billing_duration=dt.timedelta(days=1), ) self.plan.save() # Create the prorated subscription self.subscription = SubscriptionFactory.create(plan=self.plan, start_date=start_date, customer=self.customer) self.subscription.activate() self.subscription.save()
def seat_feature(self): return MeteredFeatureFactory( name="Charcoal Users", unit="Seats", included_units=Decimal('0.00'), product_code=ProductCodeFactory(value="charc-seats"), price_per_unit=Decimal('0.0'))
def test_create_plan(self): url = reverse("plan-list") ProductCodeFactory.create_batch(2) feature1_pc = ProductCode.objects.get(id=1).value plan_pc = ProductCode.objects.get(id=2).value provider = ProviderFactory.create() provider_url = reverse("provider-detail", kwargs={"pk": provider.pk}) response = self.client.post( url, json.dumps( { "name": "Hydrogen", "interval": "month", "interval_count": 1, "amount": 149.99, "currency": "USD", "trial_period_days": 15, "generate_after": 86400, "enabled": True, "private": False, "product_code": plan_pc, "metered_features": [ { "name": "Page Views", "unit": "100k", "price_per_unit": 0.01, "included_units": 0, "product_code": feature1_pc, }, { "name": "VIP Support", "price_per_unit": 49.99, "unit": 1, "included_units": 1, "product_code": "1234", }, ], "provider": provider_url, } ), content_type="application/json", ) assert response.status_code == status.HTTP_201_CREATED
def metered_feature(self): return MeteredFeatureFactory( name="Charcoal Base Units", unit="Barrels (per seat)", included_units=Decimal('0.00'), included_units_during_trial=Decimal('0.00'), product_code=ProductCodeFactory(value="charc-base"), price_per_unit=Decimal('5.00'), )
def plan(self, **kwargs): return PlanFactory.create( generate_after=0, enabled=True, product_code=ProductCodeFactory(value="monthly-deliv-plan"), amount=Decimal('10.00'), prebill_plan=False, currency='USD', trial_period_days=None, # cycle_billing_duration=dt.timedelta(days=1), **kwargs)
def setUp(self): admin_user = AdminUserFactory.create() self.client.force_authenticate(user=admin_user) ProductCodeFactory.reset_sequence(1) self.user_product_code = ProductCodeFactory.create( value="user_product_code") self.user_feature_data = { "name": "Users", "unit": "user", "price_per_unit": '1.0000', "included_units": '0.0000', "product_code": self.user_product_code.value } self.minutes_product_code = ProductCodeFactory.create( value="minutes_product_code") self.minutes_data = { "name": "Minutes", "unit": "20 minutes / user", "price_per_unit": '5.0000', "included_units": '20.0000', "product_code": self.minutes_product_code.value, } self.link = self.minutes_data.copy() self.link['linked_feature'] = self.user_product_code.value self.link['linked_feature_calculation'] = "multiply" self.prebill_minutes_product_code = ProductCodeFactory.create( value="prebill_minutes_product_code") self.prebill_minutes_data = { "name": "Minutes", "unit": "20 minutes / user", "prebill_included_units": True, "price_per_unit": '5.0000', "included_units": '20.0000', "product_code": self.minutes_product_code.value, }
def test_create_plan(self): url = reverse('plan-list') ProductCodeFactory.create_batch(2) feature1_pc = ProductCode.objects.all()[0].value plan_pc = ProductCode.objects.all()[1].value provider = ProviderFactory.create() provider_url = reverse('provider-detail', kwargs={'pk': provider.pk}) response = self.client.post(url, json.dumps({ "name": "Hydrogen", "interval": "month", "interval_count": 1, "amount": 149.99, "currency": "USD", "trial_period_days": 15, "generate_after": 86400, "enabled": True, "private": False, "product_code": plan_pc, 'metered_features': [ {'name': 'Page Views', 'unit': '100k', 'price_per_unit': 0.01, 'included_units': 0, 'product_code': feature1_pc}, {'name': 'VIP Support', 'price_per_unit': 49.99, 'unit': 1, 'included_units': 1, 'product_code': "1234"} ], 'provider': provider_url }), content_type='application/json') assert response.status_code == status.HTTP_201_CREATED
def test_subscription_yearly_proration_isnt_borked_half(self): from django.db.models import Q # Set up the timescale. start_date = dt.date(2018, 1, 1) end_date = dt.date(2018, 7, 1) freezer = freeze_time('2018-07-01') first_billing_date = generate_docs_date('2018-01-01') end_billing_date = generate_docs_date('2018-07-02') feature_usage_set = dt.date(2018, 1, 1) fst_feature_usage_start = dt.date(2018, 1, 2) snd_feature_usage_end = dt.date(2018, 1, 30) feature_increment = dt.date(2018, 3, 1) snd_feature_usage_start = dt.date(2018, 3, 2) snd_feature_usage_end = dt.date(2018, 3, 30) ## Lots of test setup {{{ provider = ProviderFactory.create(flow=Provider.FLOWS.INVOICE) customer = CustomerFactory.create(consolidated_billing=False, sales_tax_percent=Decimal('0.00')) currency = 'USD' plan = PlanFactory.create( interval=Plan.INTERVALS.YEAR, interval_count=1, generate_after=0, enabled=True, provider=provider, product_code=ProductCodeFactory(value="yearly-seat-plan"), amount=Decimal('10.00'), prebill_plan=False, currency=currency, trial_period_days=None, cycle_billing_duration=dt.timedelta(days=1), ) plan.save() # Create the prorated subscription subscription = SubscriptionFactory.create(plan=plan, start_date=start_date, customer=customer) subscription.activate() subscription.save() ## }}} ## First test billing phase {{{ call_command('generate_docs', date=fst_feature_usage_start, stdout=self.output) assert Invoice.objects.all().count() == 0 # zomg time travel so l33t with freeze_time('2018-07-01') as T: subscription.cancel(when='now') subscription.save() call_command('generate_docs', date=end_billing_date, force_generate=True, stdout=self.output) assert Invoice.objects.all().count() == 1 print(" END OF YEAR ") year_invoice = Invoice.objects.first() print_entries(year_invoice) # Final year total should be 10 for the whole base plan. assert Decimal(4.98) <= year_invoice.total <= Decimal(5.00)
def test_issued_date_works_as_expected(self): """ Test that usage under and above a certain amount tracks with assumptions. """ # Set up the timescale. start_date = dt.date(2018, 1, 1) first_invoice_date = dt.date(2018, 2, 1) prev_billing_date = generate_docs_date('2018-01-01') period_end_date = generate_docs_date('2018-01-30') curr_billing_date = generate_docs_date('2018-01-31') next_billing_date = generate_docs_date('2018-02-01') seat_feature_usage_set = dt.date(2018, 1, 1) feature_usage_start = dt.date(2018, 1, 2) feature_usage_end = dt.date(2018, 1, 30) provider = self.provider customer = self.customer currency = 'USD' seat_feature = self.seat_feature seat_feature.save() metered_feature = self.metered_feature metered_feature.save() plan = PlanFactory.create( interval=Plan.INTERVALS.MONTH, interval_count=1, generate_after=0, enabled=True, provider=provider, product_code=ProductCodeFactory(value="monthly-deliv-plan"), amount=Decimal('10.00'), prebill_plan=False, currency=currency, trial_period_days=None, cycle_billing_duration=dt.timedelta(days=1), metered_features=[metered_feature]) plan.save() # Create the prorated subscription subscription = SubscriptionFactory.create(plan=plan, start_date=start_date, customer=customer) subscription.activate() subscription.save() call_command('generate_docs', date=feature_usage_start, stdout=self.output) # Track some usage mf = MeteredFeatureUnitsLogFactory.create( subscription=subscription, metered_feature=metered_feature, start_date=feature_usage_start, end_date=feature_usage_end, consumed_units=Decimal('20.00')) mf.save() call_command('generate_docs', date=next_billing_date, stdout=self.output) assert Invoice.objects.all().count() == 1 invoice = Invoice.objects.all().first() assert invoice.issue_date == next_billing_date assert invoice.total == Decimal(110.0)