Skip to content

Commit

Permalink
Merge pull request #54 from questionlp/develop
Browse files Browse the repository at this point in the history
Replace NamedTuple Cursors with Dictionary Cursors, Update wwdtm, Add Test Code Coverage
  • Loading branch information
questionlp authored Oct 6, 2024
2 parents b372a0f + a85eead commit 1a85758
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 44 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Changes

## 3.1.0

### Application Changes

- Replace all references of `named_tuple=` in database cursors to `dictionary=` due to cursors using `NamedTuple` being marked for deprecation in future versions of MySQL Connector/Python
- Update code that is impacted by the database cursor type change from `NamedTuple` to `dict`

### Component Changes

- Upgrade wwdtm from 2.11.0 to 2.12.1.post0

### Development Changes

- Add initial pytest coverage reporting using `pytest-cov`, which can be generated by running: `pytest --cov=app tests/`
- Correct naming of testing function for `robots.txt` route

## 3.0.2

### Application Notes
Expand Down
15 changes: 9 additions & 6 deletions app/reports/panel/aggregate_scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ def empty_score_spread(use_decimal_scores: bool = False) -> dict | None:
"MAX(pm.panelistscore_decimal) AS max "
"FROM ww_showpnlmap pm;"
)
cursor = database_connection.cursor(named_tuple=True)
cursor = database_connection.cursor(dictionary=True)
cursor.execute(query)
result = cursor.fetchone()

if not result:
return None

min_score = result.min
max_score = result.max
min_score = result["min"]
max_score = result["max"]

if use_decimal_scores:
score_spread = {}
Expand Down Expand Up @@ -78,7 +78,7 @@ def retrieve_score_spread(use_decimal_scores: bool = False) -> dict | None:
GROUP BY pm.panelistscore
ORDER BY pm.panelistscore ASC;
"""
cursor = database_connection.cursor(named_tuple=True)
cursor = database_connection.cursor(dictionary=True)
cursor.execute(query)
result = cursor.fetchall()
cursor.close()
Expand All @@ -89,6 +89,9 @@ def retrieve_score_spread(use_decimal_scores: bool = False) -> dict | None:

score_spread = empty_score_spread(use_decimal_scores=use_decimal_scores)
for row in result:
score_spread[row.score] = row.count
score_spread[row["score"]] = row["count"]

return {"scores": list(score_spread.keys()), "counts": list(score_spread.values())}
return {
"scores": list(score_spread.keys()),
"counts": list(score_spread.values()),
}
25 changes: 15 additions & 10 deletions app/reports/show/bluff_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def build_bluff_data_year_month_dict() -> dict | None:
GROUP BY YEAR(s.showdate), MONTH(s.showdate)
ORDER BY YEAR(s.showdate) ASC, MONTH(s.showdate) ASC;
"""
cursor = database_connection.cursor(named_tuple=True)
cursor = database_connection.cursor(dictionary=True)
cursor.execute(query)
result = cursor.fetchall()
cursor.close()
Expand All @@ -54,7 +54,10 @@ def build_bluff_data_year_month_dict() -> dict | None:
year_month = {}

for row in result:
year_month[f"{_months[row.month]} {row.year}"] = {"correct": 0, "incorrect": 0}
year_month[f"{_months[row['month']]} {row['year']}"] = {
"correct": 0,
"incorrect": 0,
}

return year_month

Expand Down Expand Up @@ -82,7 +85,7 @@ def retrieve_all_bluff_counts() -> dict | None:
GROUP BY year(s.showdate), month(s.showdate)
ORDER BY YEAR(s.showdate) ASC, MONTH(s.showdate) ASC;
"""
cursor = database_connection.cursor(named_tuple=True)
cursor = database_connection.cursor(dictionary=True)
cursor.execute(query)
correct_result = cursor.fetchall()

Expand All @@ -104,7 +107,7 @@ def retrieve_all_bluff_counts() -> dict | None:
GROUP BY year(s.showdate), month(s.showdate)
ORDER BY YEAR(s.showdate) ASC, MONTH(s.showdate) ASC;
"""
cursor = database_connection.cursor(named_tuple=True)
cursor = database_connection.cursor(dictionary=True)
cursor.execute(query)
incorrect_result = cursor.fetchall()
cursor.close()
Expand All @@ -115,10 +118,12 @@ def retrieve_all_bluff_counts() -> dict | None:

