|
| 1 | +# Copyright (C) 2025 Cetmix OÜ |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +from odoo.addons.cetmix_tower_server.tests.common import TestTowerCommon |
| 5 | + |
| 6 | + |
| 7 | +class TestAttributeSync(TestTowerCommon): |
| 8 | + """ |
| 9 | + Test synchronization between Tower variables and product attributes |
| 10 | + """ |
| 11 | + |
| 12 | + @classmethod |
| 13 | + def setUpClass(cls): |
| 14 | + super().setUpClass() |
| 15 | + |
| 16 | + # Create a Tower variable of type 'Options' |
| 17 | + cls.tower_variable = cls.env["cx.tower.variable"].create( |
| 18 | + { |
| 19 | + "name": "Test Product Options", |
| 20 | + "reference": "test_product_options", |
| 21 | + "variable_type": "o", # Options type |
| 22 | + } |
| 23 | + ) |
| 24 | + |
| 25 | + # Create two options for this variable |
| 26 | + cls.option_1 = cls.env["cx.tower.variable.option"].create( |
| 27 | + { |
| 28 | + "variable_id": cls.tower_variable.id, |
| 29 | + "name": "Option 1", |
| 30 | + "value_char": "option_1", |
| 31 | + } |
| 32 | + ) |
| 33 | + cls.option_2 = cls.env["cx.tower.variable.option"].create( |
| 34 | + { |
| 35 | + "variable_id": cls.tower_variable.id, |
| 36 | + "name": "Option 2", |
| 37 | + "value_char": "option_2", |
| 38 | + } |
| 39 | + ) |
| 40 | + |
| 41 | + # Create a product attribute for manual syncing |
| 42 | + cls.manual_sync_attribute = cls.env["product.attribute"].create( |
| 43 | + { |
| 44 | + "name": "Manual Sync Test Attribute", |
| 45 | + "tower_variable_id": cls.tower_variable.id, |
| 46 | + "auto_sync_tower_values": False, |
| 47 | + } |
| 48 | + ) |
| 49 | + |
| 50 | + # Create a product attribute for automatic syncing |
| 51 | + cls.auto_sync_attribute = cls.env["product.attribute"].create( |
| 52 | + { |
| 53 | + "name": "Auto Sync Test Attribute", |
| 54 | + "tower_variable_id": cls.tower_variable.id, |
| 55 | + "auto_sync_tower_values": True, |
| 56 | + } |
| 57 | + ) |
| 58 | + |
| 59 | + def test_01_manual_sync_creates_values(self): |
| 60 | + """Test that manual sync creates attribute values correctly""" |
| 61 | + # Initial state - no values should exist |
| 62 | + initial_count = self.env["product.attribute.value"].search_count( |
| 63 | + [("attribute_id", "=", self.manual_sync_attribute.id)] |
| 64 | + ) |
| 65 | + self.assertEqual(initial_count, 0) |
| 66 | + |
| 67 | + # Call action_sync_tower_values |
| 68 | + result = self.manual_sync_attribute.action_sync_tower_values() |
| 69 | + |
| 70 | + # Assert that exactly two product.attribute.value records are created |
| 71 | + final_count = self.env["product.attribute.value"].search_count( |
| 72 | + [("attribute_id", "=", self.manual_sync_attribute.id)] |
| 73 | + ) |
| 74 | + self.assertEqual(final_count, 2) |
| 75 | + |
| 76 | + # Assert each new value is correctly linked to its corresponding Tower option |
| 77 | + values = self.env["product.attribute.value"].search( |
| 78 | + [("attribute_id", "=", self.manual_sync_attribute.id)] |
| 79 | + ) |
| 80 | + |
| 81 | + tower_option_ids = values.mapped("tower_option_id").ids |
| 82 | + expected_option_ids = [self.option_1.id, self.option_2.id] |
| 83 | + self.assertEqual(set(tower_option_ids), set(expected_option_ids)) |
| 84 | + |
| 85 | + # Check that names match the option names |
| 86 | + value_names = values.mapped("name") |
| 87 | + expected_names = ["Option 1", "Option 2"] |
| 88 | + self.assertEqual(set(value_names), set(expected_names)) |
| 89 | + |
| 90 | + # Verify the result structure |
| 91 | + self.assertIn("created", result) |
| 92 | + self.assertEqual(len(result["created"]), 2) |
| 93 | + |
| 94 | + def test_02_manual_sync_is_idempotent(self): |
| 95 | + """Test that running manual sync twice doesn't create duplicates""" |
| 96 | + # First sync |
| 97 | + self.manual_sync_attribute.action_sync_tower_values() |
| 98 | + |
| 99 | + first_count = self.env["product.attribute.value"].search_count( |
| 100 | + [("attribute_id", "=", self.manual_sync_attribute.id)] |
| 101 | + ) |
| 102 | + self.assertEqual(first_count, 2) |
| 103 | + |
| 104 | + # Second sync |
| 105 | + result = self.manual_sync_attribute.action_sync_tower_values() |
| 106 | + |
| 107 | + second_count = self.env["product.attribute.value"].search_count( |
| 108 | + [("attribute_id", "=", self.manual_sync_attribute.id)] |
| 109 | + ) |
| 110 | + self.assertEqual(second_count, 2) # Count should remain the same |
| 111 | + |
| 112 | + # No new values should be created in the second sync |
| 113 | + self.assertEqual(len(result["created"]), 0) |
| 114 | + |
| 115 | + def test_03_auto_sync_on_option_creation(self): |
| 116 | + """Test that auto sync triggers when new options are created""" |
| 117 | + # First, run initial sync on auto_sync_attribute |
| 118 | + self.auto_sync_attribute.action_sync_tower_values() |
| 119 | + |
| 120 | + auto_sync_initial_count = self.env["product.attribute.value"].search_count( |
| 121 | + [("attribute_id", "=", self.auto_sync_attribute.id)] |
| 122 | + ) |
| 123 | + self.assertEqual(auto_sync_initial_count, 2) |
| 124 | + |
| 125 | + # Also sync manual attribute to compare |
| 126 | + self.manual_sync_attribute.action_sync_tower_values() |
| 127 | + |
| 128 | + manual_sync_initial_count = self.env["product.attribute.value"].search_count( |
| 129 | + [("attribute_id", "=", self.manual_sync_attribute.id)] |
| 130 | + ) |
| 131 | + self.assertEqual(manual_sync_initial_count, 2) |
| 132 | + |
| 133 | + # Create a third option for the Tower variable |
| 134 | + option_3 = self.env["cx.tower.variable.option"].create( |
| 135 | + { |
| 136 | + "variable_id": self.tower_variable.id, |
| 137 | + "name": "Option 3", |
| 138 | + "value_char": "option_3", |
| 139 | + } |
| 140 | + ) |
| 141 | + |
| 142 | + # Assert that auto_sync_attribute now automatically has three values |
| 143 | + auto_sync_final_count = self.env["product.attribute.value"].search_count( |
| 144 | + [("attribute_id", "=", self.auto_sync_attribute.id)] |
| 145 | + ) |
| 146 | + self.assertEqual(auto_sync_final_count, 3) |
| 147 | + |
| 148 | + # Verify the new value exists and is linked correctly |
| 149 | + new_value = self.env["product.attribute.value"].search( |
| 150 | + [ |
| 151 | + ("attribute_id", "=", self.auto_sync_attribute.id), |
| 152 | + ("tower_option_id", "=", option_3.id), |
| 153 | + ] |
| 154 | + ) |
| 155 | + self.assertEqual(len(new_value), 1) |
| 156 | + self.assertEqual(new_value.name, "Option 3") |
| 157 | + |
| 158 | + # Assert that manual_sync_attribute still only has two values |
| 159 | + manual_sync_final_count = self.env["product.attribute.value"].search_count( |
| 160 | + [("attribute_id", "=", self.manual_sync_attribute.id)] |
| 161 | + ) |
| 162 | + self.assertEqual(manual_sync_final_count, 2) |
0 commit comments