|
1 | 1 | """Unit tests for BuildAPIWrapper.""" |
2 | 2 |
|
3 | | -from unittest.mock import Mock, patch |
| 3 | +from unittest.mock import Mock |
4 | 4 |
|
5 | 5 | from nutrient_dws.builder import BuildAPIWrapper |
6 | 6 |
|
7 | 7 |
|
8 | | -class TestBuildAPIWrapper: |
9 | | - """Test BuildAPIWrapper class.""" |
| 8 | +def test_builder_init(): |
| 9 | + """Test builder initialization.""" |
| 10 | + mock_client = Mock() |
| 11 | + builder = BuildAPIWrapper(client=mock_client, input_file="test.pdf") |
10 | 12 |
|
11 | | - def test_init(self): |
12 | | - """Test initialization.""" |
13 | | - mock_client = Mock() |
14 | | - builder = BuildAPIWrapper(client=mock_client, input_file="test.pdf") |
| 13 | + assert builder._client == mock_client |
| 14 | + assert builder._input_file == "test.pdf" |
| 15 | + assert builder._actions == [] |
15 | 16 |
|
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 | 17 |
|
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") |
| 18 | +def test_builder_add_step(): |
| 19 | + """Test adding a step.""" |
| 20 | + mock_client = Mock() |
| 21 | + builder = BuildAPIWrapper(client=mock_client, input_file="test.pdf") |
27 | 22 |
|
28 | | - result = builder.add_step("convert-to-pdf") |
| 23 | + result = builder.add_step("convert-to-pdf") |
29 | 24 |
|
30 | | - assert result == builder # Should return self for chaining |
31 | | - assert len(builder._actions) == 1 |
32 | | - assert builder._actions[0]["type"] == "convert-to-pdf" |
| 25 | + assert result == builder # Should return self for chaining |
| 26 | + assert len(builder._actions) == 1 |
| 27 | + assert builder._actions[0]["type"] == "convert-to-pdf" |
33 | 28 |
|
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 | 29 |
|
39 | | - builder.add_step("rotate-pages", {"degrees": 90}) |
| 30 | +def test_builder_chaining(): |
| 31 | + """Test method chaining.""" |
| 32 | + mock_client = Mock() |
| 33 | + builder = BuildAPIWrapper(client=mock_client, input_file="test.pdf") |
40 | 34 |
|
41 | | - assert len(builder._actions) == 1 |
42 | | - assert builder._actions[0]["type"] == "rotate" |
43 | | - assert builder._actions[0]["rotateBy"] == 90 |
| 35 | + builder.add_step("convert-to-pdf").add_step("ocr-pdf", {"language": "en"}) |
44 | 36 |
|
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") |
| 37 | + assert len(builder._actions) == 2 |
| 38 | + assert builder._actions[0]["type"] == "convert-to-pdf" |
| 39 | + assert builder._actions[1]["type"] == "ocr" |
49 | 40 |
|
50 | | - builder.add_step("convert-to-pdf").add_step("ocr-pdf", {"language": "en"}) |
51 | 41 |
|
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" |
| 42 | +def test_builder_output_options(): |
| 43 | + """Test setting output options.""" |
| 44 | + mock_client = Mock() |
| 45 | + builder = BuildAPIWrapper(client=mock_client, input_file="test.pdf") |
56 | 46 |
|
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") |
| 47 | + result = builder.set_output_options(format="pdf") |
61 | 48 |
|
62 | | - result = builder.set_output_options(format="pdf", quality=90) |
| 49 | + assert result == builder |
| 50 | + assert builder._output_options == {"format": "pdf"} |
63 | 51 |
|
64 | | - assert result == builder # Should return self for chaining |
65 | | - assert builder._output_options == {"format": "pdf", "quality": 90} |
66 | 52 |
|
67 | | - def test_build_instructions(self): |
68 | | - """Test building instructions.""" |
69 | | - mock_client = Mock() |
70 | | - builder = BuildAPIWrapper(client=mock_client, input_file="test.pdf") |
| 53 | +def test_builder_str(): |
| 54 | + """Test string representation.""" |
| 55 | + mock_client = Mock() |
| 56 | + builder = BuildAPIWrapper(client=mock_client, input_file="test.pdf") |
| 57 | + builder.add_step("convert-to-pdf") |
71 | 58 |
|
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 |
| 59 | + str_repr = str(builder) |
| 60 | + assert "BuildAPIWrapper" in str_repr |
| 61 | + assert "convert-to-pdf" in str_repr |
0 commit comments