_months = month_mapping_dict()
for row in correct_result:
bluff_data[f"{_months[row.month]} {row.year}"]["correct"] = row.correct
bluff_data[f"{_months[row['month']]} {row['year']}"]["correct"] = row["correct"]

for row in incorrect_result:
bluff_data[f"{_months[row.month]} {row.year}"]["incorrect"] = row.incorrect
bluff_data[f"{_months[row['month']]} {row['year']}"]["incorrect"] = row[
"incorrect"
]

return bluff_data

Expand Down Expand Up @@ -147,7 +152,7 @@ def retrieve_bluff_count_year(year: int) -> dict | None:
GROUP BY YEAR(s.showdate), MONTH(s.showdate)
ORDER BY YEAR(s.showdate) ASC, MONTH(s.showdate) ASC;
"""
cursor = database_connection.cursor(named_tuple=True)
cursor = database_connection.cursor(dictionary=True)
cursor.execute(query, (year,))
correct_result = cursor.fetchall()

Expand All @@ -170,7 +175,7 @@ def retrieve_bluff_count_year(year: int) -> dict | None:
GROUP BY YEAR (s.showdate), MONTH(s.showdate)
ORDER BY YEAR(s.showdate) ASC, MONTH(s.showdate) ASC;
"""
cursor = database_connection.cursor(named_tuple=True)
cursor = database_connection.cursor(dictionary=True)
cursor.execute(query, (year,))
incorrect_result = cursor.fetchall()
cursor.close()
Expand All @@ -181,9 +186,9 @@ def retrieve_bluff_count_year(year: int) -> dict | None:

_months = month_mapping_dict()
for row in correct_result:
bluff_data[_months[row.month]]["correct"] = row.correct
bluff_data[_months[row["month"]]]["correct"] = row["correct"]

for row in incorrect_result:
bluff_data[_months[row.month]]["incorrect"] = row.incorrect
bluff_data[_months[row["month"]]]["incorrect"] = row["incorrect"]

return bluff_data
2 changes: 1 addition & 1 deletion app/reports/show/gender_mix.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def retrieve_panel_gender_count_by_year(year: int, gender: str) -> dict:
GROUP BY s.showdate
HAVING COUNT(p.panelistgender) = %s;
"""
cursor = database_connection.cursor(named_tuple=True)
cursor = database_connection.cursor(dictionary=True)
cursor.execute(
query,
(
Expand Down
12 changes: 6 additions & 6 deletions app/reports/show/scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def build_all_scoring_dict(use_decimal_scores: bool = False) -> dict | None:
HAVING SUM(pm.panelistscore) IS NOT NULL
ORDER BY YEAR(s.showdate);
"""
cursor = database_connection.cursor(named_tuple=True)
cursor = database_connection.cursor(dictionary=True)
cursor.execute(query)
result = cursor.fetchall()
cursor.close()
Expand All @@ -85,7 +85,7 @@ def build_all_scoring_dict(use_decimal_scores: bool = False) -> dict | None:

all_scores_dict = {}
for row in result:
all_scores_dict[row.year] = build_year_scoring_dict()
all_scores_dict[row["year"]] = build_year_scoring_dict()

return all_scores_dict

