from rest_framework import status from rest_framework.test import APIClient, APITestCase from core.models import (GeneralTaskSubscription, Submission, SubmissionType, SubscriptionEnded) from util.factories import GradyUserFactory, make_test_data class GeneralTaskSubscriptionRandomTest(APITestCase): @classmethod def setUpTestData(cls): cls.user_factory = GradyUserFactory() def setUp(self): self.t = self.user_factory.make_tutor() self.s1 = self.user_factory.make_student() self.s2 = self.user_factory.make_student() self.submission_type = SubmissionType.objects.create( name='submission_01', full_score=14) self.submission_01 = Submission.objects.create( type=self.submission_type, student=self.s1.student, text='I really failed') self.submission_02 = Submission.objects.create( type=self.submission_type, student=self.s2.student, text='I like apples') self.subscription = GeneralTaskSubscription.objects.create( owner=self.t, query_type=GeneralTaskSubscription.RANDOM) def test_subscription_gets_an_assignment(self): self.subscription._create_new_assignment_if_subscription_empty() self.assertEqual(1, self.subscription.assignments.count()) def test_first_work_assignment_was_created_unfinished(self): self.subscription._create_new_assignment_if_subscription_empty() self.assertFalse(self.subscription.assignments.first().is_done) def test_subscription_raises_error_when_depleted(self): self.submission_01.delete() self.submission_02.delete() try: self.subscription._create_new_assignment_if_subscription_empty() except SubscriptionEnded as err: self.assertFalse(False) else: self.assertTrue(False) def test_can_prefetch(self): self.subscription._create_new_assignment_if_subscription_empty() self.subscription._eagerly_reserve_the_next_assignment() self.assertEqual(2, self.subscription.assignments.count()) def test_oldest_assignment_is_current(self): assignment = self.subscription.get_oldest_unfinished_assignment() self.assertEqual(assignment, self.subscription.get_oldest_unfinished_assignment()) class TestApiEndpoints(APITestCase): @classmethod def setUpTestData(cls): cls.data = make_test_data(data_dict={ 'submission_types': [ { 'name': '01. Sort this or that', 'full_score': 35, 'description': 'Very complicated', 'solution': 'Trivial!' }, { 'name': '02. Merge this or that or maybe even this', 'full_score': 35, 'description': 'Very complicated', 'solution': 'Trivial!' }, { 'name': '03. This one exists for the sole purpose to test', 'full_score': 30, 'description': 'Very complicated', 'solution': 'Trivial!' } ], 'students': [ {'username': 'student01'}, {'username': 'student02'} ], 'tutors': [ {'username': 'tutor01'}, {'username': 'tutor02'} ], 'submissions': [ { 'text': 'function blabl\n' ' on multi lines\n' ' for blabla in bla:\n' ' lorem ipsum und so\n', 'type': '01. Sort this or that', 'user': 'student01', 'feedback': { 'text': 'Not good!', 'score': 5, 'of_tutor': 'tutor01', 'is_final': True } }, { 'text': 'function blabl\n' ' asasxasx\n' ' lorem ipsum und so\n', 'type': '02. Merge this or that or maybe even this', 'user': 'student01' }, { 'text': 'function blabl\n' ' on multi lines\n' ' asasxasx\n' ' lorem ipsum und so\n', 'type': '03. This one exists for the sole purpose to test', 'user': 'student01' }, { 'text': 'function lorem ipsum etc\n', 'type': '03. This one exists for the sole purpose to test', 'user': 'student02' }, ]} ) def test_can_create_a_subscription(self): client = APIClient() client.force_authenticate(user=self.data['tutors'][0]) response = client.post('/api/subscription/', {'query_type': 'random'}) self.assertEqual(response.status_code, status.HTTP_201_CREATED) def test_create_subscription_and_get_one_assignment(self): client = APIClient() client.force_authenticate(user=self.data['tutors'][0]) response = client.post('/api/subscription/', {'query_type': 'random'}) self.assertEqual('tutor01', response.data['owner']) def test_subscription_has_next_assignment(self): client = APIClient() client.force_authenticate(user=self.data['tutors'][0]) response_subs = client.post( '/api/subscription/', {'query_type': 'random'}) subscription_id = response_subs.data['subscription_id'] assignment_id = response_subs.data['assignments'][0]['assignment_id'] response_current = client.get( f'/api/subscription/{subscription_id}/assignments/current/') self.assertEqual(1, len(response_subs.data['assignments'])) self.assertEqual(assignment_id, response_current.data['assignment_id']) def test_subscription_can_assign_to_student(self): client = APIClient() client.force_authenticate(user=self.data['tutors'][0]) response_subs = client.post( '/api/subscription/', { 'query_type': 'student', 'query_key': 'student01' }) assignments = response_subs.data['assignments'] self.assertEqual(2, len(assignments))