diff --git a/uncloud_pay/models.py b/uncloud_pay/models.py index 0898c60..1f75dd7 100644 --- a/uncloud_pay/models.py +++ b/uncloud_pay/models.py @@ -628,12 +628,13 @@ class Order(models.Model): super().save(*args, **kwargs) -# Bill.generate_for(self.starting_date.year, self.starting_date.month, self.owner) + def generate_initial_bill(self): + return Bill.generate_for(self.starting_date.year, self.starting_date.month, self.owner) - # # Used by uncloud_pay tests. - # @property - # def bills(self): - # return Bill.objects.filter(order=self) + # Used by uncloud_pay tests. + @property + def bills(self): + return Bill.objects.filter(order=self) def __str__(self): return "Order {} created at {}, {}->{}, recurring period {}. One time price {}, recurring price {}".format( diff --git a/uncloud_pay/tests.py b/uncloud_pay/tests.py index 64f0442..00ee294 100644 --- a/uncloud_pay/tests.py +++ b/uncloud_pay/tests.py @@ -31,11 +31,13 @@ class BillingTestCase(TestCase): starting_date=starting_date, ending_date=ending_date, recurring_period=RecurringPeriod.PER_MONTH, + recurring_price=recurring_price, + one_time_price=one_time_price, + description=description, billing_address=self.billing_address) - order.add_record(one_time_price, recurring_price, description) # Generate & check bill for first month: full recurring_price + setup. - first_month_bills = order.bills # Initial bill generated at order creation. + first_month_bills = order.generate_initial_bill() self.assertEqual(len(first_month_bills), 1) self.assertEqual(first_month_bills[0].amount, one_time_price + recurring_price) @@ -49,7 +51,7 @@ class BillingTestCase(TestCase): self.assertEqual(len(third_month_bills), 1) # 31 days in May. self.assertEqual(float(third_month_bills[0].amount), - round((7/31) * recurring_price, AMOUNT_DECIMALS)) + round(round((7/31), AMOUNT_DECIMALS) * recurring_price, AMOUNT_DECIMALS)) # Check that running Bill.generate_for() twice does not create duplicates. self.assertEqual(len(Bill.generate_for(2020, 3, self.user)), 0) @@ -66,11 +68,13 @@ class BillingTestCase(TestCase): owner=self.user, starting_date=starting_date, recurring_period=RecurringPeriod.PER_YEAR, + recurring_price=recurring_price, + one_time_price=one_time_price, + description=description, billing_address=self.billing_address) - order.add_record(one_time_price, recurring_price, description) # Generate & check bill for first year: recurring_price + setup. - first_year_bills = order.bills # Initial bill generated at order creation. + first_year_bills = order.generate_initial_bill() self.assertEqual(len(first_year_bills), 1) self.assertEqual(first_year_bills[0].starting_date.date(), date.fromisoformat('2020-03-31')) @@ -108,11 +112,13 @@ class BillingTestCase(TestCase): starting_date=starting_date, ending_date=ending_date, recurring_period=RecurringPeriod.PER_HOUR, + recurring_price=recurring_price, + one_time_price=one_time_price, + description=description, billing_address=self.billing_address) - order.add_record(one_time_price, recurring_price, description) # Generate & check bill for first month: recurring_price + setup. - first_month_bills = order.bills + first_month_bills = order.generate_initial_bill() self.assertEqual(len(first_month_bills), 1) self.assertEqual(float(first_month_bills[0].amount), round(16 * recurring_price, AMOUNT_DECIMALS) + one_time_price) @@ -137,28 +143,32 @@ class ProductActivationTestCase(TestCase): def test_product_activation(self): starting_date = datetime.fromisoformat('2020-03-01') + one_time_price = 0 + recurring_price = 1 + description = "Test Product" order = Order.objects.create( owner=self.user, starting_date=starting_date, recurring_period=RecurringPeriod.PER_MONTH, + recurring_price=recurring_price, + one_time_price=one_time_price, + description=description, billing_address=self.billing_address) product = GenericServiceProduct( - custom_description="Test product", - custom_one_time_price=0, - custom_recurring_price=20, + custom_description=description, + custom_one_time_price=one_time_price, + custom_recurring_price=recurring_price, owner=self.user, order=order) product.save() - # XXX: to be automated. - order.add_record(product.one_time_price, product.recurring_price, product.description) - # Validate initial state: must be awaiting payment. self.assertEqual(product.status, UncloudStatus.AWAITING_PAYMENT) # Pay initial bill, check that product is activated. + order.generate_initial_bill() amount = product.order.bills[0].amount payment = Payment(owner=self.user, amount=amount) payment.save()