|
2 | 2 |
|
3 | 3 | from unittest.mock import MagicMock, patch |
4 | 4 |
|
5 | | -from odoo_data_flow.exporter import run_export, run_export_for_migration |
| 5 | +import pytest |
| 6 | + |
| 7 | +from odoo_data_flow.exporter import ( |
| 8 | + _show_error_panel, |
| 9 | + run_export, |
| 10 | + run_export_for_migration, |
| 11 | + run_export_from_file, |
| 12 | +) |
6 | 13 |
|
7 | 14 |
|
8 | 15 | @patch( |
@@ -139,14 +146,78 @@ def test_run_export_for_migration_bad_domain( |
139 | 146 | assert mock_export_data.call_args.args[2] == [] |
140 | 147 |
|
141 | 148 |
|
| 149 | +@patch("odoo_data_flow.exporter.Console") |
| 150 | +def test_show_error_panel(mock_console: MagicMock) -> None: |
| 151 | + """Tests the `_show_error_panel` function.""" |
| 152 | + _show_error_panel("Test Title", "Test Message") |
| 153 | + mock_console.assert_called_once_with(stderr=True, style="bold red") |
| 154 | + mock_console.return_value.print.assert_called_once() |
| 155 | + |
| 156 | + |
| 157 | +@patch( |
| 158 | + "odoo_data_flow.exporter.export_threaded.export_data_to_file", |
| 159 | + return_value=(False, "Error"), |
| 160 | +) |
| 161 | +@patch("odoo_data_flow.exporter._show_error_panel") |
| 162 | +def test_run_export_fail( |
| 163 | + mock_show_error_panel: MagicMock, mock_export_data: MagicMock |
| 164 | +) -> None: |
| 165 | + """Tests that `run_export` calls `_show_error_panel` when the export fails.""" |
| 166 | + run_export( |
| 167 | + config="dummy.conf", |
| 168 | + filename="dummy.csv", |
| 169 | + model="dummy.model", |
| 170 | + fields="id", |
| 171 | + ) |
| 172 | + mock_show_error_panel.assert_called_once_with("Export Aborted", "Error") |
| 173 | + |
| 174 | + |
142 | 175 | @patch("odoo_data_flow.exporter.export_threaded.export_data_for_migration") |
143 | | -def test_run_export_for_migration_no_data(mock_export_data: MagicMock) -> None: |
144 | | - """Tests that `run_export_for_migration`. |
| 176 | +def test_run_export_for_migration_bad_context(mock_export_data: MagicMock) -> None: |
| 177 | + """Tests `run_export_for_migration` with a bad context string.""" |
| 178 | + mock_export_data.return_value = ([], []) |
| 179 | + run_export_for_migration( |
| 180 | + config="dummy.conf", |
| 181 | + model="res.partner", |
| 182 | + fields=["id"], |
| 183 | + context="bad-context", |
| 184 | + ) |
| 185 | + # Assert that the context passed is an empty dict |
| 186 | + assert mock_export_data.call_args.kwargs["context"] == {} |
145 | 187 |
|
146 | | - These handles the case where no data is returned. |
147 | | - """ |
148 | | - mock_export_data.return_value = (["id"], None) |
149 | | - _header, data = run_export_for_migration( |
150 | | - config="dummy.conf", model="res.partner", fields=["id"] |
| 188 | + |
| 189 | +def test_run_export_from_file_not_implemented() -> None: |
| 190 | + """Tests that `run_export_from_file` raises NotImplementedError.""" |
| 191 | + with pytest.raises(NotImplementedError): |
| 192 | + run_export_from_file( |
| 193 | + config="dummy.conf", |
| 194 | + filename="dummy.csv", |
| 195 | + ) |
| 196 | + |
| 197 | + |
| 198 | +@patch("odoo_data_flow.exporter._show_error_panel") |
| 199 | +def test_run_export_domain_not_list(mock_show_error_panel: MagicMock) -> None: |
| 200 | + """Tests that `run_export` logs an error for a domain that is not a list.""" |
| 201 | + run_export( |
| 202 | + config="dummy.conf", |
| 203 | + filename="dummy.csv", |
| 204 | + model="dummy.model", |
| 205 | + fields="id", |
| 206 | + domain="{'is_company': True}", # Not a list |
151 | 207 | ) |
152 | | - assert data is None |
| 208 | + mock_show_error_panel.assert_called_once() |
| 209 | + assert "Domain must be a list" in mock_show_error_panel.call_args[0][1] |
| 210 | + |
| 211 | + |
| 212 | +@patch("odoo_data_flow.exporter._show_error_panel") |
| 213 | +def test_run_export_context_not_dict(mock_show_error_panel: MagicMock) -> None: |
| 214 | + """Tests that `run_export` logs an error for a context that is not a dict.""" |
| 215 | + run_export( |
| 216 | + config="dummy.conf", |
| 217 | + filename="dummy.csv", |
| 218 | + model="dummy.model", |
| 219 | + fields="id", |
| 220 | + context="['lang', 'fr_FR']", # Not a dict |
| 221 | + ) |
| 222 | + mock_show_error_panel.assert_called_once() |
| 223 | + assert "Context must be a dictionary" in mock_show_error_panel.call_args[0][1] |
0 commit comments