Skip to content

Commit e0950f0

Browse files
I've added a test suite for the helper functions in examples/utils/example_helpers.py.
This includes tests for: - `get_printable_datetime`: checks for string return type and ISO format. - `get_image_bytes_from_url`: checks for bytes return type, exception handling for invalid URLs, and correct image download using a mock. I also confirmed that the Google Ads API version is already v19 by default.
1 parent 41ff022 commit e0950f0

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

examples/utils/tests/.gitkeep

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This file is intentionally left blank.
2+
# It is used to ensure that the directory is tracked by git.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import unittest
2+
from unittest.mock import patch, MagicMock
3+
from datetime import datetime
4+
import requests # Required for requests.exceptions.RequestException
5+
from examples.utils.example_helpers import get_printable_datetime, get_image_bytes_from_url
6+
7+
class TestGetPrintableDatetime(unittest.TestCase): # Renamed for clarity
8+
9+
def test_get_printable_datetime_returns_string(self):
10+
"""Tests that get_printable_datetime returns a string."""
11+
self.assertIsInstance(get_printable_datetime(), str)
12+
13+
def test_get_printable_datetime_iso_format(self):
14+
"""Tests that get_printable_datetime returns a string in ISO format."""
15+
datetime_str = get_printable_datetime()
16+
# Attempt to parse the string to ensure it's in ISO format.
17+
# datetime.fromisoformat() will raise a ValueError if not.
18+
try:
19+
datetime.fromisoformat(datetime_str)
20+
except ValueError:
21+
self.fail(f"'{datetime_str}' is not a valid ISO format datetime string.")
22+
23+
class TestGetImageBytesFromUrl(unittest.TestCase):
24+
25+
@patch('requests.get')
26+
def test_returns_bytes_for_valid_url(self, mock_requests_get):
27+
"""Tests that the function returns bytes for a valid URL."""
28+
sample_bytes = b'sampleimagedata'
29+
mock_response = MagicMock()
30+
mock_response.content = sample_bytes
31+
mock_requests_get.return_value = mock_response
32+
33+
result = get_image_bytes_from_url("http://example.com/image.jpg")
34+
self.assertIsInstance(result, bytes)
35+
self.assertEqual(result, sample_bytes)
36+
mock_requests_get.assert_called_once_with("http://example.com/image.jpg")
37+
38+
@patch('requests.get')
39+
def test_raises_exception_for_invalid_url(self, mock_requests_get):
40+
"""Tests that the function raises an exception for an invalid URL."""
41+
mock_requests_get.side_effect = requests.exceptions.RequestException("Test error")
42+
43+
with self.assertRaises(requests.exceptions.RequestException):
44+
get_image_bytes_from_url("http://invalid-url-that-does-not-exist.com/image.png")
45+
mock_requests_get.assert_called_once_with("http://invalid-url-that-does-not-exist.com/image.png")
46+
47+
@patch('requests.get')
48+
def test_correctly_downloads_image(self, mock_requests_get):
49+
"""Tests that the function correctly 'downloads' image bytes from a valid URL."""
50+
expected_bytes = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89'
51+
mock_response = MagicMock()
52+
mock_response.content = expected_bytes
53+
mock_requests_get.return_value = mock_response
54+
55+
actual_bytes = get_image_bytes_from_url("http://example.com/realimage.png")
56+
self.assertEqual(actual_bytes, expected_bytes)
57+
mock_requests_get.assert_called_once_with("http://example.com/realimage.png")
58+
59+
60+
if __name__ == '__main__':
61+
unittest.main()

0 commit comments

Comments
 (0)