import importlib.util import pathlib import unittest import pandas as pd MODULE_PATH = pathlib.Path(__file__).with_name("monthly_cloud_billing_revrec.py") SPEC = importlib.util.spec_from_file_location("monthly_cloud_billing_revrec", MODULE_PATH) monthly_cloud_billing_revrec = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(monthly_cloud_billing_revrec) class CygamesCPActualAllocationTest(unittest.TestCase): def test_mixed_cumulative_snapshot_does_not_create_on_demand(self): tenant_id = "1372813089191061283" billing = { "tenant_id": tenant_id, "accumulate_revenue_to_org_tenant_id": tenant_id, "billing_month": "2026-06", "billing_start_date": "6/1/2026", "billing_end_date": "6/30/2026", "total_amount": 149_675.43, "account_id": "account-cygames", "customer_surrogate_key": "customer-cygames", "account_number_for_netsuite": "IBG-B-20210523-000234", "customer_name": "Cygames, Inc.", "region": "Japan", "payment_type": "wire transfer", "wire_transfer_amount": 149_675.43, "marketplace_amount": 0.0, "stripe_amount": None, "stripe_tax_rate": None, "stripe_amount_including_tax": None, "stripe_invoice_number": None, "stripe_invoice": None, "stripe_invoice_paid": None, "stripe_invoice_finalized_date": None, "stripe_invoice_paid_date": None, "invoice_amount": 149_675.43, "is_salesforce_customer": True, "actual_revenue_recognition_date": "2026-07-28", } for category in monthly_cloud_billing_revrec.REVENUE_CATEGORIES: billing[f"raw_{category}_amount"] = 0.0 for credit_type in monthly_cloud_billing_revrec.CREDIT_TYPES: billing[f"raw_{credit_type}_credit_amount_{category}"] = 0.0 for segment in ("a", "b", "c"): billing[f"segment_{segment}_start_date"] = None billing[f"segment_{segment}_end_date"] = None billing[f"segment_{segment}_amount"] = None billing["raw_dedicated_amount"] = 149_675.43 orders = pd.DataFrame([{ "order_name": "JP-OF-25030306", "accumulate_revenue_to_org_tenant_id": tenant_id, "order_status": "Activated", "order_product_family": "TiDB Cloud", "cloud_order_type": "Commitment Plan", "order_service_start_date": pd.Timestamp("2025-04-01").date(), "order_service_end_date": pd.Timestamp("2028-03-31").date(), "order_is_terminated": False, "order_termination_date": None, "real_order_termination_date": None, "reason": "", "order_net_fees": 2_400_000.00, # Daily view's mixed estimated/actual total: the old allocator # considered the CP exhausted and created $36,128.25 on-demand. "accumulated_revenue_before_adjust": 2_633_694.97, "accumulated_revenue": 2_633_694.97, "accumulated_credit_amount": 0.0, "accumulated_actual_revenue_before_billing": 2_088_515.15, "accumulated_actual_adjusted_revenue_before_billing": 2_088_515.15, "accumulated_actual_credit_amount_before_billing": 0.0, }]) _, allocated_orders = monthly_cloud_billing_revrec.process( orders, pd.DataFrame([billing]), is_recognised_revenue=True, ) self.assertEqual(len(allocated_orders), 1) allocated = allocated_orders.iloc[0] self.assertFalse(allocated["is_new_order"]) self.assertEqual(allocated["order_number"], "JP-OF-25030306") self.assertAlmostEqual(allocated["revenue_amount"], 149_675.43, places=2) if __name__ == "__main__": unittest.main()