def test_get_skills_of_question(self): # If the question id doesnt exist at all, it returns an empty list. with self.assertRaisesRegexp( Exception, 'Entity for class QuestionModel with id ' 'non_existent_question_id not found'): question_services.get_skills_linked_to_question( 'non_existent_question_id') question_id_2 = question_services.get_new_question_id() self.save_new_question( question_id_2, self.editor_id, self._create_valid_question_data('ABC'), ['skill_1']) question_id_3 = question_services.get_new_question_id() self.save_new_question( question_id_3, self.editor_id, self._create_valid_question_data('ABC'), ['skill_2']) question_services.create_new_question_skill_link( self.editor_id, self.question_id, 'skill_1', 0.5) question_services.create_new_question_skill_link( self.editor_id, question_id_2, 'skill_1', 0.3) question_services.create_new_question_skill_link( self.editor_id, question_id_2, 'skill_2', 0.0) question_services.create_new_question_skill_link( self.editor_id, question_id_3, 'skill_2', 0.1) skills = ( question_services.get_skills_linked_to_question( question_id_2)) self.assertTrue(isinstance(skills[0], skill_domain.Skill)) self.assertEqual(len(skills), 2) skill_ids = [skill.id for skill in skills] self.assertItemsEqual( skill_ids, ['skill_1', 'skill_2'])
def test_linking_same_skill_to_question_twice(self): question_id_2 = question_services.get_new_question_id() self.save_new_question(question_id_2, self.editor_id, self._create_valid_question_data('ABC'), ['skill_1']) skill_ids = [ skill.id for skill in question_services.get_skills_linked_to_question(question_id_2) ] self.assertEqual(len(skill_ids), 1) self.assertEqual(skill_ids[0], 'skill_1') question_services.create_new_question_skill_link( self.editor_id, question_id_2, 'skill_1', 0.3) skill_ids = [ skill.id for skill in question_services.get_skills_linked_to_question(question_id_2) ] self.assertEqual(len(skill_ids), 1) self.assertEqual(skill_ids[0], 'skill_1') question_services.create_new_question_skill_link( self.editor_id, question_id_2, 'skill_2', 0.3) skill_ids = [ skill.id for skill in question_services.get_skills_linked_to_question(question_id_2) ] self.assertEqual(len(skill_ids), 2) self.assertItemsEqual(skill_ids, ['skill_1', 'skill_2'])
def test_get_question_skill_links_of_question(self): # If the question id doesnt exist at all, it returns an empty list. skills = (question_services.get_skills_linked_to_question( 'non_existent_question_id')) self.assertEqual(len(skills), 0) question_id_2 = question_services.get_new_question_id() self.save_new_question(question_id_2, self.editor_id, self._create_valid_question_data('ABC')) question_id_3 = question_services.get_new_question_id() self.save_new_question(question_id_3, self.editor_id, self._create_valid_question_data('ABC')) question_services.create_new_question_skill_link( self.question_id, 'skill_1', 0.5) question_services.create_new_question_skill_link( question_id_2, 'skill_1', 0.3) question_services.create_new_question_skill_link( question_id_2, 'skill_2', 0.0) question_services.create_new_question_skill_link( question_id_3, 'skill_2', 0.1) skills = ( question_services.get_skills_linked_to_question(question_id_2)) self.assertTrue(isinstance(skills[0], skill_domain.Skill)) self.assertEqual(len(skills), 2) skill_ids = [skill.id for skill in skills] self.assertItemsEqual(skill_ids, ['skill_1', 'skill_2'])
def test_create_multi_question_skill_links_for_question(self): self.question = self.save_new_question( self.question_id, self.editor_id, self._create_valid_question_data('ABC'), ['skill_1']) with self.assertRaisesRegexp( Exception, 'Skill difficulties and skill ids should match. ' 'The lengths of the two lists are different.'): question_services.link_multiple_skills_for_question( self.editor_id, self.question_id, ['skill_1', 'skill_2'], [0.5]) question_services.link_multiple_skills_for_question( self.editor_id, self.question_id, ['skill_1', 'skill_2'], [0.5, 0.7]) skill_ids = [skill.id for skill in question_services.get_skills_linked_to_question( self.question_id)] self.assertItemsEqual(skill_ids, ['skill_1', 'skill_2'])
def test_delete_question_skill_link(self): question_services.create_new_question_skill_link( self.editor_id, self.question_id, 'skill_1', 0.3) question_services.create_new_question_skill_link( self.editor_id, self.question_id, 'skill_2', 0.3) question_services.delete_question_skill_link( self.editor_id, self.question_id, 'skill_1') skill_ids = [skill.id for skill in question_services.get_skills_linked_to_question( self.question_id)] self.assertItemsEqual(skill_ids, ['skill_2']) question_services.delete_question_skill_link( self.editor_id, self.question_id, 'skill_2') question = question_services.get_question_by_id( self.question_id, strict=False) self.assertIsNone(question)
def get(self, question_id): """Gets the data for the question overview page.""" question = question_services.get_question_by_id(question_id, strict=False) if question is None: raise self.PageNotFoundException( 'The question with the given id doesn\'t exist.') associated_skills = question_services.get_skills_linked_to_question( question_id) associated_skill_dicts = [ skill.to_dict() for skill in associated_skills ] self.values.update({ 'question_dict': question.to_dict(), 'associated_skill_dicts': associated_skill_dicts }) self.render_json(self.values)