Skip to content

Commit 59b1c30

Browse files
committed
writing better tests
1 parent 01faef4 commit 59b1c30

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

tutorial/tests/test_30_introduction_data_exploration.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,11 @@ def test_add_regional_indicator(input_arg, function_to_test):
160160
clean_sol = function_to_test(cleaned_happiness_df, region_df)
161161

162162
assert isinstance(clean_sol, pd.DataFrame), "Your function should return a pd.DataFrame, but it returned None. Did you forget the return statement?"
163-
# Check if the two DataFrames are equal
164-
assert clean_ref.equals(clean_sol)
163+
# Sort both DataFrames to ensure order-independent comparison
164+
sort_cols = ["Country name", "year"]
165+
clean_ref_sorted = clean_ref.sort_values(by=sort_cols).reset_index(drop=True)
166+
clean_sol_sorted = clean_sol.sort_values(by=sort_cols).reset_index(drop=True)
167+
assert clean_ref_sorted.equals(clean_sol_sorted)
165168

166169

167170
# solution_frames_with_category
@@ -273,8 +276,14 @@ def test_frames_with_category(input_arg, function_to_test):
273276

274277
assert isinstance(clean_sol, dict), "Your function should return a dict, but it returned None. Did you forget the return statement?"
275278
assert "data" in clean_sol and "name" in clean_sol, "The returned dict should have 'data' and 'name' keys"
276-
# Check if the two are equal
277-
assert clean_ref == clean_sol
279+
assert clean_ref["name"] == clean_sol["name"], f"Frame name mismatch: expected '{clean_ref['name']}', got '{clean_sol['name']}'"
280+
# Compare traces regardless of category order
281+
ref_traces = sorted(clean_ref["data"], key=lambda t: t["name"])
282+
sol_traces = sorted(clean_sol["data"], key=lambda t: t["name"])
283+
assert len(ref_traces) == len(sol_traces), f"Expected {len(ref_traces)} traces, got {len(sol_traces)}"
284+
for ref_t, sol_t in zip(ref_traces, sol_traces):
285+
assert ref_t["name"] == sol_t["name"], f"Trace name mismatch: expected '{ref_t['name']}', got '{sol_t['name']}'"
286+
assert ref_t == sol_t, f"Trace data mismatch for category '{ref_t['name']}'"
278287

279288
from plotly.offline import iplot
280289

0 commit comments

Comments
 (0)