test_cli.py 3.0 KB

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