def deduping_treats_different_calls_to_same_task_differently(self): body = Mock() t1 = Task(body) pre = [call(t1, 5), call(t1, 7), call(t1, 5)] t2 = Task(Mock(), pre=pre) c = Collection(t1=t1, t2=t2) e = Executor(collection=c) e.execute('t2') # Does not call the second t1(5) body.assert_has_calls([mock_call(5), mock_call(7)])
def test_load(self): rb = ring.RingBuilder(8, 3, 1) devs = [{'id': 0, 'region': 0, 'zone': 0, 'weight': 1, 'ip': '127.0.0.0', 'port': 10000, 'device': 'sda1', 'meta': 'meta0'}, {'id': 1, 'region': 0, 'zone': 1, 'weight': 1, 'ip': '127.0.0.1', 'port': 10001, 'device': 'sdb1', 'meta': 'meta1'}, {'id': 2, 'region': 0, 'zone': 2, 'weight': 2, 'ip': '127.0.0.2', 'port': 10002, 'device': 'sdc1', 'meta': 'meta2'}, {'id': 3, 'region': 0, 'zone': 3, 'weight': 2, 'ip': '127.0.0.3', 'port': 10003, 'device': 'sdd1'}] for d in devs: rb.add_dev(d) rb.rebalance() real_pickle = pickle.load try: #test a legit builder fake_pickle = Mock(return_value=rb) fake_open = Mock(return_value=None) pickle.load = fake_pickle builder = RingBuilder.load('fake.builder', open=fake_open) self.assertEquals(fake_pickle.call_count, 1) fake_open.assert_has_calls([mock_call('fake.builder', 'rb')]) self.assertEquals(builder, rb) fake_pickle.reset_mock() fake_open.reset_mock() #test old style builder fake_pickle.return_value = rb.to_dict() pickle.load = fake_pickle builder = RingBuilder.load('fake.builder', open=fake_open) fake_open.assert_has_calls([mock_call('fake.builder', 'rb')]) self.assertEquals(builder.devs, rb.devs) fake_pickle.reset_mock() fake_open.reset_mock() #test old devs but no meta no_meta_builder = rb for dev in no_meta_builder.devs: del(dev['meta']) fake_pickle.return_value = no_meta_builder pickle.load = fake_pickle builder = RingBuilder.load('fake.builder', open=fake_open) fake_open.assert_has_calls([mock_call('fake.builder', 'rb')]) self.assertEquals(builder.devs, rb.devs) fake_pickle.reset_mock() finally: pickle.load = real_pickle
def test_load(self): rb = ring.RingBuilder(8, 3, 1) devs = [ {"id": 0, "zone": 0, "weight": 1, "ip": "127.0.0.0", "port": 10000, "device": "sda1", "meta": "meta0"}, {"id": 1, "zone": 1, "weight": 1, "ip": "127.0.0.1", "port": 10001, "device": "sdb1", "meta": "meta1"}, {"id": 2, "zone": 2, "weight": 2, "ip": "127.0.0.2", "port": 10002, "device": "sdc1", "meta": "meta2"}, {"id": 3, "zone": 3, "weight": 2, "ip": "127.0.0.3", "port": 10003, "device": "sdd1"}, ] for d in devs: rb.add_dev(d) rb.rebalance() real_pickle = pickle.load try: # test a legit builder fake_pickle = Mock(return_value=rb) fake_open = Mock(return_value=None) pickle.load = fake_pickle builder = RingBuilder.load("fake.builder", open=fake_open) self.assertEquals(fake_pickle.call_count, 1) fake_open.assert_has_calls([mock_call("fake.builder", "rb")]) self.assertEquals(builder, rb) fake_pickle.reset_mock() fake_open.reset_mock() # test old style builder fake_pickle.return_value = rb.to_dict() pickle.load = fake_pickle builder = RingBuilder.load("fake.builder", open=fake_open) fake_open.assert_has_calls([mock_call("fake.builder", "rb")]) self.assertEquals(builder.devs, rb.devs) fake_pickle.reset_mock() fake_open.reset_mock() # test old devs but no meta no_meta_builder = rb for dev in no_meta_builder.devs: del (dev["meta"]) fake_pickle.return_value = no_meta_builder pickle.load = fake_pickle builder = RingBuilder.load("fake.builder", open=fake_open) fake_open.assert_has_calls([mock_call("fake.builder", "rb")]) self.assertEquals(builder.devs, rb.devs) fake_pickle.reset_mock() finally: pickle.load = real_pickle
def test_submit_answer(self, events_tracker): self.submit_question_answer('p1', {'2_1': 'choice_choice_2'}) course = self.store.get_course(self.course.id, depth=0) event_transaction_id = events_tracker.emit.mock_calls[0][1][1][ 'event_transaction_id'] events_tracker.emit.assert_has_calls( [ mock_call( events.PROBLEM_SUBMITTED_EVENT_TYPE, { 'user_id': six.text_type(self.student.id), 'event_transaction_id': event_transaction_id, 'event_transaction_type': events.PROBLEM_SUBMITTED_EVENT_TYPE, 'course_id': six.text_type(self.course.id), 'problem_id': six.text_type(self.problem.location), 'weighted_earned': 2.0, 'weighted_possible': 2.0, }, ), mock_call( events.COURSE_GRADE_CALCULATED, { 'course_version': six.text_type(course.course_version), 'percent_grade': 0.02, 'grading_policy_hash': u'ChVp0lHGQGCevD0t4njna/C44zQ=', 'user_id': six.text_type(self.student.id), 'letter_grade': u'', 'event_transaction_id': event_transaction_id, 'event_transaction_type': events.PROBLEM_SUBMITTED_EVENT_TYPE, 'course_id': six.text_type(self.course.id), 'course_edited_timestamp': six.text_type(course.subtree_edited_on), }), ], any_order=True, )
def test_submit_answer(self, events_tracker): self.submit_question_answer('p1', {'2_1': 'choice_choice_2'}) course = self.store.get_course(self.course.id, depth=0) event_transaction_id = events_tracker.emit.mock_calls[0][1][1]['event_transaction_id'] events_tracker.emit.assert_has_calls( [ mock_call( events.PROBLEM_SUBMITTED_EVENT_TYPE, { 'user_id': unicode(self.student.id), 'event_transaction_id': event_transaction_id, 'event_transaction_type': events.PROBLEM_SUBMITTED_EVENT_TYPE, 'course_id': unicode(self.course.id), 'problem_id': unicode(self.problem.location), 'weighted_earned': 2.0, 'weighted_possible': 2.0, }, ), mock_call( events.COURSE_GRADE_CALCULATED, { 'course_version': unicode(course.course_version), 'percent_grade': 0.02, 'grading_policy_hash': u'ChVp0lHGQGCevD0t4njna/C44zQ=', 'user_id': unicode(self.student.id), 'letter_grade': u'', 'event_transaction_id': event_transaction_id, 'event_transaction_type': events.PROBLEM_SUBMITTED_EVENT_TYPE, 'course_id': unicode(self.course.id), 'course_edited_timestamp': unicode(course.subtree_edited_on), } ), ], any_order=True, )
def test_register_captcha_failure(self, mock_captcha): mock_captcha.return_value.verify.return_value = False self.data['captcha_id'] = 999 self.data['captcha_solution'] = 'foo bar' response, json_body = call(self.handler.create, self.url, self.data) self.assertEqual(response.status_code, 403) expected_calls = mock_call(999).verify( 'foo bar', '127.0.0.1', self.data['email']).call_list() self.assertEqual(mock_captcha.mock_calls, expected_calls) self.assertEqual(json_body['code'], "CAPTCHA_FAILURE") self.assertIn('Failed response to captcha challenge.', json_body['message']) self.assertFalse(self.mock_register.called)
def test_register_captcha_success(self, mock_captcha): mock_captcha.return_value.verify.return_value = True self.data['captcha_id'] = 999 self.data['captcha_solution'] = 'foo bar' response, json_body = call(self.handler.create, self.url, self.data) self.assertEqual(response.status_code, 201) expected_calls = mock_call(999).verify( 'foo bar', '127.0.0.1', self.data['email']).call_list() self.assertEqual(mock_captcha.mock_calls, expected_calls) self.assertIn('openid', json_body) self.assertIn('href', json_body) self.assertEqual(json_body['email'], self.data['email']) self.assertEqual(json_body['displayname'], self.data['displayname']) self.assertEqual(json_body['status'], 'Active') self.assertEqual(len(json_body['emails']), 1) self.assertIn(self.data['email'], json_body['emails'][0]['href'])
def test_login(self): now = datetime.utcnow() with patch('api.v20.handlers.authenticate_user') as mock_authenticate: mock_token = Mock() mock_token.consumer.key = 'consumer-key' mock_token.consumer.secret = 'consumer-secret' mock_token.token = 'token-key' mock_token.token_secret = 'token-secret' mock_token.created_at = now mock_token.updated_at = now mock_account = mock_authenticate.return_value mock_account.twofactor_required = False mock_account.openid_identifier = 'some-openid' mock_account.get_or_create_oauth_token.return_value = (mock_token, True) response, json_body = call(handler.create, API_URL, API_DATA) self.assertEqual(response.status_code, 201) expected_calls = ( mock_call('*****@*****.**', 'foobar') .get_or_create_oauth_token('token_name') .call_list() ) self.assertEqual(mock_authenticate.mock_calls, expected_calls) expected_response = { 'href': '/api/v2/tokens/oauth/token-key', 'token_key': 'token-key', 'token_secret': 'token-secret', 'token_name': 'token_name', 'consumer_key': 'consumer-key', 'consumer_secret': 'consumer-secret', 'date_created': now, 'date_updated': now, 'openid': 'some-openid', } self.assertEqual(json_body, expected_response)
def test_angles_from_grid(self): # Check it will gets angles from 'u_cube', and pass any kwargs on to # the angles routine. u_cube = sample_2d_latlons(regional=True, transformed=True) u_cube = u_cube[:2, :3] u_cube.units = 'ms-1' u_cube.rename('dx') u_cube.data[...] = 1.0 v_cube = u_cube.copy() v_cube.name('dy') v_cube.data[...] = 0.0 # Setup a fake angles result from the inner call to 'gridcell_angles'. angles_result_data = np.array([[0.0, 90.0, 180.0], [-180.0, -90.0, 270.0]]) angles_result_cube = Cube(angles_result_data, units='degrees') angles_kwargs = {'this': 2} angles_call_patch = self.patch( 'iris.analysis._grid_angles.gridcell_angles', Mock(return_value=angles_result_cube)) # Call the routine. result = rotate_grid_vectors(u_cube, v_cube, grid_angles_kwargs=angles_kwargs) self.assertEqual(angles_call_patch.call_args_list, [mock_call(u_cube, this=2)]) out_u, out_v = [cube.data for cube in result] # Records what results should be for the various n*90deg rotations. expect_u = np.array([[1.0, 0.0, -1.0], [-1.0, 0.0, 0.0]]) expect_v = np.array([[0.0, 1.0, 0.0], [0.0, -1.0, -1.0]]) # Check results are as expected. self.assertArrayAllClose(out_u, expect_u) self.assertArrayAllClose(out_v, expect_v)
def test_rescoring_events(self): self.submit_question_answer('p1', {'2_1': 'choice_choice_3'}) new_problem_xml = MultipleChoiceResponseXMLFactory().build_xml( question_text='The correct answer is Choice 3', choices=[False, False, False, True], choice_names=['choice_0', 'choice_1', 'choice_2', 'choice_3'] ) with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred, self.course.id): self.problem.data = new_problem_xml self.store.update_item(self.problem, self.instructor.id) self.store.publish(self.problem.location, self.instructor.id) with patch('lms.djangoapps.grades.events.tracker') as events_tracker: submit_rescore_problem_for_student( request=get_mock_request(self.instructor), usage_key=self.problem.location, student=self.student, only_if_higher=False ) course = self.store.get_course(self.course.id, depth=0) # make sure the tracker's context is updated with course info for args in events_tracker.get_tracker().context.call_args_list: self.assertEqual( args[0][1], {'course_id': unicode(self.course.id), 'org_id': unicode(self.course.org)} ) event_transaction_id = events_tracker.emit.mock_calls[0][1][1]['event_transaction_id'] events_tracker.emit.assert_has_calls( [ mock_call( events.GRADES_RESCORE_EVENT_TYPE, { 'course_id': unicode(self.course.id), 'user_id': unicode(self.student.id), 'problem_id': unicode(self.problem.location), 'new_weighted_earned': 2, 'new_weighted_possible': 2, 'only_if_higher': False, 'instructor_id': unicode(self.instructor.id), 'event_transaction_id': event_transaction_id, 'event_transaction_type': events.GRADES_RESCORE_EVENT_TYPE, }, ), mock_call( events.COURSE_GRADE_CALCULATED, { 'course_version': unicode(course.course_version), 'percent_grade': 0.02, 'grading_policy_hash': u'ChVp0lHGQGCevD0t4njna/C44zQ=', 'user_id': unicode(self.student.id), 'letter_grade': u'', 'event_transaction_id': event_transaction_id, 'event_transaction_type': events.GRADES_RESCORE_EVENT_TYPE, 'course_id': unicode(self.course.id), 'course_edited_timestamp': unicode(course.subtree_edited_on), }, ), ], any_order=True, )
def test_rescoring_events(self): self.submit_question_answer('p1', {'2_1': 'choice_choice_3'}) new_problem_xml = MultipleChoiceResponseXMLFactory().build_xml( question_text='The correct answer is Choice 3', choices=[False, False, False, True], choice_names=['choice_0', 'choice_1', 'choice_2', 'choice_3']) with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred, self.course.id): self.problem.data = new_problem_xml self.store.update_item(self.problem, self.instructor.id) self.store.publish(self.problem.location, self.instructor.id) with patch('lms.djangoapps.grades.events.tracker') as events_tracker: submit_rescore_problem_for_student(request=get_mock_request( self.instructor), usage_key=self.problem.location, student=self.student, only_if_higher=False) course = self.store.get_course(self.course.id, depth=0) # make sure the tracker's context is updated with course info for args in events_tracker.get_tracker().context.call_args_list: self.assertEqual( args[0][1], { 'course_id': six.text_type(self.course.id), 'org_id': six.text_type(self.course.org) }) event_transaction_id = events_tracker.emit.mock_calls[0][1][1][ 'event_transaction_id'] events_tracker.emit.assert_has_calls( [ mock_call( events.GRADES_RESCORE_EVENT_TYPE, { 'course_id': six.text_type(self.course.id), 'user_id': six.text_type(self.student.id), 'problem_id': six.text_type(self.problem.location), 'new_weighted_earned': 2, 'new_weighted_possible': 2, 'only_if_higher': False, 'instructor_id': six.text_type(self.instructor.id), 'event_transaction_id': event_transaction_id, 'event_transaction_type': events.GRADES_RESCORE_EVENT_TYPE, }, ), mock_call( events.COURSE_GRADE_CALCULATED, { 'course_version': six.text_type(course.course_version), 'percent_grade': 0.02, 'grading_policy_hash': u'ChVp0lHGQGCevD0t4njna/C44zQ=', 'user_id': six.text_type(self.student.id), 'letter_grade': u'', 'event_transaction_id': event_transaction_id, 'event_transaction_type': events.GRADES_RESCORE_EVENT_TYPE, 'course_id': six.text_type(self.course.id), 'course_edited_timestamp': six.text_type(course.subtree_edited_on), }, ), ], any_order=True, )