Skip to content

Commit f3b724d

Browse files
committed
Add unit tests for db module
- Fix typo with handling Paths data - Additional pragma no cover - Add test_engine fixture - Use test_engine fixture in stats_plot.load_data
1 parent 57597e3 commit f3b724d

File tree

6 files changed

+130
-9
lines changed

6 files changed

+130
-9
lines changed

conftest.py

+7
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/db.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def configure(test: bool = False, echo: bool = False) -> Engine:
1717
sqlite_file_name = (
1818
Path("tests_data/sqlite3.db") if test else SQLITE_FILE_NAME
1919
)
20-
if not sqlite_file_name.parent.exists():
20+
if not sqlite_file_name.parent.exists(): # pragma: no cover
2121
sqlite_file_name.parent.mkdir()
2222
sqlite_url = f"sqlite:///{sqlite_file_name}"
2323
log.info(f"Configuring SQLite at: {sqlite_url}")
@@ -46,7 +46,7 @@ def migrate_csv(
4646
)
4747
log.info(f"[yellow]Loading: {filename}")
4848
df = pd.read_csv(filename, header=None, skiprows=skip_rows, names=names)
49-
if isinstance(model, Paths):
49+
if model.__name__ == "Paths":
5050
repository_names = [a.split("/")[2] for a in df["path"].values]
5151
df.insert(1, "repository_name", repository_names)
5252

