Skip to content

Commit 4b8081f

Browse files
authored
Refactor tests to use constants for repository names and minimum dependents (#743)
* Refactor tests to use constants for repository names and minimum dependents * [MegaLinter] Apply linters fixes :) * check packages number * fix * [MegaLinter] Apply linters fixes * Refactor update logic in sources_all_df to ensure proper data type handling * coverage img * changelog --------- Co-authored-by: nvuillam <17500430+nvuillam@users.noreply.github.com>
1 parent d4cb1d8 commit 4b8081f

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
88

99
- Upgrade project
1010
- Upgrade dependencies
11-
- CI: Upgrade MegaLinter
11+
- Fix total dependents count (#607)
12+
- Refactor update logic in sources_all_df to ensure proper data type handling
13+
- CI
14+
- Upgrade MegaLinter
15+
- Refactor test classes so they run faster
1216

1317
## [1.6.3] 2023-03-03
1418

assets/images/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

github_dependents_info/gh_dependents_info.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import json
2-
import time
32
import logging
43
import os
54
import re
5+
import time
66
from pathlib import Path
77

88
import numpy as np
@@ -242,7 +242,10 @@ def save_progress(self, package):
242242
# update the row with the new information
243243
sources_all_df.set_index("name", inplace=True)
244244
source_df = pd.json_normalize(source_info).set_index("name", drop=True)
245-
sources_all_df.update(source_df)
245+
for column in source_df.columns:
246+
if source_df[column].dtype == object and column in sources_all_df.columns:
247+
sources_all_df[column] = sources_all_df[column].astype("object")
248+
sources_all_df.loc[source_df.index, column] = source_df[column]
246249
sources_all_df.reset_index(inplace=True, drop=False)
247250
sources_all_df.to_csv(file_path_sources, mode="w", header=True)
248251
else:

tests/test_gh_dependents_info/test_gh_dependents_info.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,67 @@
66

77
from github_dependents_info import GithubDependentsInfo
88

9+
SINGLE_PACKAGE_REPO = "nvuillam/npm-groovy-lint"
10+
SINGLE_PACKAGE_TOTAL_DOC_URL = "https://nvuillam/npm-groovy-lint"
11+
SINGLE_PACKAGE_REPO_PUBLIC_DEPENDENTS_MIN = 10
12+
MULTI_PACKAGE_REPO = "nvuillam/github-dependents-info"
13+
MULTI_PACKAGE_REPO_PUBLIC_DEPENDENTS_MIN = 10
14+
915

1016
def test_collect_stats_single_package():
1117
# Check generate single package stats file
12-
repo = "nvuillam/npm-groovy-lint"
18+
repo = SINGLE_PACKAGE_REPO
1319
tmp_md_file = tempfile.gettempdir() + os.path.sep + str(uuid.uuid4()) + "-test-single.md"
1420
gh_deps_info = GithubDependentsInfo(
1521
repo, debug=True, sort_key="stars", badge_color="pink", markdown_file=tmp_md_file
1622
)
1723
repo_stats = gh_deps_info.collect()
18-
assert repo_stats["public_dependents_number"] > 10
24+
assert repo_stats["public_dependents_number"] > SINGLE_PACKAGE_REPO_PUBLIC_DEPENDENTS_MIN
1925

2026
md = gh_deps_info.build_markdown(file=tmp_md_file)
21-
assert md.count("\n") > 10
27+
assert md.count("\n") > SINGLE_PACKAGE_REPO_PUBLIC_DEPENDENTS_MIN
2228
assert "pink" in md
2329
with open(tmp_md_file, encoding="utf-8") as file:
2430
md_content = file.read()
25-
assert md_content.count("\n") > 10
31+
assert md_content.count("\n") > SINGLE_PACKAGE_REPO_PUBLIC_DEPENDENTS_MIN
2632
# Check Update README file
2733
tmp_readme_file = tempfile.gettempdir() + os.path.sep + str(uuid.uuid4()) + "-test-single-readme.md"
2834
with open(tmp_readme_file, "w", encoding="utf-8") as file:
2935
file.write(
3036
"<!-- gh-dependents-info-used-by-start -->" + "shouldBeReplaced" + "<!-- gh-dependents-info-used-by-end -->"
3137
)
3238

33-
gh_deps_info.badges["total_doc_url"] = "https://nvuillam/npm-groovy-lint"
39+
gh_deps_info.badges["total_doc_url"] = SINGLE_PACKAGE_TOTAL_DOC_URL
3440
gh_deps_info.write_badge(tmp_readme_file, "total_doc_url")
3541
with open(tmp_readme_file, encoding="utf-8") as file:
3642
readme_content = file.read()
3743
assert "shouldBeReplaced" not in readme_content
38-
assert "nvuillam/npm-groovy-lint" in readme_content
44+
assert SINGLE_PACKAGE_REPO in readme_content
3945

4046

4147
def test_collect_stats_multi_package():
42-
repo = "oxsecurity/megalinter"
48+
repo = MULTI_PACKAGE_REPO
4349
gh_deps_info = GithubDependentsInfo(repo, debug=True, sort_key="stars")
4450
repo_stats = gh_deps_info.collect()
45-
assert repo_stats["public_dependents_number"] > 100
51+
assert repo_stats["public_dependents_number"] > MULTI_PACKAGE_REPO_PUBLIC_DEPENDENTS_MIN
4652
tmp_md_file = tempfile.gettempdir() + os.path.sep + str(uuid.uuid4()) + "-test-multiple.md"
4753
md = gh_deps_info.build_markdown(file=tmp_md_file)
48-
assert md.count("\n") > 100
54+
assert md.count("\n") > MULTI_PACKAGE_REPO_PUBLIC_DEPENDENTS_MIN
4955
with open(tmp_md_file, encoding="utf-8") as file:
5056
md_content = file.read()
51-
assert md_content.count("\n") > 100
57+
assert md_content.count("\n") > MULTI_PACKAGE_REPO_PUBLIC_DEPENDENTS_MIN
5258

5359

5460
def test_collect_stats_min_stars():
55-
repo = "nvuillam/npm-groovy-lint"
61+
repo = SINGLE_PACKAGE_REPO
5662
gh_deps_info = GithubDependentsInfo(repo, debug=True, sort_key="stars", min_stars=10)
5763
repo_stats = gh_deps_info.collect()
64+
assert repo_stats["public_dependents_number"] > 1
5865
assert repo_stats["public_dependents_number"] < 10
5966

6067

6168
def test_collect_csv():
62-
repo = "nvuillam/npm-groovy-lint"
69+
repo = SINGLE_PACKAGE_REPO
6370
with tempfile.TemporaryDirectory() as csv_directory:
6471
gh_deps_info = GithubDependentsInfo(
6572
repo, debug=True, sort_key="stars", min_stars=10, csv_directory=csv_directory
@@ -72,14 +79,18 @@ def test_collect_csv():
7279

7380

7481
def test_collect_csv_multi_package():
75-
repo = "oxsecurity/megalinter"
82+
repo = MULTI_PACKAGE_REPO
7683
with tempfile.TemporaryDirectory() as csv_directory:
7784
gh_deps_info = GithubDependentsInfo(
78-
repo, debug=True, sort_key="stars", min_stars=10, csv_directory=csv_directory
85+
repo, debug=True, sort_key="stars", min_stars=0, csv_directory=csv_directory
7986
)
8087
gh_deps_info.collect()
88+
assert len(gh_deps_info.packages) >= 2
8189
assert os.path.isfile(csv_directory + os.path.sep + f"packages_{repo.replace('/', '-')}.csv")
90+
packages_with_entries = 0
8291
for package in gh_deps_info.packages:
8392
if package["public_dependents_number"] <= 0:
8493
continue
94+
packages_with_entries += 1
8595
assert os.path.isfile(csv_directory + os.path.sep + f"dependents_{package['name'].replace('/', '-')}.csv")
96+
assert packages_with_entries >= 2

0 commit comments

Comments
 (0)