Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when parsing position of a user with 0 points and position empty #38

Merged
merged 3 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/classes/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def parse_rootme_user_data(data):
idx = int(data["id_auteur"])
username = data["nom"]
score = int(data["score"])
rank = int(data["position"])
_rank = data["position"]
rank = int(_rank) if _rank != "" else 9999999
ZynoXelek marked this conversation as resolved.
Show resolved Hide resolved
nb_solves = len(data["validations"])

keys = User.keys()
Expand Down
12 changes: 12 additions & 0 deletions tests/data/rootme_api_example_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@
],
}

# This is not the real output for /auteurs/819227 on October, 26th 2023
auteurs_with_score_zero_example_data = {
"id_auteur": "819227",
"nom": "Alphaphi",
"statut": "6forum",
"score": "0",
"position": "",
"challenges": [],
"solutions": [],
"validations": [],
}


# the real output for /challenges/5 on May, 30th 2023
challenges_example_data = [
Expand Down
23 changes: 23 additions & 0 deletions tests/test_dummy_db_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest

from data.rootme_api_example_data import auteurs_with_score_zero_example_data

from bot.dummy_db_manager import DummyDBManager
from classes import User


Expand Down Expand Up @@ -58,3 +61,23 @@ async def test_get_user(mock_dummy_db_manager):
got_user = db.get_user(1)

assert added_user is got_user


@pytest.mark.asyncio
async def test_add_user_with_empty_position(mock_rootme_api_manager):
# Regression test introduced with https://github.com/iScsc/RootPythia/pull/38
# the position was always parsed as an int however if the user's score is 0
# the root me api returns an empty position, causing the bug
# this test ensures this bug doesn't reappear

# Create an api manager returning the right data, and the associated db object
api_manager = mock_rootme_api_manager
api_manager.get_user_by_id.return_value = auteurs_with_score_zero_example_data
db = DummyDBManager(api_manager)

# Trigger test
added_user = await db.add_user(819227)

assert added_user.score == 0
assert added_user.nb_solves == 0
assert added_user.rank == 9999999 # default value, will probably have to be updated