-
Notifications
You must be signed in to change notification settings - Fork 0
π§ͺ Add unit tests for hydrogen_coupled_saw_propulsion in QAG-recordpropulsuon.pynb #2
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -664,6 +664,47 @@ | |
| "metadata": {} | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "metadata": { | ||
| "id": "unittest-hydrogen-coupled-saw" | ||
| }, | ||
| "source": [ | ||
| "import numpy as np\n", | ||
| "\n", | ||
| "def test_hydrogen_coupled_saw_propulsion():\n", | ||
| " print(\"Running UNIT TESTS for hydrogen_coupled_saw_propulsion...\")\n", | ||
| " \n", | ||
| " # Test scalar input (or single element array)\n", | ||
| " t_single = np.array([0.0])\n", | ||
| " res_single = hydrogen_coupled_saw_propulsion(t_single)\n", | ||
| " # sin(0) is 0, so result should be 0\n", | ||
| " assert np.isclose(res_single[0], 0.0), f\"Expected 0.0 at t=0, got {res_single[0]}\"\n", | ||
| " \n", | ||
| " # Test array input\n", | ||
| " t_arr = np.linspace(0, 10, 100)\n", | ||
| " res_arr = hydrogen_coupled_saw_propulsion(t_arr)\n", | ||
| " assert len(res_arr) == 100, \"Result array length mismatch\"\n", | ||
| " \n", | ||
| " # Test known value calculation\n", | ||
| " # psychon_mass_ug = 5400\n", | ||
| " # golden_freq_mhz = 0.70\n", | ||
| " # omega = 2 * np.pi * 0.70\n", | ||
| " # t = 1.0\n", | ||
| " # expected = 5400 * np.sin(omega * 1.0) * np.exp(0.05 * 1.0)\n", | ||
| " t_val = np.array([1.0])\n", | ||
| " res_val = hydrogen_coupled_saw_propulsion(t_val)\n", | ||
| " expected_val = 5400 * np.sin(2 * np.pi * 0.70 * 1.0) * np.exp(0.05 * 1.0)\n", | ||
| " assert np.isclose(res_val[0], expected_val), f\"Value mismatch at t=1.0: {res_val[0]} != {expected_val}\"\n", | ||
| " \n", | ||
| " print(\"ALL UNIT TESTS PASSED!\")\n", | ||
| "\n", | ||
| "if __name__ == \"__main__\":\n", | ||
| " test_hydrogen_coupled_saw_propulsion()" | ||
|
Comment on lines
+676
to
+704
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Placing multiple independent tests within a single test function can be problematic. If an early assertion fails, the rest of the function is skipped, and subsequent tests are not executed. This can mask other failures. To improve test isolation and ensure all cases are checked, it's better to separate each logical test into its own function (e.g., |
||
| ], | ||
| "execution_count": null, | ||
| "outputs": [] | ||
| } | ||
| ] | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test for a known value currently re-implements the logic from the
hydrogen_coupled_saw_propulsionfunction. This is a testing anti-pattern, as a logical error in the original function could be replicated in the test, causing the test to pass incorrectly. It also duplicates magic numbers (5400,0.70,0.05), which makes the code harder to maintain. Tests are more robust when they validate against a pre-calculated, known-good value. This ensures the test is independent of the implementation.