Skip to content

Commit

Permalink
Merge pull request dimagi#31319 from dimagi/dm/django3-abstract-models
Browse files Browse the repository at this point in the history
TypeError: Abstract models cannot be instantiated
  • Loading branch information
millerdev authored Mar 25, 2022
2 parents f67e0be + 45465a5 commit c24ac00
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
23 changes: 20 additions & 3 deletions corehq/messaging/scheduling/tests/test_content.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from unittest.mock import Mock

from django.test import TestCase, override_settings

from corehq.apps.domain.shortcuts import create_domain
from corehq.apps.sms.forms import (
LANGUAGE_FALLBACK_NONE,
Expand All @@ -6,15 +10,18 @@
LANGUAGE_FALLBACK_UNTRANSLATED,
)
from corehq.apps.users.models import CommCareUser
from corehq.messaging.scheduling.models import Schedule, Content, CustomContent
from corehq.messaging.scheduling.models import (
Content as AbstractContent,
CustomContent,
Schedule as AbstractSchedule,
)
from corehq.messaging.scheduling.scheduling_partitioned.models import (
AlertScheduleInstance,
TimedScheduleInstance,
CaseAlertScheduleInstance,
CaseTimedScheduleInstance,
)
from django.test import TestCase, override_settings
from unittest.mock import Mock
from corehq.util.test_utils import unregistered_django_model


AVAILABLE_CUSTOM_SCHEDULING_CONTENT = {
Expand Down Expand Up @@ -204,3 +211,13 @@ def test_sms_language_fallback(self):
content.get_translation_from_message_dict(self.domain_obj, message_dict, user_lang),
message_dict['*']
)


@unregistered_django_model
class Content(AbstractContent):
pass


@unregistered_django_model
class Schedule(AbstractSchedule):
pass
13 changes: 11 additions & 2 deletions corehq/messaging/scheduling/tests/test_recipients.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@
from corehq.messaging.scheduling.scheduling_partitioned.models import (
CaseScheduleInstanceMixin,
CaseTimedScheduleInstance,
ScheduleInstance,
ScheduleInstance as AbstractScheduleInstance,
)
from corehq.messaging.scheduling.tests.util import delete_timed_schedules
from corehq.util.test_utils import create_test_case, set_parent_case
from corehq.util.test_utils import (
create_test_case,
set_parent_case,
unregistered_django_model,
)
from testapps.test_pillowtop.utils import process_pillow_changes


Expand Down Expand Up @@ -844,3 +848,8 @@ def test_phone_number_preference(self):
self.assertPhoneEntryCount(3)
self.assertPhoneEntryCount(1, only_count_two_way=True)
self.assertTwoWayEntry(Content.get_two_way_entry_or_phone_number(user), '23456')


@unregistered_django_model
class ScheduleInstance(AbstractScheduleInstance):
pass
17 changes: 17 additions & 0 deletions corehq/util/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from time import sleep, time
from unittest import SkipTest, TestCase

from django.apps import apps
from django.conf import settings
from django.db import connections
from django.db.backends import utils
Expand Down Expand Up @@ -347,6 +348,22 @@ def get_output(self):
return self.output.getvalue()


def unregistered_django_model(model_class):
"""Model class decorator that unregisters the model from Django
Apply to model classes in test modules to prevent the models from
being seen by other tests that check registered models. Examples
of tests that check registered models include
- corehq.apps.domain.tests.test_deletion_models:test_deletion_sql_models
- corehq.sql_db.tests.test_model_partitioning
:TestPartitionedModelsWithMultipleDBs
.test_models_are_located_in_correct_dbs('scheduling', False)
"""
app_config = apps.get_app_config(model_class._meta.app_label)
del app_config.models[model_class.__name__.lower()]
return model_class


def generate_cases(argsets, cls=None):
"""Make a decorator to generate a set of parameterized test cases
Expand Down

0 comments on commit c24ac00

Please sign in to comment.