def mutate(root, info, question_id, answer_data): errors = {} current_user = get_jwt_identity() user = UserModel.find_by_id(current_user["id"]) if not user: errors["user"] = "******" question = QuestionModel.find_by_id(question_id) if not question: errors["question"] = "not found" if errors: raise GraphQLError(json.dumps(errors)) answer = AnswerModel(created_by=user, **answer_data) answer.save() if question.answers: question.answers.append(answer) else: question.answers = [answer] question.save() return AnswerQuestion(answer=answer, ok=True)
def test_answers_clue_properly(self): answers = [ Answer(pattern=r'\w+\s+(?P<second_word>\w+)', next_clue='TWOWORDS'), Answer(pattern=r'steve', next_clue="STEVE"), Answer(pattern=r'.*', next_clue='CATCHALL'), ] message = Message(text='test answer') next_clue, answer_data = get_next_clue(message, answers) self.assertEqual('TWOWORDS', next_clue) self.assertEqual({'second_word': 'answer'}, answer_data)
def test_requires_matching_receiver(self): answers = [ Answer(pattern=r'.*', next_clue='STORY:FIRSTCLUE', receiver=SECONDARY_SERVER_PHONE), Answer(pattern=r'.*', next_clue='STORY:SECONDCLUE') ] user = MagicMock() message = Message(text='correct answer') group_mock = Mock(clue=Mock(is_endpoint=False, hint='My Hint', answers=answers)) result = answer(message, user, group_mock) self.assertEqual(CLUE, result.response_type) self.assertEqual('STORY:SECONDCLUE', result.group.clue_uid)
def test_matches_if_media_given(self): answers = [ Answer(pattern=r'.*', next_clue='STORY:FIRSTCLUE', require_media=True), Answer(pattern=r'.*', next_clue='STORY:SECONDCLUE') ] user = MagicMock() message_with_media = Message(text='correct answer', media_url='www.example.com/caturday.png') group_mock = Mock(clue=Mock(is_endpoint=False, hint='My Hint', answers=answers)) result_with_media = answer(message_with_media, user, group_mock) self.assertEqual('STORY:FIRSTCLUE', result_with_media.group.clue_uid)
def test_requires_media(self): answers = [ Answer(pattern=r'.*', next_clue='STORY:FIRSTCLUE', require_media=True), Answer(pattern=r'.*', next_clue='STORY:SECONDCLUE') ] user = MagicMock() message_without_media = Message(text='correct answer') group_mock = Mock(clue=Mock(is_endpoint=False, hint='My Hint', answers=answers)) result_without_media = answer(message_without_media, user, group_mock) self.assertEqual('STORY:SECONDCLUE', result_without_media.group.clue_uid)
def mutate(root, info, answer_id, reply_data): errors = {} current_user = get_jwt_identity() user = UserModel.find_by_id(current_user["id"]) if not user: errors["user"] = "******" answer = AnswerModel.find_by_id(answer_id) if not answer: errors["answer"] = "not found" if errors: raise GraphQLError(json.dumps(errors)) reply = ReplyModel(created_by=user, **reply_data) reply.save() if answer.replies: answer.replies.append(reply) else: answer.replies = [reply] answer.save() return ReplyToAnswer(reply=reply, ok=True)
def mutate(root, info, answer_id, reaction_data): errors = {} current_user = get_jwt_identity() user = UserModel.find_by_id(current_user["id"]) if not user: errors["user"] = "******" answer = AnswerModel.find_by_id(answer_id) if not answer: errors["answer"] = "not found" if errors: raise GraphQLError(json.dumps(errors)) reaction = ReactionModel(user=user, **reaction_data) if not answer.reactions: answer.reactions = [reaction] else: old_reaction = list( filter(lambda r: r.user == reaction.user, answer.reactions)) if old_reaction: if old_reaction[0].reaction != reaction.reaction: answer.reactions.remove(old_reaction[0]) answer.reactions.append(reaction) else: answer.reactions.append(reaction) answer.save() return ReactToAnswer(answer=answer, reactions=answer.reactions, ok=True)
def setUp(self): super(TestScavenger, self).setUp() self.story = Story.from_uid('STORY', default_hint='default hint') self.story.put() self.story_code = StoryCode.from_words('salsa tacos', story_uid=self.story.uid) self.story_code.put() self.start_clue = Clue.from_uid(Clue.build_uid(self.story.uid, 'START'), text='Start the story', hint='clue hint') self.start_clue.put() self.next_clue = Clue.from_uid(Clue.build_uid(self.story.uid, 'NEXT'), text='You made it!', sender="+555") self.next_clue.put() self.answer = Answer.from_uid( Answer.build_uid('STORY', 'START', 'TRANSITION'), pattern=r'my answer is (?P<user_answer>\w+)', next_clue=self.next_clue.uid, ) self.answer.put()
def setUp(self): self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() ndb.get_context().set_cache_policy(False) self.story_uid = 'STORY' self.clue_uid = Clue.build_uid(self.story_uid, 'START') self.answer_uid = Answer.build_uid(self.story_uid, 'START', 'TRANSITION') Story.from_uid(self.story_uid, default_hint='default hint').put() Clue.from_uid(self.clue_uid, text='Start the story', hint='clue hint').put() Answer.from_uid( self.answer_uid, pattern=r'my answer is (?P<user_answer>\w+)', next_clue=self.clue_uid, ).put()
def create_answer(qid): question = Question.query.get_or_404(qid) form = AnswerForm().validate_for_api() with db.auto_commit(): answer = Answer( content=form.content.data, author_gid=g.user['gid'], # author_gid=g.user['gid'] ) if g.user['scope'] == 'TeacherScope': if not question.teacher_aid: question.teacher_answer = answer # todo: history # history = History.create_from_teacher_answer(answer, create_answer=True) else: return Duplicate() else: if not question.student_aid: question.student_answer = answer # todo: history # history = History.create_from_student_answer(answer, create_answer=True) else: return Duplicate() db.session.add(answer) return Success()
def test_gives_hints_if_incorrect(self): answers = [Answer(pattern=r'tough answer', next_clue='SOME:NEXTCLUE')] user = MagicMock() message = Message(text='this is not the correct answer') group_mock = Mock(clue=Mock(is_endpoint=False, hint='My Hint', answers=answers)) result = answer(message, user, group_mock) self.assertEqual(['My Hint'], [r.text for r in result.messages])
def view(self, key): self.scaffold.display_properties = ("title", "content", "tags") # self.context['answers'] = Answer.all_answers_by_question(key) self.context['answers'] = Answer.all_answers_by_question(key) self.context['display_properties'] = ("content",) question = self.util.decode_key(key).get() question.increase_views() return scaffold.view(self, key)
def test_sets_next_clue_on_group(self): user = MagicMock() answers = [ Answer( pattern=r'\w+\s+(?P<group_word>\w+)', next_clue=r'STORY:TWO-WORDS', ), Answer( pattern=r'.*', next_clue=r'STORY:GENERIC' ) ] message = Message(text='test answer') group_mock = Mock(data={}, clue=Mock(is_endpoint=False, answers=answers)) result = answer(message, user, group_mock) self.assertEqual("STORY:TWO-WORDS", result.group.clue_uid) self.assertEqual({'group_word': 'answer'}, result.group.data)
def get(self): stories = [s.to_dict() for s in Story.query().fetch()] clues = [c.to_dict() for c in Clue.query().fetch()] answers = [a.to_dict() for a in Answer.query().fetch()] self.request.headers['Content-Type'] = 'application/json' self.response.body = json.dumps({ 'stories': stories, 'clues': clues, 'answers': answers, }, indent=2)
def create_and_save_question(title, text, is_mcq, answers, room): ''' Helper to create a question and save it to the graph :param name: the name of the question :param active: whether or not this question will be active by default :param admin_id: the user id representing the admin :param course_id: the course code / id representing the course that this question studies :return ValueError: raised if either the course or user cannot be found in the graph ''' new_question = Question(title=title, text=text, is_multiple_choice=is_mcq) new_question.save() new_question.room.connect(room) for a in answers: temp_answ = Answer(text=a.get('text'), correct=a.get('correct')) temp_answ.save() temp_answ.question.connect(new_question) return new_question
def test_sorting_by_order(self): question_key = Question.create(text="Q1", order=1) Answer.create(question_key=question_key, text="Answer 1", value=1, order=3) Answer.create(question_key=question_key, text="Answer 2", value=2, order=1) Answer.create(question_key=question_key, text="Answer 3", value=3, order=2) answers = Answer.by_question(question_key) self.assertEquals(1, answers[0].order) self.assertEquals(2, answers[1].order) self.assertEquals(3, answers[2].order)
def to_message(cls, entity, message_class): message = QuestionMessage(key=entity.key.urlsafe(), text=entity.text, order=entity.order, answers=[]) answers = Answer.by_question(entity.key) for answer in answers: message.answers.append( AnswerMessage(key=answer.key.urlsafe(), text=answer.text, order=answer.order, value=answer.value)) return message
def init(self): Question.clear() Answer.clear() q1 = Question.create(text="Preferred date", order=1) Answer.create(question_key=q1, text="Thu 18/02", order=1, value=1) Answer.create(question_key=q1, text="Fri 19/02", order=2, value=2) Answer.create(question_key=q1, text="Sat 20/02", order=3, value=3) Answer.create(question_key=q1, text="Sun 21/02", order=4, value=4) Answer.create(question_key=q1, text="Mon 22/02", order=5, value=5) Answer.create(question_key=q1, text="Tue 23/02", order=6, value=6) Answer.create(question_key=q1, text="Wed 24/02", order=7, value=7) Answer.create(question_key=q1, text="Thu 25/02", order=8, value=8) Answer.create(question_key=q1, text="Fri 26/02", order=9, value=9) q2 = Question.create(text="Preferred place", order=2) Answer.create(question_key=q2, text="Reading", order=1, value=1) Answer.create(question_key=q2, text="Staines", order=2, value=2) Answer.create(question_key=q2, text="London", order=3, value=3) User.create('Igor') User.create('Justin') User.create('Michael') User.create('Adam') User.create('Victor') User.create('Yargi') User.create('Varun') return 200
def api_all_answers(self): self.context['data'] = Answer.all()
def test_delete_answer_removes_from_clue(self): Answer.build_key(self.answer_uid).delete() clue = Clue.get_by_id(self.clue_uid) self.assertNotIn(self.answer_uid, clue.answer_uids)
def resolve_answer(root, info, _id): answer = AnswerModel.find_by_id(_id) return answer
def to_message(cls, entity, message_class): message = QuestionMessage(key=entity.key.urlsafe(), text=entity.text, order=entity.order, answers=[]) answers = Answer.by_question(entity.key) for answer in answers: message.answers.append(AnswerMessage(key=answer.key.urlsafe(), text=answer.text, order=answer.order, value=answer.value)) return message
def test_deleting_clue_deletes_answer(self): Clue.build_key(uid=self.clue_uid).delete() self.assertIsNone(Answer.get_by_id(self.answer_uid))
def test_deleting_story_deletes_answer(self): Story.build_key(self.story_uid).delete() self.assertIsNone(Answer.get_by_id(self.answer_uid))
class Meta: prefixes = ('api',) components = (messages.Messaging, ) Message = Answer.message()