def test_batch_rollback(self): (other, teammate, contact, captain, team, classroom, ppnt) = self.create() class1ppt1 = { 'first_name': u'Je\u017cu', 'last_name': u'Kl\u0105tw', 'classroom_id': classroom.uid, 'student_id': 'duplicateId', } class1ppt2 = { 'first_name': u'Ppt', 'last_name': u'One-Two', 'classroom_id': classroom.uid, 'student_id': 'duplicateId', } def postBody(ppt): return {'method': 'POST', 'path': '/api/participants', 'body': ppt} # Posting a duplicate should roll back the whole change. self.testapp.patch_json( '/api/participants', [postBody(class1ppt1), postBody(class1ppt2)], headers=jwt_headers(captain), status=400, ) self.assertEqual(len(Participant.get()), 0)
def test_patch_with_existing_participants(self): """Custom PATCH handling: add a batch of participants, some of whom already exist in other classrooms.""" (other, teammate, contact, captain, team, classroom, ppnt) = self.create() extra_classroom = Classroom.create( name="Extra Classroom", code='foo', team_id=team.uid, contact_id=contact.uid, ) extra_classroom.put() wrapper = {'method': 'POST', 'path': '/api/participants'} original_exact = { 'team_id': team.uid, 'classroom_id': extra_classroom.uid, 'student_id': ppnt.student_id, } original_upper = { 'team_id': team.uid, 'classroom_id': extra_classroom.uid, 'student_id': ppnt.student_id.upper(), } original_lower = { 'team_id': team.uid, 'classroom_id': extra_classroom.uid, 'student_id': ppnt.student_id.lower(), } original_extra_characters = { 'team_id': team.uid, 'classroom_id': extra_classroom.uid, # Student tokens are stripped to alphanumerics only, so other # characters here should be ignored. 'student_id': ppnt.student_id + '!', } new_ppnt = { 'team_id': team.uid, 'classroom_id': extra_classroom.uid, 'student_id': 'Student_one', } response = self.testapp.patch_json( '/api/participants', [ dict(wrapper, body=original_exact), dict(wrapper, body=original_upper), dict(wrapper, body=original_lower), dict(wrapper, body=new_ppnt), ], headers=jwt_headers(contact), ) # There should now be a total of 2 participants, one of whom is in # two classrooms. All the differences in case should have been ignored. fetched_all = Participant.get() self.assertEqual(len(fetched_all), 2) fetched_original = next(p for p in fetched_all if p.uid == ppnt.uid) fetched_new = next(p for p in fetched_all if p.uid != ppnt.uid) self.assertIsNotNone(fetched_original) self.assertIsNotNone(fetched_new) self.assertEqual( set(fetched_original.classroom_ids), {classroom.uid, extra_classroom.uid}, ) self.assertEqual( set(fetched_new.classroom_ids), {extra_classroom.uid}, )