11"""
2- Tests for tind_client. fetch.
2+ Tests for TINDClient methods ( fetch operations) .
33"""
44
55import json
66
77import pytest
88import requests_mock as req_mock # noqa: F401 — activates the requests_mock fixture
99
10- from tind_client import TINDClient , fetch
10+ from tind_client import TINDClient
1111from tind_client .errors import RecordNotFoundError , TINDError
1212
1313BASE_URL = "https://tind.example.edu"
@@ -28,17 +28,15 @@ def test_fetch_metadata_success(
2828 requests_mock .get (
2929 f"{ BASE_URL } /record/12345/" , text = sample_marc_xml , status_code = 200
3030 )
31- record = fetch .fetch_metadata (
32- "12345" , api_key = client .api_key , api_url = client .api_url
33- )
31+ record = client .fetch_metadata ("12345" )
3432 assert record ["245" ]["a" ] == "Sample Title"
3533
3634
3735def test_fetch_metadata_404 (requests_mock : req_mock .Mocker , client : TINDClient ) -> None :
3836 """fetch_metadata raises RecordNotFoundError on HTTP 404."""
3937 requests_mock .get (f"{ BASE_URL } /record/99999/" , text = "" , status_code = 404 )
4038 with pytest .raises (RecordNotFoundError ):
41- fetch .fetch_metadata ("99999" , api_key = client . api_key , api_url = client . api_url )
39+ client .fetch_metadata ("99999" )
4240
4341
4442def test_fetch_metadata_empty_body (
@@ -47,7 +45,7 @@ def test_fetch_metadata_empty_body(
4745 """fetch_metadata raises RecordNotFoundError when the response body is empty."""
4846 requests_mock .get (f"{ BASE_URL } /record/11111/" , text = " " , status_code = 200 )
4947 with pytest .raises (RecordNotFoundError ):
50- fetch .fetch_metadata ("11111" , api_key = client . api_key , api_url = client . api_url )
48+ client .fetch_metadata ("11111" )
5149
5250
5351# ---------------------------------------------------------------------------
@@ -58,7 +56,7 @@ def test_fetch_metadata_empty_body(
5856def test_fetch_file_invalid_url (client : TINDClient ) -> None :
5957 """fetch_file raises ValueError for non-TIND download URLs."""
6058 with pytest .raises (ValueError ):
61- fetch .fetch_file ("https://not-a-tind-url.com/file.pdf" , api_key = client . api_key )
59+ client .fetch_file ("https://not-a-tind-url.com/file.pdf" )
6260
6361
6462def test_fetch_file_success (
@@ -74,7 +72,7 @@ def test_fetch_file_success(
7472 status_code = 200 ,
7573 headers = {"Content-Disposition" : 'attachment; filename="document.pdf"' },
7674 )
77- path = fetch .fetch_file (url , api_key = client . api_key , output_dir = str (tmp_path ))
75+ path = client .fetch_file (url , output_dir = str (tmp_path ))
7876 assert path .endswith ("document.pdf" )
7977
8078
@@ -87,7 +85,7 @@ def test_fetch_file_not_found(
8785 url = f"{ BASE_URL } /files/missing/download"
8886 requests_mock .get (url , status_code = 404 )
8987 with pytest .raises (RecordNotFoundError ):
90- fetch .fetch_file (url , api_key = client . api_key , output_dir = str (tmp_path ))
88+ client .fetch_file (url , output_dir = str (tmp_path ))
9189
9290
9391# ---------------------------------------------------------------------------
@@ -105,9 +103,7 @@ def test_fetch_file_metadata_success(
105103 text = json .dumps (payload ),
106104 status_code = 200 ,
107105 )
108- result = fetch .fetch_file_metadata (
109- "12345" , api_key = client .api_key , api_url = client .api_url
110- )
106+ result = client .fetch_file_metadata ("12345" )
111107 assert result [0 ]["name" ] == "file.pdf"
112108
113109
@@ -121,9 +117,7 @@ def test_fetch_file_metadata_error(
121117 status_code = 404 ,
122118 )
123119 with pytest .raises (TINDError ):
124- fetch .fetch_file_metadata (
125- "12345" , api_key = client .api_key , api_url = client .api_url
126- )
120+ client .fetch_file_metadata ("12345" )
127121
128122
129123# ---------------------------------------------------------------------------
@@ -140,9 +134,7 @@ def test_fetch_ids_search_success(
140134 text = json .dumps ({"hits" : ["1" , "2" , "3" ]}),
141135 status_code = 200 ,
142136 )
143- ids = fetch .fetch_ids_search (
144- "title:python" , api_key = client .api_key , api_url = client .api_url
145- )
137+ ids = client .fetch_ids_search ("title:python" )
146138 assert ids == ["1" , "2" , "3" ]
147139
148140
@@ -156,9 +148,7 @@ def test_fetch_ids_search_error(
156148 status_code = 400 ,
157149 )
158150 with pytest .raises (TINDError ):
159- fetch .fetch_ids_search (
160- "title:python" , api_key = client .api_key , api_url = client .api_url
161- )
151+ client .fetch_ids_search ("title:python" )
162152
163153
164154# ---------------------------------------------------------------------------
@@ -169,12 +159,7 @@ def test_fetch_ids_search_error(
169159def test_search_invalid_format (client : TINDClient ) -> None :
170160 """search raises ValueError for unsupported result_format values."""
171161 with pytest .raises (ValueError , match = "Unexpected result format" ):
172- fetch .search (
173- "title:test" ,
174- api_key = client .api_key ,
175- api_url = client .api_url ,
176- result_format = "csv" ,
177- )
162+ client .search ("title:test" , result_format = "csv" )
178163
179164
180165def test_search_returns_xml (
@@ -191,12 +176,7 @@ def test_search_returns_xml(
191176
192177 requests_mock .get (f"{ BASE_URL } /search" , text = wrapped , status_code = 200 )
193178
194- results = fetch .search (
195- "title:sample" ,
196- api_key = client .api_key ,
197- api_url = client .api_url ,
198- result_format = "xml" ,
199- )
179+ results = client .search ("title:sample" , result_format = "xml" )
200180 assert isinstance (results , list )
201181 assert len (results ) >= 1
202182 assert requests_mock .call_count == 1
0 commit comments