def test32_validated_breakpair_first_user(self): """test break pair simple case: a) login b) created NOT validated pàir c) break pair """ # create simple pair # with validated = False p = Pair(student1=self.user1, student2=self.user2, validated=True) p.save() pid = p.id # login 2 users self.loginTestUser(self.client1, self.user1) self.loginTestUser(self.client2, self.user2) self.assertIsNone(Pair.objects.get(pk=pid).studentBreakRequest) # POST call to BREAK_SERVICE # user1 data = {BREAK_FORM_LABEL: pid} self.client1.post(reverse(BREAK_SERVICE), data=data, follow=True) self.assertTrue(Pair.objects.filter(pk=pid).exists()) # check studentBreak is active self.assertTrue(Pair.objects.get(pk=pid).studentBreakRequest == self.user1) # user2 self.client2.post(reverse(BREAK_SERVICE), data=data, follow=True) # check pair has been deleted self.assertFalse(Pair.objects.filter(pk=pid).exists())
def test33_validated_breakpair_second_user(self): """As previous cse but user 2 beaks the pair """ # create simple pair # with validated = True p = Pair(student1=self.user1, student2=self.user2, validated=True) p.save() pid = p.id # login 2 users self.loginTestUser(self.client1, self.user1) self.loginTestUser(self.client2, self.user2) self.assertIsNone(Pair.objects.get(pk=pid).studentBreakRequest) # POST call to BREAK_SERVICE # user2 data = {BREAK_FORM_LABEL: pid} self.client2.post(reverse(BREAK_SERVICE), data=data, follow=True) self.assertTrue(Pair.objects.filter(pk=pid).exists()) # check studentBreak is active self.assertTrue(Pair.objects.get(pk=pid).studentBreakRequest == self.user2) # user1 self.client1.post(reverse(BREAK_SERVICE), data=data, follow=True) # check pair has been deleted self.assertFalse(Pair.objects.filter(pk=pid).exists())
def test31_novalidated_breakpair(self): """test break pair simple case: a) login b) created NOT validated pair c) break pair """ # create simple pair # with validated = False p = Pair(student1=self.user1, student2=self.user2) p.save() pid = p.id # login 1 user self.loginTestUser(self.client1, self.user1) # ask for break page response = self.client1.get(reverse(BREAK_SERVICE), follow=True) # check landing page is OK self.is_break_pair(response) # check list of users is OK m = re.search(self.user1.first_name, self.decode(response.content)) self.assertTrue(m) m = re.search(self.user2.first_name, self.decode(response.content)) self.assertTrue(m) m = re.search(self.user3.first_name, self.decode(response.content)) self.assertFalse(m) m = re.search(self.user4.first_name, self.decode(response.content)) self.assertFalse(m) # POST call to BREAK_SERVICE data = {BREAK_FORM_LABEL: pid} response = self.client1.post(reverse(BREAK_SERVICE), data=data, follow=True) self.assertFalse(Pair.objects.filter(pk=pid).exists())
def test05_log_in_apir(self): "login and validate login page when a Pair exists" # no student logged in self.assertFalse(self.client1.session.get(USER_SESSION_ID, False)) # create pair p = Pair(student1=self.user1, student2=self.user2) p.save() # log in response = self.client1.post(reverse(LOGIN_SERVICE), self.paramsUser1, follow=True) self.assertFalse(self.decode(response.content). find(self.user2.first_name) == -1)
def loader(request, template_name='core/loader.html'): methods = Method.objects.all() pairs = Pair.objects.all() questions = Question.objects.all() proposed_pairs = [] if request.method == 'POST': loader_form = LoaderForm(request.POST) if loader_form.is_valid(): text_pairs = filter( lambda x: len(x) == 2, map(lambda item: item.split(' '), loader_form.cleaned_data['text'].split('\r\n'))) def get_method(key): for method in methods: if method.short_name.upper() == key.upper(): return method return None key_pairs = { k: get_method(k) for k in set([item for lst in text_pairs for item in lst]) } proposed_pairs = filter(lambda x: x['left'] and x['right'], [{ 'left': key_pairs[item[0]], 'right': key_pairs[item[1]] } for item in text_pairs]) added_pairs = map(lambda x: Pair(left=x['left'], right=x['right']), proposed_pairs) for pair in added_pairs: pair.save() stats = [{ 'method': method, 'history': questions.filter(Q(left=method) | Q(right=method)).count(), 'future': pairs.filter(Q(left=method) | Q(right=method)).count(), } for method in methods] loader_form = LoaderForm() return render( request, template_name, { 'loader_form': loader_form, 'request': request, 'e_methods_h': list(enumerate(methods)), 'e_methods_v': list(enumerate(methods)), 'stats': stats, 'proposed_pairs': proposed_pairs, })
def test13_convalidation_member1_pair(self): """try to convalidate when you are member of a pair as student1""" self.loginTestUser(self.client1, self.user1) p = Pair(student1=self.user1, student2=self.user2) p.save() self.user1.gradeTheoryLastYear = 4.0 self.user1.gradeLabLastYear = 8 self.user1.save() # refresh user1 self.user1 = Student.objects.get(pk=self.user1.id) self.assertFalse(self.user1.convalidationGranted) self.client1.get(reverse(CONVALIDATION_SERVICE), follow=True) self.assertFalse(self.user1.convalidationGranted) Pair.objects.all().delete() self.client1.get(reverse(CONVALIDATION_SERVICE), follow=True) # refresh user1 self.user1 = Student.objects.get(pk=self.user1.id) self.assertTrue(self.user1.convalidationGranted)
def test34_validated_breakpair_user_twice_requested(self): """user 1 and user 2 has requested user 3 as pair user 3 has accepted user 2 request break all pairs """ # first pair with validated = False p = Pair(student1=self.user1, student2=self.user3, validated=False) p.save() p1id = p.id # second pair with validated = True p = Pair(student1=self.user2, student2=self.user3, validated=True) p.save() p2id = p.id # # login 3 users self.loginTestUser(self.client1, self.user1) self.loginTestUser(self.client2, self.user2) self.loginTestUser(self.client3, self.user3) # # assert pairs self.assertIsNone(Pair.objects.get(pk=p1id).studentBreakRequest) self.assertIsNone(Pair.objects.get(pk=p2id).studentBreakRequest) # POST call to BREAK_SERVICE # user2 tries to break second pair data = {BREAK_FORM_LABEL: p2id} self.client2.post(reverse(BREAK_SERVICE), data=data, follow=True) # but will not be broken until user3 breaks it too self.assertTrue(Pair.objects.filter(pk=p2id).exists()) # check studentBreak is active self.assertTrue(Pair.objects.get(pk=p2id).studentBreakRequest == self.user2) # user 1 break pair, since it is not validated is OK data = {BREAK_FORM_LABEL: p1id} self.client1.post(reverse(BREAK_SERVICE), data=data, follow=True) # check pair has been deleted self.assertFalse(Pair.objects.filter(pk=p1id).exists()) # finally user 3 breaks second pair data = {BREAK_FORM_LABEL: p2id} self.client3.post(reverse(BREAK_SERVICE), data=data, follow=True) # check pair has been deleted self.assertFalse(Pair.objects.filter(pk=p2id).exists())
def test_pair_1000_1001(self): print("\nTest02") student1 = Student.objects.get(pk=1000) student2 = Student.objects.get(id=1001) t = Pair.objects.create(id=123, student1=student1, student2=student2, validated=False) t.save() print("\nPair created!\n") pairs = Pair.objects.all().filter(student1=student1) for p in pairs: print(Pair.__str__(p)) p.validated = True p.save()
def test14_convalidation_member1_pair(self): """try to convalidate when you are member of a pair as student2 and the pair is convalidated""" self.loginTestUser(self.client2, self.user2) p = Pair(student1=self.user1, student2=self.user2, validated=True) p.save() self.user2.gradeTheoryLastYear = 4.0 self.user2.gradeLabLastYear = 8 self.user2.save() # refresh user2 self.user2 = Student.objects.get(pk=self.user2.id) self.assertFalse(self.user2.convalidationGranted) self.client2.get(reverse(CONVALIDATION_SERVICE), follow=True) self.assertFalse(self.user2.convalidationGranted) p.validated = False p.save() self.client2.get(reverse(CONVALIDATION_SERVICE), follow=True) # refresh user2 self.user2 = Student.objects.get(pk=self.user2.id) self.assertTrue(self.user2.convalidationGranted)
def test_43_select_group_pair_user2_request(self): """Select group for pairs""" # login self.loginTestUser(self.client2, self.user2) # set all theory groups tgQS = TheoryGroup.objects.all() self.user1.theoryGroup = tgQS.first() self.user1.save() # set othercostraint.selectGroupStart to now o = OtherConstraints.objects.all().first() now = datetime.datetime.now() now = timezone.make_aware(now, timezone.get_current_timezone()) o.selectGroupStartDate = now o.save() # pick valid groups # for each theory group p = Pair(student1=self.user1, student2=self.user2, validated=True) p.save() for theoryGroup in tgQS: print(theoryGroup.groupName) # user2 group is irrelevant self.user2.theoryGroup = theoryGroup self.user2.save() # connect to create group page as user2 response = self.client2.get(reverse(GROUP_SERVICE), follow=True) # check labgroups offered are right gcQS = GroupConstraints.objects.filter(theoryGroup=theoryGroup) for groupConstraint in gcQS: print(" ", groupConstraint.labGroup.groupName) # print(groupConstraint.labGroup.groupName) # print(self.decode(response.content)) m = re.search(groupConstraint.labGroup.groupName, self.decode(response.content)) self.assertTrue(m, "%s labgroup not found in response" % groupConstraint.labGroup.groupName) # try to create all groups with all possible combination # Theory group, labgroup. some should be invalid # use html page instead of function to select groups tgQS = TheoryGroup.objects.all() lgQS = LabGroup.objects.all() for theoryGroup in tgQS: for labGroup in lgQS: self.user2.theoryGroup = theoryGroup # reset labgroup self.user2.labGroup = None self.user2.save() self.user1.labGroup = None self.user1.save() labGroupId = labGroup.id labGroupTmp0 = LabGroup.objects.get(pk=labGroupId) counter = labGroupTmp0.counter data = {GROUP_FORM_LABEL: labGroupId} self.client2.post(reverse(GROUP_SERVICE), data=data, follow=True) valid = GroupConstraints.objects.filter( theoryGroup=theoryGroup, labGroup=labGroup).exists() if valid: self.assertTrue(Student.objects.filter( pk=self.user1.id, labGroup=labGroup).exists(), "user2 has not labGroup assigned") self.assertTrue(Student.objects.filter( pk=self.user2.id, labGroup=labGroup).exists(), "user2 has not labGroup assigned") labGroupTmp1 = LabGroup.objects.get(pk=labGroupId) self.assertEqual(labGroupTmp1.counter, counter+2) else: self.assertFalse(Student.objects.filter( pk=self.user1.id, labGroup=labGroup).exists()) self.assertFalse(Student.objects.filter( pk=self.user2.id, labGroup=labGroup).exists()) labGroupTmp2 = LabGroup.objects.get(pk=labGroupId) self.assertEqual(labGroupTmp2.counter, counter)
def generate_pairs(number): methods = Method.objects.all() for _ in xrange(int(number)): random2 = random.sample(methods, 2) pair = Pair(left=random2[0], right=random2[1]) pair.save()