Skip to content

Commit b959ea7

Browse files
bosdbosd
authored andcommitted
test(exporter): increase test coverage
Adds tests for error handling and edge cases in the exporter module, increasing test coverage to 100%.
1 parent 58c9197 commit b959ea7

File tree

1 file changed

+80
-9
lines changed

1 file changed

+80
-9
lines changed

tests/test_exporter.py

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
from unittest.mock import MagicMock, patch
44

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+
)
613

714

815
@patch(
@@ -139,14 +146,78 @@ def test_run_export_for_migration_bad_domain(
139146
assert mock_export_data.call_args.args[2] == []
140147

141148

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+
142175
@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"] == {}
145187

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
151207
)
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

Comments
 (0)