From 11ab261ad949d4f4fd1a582d18cf400a0f9cc25d Mon Sep 17 00:00:00 2001 From: gsinghpal Date: Tue, 2 Jun 2026 08:59:31 -0400 Subject: [PATCH] test(billing): make fusion_centralize_billing suite hermetic (green baseline) - test_usage / test_webhook setUp: get-or-create the cpu_seconds metric and nexacloud service so the suite no longer collides with existing rows. - test_invoice_ledger: add _fc_ensure_ca_billing_env (activate CAD + a 13% sale tax matching _fc_tax_for) so the ledger tests pass on a clean DB. Canonical test DB: a FRESH db with l10n_ca installed (a prod clone collides on fixed-code fixtures across 5 test files). Full suite now exits 0. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../tests/test_invoice_ledger.py | 18 ++++++++++++++++++ fusion_centralize_billing/tests/test_usage.py | 6 ++++-- .../tests/test_webhook.py | 10 ++++++++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/fusion_centralize_billing/tests/test_invoice_ledger.py b/fusion_centralize_billing/tests/test_invoice_ledger.py index c2f4fe29..3d2a7494 100644 --- a/fusion_centralize_billing/tests/test_invoice_ledger.py +++ b/fusion_centralize_billing/tests/test_invoice_ledger.py @@ -18,11 +18,26 @@ def _inv_fixture(): }] +def _fc_ensure_ca_billing_env(env): + """Prod (`nexamain`) is a fully-configured Canadian company; a bare test DB is not. + Give it the two things the ledger needs: an active CAD currency and a 13% sale tax + matching invoice.ledger.wizard._fc_tax_for (type_tax_use=sale, percent, amount=13).""" + cad = env.ref('base.CAD') + if not cad.active: + cad.sudo().write({'active': True}) + Tax = env['account.tax'].sudo() + if not Tax.search([('type_tax_use', '=', 'sale'), + ('amount_type', '=', 'percent'), ('amount', '=', 13.0)], limit=1): + Tax.create({'name': 'HST 13%', 'type_tax_use': 'sale', + 'amount_type': 'percent', 'amount': 13.0}) + + @tagged('post_install', '-at_install') class TestLedgerFamily(TransactionCase): def setUp(self): super().setUp() + _fc_ensure_ca_billing_env(self.env) self.W = self.env['fusion.billing.invoice.ledger.wizard'].sudo() def test_family_classification(self): @@ -47,6 +62,7 @@ class TestLedgerTax(TransactionCase): def setUp(self): super().setUp() + _fc_ensure_ca_billing_env(self.env) self.W = self.env['fusion.billing.invoice.ledger.wizard'].sudo() def test_tax_for_13pct_is_a_13_percent_sale_tax(self): @@ -68,6 +84,7 @@ class TestLedgerIngest(TransactionCase): def setUp(self): super().setUp() + _fc_ensure_ca_billing_env(self.env) self.W = self.env['fusion.billing.invoice.ledger.wizard'].sudo() self.Move = self.env['account.move'] @@ -174,6 +191,7 @@ class TestLedgerVerifiedSync(TransactionCase): def setUp(self): super().setUp() + _fc_ensure_ca_billing_env(self.env) self.W = self.env['fusion.billing.invoice.ledger.wizard'].sudo() self.Move = self.env['account.move'] ICP = self.env['ir.config_parameter'].sudo() diff --git a/fusion_centralize_billing/tests/test_usage.py b/fusion_centralize_billing/tests/test_usage.py index c31db95b..020d573e 100644 --- a/fusion_centralize_billing/tests/test_usage.py +++ b/fusion_centralize_billing/tests/test_usage.py @@ -9,7 +9,8 @@ class TestRatingCron(TransactionCase): def setUp(self): super().setUp() - self.metric = self.env['fusion.billing.metric'].sudo().create( + Metric = self.env['fusion.billing.metric'].sudo() + self.metric = Metric.search([('code', '=', 'cpu_seconds')], limit=1) or Metric.create( {'name': 'CPU seconds', 'code': 'cpu_seconds', 'aggregation': 'sum'}) self.plan_a = self.env['sale.subscription.plan'].sudo().create( {'name': 'Plan A', 'billing_period_value': 1, 'billing_period_unit': 'month'}) @@ -67,7 +68,8 @@ class TestUsageIngestion(TransactionCase): def setUp(self): super().setUp() - self.metric = self.env['fusion.billing.metric'].sudo().create( + Metric = self.env['fusion.billing.metric'].sudo() + self.metric = Metric.search([('code', '=', 'cpu_seconds')], limit=1) or Metric.create( {'name': 'CPU seconds', 'code': 'cpu_seconds', 'aggregation': 'sum'}) self.plan = self.env['sale.subscription.plan'].sudo().create( {'name': 'Monthly', 'billing_period_value': 1, 'billing_period_unit': 'month'}) diff --git a/fusion_centralize_billing/tests/test_webhook.py b/fusion_centralize_billing/tests/test_webhook.py index 38e73cab..b8710123 100644 --- a/fusion_centralize_billing/tests/test_webhook.py +++ b/fusion_centralize_billing/tests/test_webhook.py @@ -13,11 +13,17 @@ class TestWebhookEngine(TransactionCase): def setUp(self): super().setUp() - self.service = self.env['fusion.billing.service'].sudo().create({ + Service = self.env['fusion.billing.service'].sudo() + vals = { 'name': 'NexaCloud', 'code': 'nexacloud', 'webhook_url': 'https://api.vps.nexasystems.ca/billing/webhook', 'webhook_secret': 'whsec_test', - }) + } + self.service = Service.search([('code', '=', 'nexacloud')], limit=1) + if self.service: + self.service.write(vals) + else: + self.service = Service.create(vals) self.Webhook = self.env['fusion.billing.webhook'].sudo() def test_enqueue_signs_payload(self):