Skip to content

Commit

Permalink
Clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kaapstorm committed Jan 11, 2025
1 parent 0015c2e commit 2915095
Showing 1 changed file with 13 additions and 98 deletions.
111 changes: 13 additions & 98 deletions corehq/motech/repeaters/tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
from collections import namedtuple
from contextlib import contextmanager
from datetime import datetime, timedelta
from unittest.mock import MagicMock, patch
from uuid import uuid4

from django.test import SimpleTestCase, TestCase

from nose.tools import assert_equal
from freezegun import freeze_time

from corehq.apps.receiverwrapper.util import submit_form_locally
from corehq.form_processor.models import XFormInstance
from corehq.form_processor.utils.xform import (
FormSubmissionBuilder,
TestFormMetadata,
)
from corehq.motech.models import ConnectionSettings, RequestLog
from corehq.motech.repeaters.models import FormRepeater, Repeater, RepeatRecord
from corehq.motech.repeaters.tasks import (
from corehq.util.test_utils import flag_enabled

from ..const import State
from ..models import FormRepeater, Repeater, RepeatRecord
from ..tasks import (
_get_wait_duration_seconds,
_process_repeat_record,
delete_old_request_logs,
get_repeater_ids_by_domain,
iter_ready_repeater_ids,
process_repeater,
update_repeater,
)
from corehq.util.test_utils import _create_case, flag_enabled

from ..const import State

DOMAIN = 'gaidhlig'
PAYLOAD_IDS = ['aon', 'dha', 'trì', 'ceithir', 'coig', 'sia', 'seachd', 'ochd',
'naoi', 'deich']
DOMAIN = 'test-tasks'


ResponseMock = namedtuple('ResponseMock', 'status_code reason')
Expand Down Expand Up @@ -79,20 +68,6 @@ def test_num_queries_chunked(self):
self.assertEqual(count, 0)


@contextmanager
def form_context(form_ids):
for form_id in form_ids:
builder = FormSubmissionBuilder(
form_id=form_id,
metadata=TestFormMetadata(domain=DOMAIN),
)
submit_form_locally(builder.as_xml_string(), DOMAIN)
try:
yield
finally:
XFormInstance.objects.hard_delete_forms(DOMAIN, form_ids)


class TestProcessRepeatRecord(TestCase):

def test_returns_if_record_is_cancelled(self):
Expand Down Expand Up @@ -256,7 +231,7 @@ def patch(self):
self.addCleanup(patch_domain_can_forward.stop)


def test_iter_ready_repeater_ids_once():
def test_iter_ready_repeater_ids():
with (
patch(
'corehq.motech.repeaters.tasks.Repeater.objects.get_all_ready_ids_by_domain',
Expand All @@ -272,7 +247,7 @@ def test_iter_ready_repeater_ids_once():
),
):
pairs = list(iter_ready_repeater_ids())
assert_equal(pairs, [
assert pairs == [
# First round of domains
('domain1', 'repeater_id3'),
('domain2', 'repeater_id5'),
Expand All @@ -284,7 +259,7 @@ def test_iter_ready_repeater_ids_once():

# Third round
('domain1', 'repeater_id1'),
])
]


def test_get_repeater_ids_by_domain():
Expand All @@ -306,71 +281,10 @@ def test_get_repeater_ids_by_domain():
side_effect=lambda dom, __: dom == 'domain3'),
):
repeater_ids_by_domain = get_repeater_ids_by_domain()
assert_equal(repeater_ids_by_domain, {
assert repeater_ids_by_domain == {
'domain2': ['repeater_id4', 'repeater_id5'],
'domain3': ['repeater_id6'],
})


@flag_enabled('PROCESS_REPEATERS')
class TestProcessRepeater(TestCase):

@classmethod
def setUpClass(cls):
super().setUpClass()

can_forward_now_patch = patch(
'corehq.motech.repeaters.tasks.domain_can_forward_now',
return_value=True,
)
can_forward_now_patch = can_forward_now_patch.start()
cls.addClassCleanup(can_forward_now_patch.stop)

cls.set_backoff_patch = patch.object(FormRepeater, 'set_backoff')
cls.set_backoff_patch.start()
cls.addClassCleanup(cls.set_backoff_patch.stop)

connection_settings = ConnectionSettings.objects.create(
domain=DOMAIN,
url='http://www.example.com/api/'
)
cls.repeater = FormRepeater.objects.create(
domain=DOMAIN,
connection_settings=connection_settings,
)

def test_process_repeater_sends_repeat_record(self):
payload, __ = _create_case(
domain=DOMAIN,
case_id=str(uuid4()),
case_type='case',
owner_id='abc123'
)
self.repeater.register(payload)

with patch('corehq.motech.repeaters.models.simple_request') as request_mock:
request_mock.return_value = ResponseMock(status_code=200, reason='OK')
process_repeater(DOMAIN, self.repeater.repeater_id, 'token')

request_mock.assert_called_once()

def test_process_repeater_updates_repeater(self):
payload, __ = _create_case(
domain=DOMAIN,
case_id=str(uuid4()),
case_type='case',
owner_id='abc123'
)
self.repeater.register(payload)

with patch('corehq.motech.repeaters.models.simple_request') as request_mock:
request_mock.return_value = ResponseMock(
status_code=429,
reason='Too Many Requests',
)
process_repeater(DOMAIN, self.repeater.repeater_id, 'token')

self.repeater.set_backoff.assert_called_once()
}


@flag_enabled('PROCESS_REPEATERS')
Expand Down Expand Up @@ -412,6 +326,7 @@ def test_update_repeater_does_nothing_on_none(self):
mock_repeater.reset_backoff.assert_not_called()


@freeze_time('2025-01-01')
class TestGetWaitDurationSeconds(TestCase):

@classmethod
Expand Down

0 comments on commit 2915095

Please sign in to comment.