1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-12-23 07:52:23 +00:00

more tests

This commit is contained in:
bleizi 2023-08-27 23:11:40 +02:00
parent 13b9b6edea
commit 1760196578
No known key found for this signature in database
GPG Key ID: D46D7E3364433208
3 changed files with 61 additions and 44 deletions

View File

@ -1,14 +1,11 @@
# Copyright (C) 2018-2023 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
# import time
from functools import lru_cache
# from random import Random
from django import forms
from django.db import transaction
from django.db.models import Q
# from django.utils.translation import gettext_lazy as _
from .base import WEISurvey, WEISurveyInformation, WEISurveyAlgorithm, WEIBusInformation
from ...models import WEIMembership
@ -48,12 +45,6 @@ class WEISurveyForm2023(forms.Form):
"""
information = WEISurveyInformation2023(registration)
# if self.data:
# for question in WORDS:
# self.fields[question].choices = [answer for answer in WORDS[question][1]]
# if self.is_valid():
# return
question = information.questions[information.step]
self.fields[question] = forms.ChoiceField(
label=WORDS[question][0] + question,
@ -86,14 +77,6 @@ class WEISurveyInformation2023(WEISurveyInformation):
questions = list(WORDS.keys())
def __init__(self, registration):
# print(hasattr(self,"questions"))
# if not hasattr(self, "questions"):
# rng = Random(int(1000 * time.time()))
# questions = list(WORDS.keys())
# rng.shuffle(questions)
# setattr(self, "questions", questions)
# print(questions)
for question in WORDS:
setattr(self, str(question), None)
super().__init__(registration)
@ -143,15 +126,6 @@ class WEISurvey2023(WEISurvey):
return False
return True
# @classmethod
# @lru_cache()
# def word_mean(cls, word):
# """
# Calculate the mid-score given by all buses.
# """
# buses = cls.get_algorithm_class().get_buses()
# return sum([cls.get_algorithm_class().get_bus_information(bus).scores[word] for bus in buses]) / buses.count()
@lru_cache()
def score(self, bus):
if not self.is_complete():
@ -176,7 +150,6 @@ class WEISurvey2023(WEISurvey):
@classmethod
def clear_cache(cls):
# cls.word_mean.cache_clear()
return super().clear_cache()

View File

@ -2,9 +2,12 @@
# SPDX-License-Identifier: GPL-3.0-or-later
import random
from datetime import date
from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import reverse
from note.models import NoteUser
from ..forms.surveys.wei2023 import WEIBusInformation2023, WEISurvey2023, WORDS, WEISurveyInformation2023
from ..models import Bus, WEIClub, WEIRegistration
@ -20,9 +23,25 @@ class TestWEIAlgorithm(TestCase):
"""
Create some test data, with one WEI and 10 buses with random score attributions.
"""
self.user = User.objects.create_superuser(
username="weiadmin",
password="admin",
email="admin@example.com",
)
self.user.save()
self.client.force_login(self.user)
sess = self.client.session
sess["permission_mask"] = 42
sess.save()
self.wei = WEIClub.objects.create(
name="WEI 2023",
email="wei2023@example.com",
parent_club_id=2,
membership_fee_paid=12500,
membership_fee_unpaid=5500,
membership_start='2023-08-26',
membership_end='2023-09-15',
date_start='2023-09-16',
date_end='2023-09-18',
year=2023,
@ -108,3 +127,44 @@ class TestWEIAlgorithm(TestCase):
self.assertLessEqual(max_score - score, 25) # Always less than 25 % of tolerance
self.assertLessEqual(penalty / 100, 25) # Tolerance of 5 %
def test_register_1a(self):
"""
Test register a first year member to the WEI and complete the survey
"""
response = self.client.get(reverse("wei:wei_register_1A", kwargs=dict(wei_pk=self.wei.pk)))
self.assertEqual(response.status_code, 200)
user = User.objects.create(username="toto", email="toto@example.com")
NoteUser.objects.create(user=user)
response = self.client.post(reverse("wei:wei_register_1A", kwargs=dict(wei_pk=self.wei.pk)), dict(
user=user.id,
soge_credit=True,
birth_date=date(2000, 1, 1),
gender='nonbinary',
clothing_cut='female',
clothing_size='XS',
health_issues='I am a bot',
emergency_contact_name='NoteKfet2020',
emergency_contact_phone='+33123456789',
))
qs = WEIRegistration.objects.filter(user_id=user.id)
self.assertTrue(qs.exists())
registration = qs.get()
self.assertRedirects(response, reverse("wei:wei_survey", kwargs=dict(pk=registration.pk)), 302, 200)
for question in WORDS:
# Fill 1A Survey, 20 pages
# be careful if questionnary form change (number of page, type of answer...)
response = self.client.post(reverse("wei:wei_survey", kwargs=dict(pk=registration.pk)), {
question: "1"
})
registration.refresh_from_db()
survey = WEISurvey2023(registration)
self.assertRedirects(response, reverse("wei:wei_survey", kwargs=dict(pk=registration.pk)), 302,
302 if survey.is_complete() else 200)
self.assertIsNotNone(getattr(survey.information, question), "Survey page " + question + " failed")
survey = WEISurvey2023(registration)
self.assertTrue(survey.is_complete())
survey.select_bus(self.buses[0])
survey.save()
self.assertIsNotNone(survey.information.get_selected_bus())

View File

@ -380,7 +380,7 @@ class TestWEIRegistration(TestCase):
def test_register_1a(self):
"""
Test register a first year member to the WEI and complete the survey.
Test register a first year member to the WEI.
"""
response = self.client.get(reverse("wei:wei_register_1A", kwargs=dict(wei_pk=self.wei.pk)))
self.assertEqual(response.status_code, 200)
@ -402,22 +402,6 @@ class TestWEIRegistration(TestCase):
self.assertTrue(qs.exists())
registration = qs.get()
self.assertRedirects(response, reverse("wei:wei_survey", kwargs=dict(pk=registration.pk)), 302, 200)
# for i in range(1, 21):
# # Fill 1A Survey, 20 pages
# # be careful if questionnary form change (number of page, type of answer...)
# response = self.client.post(reverse("wei:wei_survey", kwargs=dict(pk=registration.pk)), dict(
# word=1,
# ))
# registration.refresh_from_db()
# survey = CurrentSurvey(registration)
# self.assertRedirects(response, reverse("wei:wei_survey", kwargs=dict(pk=registration.pk)), 302,
# 302 if survey.is_complete() else 200)
# self.assertIsNotNone(getattr(survey.information, "word" + str(i)), "Survey page #" + str(i) + " failed")
# survey = CurrentSurvey(registration)
# self.assertTrue(survey.is_complete())
# survey.select_bus(self.bus)
# survey.save()
# self.assertIsNotNone(survey.information.get_selected_bus())
# Check that the user can't be registered twice
response = self.client.post(reverse("wei:wei_register_1A", kwargs=dict(wei_pk=self.wei.pk)), dict(