def test_retrieve_grade_list(self): """Test retrieving the grade list""" sample_grade(name='Test grade 1', rank=4) sample_grade(name='Test grade 2', rank=2) res = self.client.get(GRADE_LIST_URL) grades = Grade.objects.all().order_by('rank') serializer = GradeSerializer(grades, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def test_student_optional_create_positive(self): """Test successfully creating a student object with optional params""" grade = sample_grade() classroom1 = sample_classroom() classroom2 = sample_classroom() classroom3 = sample_classroom() payload = sample_student_payload( fullname="Testonildo", active=False, departure_date='2010-10-10', guardian2='Mr. Guard', grade=grade.id, classes=[classroom1.id, classroom3.id]) res = self.client.post(STUDENT_LIST_URL, payload) payload.pop('classes') self.assertEqual(res.status_code, status.HTTP_201_CREATED) student = Student.objects.filter(**payload) self.assertTrue(student.exists) student = student[0] serializer = StudentCreateSerializer(student) self.assertEqual(res.data, serializer.data) self.assertFalse(student.active) self.assertEqual(student.departure_date, str_to_date(payload['departure_date'])) self.assertEqual(student.guardian2, payload['guardian2']) self.assertEqual(student.grade, grade) self.assertIn(classroom1, student.classes.all()) self.assertIn(classroom3, student.classes.all()) self.assertNotIn(classroom2, student.classes.all())
def test_student_optional_partial_update_positive(self): """Test successful partial update of a student with optional params""" grade = sample_grade() classroom1 = sample_classroom() classroom2 = sample_classroom() classroom3 = sample_classroom() payload = { 'active': False, 'departure_date': '2010-10-10', 'guardian2': 'Mr. Guard', 'grade': grade.id, 'classes': [classroom1.id, classroom3.id] } res = self.client.patch(student_detail_url(self.student.id), payload) self.student.refresh_from_db() payload.pop('classes') self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(self.student.active, payload['active']) self.assertEqual(self.student.departure_date, str_to_date(payload['departure_date'])) self.assertEqual(self.student.guardian2, payload['guardian2']) self.assertEqual(self.student.grade, grade) self.assertIn(classroom1, self.student.classes.all()) self.assertIn(classroom3, self.student.classes.all()) self.assertNotIn(classroom2, self.student.classes.all()) serializer = StudentCreateSerializer(self.student) self.assertEqual(res.data, serializer.data)
def test_student_optional_full_update_positive(self): """Test successfully fully updating a student with optional params""" grade = sample_grade() classroom1 = sample_classroom() classroom2 = sample_classroom() classroom3 = sample_classroom() payload = sample_student_payload( fullname="Testonildo", active=False, departure_date='2010-10-10', guardian2='Mr. Guard', grade=grade.id, classes=[classroom1.id, classroom3.id]) res = self.client.put(student_detail_url(self.student.id), payload) self.student.refresh_from_db() payload.pop('classes') self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(Student.objects.get(**payload), self.student) self.assertEqual(self.student.guardian2, payload['guardian2']) self.assertEqual(self.student.grade, grade) self.assertIn(classroom1, self.student.classes.all()) self.assertIn(classroom3, self.student.classes.all()) self.assertNotIn(classroom2, self.student.classes.all()) serializer = StudentCreateSerializer(self.student) self.assertEqual(res.data, serializer.data)
def test_unauthenticated_create_forbidden(self): """Test a classroom cannot be created without authentication""" grade = sample_grade() payload = sample_classroom_payload(grade=grade) res = self.client.post(CLASSROOM_LIST_URL, payload) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN)
def test_retrieve_grade_detail(self): """Test retrieving the detail view of a grade object""" grade = sample_grade() serializer = GradeSerializer(grade) res = self.client.get(grade_detail_url(grade.id)) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def test_retrieve_classroom_detail(self): """Test retriving the detail view of a classroom""" classroom = sample_classroom(name='Test', grade=sample_grade()) serializer = ClassroomDetailSerializer(classroom) res = self.client.get(classroom_detail_url(classroom.id)) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def test_partial_update_grade(self): """Test partially updating a grade object""" payload = {'name': 'Other Name'} grade = sample_grade(name='Test Grade', rank=1) res = self.client.patch(grade_detail_url(grade.id), payload) grade.refresh_from_db() self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(grade.name, payload['name'])
def test_create_classroom_successful(self): """Test creating a classroom object""" payload = sample_classroom_payload(name='Testing', grade=sample_grade().id, days_of_week='2,4') res = self.client.post(CLASSROOM_LIST_URL, payload) self.assertEqual(res.status_code, status.HTTP_201_CREATED) self.assertTrue(Classroom.objects.filter(**payload).exists())
def setUp(self): self.user = sample_user() self.client = APIClient() self.client.force_authenticate(self.user) self.grade1 = sample_grade() self.grade2 = sample_grade() self.grade3 = sample_grade() self.classroom1 = sample_classroom() self.classroom2 = sample_classroom() self.classroom3 = sample_classroom() self.student1 = sample_student( fullname='John Default', grade=self.grade1, ) self.student2 = sample_student( fullname='Jane Jeans', grade=self.grade2 ) self.student3 = sample_student( fullname='Dave Derik', active=False, grade=self.grade2 ) self.student4 = sample_student( fullname='Johna Default', grade=self.grade1 ) self.student5 = sample_student( fullname='Leon Daqui', grade=self.grade3 ) self.student1.classes.add(self.classroom2) self.student2.classes.add(self.classroom1) self.student3.classes.add(self.classroom2) self.student4.classes.add(self.classroom3) self.student1_serializer = StudentListSerializer(self.student1) self.student2_serializer = StudentListSerializer(self.student2) self.student3_serializer = StudentListSerializer(self.student3) self.student4_serializer = StudentListSerializer(self.student4) self.student5_serializer = StudentListSerializer(self.student5)
def test_retrive_classroom_list(self): """Test retriving the classroom list""" sample_classroom(name='CR 1', identifier='BC01') sample_classroom(name='CR 2', identifier='AB01', grade=sample_grade()) classrooms = Classroom.objects.all().order_by('identifier') serializer = ClassroomListSerializer(classrooms, many=True) res = self.client.get(CLASSROOM_LIST_URL) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data)
def test_full_update(self): """Test fully updating the a classroom object""" classroom = sample_classroom(name="Test CR", room='A45') payload = sample_classroom_payload(name="Other name", days_of_week='1', grade=sample_grade().id) res = self.client.put(classroom_detail_url(classroom.id), payload) classroom.refresh_from_db() self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(Classroom.objects.get(**payload), classroom)
def setUp(self): self.user = sample_user() self.client = APIClient() self.client.force_authenticate(self.user) self.grade = sample_grade() self.classroom = sample_classroom() self.student = sample_student(fullname='Le Studant', grade=self.grade) self.student.classes.add(self.classroom) self.serializer = StudentDetailSerializer(self.student)
def test_string_representation(self): """Test the string representation of the Grade model""" grade = sample_grade(name='Some grade') self.assertEqual(str(grade), grade.name)