|
| 1 | +"""Manual codes on SYSTEM vocabularies (OP#954 round-3). |
| 2 | +
|
| 3 | +System vocabularies (`is_system=True`) ship immutable system codes via module |
| 4 | +data files. Admins must still be able to layer their own *manual* codes |
| 5 | +(`is_local=True`) on top — these are fully editable and deletable. This test |
| 6 | +file covers the UI-facing additions: |
| 7 | +
|
| 8 | +- `code_source` Selection field is computed correctly from `is_local`. |
| 9 | +- Manual codes can be created in a system vocab. |
| 10 | +- Manual codes in a system vocab can be edited (identifying fields included) |
| 11 | + and deleted, without tripping the system-code guards. |
| 12 | +- `action_add_manual_code` on `spp.vocabulary` returns an action whose context |
| 13 | + pre-seeds `default_is_local=True` and `default_vocabulary_id` for the form. |
| 14 | +""" |
| 15 | + |
| 16 | +from odoo.tests.common import TransactionCase |
| 17 | + |
| 18 | + |
| 19 | +class TestManualCodesOnSystemVocabulary(TransactionCase): |
| 20 | + """Manual (is_local=True) codes on a system vocabulary stay fully editable.""" |
| 21 | + |
| 22 | + @classmethod |
| 23 | + def setUpClass(cls): |
| 24 | + super().setUpClass() |
| 25 | + cls.Vocabulary = cls.env["spp.vocabulary"] |
| 26 | + cls.VocabularyCode = cls.env["spp.vocabulary.code"] |
| 27 | + |
| 28 | + cls.system_vocab = cls.Vocabulary.create( |
| 29 | + { |
| 30 | + "name": "Manual-Code Host Vocab", |
| 31 | + "namespace_uri": "urn:test:manual-codes-host", |
| 32 | + "is_system": True, |
| 33 | + } |
| 34 | + ) |
| 35 | + |
| 36 | + def test_code_source_computed_from_is_local(self): |
| 37 | + """code_source reflects is_local: True -> manual, False -> system.""" |
| 38 | + system_code = self.VocabularyCode.with_context(_test_bypass_system_protection=True).create( |
| 39 | + { |
| 40 | + "vocabulary_id": self.system_vocab.id, |
| 41 | + "code": "SYS_FOR_SOURCE", |
| 42 | + "display": "System For Source", |
| 43 | + } |
| 44 | + ) |
| 45 | + manual_code = self.VocabularyCode.create( |
| 46 | + { |
| 47 | + "vocabulary_id": self.system_vocab.id, |
| 48 | + "code": "MANUAL_FOR_SOURCE", |
| 49 | + "display": "Manual For Source", |
| 50 | + "is_local": True, |
| 51 | + } |
| 52 | + ) |
| 53 | + |
| 54 | + self.assertEqual(system_code.code_source, "system") |
| 55 | + self.assertEqual(manual_code.code_source, "manual") |
| 56 | + |
| 57 | + def test_manual_code_create_allowed_on_system_vocab(self): |
| 58 | + """is_local=True codes can be created on a system vocabulary.""" |
| 59 | + manual = self.VocabularyCode.create( |
| 60 | + { |
| 61 | + "vocabulary_id": self.system_vocab.id, |
| 62 | + "code": "MANUAL_NEW", |
| 63 | + "display": "Manual New", |
| 64 | + "is_local": True, |
| 65 | + } |
| 66 | + ) |
| 67 | + self.assertTrue(manual.id) |
| 68 | + self.assertTrue(manual.is_local) |
| 69 | + self.assertEqual(manual.code_source, "manual") |
| 70 | + |
| 71 | + def test_manual_code_identifying_fields_editable(self): |
| 72 | + """Manual codes keep `code`, `display`, `definition` writeable on a system vocab.""" |
| 73 | + manual = self.VocabularyCode.create( |
| 74 | + { |
| 75 | + "vocabulary_id": self.system_vocab.id, |
| 76 | + "code": "MANUAL_EDIT", |
| 77 | + "display": "Manual Edit", |
| 78 | + "is_local": True, |
| 79 | + } |
| 80 | + ) |
| 81 | + manual.write( |
| 82 | + { |
| 83 | + "code": "MANUAL_EDIT_RENAMED", |
| 84 | + "display": "Manual Edit Renamed", |
| 85 | + "definition": "Edited definition", |
| 86 | + } |
| 87 | + ) |
| 88 | + self.assertEqual(manual.code, "MANUAL_EDIT_RENAMED") |
| 89 | + self.assertEqual(manual.display, "Manual Edit Renamed") |
| 90 | + self.assertEqual(manual.definition, "Edited definition") |
| 91 | + |
| 92 | + def test_manual_code_can_be_deleted(self): |
| 93 | + """Manual codes on a system vocabulary can be unlinked.""" |
| 94 | + manual = self.VocabularyCode.create( |
| 95 | + { |
| 96 | + "vocabulary_id": self.system_vocab.id, |
| 97 | + "code": "MANUAL_DEL", |
| 98 | + "display": "Manual Del", |
| 99 | + "is_local": True, |
| 100 | + } |
| 101 | + ) |
| 102 | + manual_id = manual.id |
| 103 | + manual.unlink() |
| 104 | + self.assertFalse(self.VocabularyCode.search([("id", "=", manual_id)])) |
| 105 | + |
| 106 | + def test_action_add_manual_code_seeds_context(self): |
| 107 | + """action_add_manual_code returns an act_window with the right defaults.""" |
| 108 | + action = self.system_vocab.action_add_manual_code() |
| 109 | + self.assertEqual(action["type"], "ir.actions.act_window") |
| 110 | + self.assertEqual(action["res_model"], "spp.vocabulary.code") |
| 111 | + self.assertEqual(action["view_mode"], "form") |
| 112 | + self.assertEqual(action["context"]["default_vocabulary_id"], self.system_vocab.id) |
| 113 | + self.assertTrue(action["context"]["default_is_local"]) |
0 commit comments