-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_main.py
More file actions
57 lines (46 loc) · 1.96 KB
/
test_main.py
File metadata and controls
57 lines (46 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pytest
from unittest.mock import patch, MagicMock
import httpx
from main import get_book_details_from_openlibrary
@patch('main.httpx.Client')
def test_get_book_details_from_openlibrary_success(mock_client):
"""Test fetching book details successfully using the search API."""
isbn = '1234567890'
# Mock the response from the /search.json endpoint
mock_api_response = {
"numFound": 1,
"docs": [
{
"title": "Test Book",
"author_name": ["Test Author"],
"first_publish_year": 2023
}
]
}
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = mock_api_response
mock_client.return_value.__enter__.return_value.get.return_value = mock_response
book_details = get_book_details_from_openlibrary(isbn)
assert book_details is not None
assert book_details['title'] == 'Test Book'
assert book_details['author'] == 'Test Author'
assert book_details['year'] == 2023
@patch('main.httpx.Client')
def test_get_book_details_not_found(mock_client):
"""Test the case where the book is not found on OpenLibrary."""
isbn = '0000000000'
mock_api_response = {"numFound": 0, "docs": []}
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = mock_api_response
mock_client.return_value.__enter__.return_value.get.return_value = mock_response
book_details = get_book_details_from_openlibrary(isbn)
assert book_details is None
@patch('main.httpx.Client')
def test_get_book_details_api_failure(mock_client):
"""Test handling of failures when the API call fails."""
# Raise an httpx error that the function is designed to catch.
mock_client.return_value.__enter__.return_value.get.side_effect = httpx.RequestError("Mocked network error")
book_details = get_book_details_from_openlibrary('0987654321')
assert book_details is None