From 32af2ed3d8e4fb8bcd089cbe96547a577d1612c1 Mon Sep 17 00:00:00 2001 From: diligejy Date: Sat, 20 Jan 2024 09:39:39 +0900 Subject: [PATCH] [Refactor] Prepare for easy change --- .../Test_Driven_Pandas/tests/test_gradebook.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/PyCon_PL/Test_Driven_Pandas/tests/test_gradebook.py b/PyCon_PL/Test_Driven_Pandas/tests/test_gradebook.py index 9526adf..5baefda 100644 --- a/PyCon_PL/Test_Driven_Pandas/tests/test_gradebook.py +++ b/PyCon_PL/Test_Driven_Pandas/tests/test_gradebook.py @@ -3,21 +3,24 @@ from typing import cast def _create_group(raw_students_group:pd.DataFrame) -> pd.DataFrame: - result = pd.DataFrame() + result = pd.DataFrame(index=raw_students_group.index) result = result.assign( - net_id = raw_students_group["NetID"].str.lower(), + net_id = raw_students_group.index, email_address = raw_students_group['Email Address'].str.lower(), ) result[['last_name', 'first_name']] = raw_students_group['Name'].str.split(", ", expand=True) return result def generate_gradebook(students_df:pd.DataFrame) -> dict[int, pd.DataFrame]: + print( + 'This is index', students_df.index) + students_df.index = students_df.index.str.lower() return { cast(int, group): _create_group(raw_students_group=table) for group, table in students_df.groupby("Group") } -def test_results_are_grouped_by_student_group(): +def test_results_are_grouped_by_student_group_for_students_in_one_group(): students = [{ "ID" : 1, "Name" : "Doe, John", @@ -25,7 +28,7 @@ def test_results_are_grouped_by_student_group(): "Email Address" : "JOHN.DOE@EXAMPLE.EDU", "Group" : 1, }] - students_df = pd.DataFrame(data=students).set_index("ID") + students_df = pd.DataFrame(data=students).set_index("NetID") result = generate_gradebook(students_df=students_df) assert list(result.keys()) == [1] @@ -50,7 +53,7 @@ def two_students_in_the_same_group() -> pd.DataFrame(): "Group": 1, } ] - return pd.DataFrame(data=students).set_index("ID") + return pd.DataFrame(data=students).set_index("NetID") def test_results_are_grouped_by_student_group_for_students_in_multiple_group(): students = [{ @@ -66,7 +69,7 @@ def test_results_are_grouped_by_student_group_for_students_in_multiple_group(): "Email Address" : "ALEC.CURRY@EXAMPLE.EDU", "Group" : 2, }] - students_df = pd.DataFrame(data=students).set_index("ID") + students_df = pd.DataFrame(data=students).set_index("NetID") result = generate_gradebook(students_df=students_df) assert list(result.keys()) == [1, 2]