|
3 | 3 | These tests require a valid API key configured in integration_config.py. |
4 | 4 | """ |
5 | 5 |
|
| 6 | +from __future__ import annotations |
| 7 | + |
6 | 8 | import pytest |
7 | 9 |
|
8 | 10 | from nutrient_dws import NutrientClient |
9 | 11 |
|
10 | 12 | try: |
11 | 13 | from . import integration_config # type: ignore[attr-defined] |
12 | 14 |
|
13 | | - API_KEY = integration_config.API_KEY |
14 | | - BASE_URL = getattr(integration_config, "BASE_URL", None) |
15 | | - TIMEOUT = getattr(integration_config, "TIMEOUT", 60) |
| 15 | + API_KEY: str | None = integration_config.API_KEY |
| 16 | + BASE_URL: str | None = getattr(integration_config, "BASE_URL", None) |
| 17 | + TIMEOUT: int = getattr(integration_config, "TIMEOUT", 60) |
16 | 18 | except ImportError: |
17 | 19 | API_KEY = None |
18 | 20 | BASE_URL = None |
@@ -158,6 +160,74 @@ def test_split_pdf_single_page_default(self, client, sample_pdf_path): |
158 | 160 | # Verify result is a valid PDF |
159 | 161 | assert_is_pdf(result[0]) |
160 | 162 |
|
| 163 | + def test_set_page_label_integration(self, client, sample_pdf_path, tmp_path): |
| 164 | + """Test set_page_label method with live API.""" |
| 165 | + labels = [{"pages": {"start": 0, "end": 1}, "label": "Cover"}] |
| 166 | + |
| 167 | + output_path = str(tmp_path / "labeled.pdf") |
| 168 | + |
| 169 | + # Try to set page labels |
| 170 | + result = client.set_page_label(sample_pdf_path, labels, output_path=output_path) |
| 171 | + |
| 172 | + # If successful, verify output |
| 173 | + assert result is None # Should return None when output_path provided |
| 174 | + assert (tmp_path / "labeled.pdf").exists() |
| 175 | + assert_is_pdf(output_path) |
| 176 | + |
| 177 | + def test_set_page_label_return_bytes(self, client, sample_pdf_path): |
| 178 | + """Test set_page_label method returning bytes.""" |
| 179 | + labels = [{"pages": {"start": 0, "end": 1}, "label": "i"}] |
| 180 | + |
| 181 | + # Test getting bytes back |
| 182 | + result = client.set_page_label(sample_pdf_path, labels) |
| 183 | + |
| 184 | + assert isinstance(result, bytes) |
| 185 | + assert len(result) > 0 |
| 186 | + assert_is_pdf(result) |
| 187 | + |
| 188 | + def test_set_page_label_multiple_ranges(self, client, sample_pdf_path): |
| 189 | + """Test set_page_label method with multiple page ranges.""" |
| 190 | + labels = [ |
| 191 | + {"pages": {"start": 0, "end": 1}, "label": "i"}, |
| 192 | + {"pages": {"start": 1, "end": 2}, "label": "intro"}, |
| 193 | + {"pages": {"start": 2, "end": 3}, "label": "final"}, |
| 194 | + ] |
| 195 | + |
| 196 | + result = client.set_page_label(sample_pdf_path, labels) |
| 197 | + |
| 198 | + assert isinstance(result, bytes) |
| 199 | + assert len(result) > 0 |
| 200 | + assert_is_pdf(result) |
| 201 | + |
| 202 | + def test_set_page_label_single_page(self, client, sample_pdf_path): |
| 203 | + """Test set_page_label method with single page label.""" |
| 204 | + labels = [{"pages": {"start": 0, "end": 1}, "label": "Cover Page"}] |
| 205 | + |
| 206 | + result = client.set_page_label(sample_pdf_path, labels) |
| 207 | + |
| 208 | + assert isinstance(result, bytes) |
| 209 | + assert len(result) > 0 |
| 210 | + assert_is_pdf(result) |
| 211 | + |
| 212 | + def test_set_page_label_empty_labels_error(self, client, sample_pdf_path): |
| 213 | + """Test set_page_label method with empty labels raises error.""" |
| 214 | + with pytest.raises(ValueError, match="labels list cannot be empty"): |
| 215 | + client.set_page_label(sample_pdf_path, labels=[]) |
| 216 | + |
| 217 | + def test_set_page_label_invalid_label_config_error(self, client, sample_pdf_path): |
| 218 | + """Test set_page_label method with invalid label configuration raises error.""" |
| 219 | + # Missing 'pages' key |
| 220 | + with pytest.raises(ValueError, match="missing required 'pages' key"): |
| 221 | + client.set_page_label(sample_pdf_path, labels=[{"label": "test"}]) |
| 222 | + |
| 223 | + # Missing 'label' key |
| 224 | + with pytest.raises(ValueError, match="missing required 'label' key"): |
| 225 | + client.set_page_label(sample_pdf_path, labels=[{"pages": {"start": 0}}]) |
| 226 | + |
| 227 | + # Invalid pages format |
| 228 | + with pytest.raises(ValueError, match="'pages' must be a dict with 'start' key"): |
| 229 | + client.set_page_label(sample_pdf_path, labels=[{"pages": "invalid", "label": "test"}]) |
| 230 | + |
161 | 231 | def test_duplicate_pdf_pages_basic(self, client, sample_pdf_path): |
162 | 232 | """Test duplicate_pdf_pages method with basic duplication.""" |
163 | 233 | # Test duplicating first page twice |
|
0 commit comments