Expand Down Expand Up @@ -122,7 +122,7 @@ def retrieve_monthly_aggregate_scores(use_decimal_scores: bool = False) -> dict
HAVING SUM(pm.panelistscore) IS NOT NULL
ORDER BY YEAR(s.showdate) ASC, MONTH(s.showdate) ASC;
"""
cursor = database_connection.cursor(named_tuple=True)
cursor = database_connection.cursor(dictionary=True)
cursor.execute(query)
result = cursor.fetchall()
cursor.close()
Expand All @@ -134,7 +134,7 @@ def retrieve_monthly_aggregate_scores(use_decimal_scores: bool = False) -> dict
_months = month_mapping_dict()
all_scores_dict = build_all_scoring_dict()
for row in result:
all_scores_dict[row.year][_months[row.month]] = int(row.total)
all_scores_dict[row["year"]][_months[row["month"]]] = int(row["total"])

return all_scores_dict

Expand Down Expand Up @@ -171,7 +171,7 @@ def retrieve_monthly_average_scores(use_decimal_scores: bool = False) -> dict |
HAVING AVG(pm.panelistscore) IS NOT NULL
ORDER BY YEAR(s.showdate) ASC, MONTH(s.showdate) ASC;
"""
cursor = database_connection.cursor(named_tuple=True)
cursor = database_connection.cursor(dictionary=True)
cursor.execute(
query,
)
Expand All @@ -185,6 +185,6 @@ def retrieve_monthly_average_scores(use_decimal_scores: bool = False) -> dict |
_months = month_mapping_dict()
all_scores_dict = build_all_scoring_dict()
for row in result:
all_scores_dict[row.year][_months[row.month]] = float(row.average)
all_scores_dict[row["year"]][_months[row["month"]]] = float(row["average"])

return all_scores_dict
22 changes: 11 additions & 11 deletions app/reports/show/show_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def retrieve_show_counts_by_year() -> dict[int, int] | None:
FROM ww_shows
ORDER BY YEAR(showdate) ASC;
"""
cursor = database_connection.cursor(named_tuple=True)
cursor = database_connection.cursor(dictionary=True)
cursor.execute(query)
result = cursor.fetchall()
cursor.close()
Expand All @@ -30,7 +30,7 @@ def retrieve_show_counts_by_year() -> dict[int, int] | None:

years = []
for row in result:
years.append(int(row.year))
years.append(int(row["year"]))

show_counts = {}
for year in years:
Expand All @@ -57,7 +57,7 @@ def retrieve_show_counts_by_year() -> dict[int, int] | None:
AND bestof = 1 AND repeatshowid IS NOT NULL
) AS 'repeat_bestof';
"""
cursor = database_connection.cursor(named_tuple=True)
cursor = database_connection.cursor(dictionary=True)
cursor.execute(
query,
(
Expand All @@ -74,14 +74,14 @@ def retrieve_show_counts_by_year() -> dict[int, int] | None:
show_counts[year] = None
else:
counts = {
"regular": result.regular,
"best_of": result.bestof,
"repeat": result.repeat,
"repeat_best_of": result.repeat_bestof,
"total": result.regular
+ result.bestof
+ result.repeat
+ result.repeat_bestof,
"regular": result["regular"],
"best_of": result["bestof"],
"repeat": result["repeat"],
"repeat_best_of": result["repeat_bestof"],
"total": result["regular"]
+ result["bestof"]
+ result["repeat"]
+ result["repeat_bestof"],
}
show_counts[year] = counts

Expand Down
2 changes: 1 addition & 1 deletion app/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# vim: set noai syntax=python ts=4 sw=4:
"""Application Version for Wait Wait Graphs Site."""

APP_VERSION = "3.0.2"
APP_VERSION = "3.1.0"
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[tool.black]
required-version = "24.4.2"
required-version = "24.8.0"
target-version = ["py310", "py311", "py312"]
line-length = 88

[tool.pytest.ini_options]
minversion = "7.4"
minversion = "8.0"
filterwarnings = [
"ignore::DeprecationWarning:mysql.*:",
]
Expand Down
9 changes: 5 additions & 4 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
ruff==0.5.1
black==24.4.2
pytest==8.1.2
ruff==0.6.9
black==24.8.0
pytest==8.3.3
pytest-cov==5.0.0

Flask==3.0.3
gunicorn==23.0.0

wwdtm==2.10.1
wwdtm==2.12.1.post0
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Flask==3.0.3
gunicorn==23.0.0

wwdtm==2.10.1
wwdtm==2.12.1.post0
3 changes: 1 addition & 2 deletions tests/test_main_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ def test_index(client: FlaskClient) -> None:
assert b"/shows/" in response.data


def robots_txt(client: FlaskClient) -> None:
def test_robots_txt(client: FlaskClient) -> None:
"""Testing main.robots_txt."""
response: TestResponse = client.get("/robots.txt")
assert response.status_code == 200
assert b"Sitemap:" in response.data
assert b"User-agent:" in response.data

0 comments on commit 1a85758

Please sign in to comment.