Skip to content

Add initial batch of unit tests for the React API#305

Merged
rgossiaux merged 3 commits into
masterfrom
rgossiaux/api-tests
Jan 8, 2021
Merged

Add initial batch of unit tests for the React API#305
rgossiaux merged 3 commits into
masterfrom
rgossiaux/api-tests

Conversation

@rgossiaux
Copy link
Copy Markdown
Contributor

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.

@rgossiaux rgossiaux force-pushed the rgossiaux/api-tests branch from bf9b2ba to 0baecfa Compare January 6, 2021 11:00
@rgossiaux
Copy link
Copy Markdown
Contributor Author

I think the test failure is legitimate -- @asdfryan in #300 it looks like you didn't handle the case when puzzle.chat_room is None

Copy link
Copy Markdown
Contributor

@erwa erwa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rgossiaux
Copy link
Copy Markdown
Contributor Author

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.

This was referenced Jan 7, 2021
Copy link
Copy Markdown
Contributor

@erwa erwa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding tests. Had a few comments.

Comment thread hunts/src/api.js

function getPuzzles(huntId) {
const puzzlesApiUrl = `/api/v1/hunt/${huntId}/puzzles`;
const puzzlesApiUrl = `/api/v1/hunts/${huntId}/puzzles`;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the common prefix "/api/v1/hunts" be defined as a constant in one place and then referenced elsewhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that's fine with me

Comment thread puzzles/tests.py Outdated
Comment thread api/tests.py
# 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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be uncommented after #309?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I linked to the wrong PR before, I meant #293

Comment thread api/tests.py Outdated
Comment on lines +5 to +7
if "unittest.util" in __import__("sys").modules:
# Show full diff in self.assertEqual.
__import__("sys").modules["unittest.util"]._MAX_LENGTH = 999999999
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you use two vs as in literally -vv for extra verbosity?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just delete it, probably simplest that way.

@asdfryan
Copy link
Copy Markdown
Collaborator

asdfryan commented Jan 7, 2021

I think the test failure is legitimate -- @asdfryan in #300 it looks like you didn't handle the case when puzzle.chat_room is None

Oops -- will fix this.

Edit: NVM looks like you already fixed.

@rgossiaux rgossiaux force-pushed the rgossiaux/api-tests branch from 10a7166 to 9f42b87 Compare January 8, 2021 05:35
@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 8, 2021

Codecov Report

Merging #305 (1611602) into master (5aa2024) will increase coverage by 5.43%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
smallboard/settings.py 89.04% <ø> (ø)
answers/tests.py 100.00% <100.00%> (ø)
api/serializers.py 95.12% <100.00%> (+26.82%) ⬆️
api/test_helpers.py 100.00% <100.00%> (ø)
api/tests.py 100.00% <100.00%> (ø)
api/urls.py 100.00% <100.00%> (ø)
api/views.py 89.61% <100.00%> (+63.96%) ⬆️
puzzles/tests.py 100.00% <100.00%> (ø)
puzzles/views.py 36.18% <0.00%> (-28.95%) ⬇️
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5aa2024...1611602. Read the comment docs.

@rawxfish rawxfish temporarily deployed to smallboard-rgossiaux-ap-d1gkrr January 8, 2021 05:38 Inactive
Copy link
Copy Markdown
Collaborator

@asdfryan asdfryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I commented some other edge cases worth testing. Feel free to merge this in first though. I can add those later.

Comment thread api/tests.py

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, {})
self.assertEqual(Puzzle.objects.count(), 0)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another edge case worth testing: Trying to delete a puzzle that has already been deleted

Comment thread api/tests.py
def test_edit_puzzle(self):
self.create_puzzle({"name": "test name", "url": TEST_URL})
puzzle = Puzzle.objects.get()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edge cases:

  • Editing more than 1 field at a time
  • Editing a non-editable field
  • New name matches a puzzle already in DB

Comment thread api/tests.py
self.assertEqual(puzzle.status, Puzzle.SOLVING)
self.assertEqual(len(puzzle.correct_answers()), 0)

def test_edit_answer(self):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edge cases:

  • Editing an answer to another answer that already exists
  • Special characters / emojis

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should definitely test these but I think the answers model tests do/should take care of these ones

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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
@rgossiaux rgossiaux force-pushed the rgossiaux/api-tests branch from 9f42b87 to 1611602 Compare January 8, 2021 08:23
@rawxfish rawxfish temporarily deployed to smallboard-rgossiaux-ap-d1gkrr January 8, 2021 08:24 Inactive
@rgossiaux rgossiaux merged commit 0e83739 into master Jan 8, 2021
@rgossiaux rgossiaux deleted the rgossiaux/api-tests branch January 8, 2021 08:26
@rgossiaux rgossiaux mentioned this pull request Jan 9, 2021
20 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants