/tests/test_flask_get.py

https://github.com/fredrik-corneliusson/click-web · Python · 74 lines · 60 code · 14 blank · 0 comment · 1 complexity · 023b67de6d2bf63e8e35da0ee3f4917a MD5 · raw file

  1. import pytest
  2. from bs4 import BeautifulSoup
  3. def test_get_index(app, client):
  4. resp = client.get('/')
  5. assert resp.status_code == 200
  6. assert b'the root command' in resp.data
  7. @pytest.mark.parametrize(
  8. 'command_path, response_code, expected_msg, expected_form_ids',
  9. [
  10. ('/cli/simple-no-params-command', 200, b'>Simple-No-Params-Command</',
  11. [
  12. '0.0.flag.bool_flag.1.checkbox.--debug'
  13. ]),
  14. ('/cli/unicode-test', 200, 'Åäö'.encode('utf-8'),
  15. [
  16. '0.0.flag.bool_flag.1.checkbox.--debug',
  17. '1.0.option.choice.1.option.--unicode-msg'
  18. ]
  19. ),
  20. ('/cli/command-with-option-and-argument', 200, b'>Command-With-Option-And-Argument</',
  21. [
  22. '0.0.flag.bool_flag.1.checkbox.--debug',
  23. '1.0.option.text.1.text.--an-option',
  24. '1.1.argument.int.1.number.an-argument'
  25. ]),
  26. ('/cli/sub-group/a-sub-group-command', 200, b'>A-Sub-Group-Command</',
  27. [
  28. '0.0.flag.bool_flag.1.checkbox.--debug'
  29. ]),
  30. ('/cli/command-with-input-folder', 200, b'>Command-With-Input-Folder</',
  31. [
  32. '0.0.flag.bool_flag.1.checkbox.--debug',
  33. '1.0.argument.path[r].1.file.folder'
  34. ]),
  35. ('/cli/command-with-output-folder', 200, b'>Command-With-Output-Folder</',
  36. [
  37. '0.0.flag.bool_flag.1.checkbox.--debug',
  38. '1.0.argument.path[w].1.hidden.folder']),
  39. ('/cli/command-with-flag-without-off-option', 200, b'>Command-With-Flag-Without-Off-Option</',
  40. [
  41. '0.0.flag.bool_flag.1.checkbox.--debug',
  42. '1.0.flag.bool_flag.1.checkbox.--flag']),
  43. ('/cli/command-with-variadic-args', 200, b'>Command-With-Variadic-Args</',
  44. [
  45. '0.0.flag.bool_flag.1.checkbox.--debug',
  46. '1.0.argument.text.-1.text.users']),
  47. ]
  48. )
  49. def test_get_command(command_path, response_code, expected_msg, expected_form_ids, app, client):
  50. resp = client.get(command_path)
  51. form_ids = _get_form_ids(resp.data)
  52. print(form_ids)
  53. print(resp.data)
  54. assert resp.status_code == response_code
  55. assert expected_msg in resp.data
  56. assert expected_form_ids == form_ids
  57. def _get_form_ids(html):
  58. soup = BeautifulSoup(html, 'html.parser')
  59. form_ids = [elem['name'] for elem in soup.find_all(['input', 'select', 'textarea'])]
  60. return form_ids