|
| 1 | +from pathlib import Path |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | +import typer |
| 6 | +from pydantic import BaseModel |
| 7 | +from pydantic.fields import Field, FieldInfo |
| 8 | +from rich.table import Column, Table |
| 9 | + |
| 10 | +from ragbits.cli.state import OutputType, _get_nested_field, print_output, print_output_table |
| 11 | +from ragbits.document_search.documents.sources import LocalFileSource |
| 12 | + |
| 13 | + |
| 14 | +class InnerTestModel(BaseModel): |
| 15 | + id: int |
| 16 | + name: str = Field(title="Name of the inner model", description="Name of the inner model") |
| 17 | + location: LocalFileSource |
| 18 | + |
| 19 | + |
| 20 | +class OtherTestModel(BaseModel): |
| 21 | + id: int |
| 22 | + name: str |
| 23 | + location: InnerTestModel |
| 24 | + |
| 25 | + |
| 26 | +class MainTestModel(BaseModel): |
| 27 | + id: int |
| 28 | + name: str |
| 29 | + model: OtherTestModel | None |
| 30 | + |
| 31 | + |
| 32 | +data = [ |
| 33 | + MainTestModel( |
| 34 | + id=1, |
| 35 | + name="A", |
| 36 | + model=OtherTestModel( |
| 37 | + id=11, |
| 38 | + name="aa", |
| 39 | + location=InnerTestModel(id=111, name="aa1", location=LocalFileSource(path=Path("folder_1"))), |
| 40 | + ), |
| 41 | + ), |
| 42 | + MainTestModel( |
| 43 | + id=2, |
| 44 | + name="B", |
| 45 | + model=OtherTestModel( |
| 46 | + id=22, |
| 47 | + name="bb", |
| 48 | + location=InnerTestModel(id=222, name="aa2", location=LocalFileSource(path=Path("folder_2"))), |
| 49 | + ), |
| 50 | + ), |
| 51 | +] |
| 52 | + |
| 53 | + |
| 54 | +@patch("ragbits.cli.state.print_output_table") |
| 55 | +@patch("ragbits.cli.state.print_output_json") |
| 56 | +def test_print_output_text(mock_print_output_json: MagicMock, mock_print_output_table: MagicMock): |
| 57 | + with patch("ragbits.cli.state.cli_state") as mock_cli_state: |
| 58 | + mock_cli_state.output_type = OutputType.text |
| 59 | + columns = {"id": Column(), "name": Column()} |
| 60 | + print_output(data, columns=columns) |
| 61 | + mock_print_output_table.assert_called_once_with(data, columns) |
| 62 | + mock_print_output_json.assert_not_called() |
| 63 | + |
| 64 | + |
| 65 | +@patch("ragbits.cli.state.print_output_table") |
| 66 | +@patch("ragbits.cli.state.print_output_json") |
| 67 | +def test_print_output_json(mock_print_output_json: MagicMock, mock_print_output_table: MagicMock): |
| 68 | + with patch("ragbits.cli.state.cli_state") as mock_cli_state: |
| 69 | + mock_cli_state.output_type = OutputType.json |
| 70 | + print_output(data) |
| 71 | + mock_print_output_table.assert_not_called() |
| 72 | + mock_print_output_json.assert_called_once_with(data) |
| 73 | + |
| 74 | + |
| 75 | +def test_print_output_unsupported_output_type(): |
| 76 | + with patch("ragbits.cli.state.cli_state") as mock_cli_state: |
| 77 | + mock_cli_state.output_type = "unsupported_type" |
| 78 | + with pytest.raises(ValueError, match="Unsupported output type: unsupported_type"): |
| 79 | + print_output(data) |
| 80 | + |
| 81 | + |
| 82 | +def test_print_output_table(): |
| 83 | + with patch("rich.console.Console.print") as mock_print: |
| 84 | + columns = {"id": Column(), "model.location.location.path": Column(), "model.location.name": Column()} |
| 85 | + print_output_table(data, columns) |
| 86 | + mock_print.assert_called_once() |
| 87 | + args, _ = mock_print.call_args_list[0] |
| 88 | + printed_table = args[0] |
| 89 | + assert isinstance(printed_table, Table) |
| 90 | + assert printed_table.columns[0].header == "Id" |
| 91 | + assert printed_table.columns[1].header == "Model Location Location Path" |
| 92 | + assert printed_table.columns[2].header == "Name of the inner model" |
| 93 | + assert printed_table.row_count == 2 |
| 94 | + |
| 95 | + |
| 96 | +def test_get_nested_field(): |
| 97 | + column = "model.location.location.path" |
| 98 | + fields = {"name": FieldInfo(annotation=str), "model": FieldInfo(annotation=OtherTestModel)} |
| 99 | + |
| 100 | + try: |
| 101 | + result = _get_nested_field(column, fields) |
| 102 | + assert result.annotation == Path |
| 103 | + except typer.Exit: |
| 104 | + pytest.fail("typer.Exit was raised unexpectedly") |
| 105 | + |
| 106 | + |
| 107 | +def test_get_nested_field_wrong_field(): |
| 108 | + column_names = [ |
| 109 | + ("model.location.wrong_field", "wrong_field"), |
| 110 | + ("model.wrong_path.location.path", "wrong_path"), |
| 111 | + ("wrong_path.location.location.path", "wrong_path"), |
| 112 | + ("model.location.path", "path"), |
| 113 | + ("model.location.location.path.additional_field", "additional_field"), |
| 114 | + ] |
| 115 | + fields = {"name": FieldInfo(annotation=str), "model": FieldInfo(annotation=OtherTestModel)} |
| 116 | + |
| 117 | + for wrong_column, wrong_fragment in column_names: |
| 118 | + with patch("rich.console.Console.print") as mock_print: |
| 119 | + with pytest.raises(typer.Exit, match="1"): |
| 120 | + _get_nested_field(wrong_column, fields) |
| 121 | + mock_print.assert_called_once_with(f"Unknown column: {wrong_column} ({wrong_fragment} not found)") |
0 commit comments