Add initial batch of unit tests for the React API#305
Conversation
bf9b2ba to
0baecfa
Compare
erwa
left a comment
There was a problem hiding this comment.
I haven't looked closely at the tests you added, but just a thought: do we want to add JavaScript tests that directly test the React/Redux code? I think that would add additional robustness on top of testing the Python code.
|
Eventually, yes. I don't want to spend the time on them before mystery hunt though because I think there are other more critical things to do, but I'm hoping to get to it after this mystery hunt. Added #307 to track. |
erwa
left a comment
There was a problem hiding this comment.
Thanks for adding tests. Had a few comments.
|
|
||
| function getPuzzles(huntId) { | ||
| const puzzlesApiUrl = `/api/v1/hunt/${huntId}/puzzles`; | ||
| const puzzlesApiUrl = `/api/v1/hunts/${huntId}/puzzles`; |
There was a problem hiding this comment.
Can the common prefix "/api/v1/hunts" be defined as a constant in one place and then referenced elsewhere?
There was a problem hiding this comment.
Yes. Actually I think the URL scheme is kind of bad, there's no need for the /hunts/:id/ prefix on most of the endpoints
There was a problem hiding this comment.
I'm gonna update this in another PR after this because otherwise I think the merge conflicts will get a little messy. But I think I want to move:
/hunts/:id/puzzles/:id/tags => /puzzles/:id/tags
/hunts/:id/puzzles/:id/tags/:id => /puzzles/:id/tags/:id
/hunts/:id/puzzles/:id/answers => /puzzles/:id/answers
/hunts/:id/puzzles/:id/answers/:id => /puzzles/:id/answers/:id
Hopefully that's a little more readable. Then I'll change this api.js file to use a common /api/v1/ prefix
| # Malformatted URL (TODO) | ||
| # response = self.create_puzzle( | ||
| # {"name": "test name", "url": "bad_url", "is_meta": False} | ||
| # ) | ||
| # self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
There was a problem hiding this comment.
Yeah, that was the idea / why I included this. If I merge this PR first (likely) I'll update the other one to uncomment this.
There was a problem hiding this comment.
Oops, I linked to the wrong PR before, I meant #293
| if "unittest.util" in __import__("sys").modules: | ||
| # Show full diff in self.assertEqual. | ||
| __import__("sys").modules["unittest.util"]._MAX_LENGTH = 999999999 |
There was a problem hiding this comment.
This seems a little strange to include here. I think you can just add -vv when running ./manage.py test to get more info including the full diff. I think it'd be better to increase the verbosity when running tests so it applies to all tests rather than making the change just for these api tests.
There was a problem hiding this comment.
Oh nice, I didn't know about -v. But I think that doesn't prevent the test output from being truncated:
======================================================================
FAIL: test_create_puzzle (api.tests.ApiTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/src/smallboard/api/tests.py", line 49, in test_create_puzzle
self.assertEqual(
AssertionError: {'id'[137 chars]G', 'tags': [], 'guesses': [], 'metas': [], 'f[25 chars]alse} != {'id'[137 chars]G', 'guesses': [], 'metas': [], 'feeders': [],[13 chars]alse}
Since this runs during import I believe it applies to all tests as long as you're including api/tests.py but it wouldn't apply if you ran eg test answers, so I could move it somewhere else (maybe api/test_helpers.py?)
There was a problem hiding this comment.
What if you use two vs as in literally -vv for extra verbosity?
There was a problem hiding this comment.
usage: manage.py test [-h] [--noinput] [--failfast] [--testrunner TESTRUNNER] [-t TOP_LEVEL]
[-p PATTERN] [--keepdb] [-r] [--debug-mode] [-d] [--parallel [N]]
[--tag TAGS] [--exclude-tag EXCLUDE_TAGS] [--pdb] [-b]
[-k TEST_NAME_PATTERNS] [--version] [-v {0,1,2,3}]
[--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback]
[--no-color] [--force-color]
[test_label [test_label ...]]
manage.py test: error: argument -v/--verbosity: invalid int value: 'v'
The previous comment was with the most permissive verbosity level (3).
There was a problem hiding this comment.
I see... I am surprised at unittest's behavior.
I wonder if you can move this unittest configuration setup to the root directory and have it always be picked up?
If not, I would prefer not changing the MAX_LENGTH at all, since I feel this sort of configuration should be done globally (and put in a global place if so) rather than done in a specific test, even if it applies globally. But if you feel strongly for including it, I'm fine with keeping it, too.
There was a problem hiding this comment.
I'll just delete it, probably simplest that way.
10a7166 to
9f42b87
Compare
Codecov Report
@@ Coverage Diff @@
## master #305 +/- ##
==========================================
+ Coverage 77.79% 83.23% +5.43%
==========================================
Files 100 102 +2
Lines 1950 2099 +149
==========================================
+ Hits 1517 1747 +230
+ Misses 433 352 -81
Continue to review full report at Codecov.
|
asdfryan
left a comment
There was a problem hiding this comment.
Thanks! I commented some other edge cases worth testing. Feel free to merge this in first though. I can add those later.
|
|
||
| self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
| self.assertEqual(response.data, {}) | ||
| self.assertEqual(Puzzle.objects.count(), 0) |
There was a problem hiding this comment.
Another edge case worth testing: Trying to delete a puzzle that has already been deleted
| def test_edit_puzzle(self): | ||
| self.create_puzzle({"name": "test name", "url": TEST_URL}) | ||
| puzzle = Puzzle.objects.get() | ||
|
|
There was a problem hiding this comment.
Edge cases:
- Editing more than 1 field at a time
- Editing a non-editable field
- New name matches a puzzle already in DB
| self.assertEqual(puzzle.status, Puzzle.SOLVING) | ||
| self.assertEqual(len(puzzle.correct_answers()), 0) | ||
|
|
||
| def test_edit_answer(self): |
There was a problem hiding this comment.
Edge cases:
- Editing an answer to another answer that already exists
- Special characters / emojis
There was a problem hiding this comment.
We should definitely test these but I think the answers model tests do/should take care of these ones
There was a problem hiding this comment.
(though from the API side I think ideally we should still test that the API returns reasonable responses in these cases)
Renaming hunt => hunts and answer => answers for consistency and tweaking some small pieces of code.
These are still pretty imperfect/incomplete but it's a start
9f42b87 to
1611602
Compare
These could be improved / made more complete but I think it's a start, and it's more testing than the old endpoints had anyway.