def test_get_students(self): """Verify being able to get students for section/gradebook.""" self._register_get_gradebook() gradebook = GradeBook(self.CERT, self.URLBASE, self.GBUUID) self._register_get_students() # Without section specified students = gradebook.get_students() self.assertEqual(students, self.STUDENT_BODY["data"]) # Simple data return (and upcasing mit.edu) students = gradebook.get_students(simple=True) mapped_data = [] for student in self.STUDENT_BODY["data"]: email = student["accountEmail"] if "mit.edu" in email: email = email.replace("mit.edu", "MIT.EDU") mapped_data.append(dict(email=email, name=student["displayName"], section=student["section"])) self.assertEqual(mapped_data, students) # With valid section specified self._register_get_sections() self._register_get_students_in_section() section_name = self.SECTION_BODY["data"]["recitation"][0]["name"] students = gradebook.get_students(section_name=section_name) self.assertEqual(students, [x for x in self.STUDENT_BODY["data"] if x["section"] == section_name]) # With invalid section with self.assertRaises(PyLmodNoSuchSection): students = gradebook.get_students(section_name="nope")
def test_create_assignment(self): """Verify creating a new assignment.""" response_data = {"message": "success"} self._register_create_assignment(response_data) self._register_get_gradebook() gradebook = GradeBook(self.CERT, self.URLBASE, self.GBUUID) response = gradebook.create_assignment("Test Assign", "test-assign", 1.0, 100.0, "11-04-2999") self.assertEqual(response_data, response) last_request = httpretty.last_request() self.assertEqual( last_request.body, json.dumps( { "name": "Test Assign", "shortName": "test-assign", "weight": 1.0, "graderVisible": False, "gradingSchemeType": "NUMERIC", "gradebookId": self.GRADEBOOK_ID, "maxPointsTotal": 100.0, "dueDateString": "11-04-2999", } ), )
def test_delete_assignment(self): """Verify deleting a new assignment.""" response_data = {"message": "success"} httpretty.register_uri( httpretty.DELETE, "{0}assignment/1".format(self.GRADEBOOK_REGISTER_BASE), body=json.dumps(response_data) ) self._register_get_gradebook() gradebook = GradeBook(self.CERT, self.URLBASE, self.GBUUID) response = gradebook.delete_assignment(1) self.assertEqual(response_data, response)
def test_get_sections(self): """Verify we can get sections for a course.""" self._register_get_gradebook() self._register_get_sections() gradebook = GradeBook(self.CERT, self.URLBASE, self.GBUUID) sections = gradebook.get_sections() self.assertEqual(sections, self.SECTION_BODY["data"]) # Check simple style sections = gradebook.get_sections(simple=True) expected_sections = gradebook.unravel_sections(self.SECTION_BODY["data"]) self.assertEqual(sections, [{"SectionName": x["name"]} for x in expected_sections])
def test_get_options(self): """Verify that we can get the options for a gradebook.""" self._register_get_options(True) self._register_get_gradebook() gradebook = GradeBook(self.CERT, self.URLBASE, self.GBUUID) options = gradebook.get_options(gradebook_id=self.GRADEBOOK_ID) self.assertIn("membershipQualifier", options) # check for no data self._register_get_options(False) options = gradebook.get_options(gradebook_id=self.GRADEBOOK_ID) self.assertNotIn("membershipQualifier", options)
def test_multi_grade(self): """Verify that we can set multiple grades at once """ response_data = {"message": "success"} self._register_multi_grade(response_data) self._register_get_gradebook() gradebook = GradeBook(self.CERT, self.URLBASE, self.GBUUID) grades = self._get_grades() response = gradebook.multi_grade(grades) self.assertEqual(response_data, response) last_request = httpretty.last_request() self.assertEqual(last_request.body, json.dumps(grades))
def test_get_gradebook_id(self): """Verify get_gradebook_id works and sets the property as expected.""" self._register_get_gradebook() test_gradebook = GradeBook(self.CERT, self.URLBASE) gradebook_id = test_gradebook.get_gradebook_id(self.GBUUID) self.assertEqual(gradebook_id, self.GRADEBOOK_ID) last_request = httpretty.last_request() self.assertEqual(last_request.querystring, dict(uuid=[self.GBUUID])) # Remove data and assert exception raised self._register_get_gradebook(False) with self.assertRaises(PyLmodUnexpectedData): test_gradebook.get_gradebook_id(self.GBUUID)
def test_get_assignment_by_name(self): """Verify grabbing an assignment by name.""" self._register_get_gradebook() # Verify just with a list (no API) gradebook = GradeBook(self.CERT, self.URLBASE, self.GBUUID) static_assignments = [dict(name="blah", assignmentId=1)] # No match self.assertEqual((None, None), gradebook.get_assignment_by_name("stuff", static_assignments)) # Match self.assertEqual((1, static_assignments[0]), gradebook.get_assignment_by_name("blah", static_assignments)) # Verify we can get assignments via API and match self._register_get_assignments() assignment = self.ASSIGNMENT_BODY["data"][0] self.assertEqual((assignment["assignmentId"], assignment), gradebook.get_assignment_by_name(assignment["name"]))
def test_get_section_by_name(self): """Verify grabbing a section by name.""" self._register_get_gradebook() gradebook = GradeBook(self.CERT, self.URLBASE, self.GBUUID) # With match self._register_get_sections() section_type = "recitation" section = self.SECTION_BODY["data"][section_type][0] # Add the type modifier we now add to the structure section["sectionType"] = section_type self.assertEqual((section["groupId"], section), gradebook.get_section_by_name(section["name"])) # Without match self._register_get_sections() section = "Nope" self.assertEqual((None, None), gradebook.get_section_by_name(section))
def test_get_students_by_email(self): """Verify being able to get students by e-mail""" self._register_get_gradebook() gradebook = GradeBook(self.CERT, self.URLBASE, self.GBUUID) self._register_get_students() real_student = self.STUDENT_BODY["data"][0] # Match against passed in list self.assertEqual( gradebook.get_student_by_email(real_student["accountEmail"], students=self.STUDENT_BODY["data"]), (real_student["studentId"], real_student), ) # Get legitimate email student = gradebook.get_student_by_email(real_student["accountEmail"]) self.assertEqual(student, (real_student["studentId"], real_student)) # And with non-existent student self.assertEqual((None, None), gradebook.get_student_by_email("cheese"))
def test_get_staff(self): """Verify staff list is returned.""" httpretty.register_uri( httpretty.GET, "{0}staff/{1}".format(self.GRADEBOOK_REGISTER_BASE, self.GRADEBOOK_ID), body=json.dumps(self.STAFF_BODY), ) self._register_get_gradebook() gradebook = GradeBook(self.CERT, self.URLBASE, self.GBUUID) staff = gradebook.get_staff(self.GRADEBOOK_ID) self.assertEqual(staff, self.STAFF_BODY["data"]) # Check simple style staff = gradebook.get_staff(self.GRADEBOOK_ID, simple=True) expected_staff = gradebook.unravel_staff(self.STAFF_BODY) simple_list = [] for member in expected_staff.__iter__(): simple_list.append( {"accountEmail": member["accountEmail"], "displayName": member["displayName"], "role": member["role"]} ) for member in staff: self.assertIn(member, simple_list)
def test_set_grade(self): """Verify the setting of grades is as we expect. """ response_data = {"message": "success"} httpretty.register_uri( httpretty.POST, "{0}grades/{1}".format(self.GRADEBOOK_REGISTER_BASE, self.GRADEBOOK_ID), body=json.dumps(response_data), ) self._register_get_gradebook() gradebook = GradeBook(self.CERT, self.URLBASE, self.GBUUID) grade = self._get_grades()[0] response = gradebook.set_grade( assignment_id=grade["assignmentId"], student_id=grade["studentId"], grade_value=grade["numericGradeValue"], isGradeApproved=False, ) self.assertEqual(response_data, response) last_request = httpretty.last_request() self.assertEqual(last_request.body, json.dumps(grade))
def test_get_assignments(self): """Verify we can get assignments as requested""" self._register_get_gradebook() self._register_get_assignments() gradebook = GradeBook(self.CERT, self.URLBASE, self.GBUUID) assignments = gradebook.get_assignments() self.assertEqual(assignments, self.ASSIGNMENT_BODY["data"]) last_request = httpretty.last_request() self.assertEqual( last_request.querystring, dict(includeMaxPoints=["true"], includeAvgStats=["false"], includeGradingStats=["false"]), ) # Check simple style assignments = gradebook.get_assignments(simple=True) self.assertEqual(assignments, [{"AssignmentName": x["name"]} for x in self.ASSIGNMENT_BODY["data"]]) # Verify parameter handling assignments = gradebook.get_assignments(max_points=False, avg_stats=True, grading_stats=True) last_request = httpretty.last_request() self.assertEqual( last_request.querystring, dict(includeMaxPoints=["false"], includeAvgStats=["true"], includeGradingStats=["true"]), )
def test_spreadshee2gradebook(self, csv_patch, multi_patch): """Do a simple test of the spreadsheet to gradebook public method""" non_assignment_fields = ["ID", "Username", "Full Name", "edX email", "External email"] email_field = "External email" gradebook = GradeBook(self.CERT, self.URLBASE) # Test with tmp file handle with tempfile.NamedTemporaryFile(delete=True) as temp_file: gradebook.spreadsheet2gradebook(temp_file.name) called_with = multi_patch.call_args csv_patch.assert_called_once() self.assertEqual(called_with[0][1], email_field) self.assertEqual(called_with[0][2], non_assignment_fields) # Test with tmp file handle, approve_grades=False with tempfile.NamedTemporaryFile(delete=True) as temp_file: gradebook.spreadsheet2gradebook(temp_file.name, approve_grades=False) called_with = multi_patch.call_args csv_patch.assert_called_once() self.assertEqual(called_with[0][1], email_field) self.assertEqual(called_with[0][2], non_assignment_fields) # Test with tmp file handle, approve_grades=True with tempfile.NamedTemporaryFile(delete=True) as temp_file: gradebook.spreadsheet2gradebook(temp_file.name, approve_grades=True) called_with = multi_patch.call_args csv_patch.assert_called_once() self.assertEqual(called_with[0][1], email_field) self.assertEqual(called_with[0][2], non_assignment_fields) # Test with patched csvReader and named e-mail field alternate_email_field = "stuff" gradebook.spreadsheet2gradebook(csv_patch, alternate_email_field) non_assignment_fields.append(alternate_email_field) called_with = multi_patch.call_args csv_patch.assert_called_once() self.assertEqual(called_with[0][1], alternate_email_field) self.assertEqual(called_with[0][2], non_assignment_fields)
def test_spreadsheet2gradebook_multi(self): """Verify that we can use a spreadsheet to set grades """ response_data = {"message": "success"} self._register_get_gradebook() self._register_get_assignments() self._register_get_students() self._register_multi_grade(response_data) gradebook = GradeBook(self.CERT, self.URLBASE, self.GBUUID) # Create "spreadsheet" that doesn't require creating assignments. spreadsheet = [ {"External email": "*****@*****.**", "Homework 1": 2.2}, {"External email": "cheese", "Homework 1": 1.1}, ] gradebook._spreadsheet2gradebook_multi( csv_reader=spreadsheet, email_field="External email", non_assignment_fields=["External email"], approve_grades=False, ) # Verify that we got the grades we expect last_request = httpretty.last_request() self.assertEqual(last_request.body, json.dumps(self._get_multigrade(approve_grades=False))) # Verify that we got the same grades, setting auto-approve = False gradebook._spreadsheet2gradebook_multi( csv_reader=spreadsheet, email_field="External email", non_assignment_fields=["External email"], approve_grades=False, ) # Verify that we got the grades we expect last_request = httpretty.last_request() self.assertEqual(last_request.body, json.dumps(self._get_multigrade(approve_grades=False))) # Verify that we got the same grades, setting auto-approve = False gradebook._spreadsheet2gradebook_multi( csv_reader=spreadsheet, email_field="External email", non_assignment_fields=["External email"], approve_grades=False, ) # Verify that we got the grades we expect last_request = httpretty.last_request() self.assertEqual(last_request.body, json.dumps(self._get_multigrade(approve_grades=False))) # Verify that we got the same grades, setting auto-approve = True gradebook._spreadsheet2gradebook_multi( csv_reader=spreadsheet, email_field="External email", non_assignment_fields=["External email"], approve_grades=True, ) # Verify that we got the grades we expect last_request = httpretty.last_request() self.assertEqual(last_request.body, json.dumps(self._get_multigrade(approve_grades=True))) # Verify that we got the same grades, setting auto-approve = True gradebook._spreadsheet2gradebook_multi( csv_reader=spreadsheet, email_field="External email", non_assignment_fields=["External email"], approve_grades=True, ) # Verify that we got the grades we expect last_request = httpretty.last_request() self.assertEqual(last_request.body, json.dumps(self._get_multigrade(approve_grades=True))) # Now run it when the assignment doesn't exist to exercise # assignment creation code. # Setup create to return an assignment ID as expected by the API assignment_id = 3 self._register_create_assignment(dict(data=dict(assignmentId=assignment_id))) spreadsheet = [{"External email": "*****@*****.**", "Homework 8": 2.2}] gradebook._spreadsheet2gradebook_multi( csv_reader=spreadsheet, email_field="External email", non_assignment_fields=["External email"] ) last_request = httpretty.last_request() expected_response = self._get_multigrade(approve_grades=False)[0] expected_response["assignmentId"] = assignment_id self.assertEqual(last_request.body, json.dumps([expected_response])) # Now with assignment failing to be created self._register_create_assignment({}) with self.assertRaises(PyLmodFailedAssignmentCreation): gradebook._spreadsheet2gradebook_multi( csv_reader=spreadsheet, email_field="External email", non_assignment_fields=["External email"], approve_grades=False, ) # And finally with a bad grade spreadsheet = [ {"External email": "*****@*****.**", "Homework 1": "foo"}, {"External email": "*****@*****.**", "midterm1": 1.1}, ] gradebook._spreadsheet2gradebook_multi( csv_reader=spreadsheet, email_field="External email", non_assignment_fields=["External email"], approve_grades=False, ) last_request = httpretty.last_request() self.assertEqual( last_request.body, json.dumps( [ { u"assignmentId": 2, u"isGradeApproved": False, u"mode": 2, u"numericGradeValue": 1.1, u"studentId": 1, } ] ), )