File size: 7,319 Bytes
658c35a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# test_api.py — Grader for the medium task (requests → httpx)
# Tests pass ONLY after all 3 files are migrated: api_client, session_manager, downloader.
# Uses unittest.mock — no extra deps.

import ast
import inspect
import os
import sys
import importlib.util
import pytest
from unittest.mock import patch, MagicMock

REPO_DIR = os.path.join(os.path.dirname(__file__), "..", "repo")
API_FILE = os.path.join(REPO_DIR, "api_client.py")
SESSION_FILE = os.path.join(REPO_DIR, "session_manager.py")
DOWNLOAD_FILE = os.path.join(REPO_DIR, "downloader.py")
ALL_FILES = [API_FILE, SESSION_FILE, DOWNLOAD_FILE]

if REPO_DIR not in sys.path:
    sys.path.insert(0, REPO_DIR)


def _get_imports(filepath):
    with open(filepath, "r") as f:
        tree = ast.parse(f.read(), filename=filepath)
    modules = set()
    for node in ast.walk(tree):
        if isinstance(node, ast.Import):
            for alias in node.names:
                modules.add(alias.name.split(".")[0])
        elif isinstance(node, ast.ImportFrom) and node.module:
            modules.add(node.module.split(".")[0])
    return modules


def _get_source(filepath):
    with open(filepath, "r") as f:
        return f.read()


def _load_module(name, filepath):
    spec = importlib.util.spec_from_file_location(name, filepath)
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)
    return mod


# ===========================================================================
# LAYER 1 — Import checks (6 tests)
# ===========================================================================
class TestImportsApiClient:
    def test_no_requests(self):
        assert "requests" not in _get_imports(API_FILE), \
            "api_client.py still imports requests"

    def test_has_httpx(self):
        assert "httpx" in _get_imports(API_FILE), \
            "api_client.py does not import httpx"


class TestImportsSessionManager:
    def test_no_requests(self):
        assert "requests" not in _get_imports(SESSION_FILE), \
            "session_manager.py still imports requests"

    def test_has_httpx(self):
        assert "httpx" in _get_imports(SESSION_FILE), \
            "session_manager.py does not import httpx"


class TestImportsDownloader:
    def test_no_requests(self):
        assert "requests" not in _get_imports(DOWNLOAD_FILE), \
            "downloader.py still imports requests"

    def test_has_httpx(self):
        assert "httpx" in _get_imports(DOWNLOAD_FILE), \
            "downloader.py does not import httpx"


# ===========================================================================
# LAYER 2 — API surface checks (8 tests)
# ===========================================================================
class TestAPISurface:
    def test_no_allow_redirects_in_api_client(self):
        src = _get_source(API_FILE)
        assert "allow_redirects" not in src, \
            "api_client.py still uses allow_redirects (should be follow_redirects)"

    def test_follow_redirects_in_api_client(self):
        src = _get_source(API_FILE)
        assert "follow_redirects" in src, \
            "api_client.py should use follow_redirects"

    def test_no_allow_redirects_in_downloader(self):
        src = _get_source(DOWNLOAD_FILE)
        assert "allow_redirects" not in src, \
            "downloader.py still uses allow_redirects"

    def test_follow_redirects_in_downloader(self):
        src = _get_source(DOWNLOAD_FILE)
        assert "follow_redirects" in src, \
            "downloader.py should use follow_redirects"

    def test_no_iter_content_in_downloader(self):
        src = _get_source(DOWNLOAD_FILE)
        assert "iter_content" not in src, \
            "downloader.py still uses iter_content (should be iter_bytes)"

    def test_no_requests_session(self):
        src = _get_source(SESSION_FILE)
        assert "requests.Session" not in src, \
            "session_manager.py still uses requests.Session"

    def test_httpx_client_in_session_manager(self):
        src = _get_source(SESSION_FILE)
        assert "httpx.Client" in src or "Client(" in src, \
            "session_manager.py should use httpx.Client"

    def test_httpx_timeout_in_session_manager(self):
        """Tuple timeout (5, 30) must become httpx.Timeout object."""
        src = _get_source(SESSION_FILE)
        assert "Timeout" in src, \
            "session_manager.py should use httpx.Timeout, not a tuple"
        assert "(5, 30)" not in src and "(5,30)" not in src, \
            "session_manager.py should convert tuple timeout to httpx.Timeout"


# ===========================================================================
# LAYER 3 — Functional tests (8 tests)
# ===========================================================================
class TestApiClientFunctional:
    @patch("httpx.get")
    def test_fetch_data(self, mock_get):
        mock_resp = MagicMock()
        mock_resp.json.return_value = {"id": 1}
        mock_resp.raise_for_status = MagicMock()
        mock_get.return_value = mock_resp

        mod = _load_module("api_client", API_FILE)
        result = mod.fetch_data("https://example.com/data")
        assert result == {"id": 1}
        mock_get.assert_called_once()

    @patch("httpx.post")
    def test_post_json(self, mock_post):
        mock_resp = MagicMock()
        mock_resp.json.return_value = {"status": "ok"}
        mock_resp.raise_for_status = MagicMock()
        mock_post.return_value = mock_resp

        mod = _load_module("api_client", API_FILE)
        result = mod.post_json("https://example.com/api", {"key": "val"})
        assert result == {"status": "ok"}

    @patch("httpx.head")
    def test_check_health(self, mock_head):
        mock_resp = MagicMock()
        mock_resp.status_code = 200
        mock_head.return_value = mock_resp

        mod = _load_module("api_client", API_FILE)
        assert mod.check_health("https://example.com") == 200
        call_kwargs = mock_head.call_args
        assert "follow_redirects" in str(call_kwargs), \
            "check_health should pass follow_redirects"


class TestSessionManagerFunctional:
    def test_class_exists(self):
        mod = _load_module("session_manager", SESSION_FILE)
        assert hasattr(mod, "SessionManager")

    def test_context_manager(self):
        """SessionManager must work as a context manager."""
        mod = _load_module("session_manager", SESSION_FILE)
        sm_cls = mod.SessionManager
        assert hasattr(sm_cls, "__enter__") and hasattr(sm_cls, "__exit__"), \
            "SessionManager must support context manager protocol"

    def test_has_close_method(self):
        mod = _load_module("session_manager", SESSION_FILE)
        sm = mod.SessionManager.__new__(mod.SessionManager)
        assert callable(getattr(sm, "close", None))


class TestDownloaderFunctional:
    def test_download_file_callable(self):
        mod = _load_module("downloader", DOWNLOAD_FILE)
        assert callable(mod.download_file)

    def test_download_parallel_is_async(self):
        """download_parallel must be an async function after migration."""
        mod = _load_module("downloader", DOWNLOAD_FILE)
        assert inspect.iscoroutinefunction(mod.download_parallel), \
            "download_parallel should be async (use httpx.AsyncClient)"