|
| 1 | +"""Unit tests for BuildAPIWrapper.""" |
| 2 | + |
| 3 | +from unittest.mock import Mock, patch |
| 4 | + |
| 5 | +from nutrient_dws.builder import BuildAPIWrapper |
| 6 | + |
| 7 | + |
| 8 | +class TestBuildAPIWrapper: |
| 9 | + """Test BuildAPIWrapper class.""" |
| 10 | + |
| 11 | + def test_init(self): |
| 12 | + """Test initialization.""" |
| 13 | + mock_client = Mock() |
| 14 | + builder = BuildAPIWrapper(client=mock_client, input_file="test.pdf") |
| 15 | + |
| 16 | + assert builder._client == mock_client |
| 17 | + assert builder._input_file == "test.pdf" |
| 18 | + assert builder._actions == [] |
| 19 | + assert builder._output_options == {} |
| 20 | + assert builder._parts == [{"file": "file"}] |
| 21 | + assert "file" in builder._files |
| 22 | + |
| 23 | + def test_add_step_basic(self): |
| 24 | + """Test adding a basic step.""" |
| 25 | + mock_client = Mock() |
| 26 | + builder = BuildAPIWrapper(client=mock_client, input_file="test.pdf") |
| 27 | + |
| 28 | + result = builder.add_step("convert-to-pdf") |
| 29 | + |
| 30 | + assert result == builder # Should return self for chaining |
| 31 | + assert len(builder._actions) == 1 |
| 32 | + assert builder._actions[0]["type"] == "convert-to-pdf" |
| 33 | + |
| 34 | + def test_add_step_with_options(self): |
| 35 | + """Test adding a step with options.""" |
| 36 | + mock_client = Mock() |
| 37 | + builder = BuildAPIWrapper(client=mock_client, input_file="test.pdf") |
| 38 | + |
| 39 | + builder.add_step("rotate-pages", {"degrees": 90}) |
| 40 | + |
| 41 | + assert len(builder._actions) == 1 |
| 42 | + assert builder._actions[0]["type"] == "rotate" |
| 43 | + assert builder._actions[0]["rotateBy"] == 90 |
| 44 | + |
| 45 | + def test_chaining_steps(self): |
| 46 | + """Test chaining multiple steps.""" |
| 47 | + mock_client = Mock() |
| 48 | + builder = BuildAPIWrapper(client=mock_client, input_file="test.pdf") |
| 49 | + |
| 50 | + builder.add_step("convert-to-pdf").add_step("ocr-pdf", {"language": "en"}) |
| 51 | + |
| 52 | + assert len(builder._actions) == 2 |
| 53 | + assert builder._actions[0]["type"] == "convert-to-pdf" |
| 54 | + assert builder._actions[1]["type"] == "ocr" |
| 55 | + assert builder._actions[1]["language"] == "english" |
| 56 | + |
| 57 | + def test_set_output_options(self): |
| 58 | + """Test setting output options.""" |
| 59 | + mock_client = Mock() |
| 60 | + builder = BuildAPIWrapper(client=mock_client, input_file="test.pdf") |
| 61 | + |
| 62 | + result = builder.set_output_options(format="pdf", quality=90) |
| 63 | + |
| 64 | + assert result == builder # Should return self for chaining |
| 65 | + assert builder._output_options == {"format": "pdf", "quality": 90} |
| 66 | + |
| 67 | + def test_build_instructions(self): |
| 68 | + """Test building instructions.""" |
| 69 | + mock_client = Mock() |
| 70 | + builder = BuildAPIWrapper(client=mock_client, input_file="test.pdf") |
| 71 | + |
| 72 | + builder.add_step("convert-to-pdf") |
| 73 | + builder.add_step("ocr-pdf", {"language": "en"}) |
| 74 | + builder.set_output_options(format="pdf") |
| 75 | + |
| 76 | + instructions = builder._build_instructions() |
| 77 | + |
| 78 | + assert "parts" in instructions |
| 79 | + assert instructions["actions"] == builder._actions |
| 80 | + assert instructions["output"] == {"format": "pdf"} |
| 81 | + |
| 82 | + def test_execute_returns_bytes(self): |
| 83 | + """Test execute returns bytes when no output path.""" |
| 84 | + mock_client = Mock() |
| 85 | + mock_http_client = Mock() |
| 86 | + mock_client._http_client = mock_http_client |
| 87 | + |
| 88 | + # Mock the response |
| 89 | + mock_response = b"PDF content" |
| 90 | + mock_http_client.post.return_value = mock_response |
| 91 | + |
| 92 | + builder = BuildAPIWrapper(client=mock_client, input_file=b"input content") |
| 93 | + builder.add_step("convert-to-pdf") |
| 94 | + |
| 95 | + with patch("nutrient_dws.builder.prepare_file_for_upload") as mock_prepare: |
| 96 | + mock_prepare.return_value = ("file", ("doc.pdf", b"input content", "application/pdf")) |
| 97 | + |
| 98 | + result = builder.execute() |
| 99 | + |
| 100 | + assert result == b"PDF content" |
| 101 | + |
| 102 | + def test_execute_saves_to_file(self): |
| 103 | + """Test execute saves to file when output path provided.""" |
| 104 | + mock_client = Mock() |
| 105 | + mock_http_client = Mock() |
| 106 | + mock_client._http_client = mock_http_client |
| 107 | + |
| 108 | + # Mock the response |
| 109 | + mock_response = b"PDF content" |
| 110 | + mock_http_client.post.return_value = mock_response |
| 111 | + |
| 112 | + builder = BuildAPIWrapper(client=mock_client, input_file=b"input content") |
| 113 | + builder.add_step("convert-to-pdf") |
| 114 | + |
| 115 | + with patch("nutrient_dws.builder.prepare_file_for_upload") as mock_prepare: |
| 116 | + mock_prepare.return_value = ("file", ("doc.pdf", b"input content", "application/pdf")) |
| 117 | + |
| 118 | + with patch("nutrient_dws.builder.save_file_output") as mock_save: |
| 119 | + result = builder.execute("output.pdf") |
| 120 | + |
| 121 | + assert result is None |
| 122 | + mock_save.assert_called_once_with(b"PDF content", "output.pdf") |
| 123 | + |
| 124 | + def test_str_representation(self): |
| 125 | + """Test string representation.""" |
| 126 | + mock_client = Mock() |
| 127 | + builder = BuildAPIWrapper(client=mock_client, input_file="test.pdf") |
| 128 | + builder.add_step("convert-to-pdf") |
| 129 | + |
| 130 | + str_repr = str(builder) |
| 131 | + assert "BuildAPIWrapper" in str_repr |
| 132 | + assert "convert-to-pdf" in str_repr |
| 133 | + |
| 134 | + def test_repr_representation(self): |
| 135 | + """Test repr representation.""" |
| 136 | + mock_client = Mock() |
| 137 | + builder = BuildAPIWrapper(client=mock_client, input_file="test.pdf") |
| 138 | + builder.add_step("convert-to-pdf") |
| 139 | + builder.add_step("ocr-pdf") |
| 140 | + |
| 141 | + repr_str = repr(builder) |
| 142 | + assert "BuildAPIWrapper" in repr_str |
| 143 | + assert "input_file=" in repr_str |
| 144 | + assert "actions=" in repr_str |
0 commit comments