aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_api.py
diff options
context:
space:
mode:
authorGravatar Sebastiaan Zeeff <[email protected]>2019-10-02 16:59:03 +0200
committerGravatar Sebastiaan Zeeff <[email protected]>2019-10-11 17:42:21 +0200
commitc4213744c18be23e3e4484f126ae0b2d0eba4437 (patch)
treefa26b8d115eac7b9d46fd2abae966c3030f32e78 /tests/test_api.py
parentMerge pull request #505 from python-discord/user-log-display-name-changes (diff)
Migrate pytest to unittest
After a discussion in the core developers channel, we have decided to migrate from `pytest` to `unittest` as the testing framework. This commit sets up the repository to use `unittest` and migrates the first couple of tests files to the new framework. What I have done to migrate to `unitest`: - Removed all `pytest` test files, since they are incompatible. - Removed `pytest`-related dependencies from the Pipfile. - Added `coverage.py` to the Pipfile dev-packages and relocked. - Added convenience scripts to Pipfile for running the test suite. - Adjust to `azure-pipelines.yml` to use `coverage.py` and `unittest`. - Migrated four test files from `pytest` to `unittest` format. In addition, I've added five helper Mock subclasses in `helpers.py` and created a `TestCase` subclass in `base.py` to add an assertion that asserts that no log records were logged within the context of the context manager. Obviously, these new utility functions and classes are fully tested in their respective `test_` files. Finally, I've started with an introductory guide for writing tests for our bot in `README.md`.
Diffstat (limited to 'tests/test_api.py')
-rw-r--r--tests/test_api.py106
1 files changed, 0 insertions, 106 deletions
diff --git a/tests/test_api.py b/tests/test_api.py
deleted file mode 100644
index ce69ef187..000000000
--- a/tests/test_api.py
+++ /dev/null
@@ -1,106 +0,0 @@
-import logging
-from unittest.mock import MagicMock, patch
-
-import pytest
-
-from bot import api
-from tests.helpers import async_test
-
-
-def test_loop_is_not_running_by_default():
- assert not api.loop_is_running()
-
-
-@async_test
-async def test_loop_is_running_in_async_test():
- assert api.loop_is_running()
-
-
-def error_api_response():
- response = MagicMock()
- response.status = 999
- return response
-
-
-def api_log_handler():
- return api.APILoggingHandler(None)
-
-
-def debug_log_record():
- return logging.LogRecord(
- name='my.logger', level=logging.DEBUG,
- pathname='my/logger.py', lineno=666,
- msg="Lemon wins", args=(),
- exc_info=None
- )
-
-
-def test_response_code_error_default_initialization(error_api_response):
- error = api.ResponseCodeError(response=error_api_response)
- assert error.status is error_api_response.status
- assert not error.response_json
- assert not error.response_text
- assert error.response is error_api_response
-
-
-def test_response_code_error_default_representation(error_api_response):
- error = api.ResponseCodeError(response=error_api_response)
- assert str(error) == f"Status: {error_api_response.status} Response: "
-
-
-def test_response_code_error_representation_with_nonempty_response_json(error_api_response):
- error = api.ResponseCodeError(
- response=error_api_response,
- response_json={'hello': 'world'}
- )
- assert str(error) == f"Status: {error_api_response.status} Response: {{'hello': 'world'}}"
-
-
-def test_response_code_error_representation_with_nonempty_response_text(error_api_response):
- error = api.ResponseCodeError(
- response=error_api_response,
- response_text='Lemon will eat your soul'
- )
- assert str(error) == f"Status: {error_api_response.status} Response: Lemon will eat your soul"
-
-
-@patch('bot.api.APILoggingHandler.ship_off')
-def test_emit_appends_to_queue_with_stopped_event_loop(
- ship_off_patch, api_log_handler, debug_log_record
-):
- # This is a coroutine so returns something we should await,
- # but asyncio complains about that. To ease testing, we patch
- # `ship_off` to just return a regular value instead.
- ship_off_patch.return_value = 42
- api_log_handler.emit(debug_log_record)
-
- assert api_log_handler.queue == [42]
-
-
-def test_emit_ignores_less_than_debug(debug_log_record, api_log_handler):
- debug_log_record.levelno = logging.DEBUG - 5
- api_log_handler.emit(debug_log_record)
- assert not api_log_handler.queue
-
-
-def test_schedule_queued_tasks_for_empty_queue(api_log_handler, caplog):
- api_log_handler.schedule_queued_tasks()
- # Logs when tasks are scheduled
- assert not caplog.records
-
-
-@patch('asyncio.create_task')
-def test_schedule_queued_tasks_for_nonempty_queue(create_task_patch, api_log_handler, caplog):
- api_log_handler.queue = [555]
- api_log_handler.schedule_queued_tasks()
- assert not api_log_handler.queue
- create_task_patch.assert_called_once_with(555)
-
- [record] = caplog.records
- assert record.message == "Scheduled 1 pending logging tasks."
- assert record.levelno == logging.DEBUG
- assert record.name == 'bot.api'
- assert record.__dict__['via_handler']