From c301aac3db70e9b8cfada688112cbcdc2d846cc2 Mon Sep 17 00:00:00 2001 From: William Lachance Date: Thu, 18 Jan 2018 13:34:51 -0500 Subject: [PATCH] Add a unit test to make sure all update_measure calls are scheduled --- pytest.ini | 2 +- setup.cfg | 14 +++++++++++++- tests/settings.py | 5 +++++ tests/test_etl_measure.py | 20 +++++++++++++++++++- 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 tests/settings.py diff --git a/pytest.ini b/pytest.ini index a5a2516..d88e217 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,4 +1,4 @@ [pytest] norecursedirs = .git .* static addopts = -rsxX --showlocals --tb=native --nomigrations --flake8 --cov-report term --cov-report xml --cov missioncontrol -DJANGO_SETTINGS_MODULE = missioncontrol.settings +DJANGO_SETTINGS_MODULE = tests.settings diff --git a/setup.cfg b/setup.cfg index 9fd5319..9370df2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,15 @@ [flake8] max-line-length=100 -exclude=missioncontrol/*/migrations/* +exclude= + # ignore the migrations since they are created faulty by default + missioncontrol/*/migrations/*, + # No use in checking the Node modules + node_modules/*/*/*, + # No need to traverse our git directory + .git, + # There's no value in checking cache directories + __pycache__, + # test settings uses a hacky import * import which we normally + # want to discourage but is convenient for this one specific + # purpose + tests/settings.py diff --git a/tests/settings.py b/tests/settings.py new file mode 100644 index 0000000..2ceddb0 --- /dev/null +++ b/tests/settings.py @@ -0,0 +1,5 @@ +from missioncontrol.settings import * + +# this makes celery calls synchronous, useful for unit testing +CELERY_ALWAYS_EAGER = True +CELERY_EAGER_PROPAGATES_EXCEPTIONS = True diff --git a/tests/test_etl_measure.py b/tests/test_etl_measure.py index 64bf754..00e4dee 100644 --- a/tests/test_etl_measure.py +++ b/tests/test_etl_measure.py @@ -3,8 +3,11 @@ import datetime import pytest from freezegun import freeze_time from dateutil.tz import tzutc +from unittest.mock import (call, patch) -from missioncontrol.base.models import Datum +from missioncontrol.base.models import (Channel, + Datum, + Measure) from missioncontrol.etl.date import datetime_to_utc @@ -88,3 +91,18 @@ def test_get_measure_summary(fake_measure_data, prepopulated_version_cache): 'version': None } } + + +def test_all_measure_update_tasks_scheduled(initial_data, *args): + # this test is a bit tautological, but at least exercises the function + expected_calls = [] + for channel_name in Channel.objects.values_list('name', flat=True): + for (measure_name, platform_name) in Measure.objects.exclude( + platform=None).values_list('name', 'platform__name'): + expected_calls.append(call(args=[platform_name, channel_name, + measure_name])) + + from missioncontrol.etl.tasks import update_measures + with patch('missioncontrol.etl.measure.update_measure.apply_async') as mock_task: + update_measures() + mock_task.assert_has_calls(expected_calls, any_order=True)