8000 Add test_engine fixture · astrochun/github-stats-pages@7bda06b · GitHub
[go: up one dir, main page]

Skip to content

Commit 7bda06b

Browse files
committed
Add test_engine fixture
- Update db tests - Use test_engine fixture in stats_plot.load_data
1 parent b498bf0 commit 7bda06b

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pytest
22

3+
from github_stats_pages import db
4+
35

46
def pytest_addoption(parser):
57
parser.addoption("--username", action="store", default="GitHub username")
@@ -22,3 +24,8 @@ def token(request):
2224
if name_value is None:
2325
pytest.skip()
2426
return name_value
27+
28+
29+
@pytest.fixture(scope="session")
30+
def test_engine():
31+
return db.create_db_and_tables(test=True)

github_stats_pages/stats_plots.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@
3232
main_p = Path(__file__).parent
3333

3434

35-
def load_data(test: bool = False) -> Dict[str, pd.DataFrame]:
35+
def load_data(
36+
test: bool = False, engine: Optional[db.Engine] = None
37+
) -> Dict[str, pd.DataFrame]:
3638
"""
3739
Load stats CSV as dict of pandas DataFrame
3840
3941
:return: Dict of pandas DataFrame
4042
"""
4143

42-
engine = db.create_db_and_tables(test=test)
44+
if not engine:
45+
engine = db.create_db_and_tables(test=test)
4346

4447
dict_df = {}
4548

tests/test_db.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
from github_stats_pages import db
22

3-
engine = db.create_db_and_tables(test=True)
43

5-
6-
def test_migrate_csv():
4+
def test_migrate_csv(test_engine):
75
# This CSV is already present so no new records exist
8-
db.migrate_csv("tests_data/data/merged_clone.csv", db.Clone, engine)
6+
db.migrate_csv("tests_data/data/merged_clone.csv", db.Clone, test_engine)
97

108
# This will add new records and ensure Paths testing
11-
db.migrate_csv("tests_data/data/merged_paths.csv", db.Paths, engine)
9+
db.migrate_csv("tests_data/data/merged_paths.csv", db.Paths, test_engine)
1210

1311

14-
def test_query():
12+
def test_query(test_engine):
1513

16-
t_query = db.query("github-stats-pages", "2021-02-28", engine, db.Clone)
14+
t_query = db.query(
15+
"github-stats-pages", "2021-02-28", test_engine, db.Clone
16+
)
1717
assert isinstance(t_query, db.Clone)
1818

19-
t_query = db.query("github-stats-pages", "2021-02-28", engine, db.Traffic)
19+
t_query = db.query(
20+
"github-stats-pages", "2021-02-28", test_engine, db.Traffic
21+
)
2022
assert isinstance(t_query, db.Traffic)
2123

2224
# This returns a None result
23-
assert not db.query("github-stats-pages", "2020-01-01", engine, db.Clone)
25+
assert not db.query(
26+
"github-stats-pages", "2020-01-01", test_engine, db.Clone
27+
)
2428

2529

26-
def test_query_all():
30+
def test_query_all(test_engine):
2731

28-
t_query = db.query_all(engine, db.Clone)
32+
t_query = db.query_all(test_engine, db.Clone)
2933
assert isinstance(t_query, list)

tests/test_stats_plots.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
tests_data_folder = Path("tests_data")
1010

1111

12-
def test_load_data():
13-
dict_df = stats_plots.load_data(test=True)
12+
def test_load_data(test_engine):
13+
dict_df = stats_plots.load_data(test=True, engine=test_engine)
1414
assert isinstance(dict_df, dict)
1515

1616

0 commit comments

Comments
 (0)
0