From 733c71a43d8f352e62609b625d47bfd0983905ad Mon Sep 17 00:00:00 2001 From: ctmbl Date: Wed, 18 Oct 2023 20:31:40 +0200 Subject: [PATCH 1/3] Fix bug when parsing position of a user with 0 points and position empty --- src/classes/user.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/classes/user.py b/src/classes/user.py index ac11afd..27d5867 100644 --- a/src/classes/user.py +++ b/src/classes/user.py @@ -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 nb_solves = len(data["validations"]) keys = User.keys() From 894ee52c033211d6685069849b9800527f249f43 Mon Sep 17 00:00:00 2001 From: ctmbl Date: Thu, 26 Oct 2023 23:43:42 +0200 Subject: [PATCH 2/3] Add a regression test --- tests/data/rootme_api_example_data.py | 12 ++++++++++++ tests/test_dummy_db_manager.py | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/tests/data/rootme_api_example_data.py b/tests/data/rootme_api_example_data.py index 6a18be5..ad67a11 100644 --- a/tests/data/rootme_api_example_data.py +++ b/tests/data/rootme_api_example_data.py @@ -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 = [ diff --git a/tests/test_dummy_db_manager.py b/tests/test_dummy_db_manager.py index 707a47c..4da0a83 100644 --- a/tests/test_dummy_db_manager.py +++ b/tests/test_dummy_db_manager.py @@ -1,6 +1,9 @@ import pytest from classes import User +from bot.dummy_db_manager import DummyDBManager + +from data.rootme_api_example_data import auteurs_with_score_zero_example_data @pytest.mark.asyncio @@ -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 From dcf839eb374350ec624de848c4dd578fa821bf6a Mon Sep 17 00:00:00 2001 From: ctmbl Date: Thu, 26 Oct 2023 23:50:35 +0200 Subject: [PATCH 3/3] Fix pylint --- tests/test_dummy_db_manager.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_dummy_db_manager.py b/tests/test_dummy_db_manager.py index 4da0a83..b7c7a94 100644 --- a/tests/test_dummy_db_manager.py +++ b/tests/test_dummy_db_manager.py @@ -1,10 +1,10 @@ import pytest -from classes import User -from bot.dummy_db_manager import DummyDBManager - from data.rootme_api_example_data import auteurs_with_score_zero_example_data +from bot.dummy_db_manager import DummyDBManager +from classes import User + @pytest.mark.asyncio async def test_add_user(mock_dummy_db_manager):