Skip to content

Commit f32ccd4

Browse files
committed
fix: resolve CI linting and type checking issues
- Fix line length violations in test files - Apply ruff formatting to maintain consistent code style - Add type annotations to test helper functions - Update error message assertions to avoid line length issues
1 parent 5ae5371 commit f32ccd4

File tree

4 files changed

+71
-78
lines changed

4 files changed

+71
-78
lines changed

src/nutrient_dws/api/direct.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,10 @@ def watermark_pdf(
217217
"height": height,
218218
"opacity": opacity,
219219
"position": position,
220-
"image": "watermark" # Reference to the uploaded image file
220+
"image": "watermark", # Reference to the uploaded image file
221221
}
222222

223-
instructions = {
224-
"parts": [{"file": "file"}],
225-
"actions": [action]
226-
}
223+
instructions = {"parts": [{"file": "file"}], "actions": [action]}
227224

228225
# Make API request
229226
# Type checking: at runtime, self is NutrientClient which has _http_client

tests/integration/test_watermark_image_file_integration.py

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Integration tests for image file watermark functionality."""
22

33
import os
4-
from typing import Optional
4+
from pathlib import Path
5+
from typing import Optional, Union
56

67
import pytest
78

@@ -19,7 +20,7 @@
1920
TIMEOUT = 60
2021

2122

22-
def assert_is_pdf(file_path_or_bytes):
23+
def assert_is_pdf(file_path_or_bytes: Union[str, bytes]) -> None:
2324
"""Assert that a file or bytes is a valid PDF."""
2425
if isinstance(file_path_or_bytes, str):
2526
with open(file_path_or_bytes, "rb") as f:
@@ -32,13 +33,13 @@ def assert_is_pdf(file_path_or_bytes):
3233
)
3334

3435

35-
def create_test_image(tmp_path, filename="watermark.png"):
36+
def create_test_image(tmp_path: Path, filename: str = "watermark.png") -> str:
3637
"""Create a simple test PNG image."""
3738
# PNG header for a 1x1 transparent pixel
3839
png_data = (
39-
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01'
40-
b'\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\x9cc\xf8\x0f'
41-
b'\x00\x00\x01\x01\x00\x00\xcb\xd6\x8e\n\x00\x00\x00\x00IEND\xaeB`\x82'
40+
b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01"
41+
b"\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\x9cc\xf8\x0f"
42+
b"\x00\x00\x01\x01\x00\x00\xcb\xd6\x8e\n\x00\x00\x00\x00IEND\xaeB`\x82"
4243
)
4344

4445
image_path = tmp_path / filename
@@ -73,7 +74,7 @@ def test_watermark_pdf_with_image_file_path(self, client, sample_pdf_path, tmp_p
7374
width=100,
7475
height=50,
7576
opacity=0.5,
76-
position="bottom-right"
77+
position="bottom-right",
7778
)
7879

7980
assert isinstance(result, bytes)
@@ -84,9 +85,9 @@ def test_watermark_pdf_with_image_bytes(self, client, sample_pdf_path):
8485
"""Test watermark_pdf with image as bytes."""
8586
# PNG header for a 1x1 transparent pixel
8687
png_bytes = (
87-
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01'
88-
b'\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\x9cc\xf8\x0f'
89-
b'\x00\x00\x01\x01\x00\x00\xcb\xd6\x8e\n\x00\x00\x00\x00IEND\xaeB`\x82'
88+
b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01"
89+
b"\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\rIDATx\x9cc\xf8\x0f"
90+
b"\x00\x00\x01\x01\x00\x00\xcb\xd6\x8e\n\x00\x00\x00\x00IEND\xaeB`\x82"
9091
)
9192

9293
result = client.watermark_pdf(
@@ -95,7 +96,7 @@ def test_watermark_pdf_with_image_bytes(self, client, sample_pdf_path):
9596
width=150,
9697
height=75,
9798
opacity=0.8,
98-
position="top-left"
99+
position="top-left",
99100
)
100101

101102
assert isinstance(result, bytes)
@@ -115,7 +116,7 @@ def test_watermark_pdf_with_image_file_output_path(self, client, sample_pdf_path
115116
height=100,
116117
opacity=0.7,
117118
position="center",
118-
output_path=output_path
119+
output_path=output_path,
119120
)
120121

121122
assert result is None
@@ -136,7 +137,7 @@ def test_watermark_pdf_with_file_like_object(self, client, sample_pdf_path, tmp_
136137
width=120,
137138
height=60,
138139
opacity=0.6,
139-
position="top-center"
140+
position="top-center",
140141
)
141142

142143
assert isinstance(result, bytes)
@@ -151,13 +152,16 @@ def test_builder_api_with_image_file_watermark(self, client, sample_pdf_path, tm
151152
# Use builder API
152153
result = (
153154
client.build(sample_pdf_path)
154-
.add_step("watermark-pdf", options={
155-
"image_file": image_path,
156-
"width": 180,
157-
"height": 90,
158-
"opacity": 0.4,
159-
"position": "bottom-left"
160-
})
155+
.add_step(
156+
"watermark-pdf",
157+
options={
158+
"image_file": image_path,
159+
"width": 180,
160+
"height": 90,
161+
"opacity": 0.4,
162+
"position": "bottom-left",
163+
},
164+
)
161165
.execute()
162166
)
163167

@@ -173,24 +177,29 @@ def test_multiple_watermarks_with_image_files(self, client, sample_pdf_path, tmp
173177
# Chain multiple watermark operations
174178
result = (
175179
client.build(sample_pdf_path)
176-
.add_step("watermark-pdf", options={
177-
"text": "DRAFT",
178-
"width": 200,
179-
"height": 100,
180-
"opacity": 0.3,
181-
"position": "center"
182-
})
183-
.add_step("watermark-pdf", options={
184-
"image_file": image1_path,
185-
"width": 100,
186-
"height": 50,
187-
"opacity": 0.5,
188-
"position": "top-right"
189-
})
180+
.add_step(
181+
"watermark-pdf",
182+
options={
183+
"text": "DRAFT",
184+
"width": 200,
185+
"height": 100,
186+
"opacity": 0.3,
187+
"position": "center",
188+
},
189+
)
190+
.add_step(
191+
"watermark-pdf",
192+
options={
193+
"image_file": image1_path,
194+
"width": 100,
195+
"height": 50,
196+
"opacity": 0.5,
197+
"position": "top-right",
198+
},
199+
)
190200
.execute()
191201
)
192202

193203
assert isinstance(result, bytes)
194204
assert len(result) > 0
195205
assert_is_pdf(result)
196-

tests/unit/test_direct_api.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ def test_watermark_pdf_with_image_url(self, mock_process):
162162

163163
def test_watermark_pdf_no_text_or_image_raises_error(self):
164164
"""Test watermark_pdf raises ValueError when neither text nor image_url provided."""
165-
with pytest.raises(ValueError, match="Either text, image_url, or image_file must be provided"):
165+
err_msg = "Either text, image_url, or image_file must be provided"
166+
with pytest.raises(ValueError, match=err_msg):
166167
self.client.watermark_pdf("test.pdf")
167168

168169
@patch("nutrient_dws.client.NutrientClient._process_file")
@@ -314,16 +315,18 @@ def setup_method(self):
314315

315316
def test_watermark_pdf_validation_error(self):
316317
"""Test watermark_pdf parameter validation."""
318+
err_msg = "Either text, image_url, or image_file must be provided"
319+
317320
# Test missing text, image_url, and image_file
318-
with pytest.raises(ValueError, match="Either text, image_url, or image_file must be provided"):
321+
with pytest.raises(ValueError, match=err_msg):
319322
self.client.watermark_pdf("test.pdf")
320323

321324
# Test empty text and no image_url or image_file
322-
with pytest.raises(ValueError, match="Either text, image_url, or image_file must be provided"):
325+
with pytest.raises(ValueError, match=err_msg):
323326
self.client.watermark_pdf("test.pdf", text="")
324327

325328
# Test None text and no image_url or image_file
326-
with pytest.raises(ValueError, match="Either text, image_url, or image_file must be provided"):
329+
with pytest.raises(ValueError, match=err_msg):
327330
self.client.watermark_pdf("test.pdf", text=None)
328331

329332
def test_merge_pdfs_validation_error(self):

tests/unit/test_watermark_image_file.py

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_watermark_pdf_with_image_file_bytes(self, client, mock_http_client):
3535
width=150,
3636
height=75,
3737
opacity=0.8,
38-
position="top-right"
38+
position="top-right",
3939
)
4040

4141
assert result == b"PDF content"
@@ -70,12 +70,7 @@ def test_watermark_pdf_with_image_file_object(self, client, mock_http_client):
7070
pdf_file = BytesIO(b"PDF file content")
7171
image_file = BytesIO(b"PNG image data")
7272

73-
result = client.watermark_pdf(
74-
pdf_file,
75-
image_file=image_file,
76-
width=200,
77-
height=100
78-
)
73+
result = client.watermark_pdf(pdf_file, image_file=image_file, width=200, height=100)
7974

8075
assert result == b"PDF content"
8176

@@ -91,9 +86,7 @@ def test_watermark_pdf_with_output_path(self, client, mock_http_client):
9186

9287
with patch("nutrient_dws.file_handler.save_file_output") as mock_save:
9388
result = client.watermark_pdf(
94-
pdf_bytes,
95-
image_file=image_bytes,
96-
output_path="output.pdf"
89+
pdf_bytes, image_file=image_bytes, output_path="output.pdf"
9790
)
9891

9992
assert result is None
@@ -110,10 +103,7 @@ def test_watermark_pdf_text_still_works(self, client, mock_http_client):
110103
# Mock _process_file method
111104
with patch.object(client, "_process_file", return_value=b"PDF content") as mock_process:
112105
result = client.watermark_pdf(
113-
b"PDF content",
114-
text="CONFIDENTIAL",
115-
width=200,
116-
height=100
106+
b"PDF content", text="CONFIDENTIAL", width=200, height=100
117107
)
118108

119109
assert result == b"PDF content"
@@ -125,18 +115,15 @@ def test_watermark_pdf_text_still_works(self, client, mock_http_client):
125115
height=100,
126116
opacity=1.0,
127117
position="center",
128-
text="CONFIDENTIAL"
118+
text="CONFIDENTIAL",
129119
)
130120

131121
def test_watermark_pdf_url_still_works(self, client, mock_http_client):
132122
"""Test that URL watermarks still work with new implementation."""
133123
# Mock _process_file method
134124
with patch.object(client, "_process_file", return_value=b"PDF content") as mock_process:
135125
result = client.watermark_pdf(
136-
b"PDF content",
137-
image_url="https://example.com/logo.png",
138-
width=200,
139-
height=100
126+
b"PDF content", image_url="https://example.com/logo.png", width=200, height=100
140127
)
141128

142129
assert result == b"PDF content"
@@ -148,7 +135,7 @@ def test_watermark_pdf_url_still_works(self, client, mock_http_client):
148135
height=100,
149136
opacity=1.0,
150137
position="center",
151-
image_url="https://example.com/logo.png"
138+
image_url="https://example.com/logo.png",
152139
)
153140

154141
def test_builder_api_with_image_file(self, client, mock_http_client):
@@ -157,13 +144,16 @@ def test_builder_api_with_image_file(self, client, mock_http_client):
157144
image_bytes = b"PNG image data"
158145

159146
builder = client.build(pdf_bytes)
160-
builder.add_step("watermark-pdf", options={
161-
"image_file": image_bytes,
162-
"width": 150,
163-
"height": 75,
164-
"opacity": 0.5,
165-
"position": "bottom-right"
166-
})
147+
builder.add_step(
148+
"watermark-pdf",
149+
options={
150+
"image_file": image_bytes,
151+
"width": 150,
152+
"height": 75,
153+
"opacity": 0.5,
154+
"position": "bottom-right",
155+
},
156+
)
167157

168158
result = builder.execute()
169159

@@ -197,16 +187,10 @@ def test_watermark_pdf_precedence(self, client, mock_http_client):
197187
# But for clarity, let's test that providing text uses text watermark
198188
with patch.object(client, "_process_file", return_value=b"PDF content") as mock_process:
199189
# Test with text - should use _process_file
200-
client.watermark_pdf(
201-
b"PDF content",
202-
text="TEXT",
203-
width=100,
204-
height=50
205-
)
190+
client.watermark_pdf(b"PDF content", text="TEXT", width=100, height=50)
206191

207192
# Should use text path
208193
mock_process.assert_called_once()
209194
call_args = mock_process.call_args[1]
210195
assert "text" in call_args
211196
assert call_args["text"] == "TEXT"
212-

0 commit comments

Comments
 (0)