test_cli.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import logging
  2. import pathlib
  3. import unittest.mock
  4. import pytest
  5. from location_guessing_game_telegram_bot import _main
  6. @pytest.mark.parametrize(
  7. ("args", "env", "telegram_token_path", "wikimap_export_path"),
  8. (
  9. (
  10. [
  11. "--telegram-token-path",
  12. "/telegram/token.txt",
  13. "--wikimap-export-path",
  14. "/wikimap/export.json",
  15. ],
  16. {},
  17. "/telegram/token.txt",
  18. "/wikimap/export.json",
  19. ),
  20. (
  21. [
  22. "--telegram-token-path",
  23. "/telegram/token.txt",
  24. "--wikimap-export-path",
  25. "/wikimap/export.json",
  26. ],
  27. {
  28. "TELEGRAM_TOKEN_PATH": "overruled.txt",
  29. "WIKIMAP_EXPORT_PATH": "/ineffective.json",
  30. },
  31. "/telegram/token.txt",
  32. "/wikimap/export.json",
  33. ),
  34. (
  35. ["--wikimap-export-path", "/wikimap/export.json"],
  36. {
  37. "TELEGRAM_TOKEN_PATH": "/telegram/token-via-env.txt",
  38. "WIKIMAP_EXPORT_PATH": "/ineffective.json",
  39. },
  40. "/telegram/token-via-env.txt",
  41. "/wikimap/export.json",
  42. ),
  43. (
  44. [],
  45. {
  46. "TELEGRAM_TOKEN_PATH": "/telegram/token-via-env.txt",
  47. "WIKIMAP_EXPORT_PATH": "/export.json",
  48. },
  49. "/telegram/token-via-env.txt",
  50. "/export.json",
  51. ),
  52. ),
  53. )
  54. def test__main(args, env, telegram_token_path, wikimap_export_path):
  55. with unittest.mock.patch(
  56. "location_guessing_game_telegram_bot._run"
  57. ) as run_mock, unittest.mock.patch(
  58. "sys.argv", [""] + args
  59. ), unittest.mock.patch.dict(
  60. "os.environ", env
  61. ):
  62. _main()
  63. run_mock.assert_called_once_with(
  64. telegram_token_path=pathlib.Path(telegram_token_path),
  65. wikimap_export_path=pathlib.Path(wikimap_export_path),
  66. )
  67. @pytest.mark.parametrize(
  68. ("args", "root_log_level", "log_format"),
  69. (
  70. ([], logging.INFO, "%(message)s"),
  71. (
  72. ["--debug"],
  73. logging.DEBUG,
  74. "%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s",
  75. ),
  76. ),
  77. )
  78. def test_logging_config(args, root_log_level, log_format):
  79. with unittest.mock.patch(
  80. "location_guessing_game_telegram_bot._run"
  81. ) as run_mock, unittest.mock.patch(
  82. "sys.argv",
  83. ["", "--telegram-token-path", "/t", "--wikimap-export-path", "/w"] + args,
  84. ), unittest.mock.patch(
  85. "logging.basicConfig"
  86. ) as logging_basic_config_mock:
  87. _main()
  88. run_mock.assert_called_once_with(
  89. telegram_token_path=pathlib.Path("/t"), wikimap_export_path=pathlib.Path("/w")
  90. )
  91. logging_basic_config_mock.assert_called_once()
  92. assert logging_basic_config_mock.call_args[1]["level"] == root_log_level
  93. assert logging_basic_config_mock.call_args[1]["format"] == log_format