def test_to_internal_value(self): Request = namedtuple('Request', ['site']) serializer = UserGradeSerializer( context={'request': Request(site=self.site)}) data = { 'username': '******', 'course_run': 'nope', 'letter_grade': 'A', 'percent_grade': 0.9, 'verified': True, } with self.assertRaisesMessage(ValidationError, 'No CourseRun exists for key [nope]'): serializer.to_internal_value(data) course = CourseFactory(site=self.site) course_run = CourseRunFactory(course=course) data['course_run'] = course_run.key grade = serializer.to_internal_value(data) self.assertEqual(grade['username'], 'alice') self.assertEqual(grade['course_run'], course_run) self.assertEqual(grade['verified'], True) self.assertEqual(grade['letter_grade'], 'A') self.assertEqual(str(grade['percent_grade']), '0.9000')
def test_to_internal_value(self): Request = namedtuple("Request", ["site"]) serializer = UserGradeSerializer( context={"request": Request(site=self.site)}) data = { "username": "******", "course_run": "nope", "letter_grade": "A", "percent_grade": 0.9, "verified": True, } with self.assertRaisesMessage(ValidationError, "No CourseRun exists for key [nope]"): serializer.to_internal_value(data) course = CourseFactory(site=self.site) course_run = CourseRunFactory(course=course) data["course_run"] = course_run.key grade = serializer.to_internal_value(data) self.assertEqual(grade["username"], "alice") self.assertEqual(grade["course_run"], course_run) self.assertEqual(grade["verified"], True) self.assertEqual(grade["letter_grade"], "A") self.assertEqual(str(grade["percent_grade"]), "0.9000")
def serialize_user_grade(self, user_grade, many=False): """ Serialize the given UserGrade object(s). """ request = APIRequestFactory(SERVER_NAME=self.site.domain).get('/') return UserGradeSerializer(user_grade, context={ 'request': request }, many=many).data
def test_to_representation(self): grade = UserGradeFactory() expected = { 'id': grade.id, 'username': grade.username, 'course_run': grade.course_run.key, 'letter_grade': grade.letter_grade, 'percent_grade': str(grade.percent_grade), 'verified': grade.verified, 'created': grade.created.strftime(api_settings.DATETIME_FORMAT), 'modified': grade.modified.strftime(api_settings.DATETIME_FORMAT), } actual = UserGradeSerializer(grade).data self.assertDictEqual(actual, expected)
def test_to_representation(self): grade = UserGradeFactory() expected = { "id": grade.id, "username": grade.username, "course_run": grade.course_run.key, "letter_grade": grade.letter_grade, "percent_grade": str(grade.percent_grade), "verified": grade.verified, "created": grade.created.strftime(api_settings.DATETIME_FORMAT), "modified": grade.modified.strftime(api_settings.DATETIME_FORMAT), } actual = UserGradeSerializer(grade).data self.assertDictEqual(actual, expected)