|
| 1 | +from unittest import TestCase |
| 2 | + |
| 3 | +import pandas |
| 4 | + |
| 5 | +from process_report.invoices import invoice |
| 6 | +from process_report.processors.pi_su_credit_processor import PISUCreditProcessor |
| 7 | + |
| 8 | + |
| 9 | +class TestDiscountProcessor(TestCase): |
| 10 | + def _get_test_invoice( |
| 11 | + self, |
| 12 | + pi, |
| 13 | + costs, |
| 14 | + su_type, |
| 15 | + credit=None, |
| 16 | + credit_code=None, |
| 17 | + pi_balance=None, |
| 18 | + balance=None, |
| 19 | + ): |
| 20 | + if credit is None: |
| 21 | + credit = [None for _ in range(len(pi))] |
| 22 | + if credit_code is None: |
| 23 | + credit_code = [None for _ in range(len(pi))] |
| 24 | + if pi_balance is None: |
| 25 | + pi_balance = costs |
| 26 | + if balance is None: |
| 27 | + balance = costs |
| 28 | + |
| 29 | + return pandas.DataFrame( |
| 30 | + { |
| 31 | + invoice.PI_FIELD: pi, |
| 32 | + invoice.SU_TYPE_FIELD: su_type, |
| 33 | + invoice.COST_FIELD: costs, |
| 34 | + invoice.CREDIT_FIELD: credit, |
| 35 | + invoice.CREDIT_CODE_FIELD: credit_code, |
| 36 | + invoice.PI_BALANCE_FIELD: pi_balance, |
| 37 | + invoice.BALANCE_FIELD: balance, |
| 38 | + } |
| 39 | + ) |
| 40 | + |
| 41 | + def test_preexisting_credit( |
| 42 | + self, |
| 43 | + ): |
| 44 | + """Tests that if there is already a credit in the invoice, the discount is added to it rather than overwriting it""" |
| 45 | + invoice_data = self._get_test_invoice( |
| 46 | + pi=["PI"], |
| 47 | + costs=[100], |
| 48 | + su_type=["Openstack Storage"], |
| 49 | + credit=[10], |
| 50 | + credit_code=["0003"], |
| 51 | + pi_balance=[90], |
| 52 | + balance=[90], |
| 53 | + ) |
| 54 | + |
| 55 | + processor = PISUCreditProcessor( |
| 56 | + invoice_month="2024-06", |
| 57 | + data=invoice_data, |
| 58 | + name="test", |
| 59 | + pi_su_mapping={"PI": ["Openstack Storage"]}, |
| 60 | + ) |
| 61 | + processor.process() |
| 62 | + output_invoice = processor.data |
| 63 | + |
| 64 | + expected_invoice = self._get_test_invoice( |
| 65 | + pi=["PI"], |
| 66 | + costs=[100], |
| 67 | + su_type=["Openstack Storage"], |
| 68 | + credit=[100], |
| 69 | + credit_code=["0003,0005"], |
| 70 | + pi_balance=[0], |
| 71 | + balance=[0], |
| 72 | + ) |
| 73 | + |
| 74 | + assert expected_invoice.equals(output_invoice) |
| 75 | + |
| 76 | + def test_one_eligible_project_only(self): |
| 77 | + """If PI has multiple projects but only one is eligible, only that project should get the credit""" |
| 78 | + invoice_data = self._get_test_invoice( |
| 79 | + pi=["PI", "PI"], |
| 80 | + costs=[50, 75], |
| 81 | + su_type=["Openstack Storage", "HPC"], |
| 82 | + ) |
| 83 | + |
| 84 | + processor = PISUCreditProcessor( |
| 85 | + invoice_month="2024-06", |
| 86 | + data=invoice_data, |
| 87 | + name="test", |
| 88 | + pi_su_mapping={ |
| 89 | + "PI": ["Openstack Storage"] |
| 90 | + }, # Only Openstack Storage SU type is eligible for credit, not HPC |
| 91 | + ) |
| 92 | + processor.process() |
| 93 | + output_invoice = processor.data |
| 94 | + |
| 95 | + expected_invoice = self._get_test_invoice( |
| 96 | + pi=["PI", "PI"], |
| 97 | + costs=[50, 75], |
| 98 | + su_type=["Openstack Storage", "HPC"], |
| 99 | + credit=[50, None], |
| 100 | + credit_code=["0005", None], |
| 101 | + pi_balance=[0, 75], |
| 102 | + balance=[0, 75], |
| 103 | + ) |
| 104 | + |
| 105 | + expected_invoice = expected_invoice.astype(output_invoice.dtypes) |
| 106 | + assert expected_invoice.equals(output_invoice) |
| 107 | + |
| 108 | + def test_all_eligible_projects(self): |
| 109 | + """PI has multiple projects and all are eligible, so all should get the credit""" |
| 110 | + invoice_data = self._get_test_invoice( |
| 111 | + pi=["PI", "PI"], |
| 112 | + costs=[40, 60], |
| 113 | + su_type=["Openstack Storage", "Openstack Compute"], |
| 114 | + ) |
| 115 | + |
| 116 | + processor = PISUCreditProcessor( |
| 117 | + invoice_month="2024-06", |
| 118 | + data=invoice_data, |
| 119 | + name="test", |
| 120 | + pi_su_mapping={ |
| 121 | + "PI": ["Openstack Storage", "Openstack Compute"] |
| 122 | + }, # Both SU types are eligible for credit |
| 123 | + ) |
| 124 | + processor.process() |
| 125 | + output_invoice = processor.data |
| 126 | + |
| 127 | + expected_invoice = self._get_test_invoice( |
| 128 | + pi=["PI", "PI"], |
| 129 | + costs=[40, 60], |
| 130 | + su_type=["Openstack Storage", "Openstack Compute"], |
| 131 | + credit=[40, 60], |
| 132 | + credit_code=["0005", "0005"], |
| 133 | + pi_balance=[0, 0], |
| 134 | + balance=[0, 0], |
| 135 | + ) |
| 136 | + |
| 137 | + expected_invoice = expected_invoice.astype(output_invoice.dtypes) |
| 138 | + assert expected_invoice.equals(output_invoice) |
0 commit comments