|
17 | 17 | _setup_fail_file, |
18 | 18 | import_data, |
19 | 19 | ) |
| 20 | +from odoo_data_flow.lib.internal.tools import to_xmlid |
20 | 21 |
|
21 | 22 |
|
22 | 23 | class TestImportData: |
@@ -220,7 +221,12 @@ def test_batch_scales_down_on_gateway_error( |
220 | 221 | "ignore_list": [], |
221 | 222 | } |
222 | 223 | batch_header = ["id", "name"] |
223 | | - batch_lines = [["rec1", "A"], ["rec2", "B"], ["rec3", "C"], ["rec4", "D"]] |
| 224 | + batch_lines = [ |
| 225 | + ["rec1", "A"], |
| 226 | + ["rec2", "B"], |
| 227 | + ["rec3", "C"], |
| 228 | + ["rec4", "D"], |
| 229 | + ] |
224 | 230 |
|
225 | 231 | # Set up the return value for the mocked function before the call |
226 | 232 | mock_create_individually.return_value = { |
@@ -415,7 +421,10 @@ def test_pass_2_handles_failed_batch(self, mock_run_pass: MagicMock) -> None: |
415 | 421 | (2, {"parent_id": 101}, "Access Error"), |
416 | 422 | ], |
417 | 423 | } |
418 | | - mock_run_pass.return_value = (failed_write_result, False) # result, aborted |
| 424 | + mock_run_pass.return_value = ( |
| 425 | + failed_write_result, |
| 426 | + False, |
| 427 | + ) # result, aborted |
419 | 428 |
|
420 | 429 | # Act |
421 | 430 | with Progress() as progress: |
@@ -538,7 +547,10 @@ def test_run_threaded_pass_keyboard_interrupt( |
538 | 547 | self, mock_as_completed: MagicMock |
539 | 548 | ) -> None: |
540 | 549 | """Test that a KeyboardInterrupt is handled gracefully.""" |
541 | | - from odoo_data_flow.import_threaded import RPCThreadImport, _run_threaded_pass |
| 550 | + from odoo_data_flow.import_threaded import ( |
| 551 | + RPCThreadImport, |
| 552 | + _run_threaded_pass, |
| 553 | + ) |
542 | 554 |
|
543 | 555 | rpc_thread = RPCThreadImport(1, Progress(), MagicMock()) |
544 | 556 | rpc_thread.task_id = rpc_thread.progress.add_task("test") |
@@ -743,3 +755,94 @@ def test_execute_load_batch_successfully_aggregates_all_records() -> None: |
743 | 755 | assert result["id_map"]["rec4"] == 4 |
744 | 756 | # Should have no failed lines |
745 | 757 | assert len(result["failed_lines"]) == 0 |
| 758 | + |
| 759 | + |
| 760 | +def test_execute_load_batch_sanitizes_ids_when_model_has_no_fields() -> None: |
| 761 | + """Test that unique ID field values are sanitized.""" |
| 762 | + mock_model = MagicMock() |
| 763 | + # Model has no _fields attribute |
| 764 | + mock_model._fields = None |
| 765 | + mock_model.load.return_value = {"ids": [1, 2]} |
| 766 | + mock_progress = MagicMock() |
| 767 | + thread_state = { |
| 768 | + "model": mock_model, |
| 769 | + "progress": mock_progress, |
| 770 | + "unique_id_field_index": 0, # Index of the ID column |
| 771 | + "ignore_list": [], |
| 772 | + } |
| 773 | + batch_header = ["id", "name"] |
| 774 | + # IDs with spaces that should be sanitized |
| 775 | + batch_lines = [ |
| 776 | + ["product_template_2023_02_08 09_45_32_0001", "Product 1"], |
| 777 | + ["another id with spaces", "Product 2"], |
| 778 | + ] |
| 779 | + |
| 780 | + from odoo_data_flow.import_threaded import _execute_load_batch |
| 781 | + from odoo_data_flow.lib.internal.tools import to_xmlid |
| 782 | + |
| 783 | + # Call the function |
| 784 | + result = _execute_load_batch(thread_state, batch_lines, batch_header, 1) |
| 785 | + |
| 786 | + # Verify that model.load was called with properly sanitized IDs |
| 787 | + # The call_args should show that the IDs were sanitized |
| 788 | + # (spaces replaced with underscores) |
| 789 | + call_args = mock_model.load.call_args |
| 790 | + sent_header, sent_data = call_args[0] # Get the positional arguments |
| 791 | + |
| 792 | + # Verify header is unchanged |
| 793 | + assert sent_header == batch_header |
| 794 | + # Verify that the IDs in the data have been sanitized |
| 795 | + assert sent_data[0][0] == to_xmlid( |
| 796 | + "product_template_2023_02_08 09_45_32_0001" |
| 797 | + ) # Should be sanitized |
| 798 | + assert sent_data[1][0] == to_xmlid("another id with spaces") # Should be sanitized |
| 799 | + |
| 800 | + # Verify the id_map uses the sanitized IDs |
| 801 | + expected_id1 = to_xmlid("product_template_2023_02_08 09_45_32_0001") |
| 802 | + expected_id2 = to_xmlid("another id with spaces") |
| 803 | + assert result["id_map"][expected_id1] == 1 |
| 804 | + assert result["id_map"][expected_id2] == 2 |
| 805 | + |
| 806 | + |
| 807 | +def test_execute_load_batch_sanitizes_ids_in_model_fields_case() -> None: |
| 808 | + """Test that unique ID field values are sanitized.""" |
| 809 | + mock_model = MagicMock() |
| 810 | + # Model has _fields attribute (like normal Odoo models) |
| 811 | + mock_model._fields = {"id": {"type": "char"}, "name": {"type": "char"}} |
| 812 | + mock_model.load.return_value = {"ids": [1, 2]} |
| 813 | + mock_progress = MagicMock() |
| 814 | + thread_state = { |
| 815 | + "model": mock_model, |
| 816 | + "progress": mock_progress, |
| 817 | + "unique_id_field_index": 0, # Index of the ID column |
| 818 | + "ignore_list": [], |
| 819 | + } |
| 820 | + batch_header = ["id", "name"] |
| 821 | + # IDs with spaces that should be sanitized |
| 822 | + batch_lines = [ |
| 823 | + ["product_template_2023_02_08 09_45_32_0003", "Product 1"], |
| 824 | + ["different id with spaces", "Product 2"], |
| 825 | + ] |
| 826 | + |
| 827 | + # Call the function |
| 828 | + result = _execute_load_batch(thread_state, batch_lines, batch_header, 1) |
| 829 | + |
| 830 | + # Verify that model.load was called with properly sanitized IDs |
| 831 | + call_args = mock_model.load.call_args |
| 832 | + sent_header, sent_data = call_args[0] # Get the positional arguments |
| 833 | + |
| 834 | + # Verify header is unchanged |
| 835 | + assert sent_header == batch_header |
| 836 | + # Verify that the IDs in the data have been sanitized |
| 837 | + assert sent_data[0][0] == to_xmlid( |
| 838 | + "product_template_2023_02_08 09_45_32_0003" |
| 839 | + ) # Should be sanitized |
| 840 | + assert sent_data[1][0] == to_xmlid( |
| 841 | + "different id with spaces" |
| 842 | + ) # Should be sanitized |
| 843 | + |
| 844 | + # Verify the id_map uses the sanitized IDs |
| 845 | + expected_id1 = to_xmlid("product_template_2023_02_08 09_45_32_0003") |
| 846 | + expected_id2 = to_xmlid("different id with spaces") |
| 847 | + assert result["id_map"][expected_id1] == 1 |
| 848 | + assert result["id_map"][expected_id2] == 2 |
0 commit comments