-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix unpacking message, default to modelform fields if no fieldsets, a…
…dd tests and testapp
- Loading branch information
Showing
14 changed files
with
579 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
local_date,label,country | ||
2017-01-02,Public Holiday,botswana | ||
2017-04-14,Good Friday,botswana | ||
2017-04-15,Public Holiday,botswana | ||
2017-04-17,Easter Monday,botswana | ||
2017-05-01,May Day/Labour Day,botswana | ||
2017-05-25,Ascension Day,botswana | ||
2017-07-01,Sir Seretse Khama Day,botswana | ||
2017-07-17,President's Day,botswana | ||
2017-07-18,Public Holiday,botswana | ||
2017-09-30,Botswana Day,botswana | ||
2017-10-02,Public Holiday,botswana | ||
2017-12-25,Christmas Day,botswana | ||
2017-12-26,Boxing Day,botswana | ||
2017-01-01,New Year,botswana |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
from datetime import datetime | ||
from uuid import uuid4 | ||
from zoneinfo import ZoneInfo | ||
|
||
import time_machine | ||
from django.core.exceptions import ObjectDoesNotExist | ||
from django.test import TestCase, override_settings | ||
from edc_appointment.models import Appointment | ||
from edc_appointment.tests.helper import Helper | ||
from edc_consent.site_consents import site_consents | ||
from edc_facility import import_holidays | ||
from edc_visit_schedule.site_visit_schedules import site_visit_schedules | ||
from edc_visit_tracking.constants import SCHEDULED | ||
from edc_visit_tracking.models import SubjectVisit | ||
|
||
from edc_form_runners.exceptions import FormRunnerModelFormNotFound | ||
from edc_form_runners.form_runner import FormRunner | ||
from edc_form_runners.models import Issue | ||
from form_runners_app.consents import consent_v1 | ||
from form_runners_app.models import Member, Team, TeamWithDifferentFields, Venue | ||
from form_runners_app.visit_schedules import visit_schedule | ||
|
||
|
||
@override_settings(SITE_ID=10) | ||
@time_machine.travel(datetime(2019, 6, 11, 8, 00, tzinfo=ZoneInfo("UTC"))) | ||
class TestRunners(TestCase): | ||
|
||
helper_cls = Helper | ||
|
||
@classmethod | ||
def setUpTestData(cls): | ||
import_holidays() | ||
site_consents.registry = {} | ||
site_consents.register(consent_v1) | ||
|
||
def setUp(self): | ||
site_consents.registry = {} | ||
site_consents.register(consent_v1) | ||
site_visit_schedules._registry = {} | ||
|
||
site_visit_schedules.loaded = False | ||
site_visit_schedules.register(visit_schedule) | ||
|
||
self.subject_identifier = "1235" | ||
self.helper = self.helper_cls(subject_identifier=self.subject_identifier) | ||
self.visit_schedule_name = "visit_schedule" | ||
self.schedule_name = "schedule" | ||
|
||
self.helper.consent_and_put_on_schedule( | ||
visit_schedule_name="visit_schedule", schedule_name="schedule" | ||
) | ||
|
||
def test_appointment(self): | ||
form_runner = FormRunner(model_name="edc_appointment.appointment") | ||
form_runner.run_all() | ||
|
||
def test_crfs(self): | ||
for appointment in Appointment.objects.all().order_by("timepoint_datetime"): | ||
subject_visit = SubjectVisit.objects.create( | ||
appointment=appointment, | ||
subject_identifier=self.subject_identifier, | ||
reason=SCHEDULED, | ||
) | ||
TeamWithDifferentFields.objects.create(subject_visit=subject_visit, size=11) | ||
Venue.objects.create(subject_visit=subject_visit, name=uuid4()) | ||
team = Team.objects.create(subject_visit=subject_visit, name=uuid4()) | ||
Member.objects.create(team=team) | ||
Member.objects.create(team=team) | ||
Member.objects.create(team=team) | ||
|
||
# raise on VenueModelAdmin has no custom ModelForm | ||
self.assertRaises( | ||
FormRunnerModelFormNotFound, FormRunner, model_name="form_runners_app.venue" | ||
) | ||
|
||
# run to find name field may not be a UUID | ||
# see form validator | ||
form_runner = FormRunner(model_name="form_runners_app.team") | ||
form_runner.run_all() | ||
self.assertEqual(Issue.objects.all().count(), 1) | ||
try: | ||
Issue.objects.get( | ||
label_lower="form_runners_app.team", | ||
visit_code="1000", | ||
visit_code_sequence=0, | ||
field_name="name", | ||
message__icontains="Cannot be a UUID", | ||
) | ||
except ObjectDoesNotExist: | ||
self.fail("Issue model instance unexpectedly does not exist") | ||
|
||
# run to find player_name field may not be a UUID | ||
# see form validator | ||
form_runner = FormRunner(model_name="form_runners_app.member") | ||
form_runner.run_all() | ||
try: | ||
Issue.objects.get( | ||
label_lower="form_runners_app.member", | ||
visit_code="1000", | ||
visit_code_sequence=0, | ||
field_name="player_name", | ||
message__icontains="Cannot be a UUID", | ||
) | ||
except ObjectDoesNotExist: | ||
self.fail("Issue model instance unexpectedly does not exist") | ||
|
||
# run to assert ignores `name` field because it IS NOT IN admin fieldsets | ||
# even though the model instance field class has blank=False. | ||
form_runner = FormRunner(model_name="form_runners_app.teamwithdifferentfields") | ||
form_runner.run_all() | ||
try: | ||
Issue.objects.get( | ||
label_lower="form_runners_app.teamwithdifferentfields", | ||
field_name="name", | ||
) | ||
except ObjectDoesNotExist: | ||
pass | ||
else: | ||
self.fail("Issue model instance unexpectedly does not exist") | ||
# assert does not ignore `color` field because it IS IN admin fieldsets | ||
# and the model instance field class has blank=False. | ||
try: | ||
Issue.objects.get( | ||
label_lower="form_runners_app.teamwithdifferentfields", | ||
field_name="color", | ||
) | ||
except ObjectDoesNotExist: | ||
self.fail("Issue model instance unexpectedly does not exist") |
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from django.contrib import admin | ||
from edc_model_admin.admin_site import EdcAdminSite | ||
from edc_model_admin.dashboard import ( | ||
ModelAdminCrfDashboardMixin, | ||
ModelAdminDashboardMixin, | ||
) | ||
from edc_model_admin.history import SimpleHistoryAdmin | ||
from edc_sites.admin import SiteModelAdminMixin | ||
|
||
from .forms import MemberForm, TeamForm, TeamWithDifferentFieldsForm | ||
from .models import Member, Team, TeamWithDifferentFields, Venue | ||
|
||
my_admin_site = EdcAdminSite(name="my_admin_site") | ||
|
||
|
||
class MemberInlineAdmin(admin.TabularInline): | ||
model = Member | ||
form = MemberForm | ||
|
||
extra = 0 | ||
|
||
|
||
@admin.register(Member, site=my_admin_site) | ||
class MembersAdmin(SiteModelAdminMixin, ModelAdminDashboardMixin, SimpleHistoryAdmin): | ||
form = MemberForm | ||
|
||
|
||
@admin.register(Team, site=my_admin_site) | ||
class TeamAdmin(SiteModelAdminMixin, ModelAdminCrfDashboardMixin, SimpleHistoryAdmin): | ||
form = TeamForm | ||
|
||
inlines = [MemberInlineAdmin] | ||
|
||
fieldsets = ((None, ({"fields": ("name", "created", "modified")})),) | ||
|
||
|
||
@admin.register(TeamWithDifferentFields, site=my_admin_site) | ||
class TeamWithDifferentFieldsAdmin( | ||
SiteModelAdminMixin, ModelAdminCrfDashboardMixin, SimpleHistoryAdmin | ||
): | ||
form = TeamWithDifferentFieldsForm | ||
|
||
# do not include "name" to show that the field is ignored | ||
# by FormRunner.run even though blank=False | ||
fieldsets = ((None, ({"fields": ("size", "color", "mood")})),) | ||
|
||
|
||
@admin.register(Venue, site=my_admin_site) | ||
class VenueAdmin(SiteModelAdminMixin, ModelAdminDashboardMixin, SimpleHistoryAdmin): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig as DjangoAppConfig | ||
|
||
|
||
class AppConfig(DjangoAppConfig): | ||
name = "form_runners_app" | ||
verbose_name = "form_runners test app" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from edc_consent.consent_definition import ConsentDefinition | ||
from edc_consent.site_consents import site_consents | ||
from edc_constants.constants import FEMALE, MALE | ||
from edc_protocol.research_protocol_config import ResearchProtocolConfig | ||
|
||
consent_v1 = ConsentDefinition( | ||
"form_runners_app.subjectconsent", | ||
version="1", | ||
start=ResearchProtocolConfig().study_open_datetime, | ||
end=ResearchProtocolConfig().study_close_datetime, | ||
age_min=18, | ||
age_is_adult=18, | ||
age_max=64, | ||
gender=[MALE, FEMALE], | ||
) | ||
site_consents.register(consent_v1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from django import forms | ||
from edc_form_validators import INVALID_ERROR, FormValidator, FormValidatorMixin | ||
|
||
from .models import Member, Team, TeamWithDifferentFields | ||
|
||
|
||
class MemberFormValidator(FormValidator): | ||
def clean(self) -> None: | ||
if self.cleaned_data.get("player_name") != "not-a-uuid": | ||
self.raise_validation_error({"player_name": "Cannot be a UUID"}, INVALID_ERROR) | ||
|
||
|
||
class TeamFormValidator(FormValidator): | ||
def clean(self) -> None: | ||
if self.cleaned_data.get("name") != "not-a-uuid": | ||
self.raise_validation_error({"name": "Cannot be a UUID"}, INVALID_ERROR) | ||
|
||
|
||
class MemberForm(FormValidatorMixin, forms.ModelForm): | ||
form_validator_cls = MemberFormValidator | ||
|
||
class Meta: | ||
model = Member | ||
fields = "__all__" | ||
|
||
|
||
class TeamForm(FormValidatorMixin, forms.ModelForm): | ||
form_validator_cls = TeamFormValidator | ||
|
||
class Meta: | ||
model = Team | ||
fields = "__all__" | ||
|
||
|
||
class TeamWithDifferentFieldsForm(FormValidatorMixin, forms.ModelForm): | ||
form_validator_cls = None | ||
|
||
def clean(self): | ||
if self.cleaned_data.get("name") != "not-a-uuid": | ||
raise forms.ValidationError({"name": "Cannot be a UUID"}, INVALID_ERROR) | ||
|
||
class Meta: | ||
model = TeamWithDifferentFields | ||
fields = "__all__" |
Oops, something went wrong.