|
| 1 | +import base64 |
| 2 | + |
| 3 | +from dateutil.relativedelta import relativedelta |
| 4 | +from odoo import fields |
| 5 | +from odoo.tests import common |
| 6 | + |
| 7 | + |
| 8 | +class TestPadronCleanupCron(common.TransactionCase): |
| 9 | + @classmethod |
| 10 | + def setUpClass(cls): |
| 11 | + super().setUpClass() |
| 12 | + cls.padron_model = cls.env["res.company.jurisdiction.padron"] |
| 13 | + cls.state_arba = cls.env.ref("base.state_ar_b") |
| 14 | + cls.state_santa_fe = cls.env.ref("base.state_ar_s") |
| 15 | + cls.company = cls.env.company |
| 16 | + cls.dummy_file = base64.b64encode(b"dummy padron").decode() |
| 17 | + |
| 18 | + def _create_padron(self, state, to_date): |
| 19 | + return self.padron_model.create( |
| 20 | + { |
| 21 | + "company_id": self.company.id, |
| 22 | + "state_id": state.id, |
| 23 | + "file_padron": self.dummy_file, |
| 24 | + "filename": "padron.txt", |
| 25 | + "l10n_ar_padron_from_date": to_date + relativedelta(days=-30), |
| 26 | + "l10n_ar_padron_to_date": to_date, |
| 27 | + } |
| 28 | + ) |
| 29 | + |
| 30 | + def test_cron_deletes_only_old_padrons(self): |
| 31 | + """Create 2 old and 2 new padrones, then verify the cron deletes only the 2 old ones.""" |
| 32 | + threshold_date = fields.Date.start_of(fields.Date.context_today(self.padron_model), "month") + relativedelta( |
| 33 | + years=-1 |
| 34 | + ) |
| 35 | + |
| 36 | + old_arba = self._create_padron(self.state_arba, threshold_date + relativedelta(days=-1)) |
| 37 | + old_santa_fe = self._create_padron(self.state_santa_fe, threshold_date + relativedelta(days=-10)) |
| 38 | + new_arba = self._create_padron(self.state_arba, threshold_date + relativedelta(days=1)) |
| 39 | + new_santa_fe = self._create_padron(self.state_santa_fe, threshold_date + relativedelta(days=30)) |
| 40 | + |
| 41 | + self.padron_model._cron_clean_old_padron_files() |
| 42 | + |
| 43 | + self.assertFalse(old_arba.exists()) |
| 44 | + self.assertFalse(old_santa_fe.exists()) |
| 45 | + self.assertTrue(new_arba.exists()) |
| 46 | + self.assertTrue(new_santa_fe.exists()) |
0 commit comments