Skip to content

Commit

Permalink
Fix/registration bug (#152)
Browse files Browse the repository at this point in the history
* added test to reproduce bug

* avoid checking relations for a new Participant that's not yet saved

* removed test skip
  • Loading branch information
bbonf authored Sep 9, 2024
1 parent a7bd97f commit 9f219dd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
10 changes: 6 additions & 4 deletions api/utils/make_appointment.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,11 @@ def _handle_specific_criteria(
messages.append(specific_criterion.message_failed)
continue

existing_answer = participant.criterionanswer_set.filter(
criterion=specific_criterion.criterion
).first()
existing_answer = None
if participant.pk:
existing_answer = participant.criterionanswer_set.filter(
criterion=specific_criterion.criterion
).first()

value = data.get(name_form, None)

Expand Down Expand Up @@ -429,7 +431,7 @@ def _handle_excluded_experiments(
:return:
"""
invalid_experiments = []
appointments = participant.appointments.all()
appointments = participant.appointments.all() if participant.pk else []
participated_experiments = [x.experiment for x in appointments]

for excluded_experiment in experiment.excluded_experiments.all():
Expand Down
15 changes: 15 additions & 0 deletions experiments/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from api.auth.models import ApiUser
from leaders.models import Leader
from ..models import Location

def _get_or_create_leader() -> Leader:
if Leader.objects.exists():
return Leader.objects.first()
user = ApiUser.objects.create()
return Leader.objects.create(api_user=user)


def _get_or_create_location() -> Location:
if Location.objects.exists():
return Location.objects.first()
return Location.objects.create(name="Cyberspace")
21 changes: 3 additions & 18 deletions experiments/tests.py → experiments/tests/test_exclusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,10 @@
from dateutil.relativedelta import relativedelta
from django.test import TestCase

from api.auth.models import ApiUser
from leaders.models import Leader
from . import _get_or_create_leader, _get_or_create_location
from experiments.models import Experiment, Criterion, ExperimentCriterion, Appointment, TimeSlot
from experiments.utils.exclusion import get_eligible_participants_for_experiment
from participants.models import Participant, CriterionAnswer
from .models import Experiment, Criterion, ExperimentCriterion, Appointment, \
Location, TimeSlot
from .utils.exclusion import get_eligible_participants_for_experiment


def _get_or_create_leader() -> Leader:
if Leader.objects.exists():
return Leader.objects.first()
user = ApiUser.objects.create()
return Leader.objects.create(api_user=user)


def _get_or_create_location() -> Location:
if Location.objects.exists():
return Location.objects.first()
return Location.objects.create(name="Cyberspace")


class ExclusionTests(TestCase):
Expand Down
37 changes: 37 additions & 0 deletions experiments/tests/test_registration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from unittest import skip
from api.auth.models import ApiUser
from api.utils.make_appointment import register_participant

from django.test import TestCase

from experiments.models import Experiment
from experiments.models.criteria_models import Criterion
from leaders.models import Leader
from main.models import User
from participants.models import Participant


class RegistrationTests(TestCase):
def setUp(self):
admin = User.objects.create(is_supreme_admin=True)
self.pp = Participant(
email='[email protected]',
dyslexic=False,
language='nl'
)
leader_user = ApiUser.objects.create(email='[email protected]')
leader = Leader.objects.create(api_user=leader_user)
self.exp = Experiment.objects.create(leader=leader, default_max_places=10, use_timeslots=False)

def test_new_participant(self):
success, _, messages = register_participant(dict(email=self.pp.email, specific_criteria=[], language='nl'), self.exp)
assert success, messages

def test_new_participant_specific_criteria(self):
criterion = Criterion.objects.create(name_form='test_crit', values='yes,no')
self.exp.experimentcriterion_set.create(criterion=criterion, correct_value='yes')
self.exp.save()
specific_criteria = [dict(name='test_crit', value='0')]
success, _, messages = register_participant(dict(email=self.pp.email, specific_criteria=specific_criteria, language='nl'),
self.exp)
assert success, messages

0 comments on commit 9f219dd

Please sign in to comment.