@@ -64,7 +64,7 @@ def migrate_csv(
6464
new_df.to_sql(
6565
model.__name__.lower(), engine, if_exists="append", index=False
6666
)
67-
if len(new_df) < len(df):
67+
if len(new_df) < len(df): # pragma: no cover
6868
log.info("[orange]Some records exists in db")
6969

7070

github_stats_pages/stats_plots.py

+7-4
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

@@ -78,8 +81,8 @@ def get_date_range(df_list: List[pd.DataFrame]) -> Optional[Tuple[dt, dt]]:
7881

7982
if len(x_min) > 0:
8083
return min(x_min) - td(days=1), max(x_max) + td(days=1)
81-
else:
82-
return None
84+
else: # pragma: no cover
85+
return
8386

8487

8588
def date_subplots(

tests/test_db.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from github_stats_pages import db
2+
3+
4+
def test_migrate_csv(test_engine):
5+
# This CSV is already present so no new records exist
6+
db.migrate_csv("tests_data/data/merged_clone.csv", db.Clone, test_engine)
7+
8+
# This will add new records and ensure Paths testing
9+
db.migrate_csv("tests_data/data/merged_paths.csv", db.Paths, test_engine)
10+
11+
12+
def test_query(test_engine):
13+
14+
t_query = db.query(
15+
"github-stats-pages", "2021-02-28", test_engine, db.Clone
16+
)
17+
assert isinstance(t_query, db.Clone)
18+
19+
t_query = db.query(
20+
"github-stats-pages", "2021-02-28", test_engine, db.Traffic
21+
)
22+
assert isinstance(t_query, db.Traffic)
23+
24+
# This returns a None result
25+
assert not db.query(
26+
"github-stats-pages", "2020-01-01", test_engine, db.Clone
27+
)
28+
29+
30+
def test_query_all(test_engine):
31+
32+
t_query = db.query_all(test_engine, db.Clone)
33+
assert isinstance(t_query, list)

tests/test_stats_plots.py

+2-2
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

tests_data/data/merged_paths.csv

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2021-05-13,/astrochun/Evolution-of-Galaxies,astrochun/Evolution-of-Galaxies: Research with Dr. Chun Ly. I am working with...,23,3
2+
2021-05-13,/astrochun/Evolution-of-Galaxies/blob/master/Analysis/emission_line_fit.py,Evolution-of-Galaxies/emission_line_fit.py at master · astrochun/Evolution-of...,4,2
3+
2021-05-13,/astrochun/Evolution-of-Galaxies/issues,Issues · astrochun/Evolution-of-Galaxies,10,3
4+
2021-05-13,/astrochun/Evolution-of-Galaxies/issues/55,Bug with saving fitting results · Issue #55 · astrochun/Evolution-of-Galaxies,3,2
5+
2021-05-13,/astrochun/Evolution-of-Galaxies/pull/56,hotfix/v0.8.2 by astrochun · Pull Request #56 · astrochun/Evolution-of-Galaxies,6,2
6+
2021-05-13,/astrochun/Evolution-of-Galaxies/pull/56/files,hotfix/v0.8.2 by astrochun · Pull Request #56 · astrochun/Evolution-of-Galaxies,4,2
7+
2021-05-13,/astrochun/Evolution-of-Galaxies/pull/57,Feature/53 windows uname by astrochun · Pull Request #57 · astrochun/Evolutio...,3,2
8+
2021-05-13,/astrochun/Evolution-of-Galaxies/pulls,Pull requests · astrochun/Evolution-of-Galaxies,19,3
9+
2021-05-13,/astrochun/Evolution-of-Galaxies/tree/develop,astrochun/Evolution-of-Galaxies at develop,4,2
10+
2021-05-13,/astrochun/Evolution-of-Galaxies/tree/master/Analysis,Evolution-of-Galaxies/Analysis at master · astrochun/Evolution-of-Galaxies,5,3
11+
2021-05-13,/astrochun/GNIRSLongSlit,GitHub - astrochun/GNIRSLongSlit: Python 2.7 codes to reduce Longslit data fr...,2,2
12+
2021-05-13,/astrochun/MMTtools,GitHub - astrochun/MMTtools: A set of Python 2.7 and 3.x codes to use with da...,2,2
13+
2021-05-13,/astrochun/Metallicity_Stack_Commons,astrochun/Metallicity_Stack_Commons: Set of common codes used in metallicity ...,19,3
14+
2021-05-13,/astrochun/Metallicity_Stack_Commons/blob/master/Metallicity_Stack_Commons/__init__.py,Metallicity_Stack_Commons/__init__.py at master · astrochun/Metallicity_Stack...,5,2
15+
2021-05-13,/astrochun/Metallicity_Stack_Commons/issues,Issues · astrochun/Metallicity_Stack_Commons,18,3
16+
2021-05-13,/astrochun/Metallicity_Stack_Commons/issues/114,Bug: Change HbHgHd_fit to have same y-axis scale · Issue #114 · astrochun/Met...,4,2
17+
2021-05-13,/astrochun/Metallicity_Stack_Commons/pull/115,hotfix/1.4.6 by astrochun · Pull Request #115 · astrochun/Metallicity_Stack_C...,4,2
18+
2021-05-13,/astrochun/Metallicity_Stack_Commons/pull/117,hotfix/v1.4.7 by Reagen · Pull Request #117 · astrochun/Metallicity_Stack_Com...,4,2
19+
2021-05-13,/astrochun/Metallicity_Stack_Commons/pull/118,hotfix/1.4.7 by Reagen · Pull Request #118 · astrochun/Metallicity_Stack_Comm...,11,2
20+
2021-05-13,/astrochun/Metallicity_Stack_Commons/pulls,Pull requests · astrochun/Metallicity_Stack_Commons,28,3
21+
2021-05-13,/astrochun/Metallicity_Stack_Commons/tree/master/Metallicity_Stack_Commons,Metallicity_Stack_Commons/Metallicity_Stack_Commons at master · astrochun/Met...,9,3
22+
2021-05-13,/astrochun/Metallicity_Stack_Commons/tree/master/Metallicity_Stack_Commons/analysis,Metallicity_Stack_Commons/Metallicity_Stack_Commons/analysis at master · astr...,4,2
23+
2021-05-13,/astrochun/PyMontage,GitHub - astrochun/PyMontage: Python 2.7 scripts to running the IPAC Montage ...,1,1
24+
2021-05-13,/astrochun/Zcalbase_gal,astrochun/Zcalbase_gal: Python 3.x codes for Metallicity Calibration Database...,18,2
25+
2021-05-13,/astrochun/Zcalbase_gal/blob/master/analysis/deep2_r23_o32/zoom_and_gauss_general.py,Zcalbase_gal/zoom_and_gauss_general.py at master · astrochun/Zcalbase_gal,3,1
26+
2021-05-13,/astrochun/Zcalbase_gal/issues,Issues · astrochun/Zcalbase_gal,13,2
27+
2021-05-13,/astrochun/Zcalbase_gal/issues/100,Bug: Incorrect use of normalization for fits · Issue #100 · astrochun/Zcalbas...,5,2
28+
2021-05-13,/astrochun/Zcalbase_gal/pull/99,hotfix/v0.13.1 by Reagen · Pull Request #99 · astrochun/Zcalbase_gal,14,2
29+
2021-05-13,/astrochun/Zcalbase_gal/pull/99/commits,hotfix/v0.13.1 by Reagen · Pull Request #99 · astrochun/Zcalbase_gal,4,2
30+
2021-05-13,/astrochun/Zcalbase_gal/pull/99/files,hotfix/v0.13.1 by Reagen · Pull Request #99 · astrochun/Zcalbase_gal,9,1
31+
2021-05-13,/astrochun/Zcalbase_gal/pulls,Pull requests · astrochun/Zcalbase_gal,21,2
32+
2021-05-13,/astrochun/Zcalbase_gal/tree/master/analysis,Zcalbase_gal/analysis at master · astrochun/Zcalbase_gal,4,1
33+
2021-05-13,/astrochun/Zcalbase_gal/tree/master/analysis/deep2_r23_o32,Zcalbase_gal/analysis/deep2_r23_o32 at master · astrochun/Zcalbase_gal,4,1
34+
2021-05-13,/astrochun/academic-ads-bibtex/blob/main/setup.py,academic-ads-bibtex/setup.py at main · astrochun/academic-ads-bibtex,1,1
35+
2021-05-13,/astrochun/figshare_autosync_check,astrochun/figshare_autosync_check,6,1
36+
2021-05-13,/astrochun/figshare_autosync_check/actions,Actions · astrochun/figshare_autosync_check,3,1
37+
2021-05-13,/astrochun/figshare_autosync_check/actions/runs/415969321,Add data.csv 2020-08-15 data · astrochun/figshare_autosync_check@1ecfbfa,2,1
38+
2021-05-13,/astrochun/figshare_autosync_check/actions/workflows/create_release.yml,Actions · astrochun/figshare_autosync_check,3,1
39+
2021-05-13,/astrochun/figshare_autosync_check/blob/main/.github/workflows/create_release.yml,figshare_autosync_check/create_release.yml at main · astrochun/figshare_autos...,4,1
40+
2021-05-13,/astrochun/figshare_autosync_check/commit/9829445961cfeef4b203925a3d5dcb073341863d,Add .git dot files · astrochun/figshare_autosync_check@9829445,1,1
41+
2021-05-13,/astrochun/figshare_autosync_check/commits/main,Commits · astrochun/figshare_autosync_check,1,1
42+
2021-05-13,/astrochun/figshare_autosync_check/tree/main/.github/workflows,figshare_autosync_check/.github/workflows at main · astrochun/figshare_autosy...,5,1
43+
2021-05-13,/astrochun/github-stats,astrochun/github-stats: My GitHub stats,14,4
44+
2021-05-13,/astrochun/github-stats-pages,astrochun/github-stats-pages: Retrieve statistics for a user's repositories a...,37,2
45+
2021-05-13,/astrochun/github-stats-pages/actions,Actions · astrochun/github-stats-pages,17,1
46+
2021-05-13,/astrochun/github-stats-pages/graphs/traffic,Traffic · astrochun/github-stats-pages,5,1
47+
2021-05-13,/astrochun/github-stats-pages/issues,Issues · astrochun/github-stats-pages,24,2
48+
2021-05-13,/astrochun/github-stats-pages/issues/52,Add popular content · Issue #52 · astrochun/github-stats-pages,10,2
49+
2021-05-13,/astrochun/github-stats-pages/pull/55,Feature: Add GitHub repo description on repo pages by astrochun · Pull Reques...,8,1
50+
2021-05-13,/astrochun/github-stats-pages/pull/56,Switch over to use PyGitHub for more capabilities by astrochun · Pull Request...,8,1
51+
2021-05-13,/astrochun/github-stats-pages/pulls,Pull requests · astrochun/github-stats-pages,20,1
52+
2021-05-13,/astrochun/github-stats-pages/pulse,Pulse · astrochun/github-stats-pages,6,2
53+
2021-05-13,/astrochun/github-stats-pages/releases,Releases · astrochun/github-stats-pages,26,2
54+
2021-05-13,/astrochun/github-stats/blob/gh-pages/repos/Extract1D.html,github-stats/Extract1D.html at gh-pages · astrochun/github-stats,1,1
55+
2021-05-13,/astrochun/github-stats/blob/gh-pages/repositories.html,github-stats/repositories.html at gh-pages · astrochun/github-stats,1,1
56+
2021-05-13,/astrochun/github-stats/blob/main/.github/workflows/gh-pages-deploy.yml,github-stats/gh-pages-deploy.yml at main · astrochun/github-stats,1,1
57+
2021-05-13,/astrochun/github-stats/network,Network Graph · astrochun/github-stats,1,1
58+
2021-05-13,/astrochun/github-stats/network/dependencies,Dependencies · astrochun/github-stats,1,1
59+
2021-05-13,/astrochun/github-stats/tree/gh-pages,astrochun/github-stats at gh-pages,4,2
60+
2021-05-13,/astrochun/github-stats/tree/gh-pages/repos,github-stats/repos at gh-pages · astrochun/github-stats,2,1
61+
2021-05-13,/astrochun/github-stats/tree/gh-pages/styles,github-stats/styles at gh-pages · astrochun/github-stats,2,1
62+
2021-05-13,/astrochun/github-stats/tree/main/.github/workflows,github-stats/.github/workflows at main · astrochun/github-stats,3,1
63+
2021-05-13,/astrochun/site-hugo-academic,astrochun/site-hugo-academic,2,2
64+
2021-05-13,/astrochun/test-github-stats,astrochun/test-github-stats: Testing of GitHub action for GitHub pages deploy...,2,1
65+
2021-05-13,/astrochun/test-github-stats/blob/main/data/2021-01-17-00h-46m-clone-stats.csv,test-github-stats/2021-01-17-00h-46m-clone-stats.csv at main · astrochun/test...,1,1
66+
2021-05-13,/astrochun/test-github-stats/blob/main/data/2021-01-17-00h-46m-referrer-stats.csv,test-github-stats/2021-01-17-00h-46m-referrer-stats.csv at main · astrochun/t...,3,1
67+
2021-05-13,/astrochun/test-github-stats/blob/main/data/2021-01-17-00h-46m-traffic-stats.csv,test-github-stats/2021-01-17-00h-46m-traffic-stats.csv at main · astrochun/te...,2,1
68+
2021-05-13,/astrochun/test-github-stats/blob/main/data/2021-01-17-00h-52m-clone-stats.csv,test-github-stats/2021-01-17-00h-52m-clone-stats.csv at main · astrochun/test...,1,1
69+
2021-05-13,/astrochun/test-github-stats/blob/main/data/2021-02-20-08h-55m-referrer-stats.csv,test-github-stats/2021-02-20-08h-55m-referrer-stats.csv at main · astrochun/t...,1,1
70+
2021-05-13,/astrochun/test-github-stats/blob/main/data/2021-02-26-22h-11m-traffic-stats.csv,test-github-stats/2021-02-26-22h-11m-traffic-stats.csv at main · astrochun/te...,1,1
71+
2021-05-13,/astrochun/test-github-stats/commit/27a681b3ce456710f7db46606566f000e144d6f4,Update data: 2021-05-11 · astrochun/test-github-stats@27a681b,1,1
72+
2021-05-13,/astrochun/test-github-stats/commit/de2412de46852a7acdfad4db7a10bbd956ef8448,Update data: 2021-05-12 · astrochun/test-github-stats@de2412d,2,1
73+
2021-05-13,/astrochun/test-github-stats/tree/main/data,test-github-stats/data at main · astrochun/test-github-stats,3,1
74+
2021-05-13,/astrochun/test-stats,astrochun/test-stats,2,1
75+
2021-05-13,/astrochun/test-stats/actions,Actions · astrochun/test-stats,1,1
76+
2021-05-13,/astrochun/test-stats/tree/gh-pages,astrochun/test-stats at gh-pages,1,1
77+
2021-05-13,/astrochun/test-stats/tree/gh-pages/repos,test-stats/repos at gh-pages · astrochun/test-stats,1,1
78+
2021-05-13,/astrochun/voxcharta-my-voting-record,GitHub - astrochun/voxcharta-my-voting-record: A Python tool to extract infor...,1,1

0 commit comments

Comments
 (0)