Skip to content

Commit b675b92

Browse files
committed
Separate out api vs function tests
1 parent 8cab51a commit b675b92

File tree

3 files changed

+37
-40
lines changed

3 files changed

+37
-40
lines changed

tests/conftest.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
import json
33
from enum import StrEnum
44
from pathlib import Path
5-
from typing import Any, Iterator
5+
from typing import Any, Iterator, NamedTuple
66

77
import httpx
88
import pytest
99
from database.setup import expdb_database, user_database
1010
from fastapi.testclient import TestClient
1111
from main import create_api
1212
from routers.dependencies import expdb_connection, userdb_connection
13-
from sqlalchemy import Connection, Engine
13+
from sqlalchemy import Connection, Engine, text
1414

1515

1616
class ApiKey(StrEnum):
@@ -65,3 +65,25 @@ def dataset_130() -> Iterator[dict[str, Any]]:
6565
@pytest.fixture()
6666
def default_configuration_file() -> Path:
6767
return Path().parent.parent / "src" / "config.toml"
68+
69+
70+
class Flow(NamedTuple):
71+
"""To be replaced by an actual ORM class."""
72+
73+
id: int
74+
name: str
75+
external_version: str
76+
77+
78+
@pytest.fixture()
79+
def flow(expdb_test: Connection) -> Flow:
80+
expdb_test.execute(
81+
text(
82+
"""
83+
INSERT INTO implementation(fullname,name,version,external_version,uploadDate)
84+
VALUES ('a','name',2,'external_version','2024-02-02 02:23:23');
85+
""",
86+
),
87+
)
88+
(flow_id,) = expdb_test.execute(text("""SELECT LAST_INSERT_ID();""")).one()
89+
return Flow(id=flow_id, name="name", external_version="external_version")

tests/database/flows_test.py

+2-25
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,7 @@
1-
from typing import NamedTuple
2-
31
import database.flows
4-
import pytest
5-
from sqlalchemy import Connection, text
6-
7-
8-
class Flow(NamedTuple):
9-
"""To be replaced by an actual ORM class."""
2+
from sqlalchemy import Connection
103

11-
id: int
12-
name: str
13-
external_version: str
14-
15-
16-
@pytest.fixture()
17-
def flow(expdb_test: Connection) -> Flow:
18-
expdb_test.execute(
19-
text(
20-
"""
21-
INSERT INTO implementation(fullname,name,version,external_version,uploadDate)
22-
VALUES ('a','name',2,'external_version','2024-02-02 02:23:23');
23-
""",
24-
),
25-
)
26-
(flow_id,) = expdb_test.execute(text("""SELECT LAST_INSERT_ID();""")).one()
27-
return Flow(id=flow_id, name="name", external_version="external_version")
4+
from tests.conftest import Flow
285

296

307
def test_database_flow_exists(flow: Flow, expdb_test: Connection) -> None:

tests/routers/openml/flows_test.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
import deepdiff.diff
44
import pytest
5+
from fastapi import HTTPException
56
from pytest_mock import MockerFixture
67
from routers.openml.flows import flow_exists
78
from sqlalchemy import Connection
89
from starlette.testclient import TestClient
910

10-
from tests.database.flows_test import Flow
11+
from tests.conftest import Flow
1112

1213

1314
@pytest.mark.parametrize(
@@ -36,7 +37,7 @@ def test_flow_exists_calls_db_correctly(
3637
"flow_id",
3738
[1, 2],
3839
)
39-
def test_flow_exists_handles_flow_found(
40+
def test_flow_exists_processes_found(
4041
flow_id: int,
4142
mocker: MockerFixture,
4243
expdb_test: Connection,
@@ -50,27 +51,24 @@ def test_flow_exists_handles_flow_found(
5051
assert response == {"flow_id": fake_flow.id}
5152

5253

53-
def test_flow_exists_handles_flow_not_found(py_api: TestClient, mocker: MockerFixture) -> None:
54+
def test_flow_exists_handles_flow_not_found(mocker: MockerFixture, expdb_test: Connection) -> None:
5455
mocker.patch("database.flows.get_by_name", return_value=None)
55-
response = py_api.get("/flows/exists/weka.ZeroR/Weka_3.9.0_12024")
56-
assert response.status_code == http.client.NOT_FOUND
56+
with pytest.raises(HTTPException) as error:
57+
flow_exists("foo", "bar", expdb_test)
58+
assert error.value.status_code == http.client.NOT_FOUND
59+
assert error.value.detail == "Flow not found."
5760

5861

59-
def test_full_stack_exists(flow: Flow, py_api: TestClient) -> None:
62+
def test_flow_exists(flow: Flow, py_api: TestClient) -> None:
6063
response = py_api.get(f"/flows/exists/{flow.name}/{flow.external_version}")
6164
assert response.status_code == http.client.OK
6265
assert response.json() == {"flow_id": flow.id}
6366

6467

65-
def test_flow_exists(py_api: TestClient) -> None:
66-
response = py_api.get("/flows/exists/weka.ZeroR/Weka_3.9.0_12024")
67-
assert response.status_code == http.client.OK
68-
assert response.json() == {"flow_id": 1}
69-
70-
7168
def test_flow_exists_not_exists(py_api: TestClient) -> None:
72-
response = py_api.get("/flows/exists/does_not_exist/Weka_3.9.0_12024")
69+
response = py_api.get("/flows/exists/foo/bar")
7370
assert response.status_code == http.client.NOT_FOUND
71+
assert response.json()["detail"] == "Flow not found."
7472

7573

7674
def test_get_flow_no_subflow(py_api: TestClient) -> None:

0 commit comments

Comments
 (0)