def test_view_fact_detail_with_id(self): studybot.create_user(DUMMY_SENDER_ID) fact1 = create_dummy_fact("Dummy Question 1", "Dummy Answer 1") studybot.set_user(studybot.ConvoState(fact1.user_id, studybot.State.DEFAULT)) studybot.current_user.tmp_fact = fact1 studybot.create_fact() fact_id1 = studybot.current_user.tmp_fact.id payload = get_payload("View fact with id " + str(fact_id1), [get_intent_object("view_detailed_fact")]) headers = { 'Content-type': 'application/json' } response = self.app.post('/', data=json.dumps(payload), headers=headers) self.assertEqual(response.status_code, 200) self.assertNotEqual(RESPONSES, []) self.assertEqual(len(RESPONSES), 2) fact = studybot.get_fact(fact_id1) return_msg = "%d. %s\n" % (fact.id, fact.question) return_msg += "Answer: %s\n" % fact.answer return_msg += "Easiness: %s\n" % fact.easiness return_msg += "Consecutive Correct Answers: %s\n" % fact.consecutive_correct_answers return_msg += "Next Study Time: %s\n" % studybot.format_date_time(fact.next_due_date) return_msg += "Last Seen: %s\n\n" % studybot.format_date_time(fact.last_seen) self.assertEqual(RESPONSES[0]["message"]["text"], "Here's the fact.") self.assertEqual(RESPONSES[1]["message"]["text"], return_msg)
def test_view_facts(self): studybot.create_user(DUMMY_SENDER_ID) fact1 = create_dummy_fact("Dummy Question 1", "Dummy Answer 1") studybot.set_user(studybot.ConvoState(fact1.user_id, studybot.State.DEFAULT)) studybot.current_user.tmp_fact = fact1 studybot.create_fact() fact_id1 = studybot.current_user.tmp_fact.id fact2 = create_dummy_fact("Dummy Question 2", "Dummy Answer 2") studybot.set_user(studybot.ConvoState(fact2.user_id, studybot.State.DEFAULT)) studybot.current_user.tmp_fact = fact2 studybot.create_fact() fact_id2= studybot.current_user.tmp_fact.id payload = get_payload("View facts", [get_intent_object("view_facts")]) headers = { 'Content-type': 'application/json' } response = self.app.post('/', data=json.dumps(payload), headers=headers) self.assertEqual(response.status_code, 200) self.assertNotEqual(RESPONSES, []) self.assertEqual(len(RESPONSES), 3) bot_msg1 = "%d. %s\n" % (fact_id1, fact1.question) bot_msg2 = "%d. %s\n" % (fact_id2, fact2.question) self.assertEqual(RESPONSES[0]["message"]["text"], "Ok, here are the facts we have.") self.assertEqual(RESPONSES[1]["message"]["text"], bot_msg1) self.assertEqual(RESPONSES[2]["message"]["text"], bot_msg2)
def test_delete_fact_cancel(self): studybot.create_user(DUMMY_SENDER_ID) fact = create_dummy_fact("Dummy Question", "Dummy Answer") studybot.set_user(studybot.ConvoState(fact.user_id, studybot.State.DEFAULT)) studybot.current_user.tmp_fact = fact studybot.create_fact() fact_id = studybot.current_user.tmp_fact.id payload = get_payload("I want to delete fact " + str(fact_id), [get_intent_object("delete_fact")]) headers = { 'Content-type': 'application/json' } response = self.app.post('/', data=json.dumps(payload), headers=headers) self.assertEqual(response.status_code, 200) payload = get_payload("No", [get_intent_object("negation")]) headers = { 'Content-type': 'application/json' } response = self.app.post('/', data=json.dumps(payload), headers=headers) self.assertEqual(response.status_code, 200) self.assertNotEqual(RESPONSES, []) self.assertEqual(len(RESPONSES), 2) bot_msg = "Are you sure you want to delete this fact?\n" bot_msg += "Question: %s\n" % fact.question self.assertEqual(RESPONSES[0]["message"]["text"], bot_msg) self.assertEqual(RESPONSES[1]["message"]["text"], "Ok, I won't delete this fact.") fact = studybot.get_fact(fact_id) self.assertIsNotNone(fact)
def test_update_fact(self): studybot.create_user(DUMMY_SENDER_ID) dummy_fact = create_dummy_fact("Dummy Question", "Dummy Answer") studybot.set_user(studybot.ConvoState(dummy_fact.user_id, studybot.State.DEFAULT)) studybot.current_user.tmp_fact = dummy_fact studybot.create_fact() fact_id = studybot.current_user.tmp_fact.id payload = get_payload("I want to change a fact", [get_intent_object("change_fact")]) headers = { 'Content-type': 'application/json' } response = self.app.post('/', data=json.dumps(payload), headers=headers) self.assertEqual(response.status_code, 200) payload = get_payload("Fact with id " + str(fact_id), [get_intent_object("change_fact")]) headers = { 'Content-type': 'application/json' } response = self.app.post('/', data=json.dumps(payload), headers=headers) self.assertEqual(response.status_code, 200) payload = get_payload("This is a question?", [get_intent_object("change_fact")]) headers = { 'Content-type': 'application/json' } response = self.app.post('/', data=json.dumps(payload), headers=headers) self.assertEqual(response.status_code, 200) payload = get_payload("This is answer.", [get_intent_object("change_fact")]) headers = { 'Content-type': 'application/json' } response = self.app.post('/', data=json.dumps(payload), headers=headers) self.assertEqual(response.status_code, 200) self.assertNotEqual(RESPONSES, []) self.assertEqual(len(RESPONSES), 4) self.assertEqual(RESPONSES[0]["message"]["text"], "Ok, which fact do you want to change?") self.assertEqual(RESPONSES[1]["message"]["text"], "Ok, let's update that fact. What is the question?") self.assertEqual(RESPONSES[2]["message"]["text"], "Thanks, what's the answer to that question?") self.assertEqual(RESPONSES[3]["message"]["text"], get_fact_created_updated_message(False)) fact = studybot.get_fact(studybot.current_user.tmp_fact.question) self.assertIsNotNone(fact) self.assertNotEqual(dummy_fact.question, fact.question) self.assertNotEqual(dummy_fact.answer, fact.answer)
def test_study_fact_low_perf(self): studybot.create_user(DUMMY_SENDER_ID) fact1 = create_dummy_fact("Dummy Question 1", "Dummy Answer 1") studybot.set_user(studybot.ConvoState(fact1.user_id, studybot.State.DEFAULT)) studybot.current_user.tmp_fact = fact1 studybot.create_fact() fact1.next_due_date = studybot.datetime.utcnow() studybot.db.session.commit() payload = get_payload("Study time!", [get_intent_object("study_next_fact")]) print(payload) headers = { 'Content-type': 'application/json' } response = self.app.post('/', data=json.dumps(payload), headers=headers) self.assertEqual(response.status_code, 200) payload = get_payload("Dummy Answer 1", [get_intent_object("study_next_fact")]) headers = { 'Content-type': 'application/json' } response = self.app.post('/', data=json.dumps(payload), headers=headers) self.assertEqual(response.status_code, 200) payload = get_payload("2", [get_intent_object("study_next_fact")]) headers = { 'Content-type': 'application/json' } response = self.app.post('/', data=json.dumps(payload), headers=headers) self.assertEqual(response.status_code, 200) self.assertNotEqual(RESPONSES, []) self.assertEqual(len(RESPONSES), 3) self.assertEqual(RESPONSES[0]["message"]["text"], "Ok, let's study!\n%s" % fact1.question) self.assertEqual(RESPONSES[1]["message"]["text"], "Here is the answer:\n%s\nHow hard was that on a scale from 0 (impossible) to 5 (trivial)?" % fact1.answer) self.assertEqual(RESPONSES[2]["message"]["text"], "Got it, fact studied!") fact = studybot.get_fact(fact1.id) self.assertEqual(fact.consecutive_correct_answers, 0) self.assertLessEqual(fact.easiness, fact1.easiness) self.assertGreaterEqual(fact.next_due_date, fact1.next_due_date)
def test_study_fact_invalid_perf_value(self): studybot.create_user(DUMMY_SENDER_ID) fact1 = create_dummy_fact("Dummy Question 1", "Dummy Answer 1") studybot.set_user(studybot.ConvoState(fact1.user_id, studybot.State.DEFAULT)) studybot.current_user.tmp_fact = fact1 studybot.create_fact() fact1.next_due_date = studybot.datetime.utcnow() studybot.db.session.commit() payload = get_payload("Study time!", [get_intent_object("study_next_fact")]) print(payload) headers = { 'Content-type': 'application/json' } response = self.app.post('/', data=json.dumps(payload), headers=headers) self.assertEqual(response.status_code, 200) payload = get_payload("Dummy Answer 1", [get_intent_object("study_next_fact")]) headers = { 'Content-type': 'application/json' } response = self.app.post('/', data=json.dumps(payload), headers=headers) self.assertEqual(response.status_code, 200) payload = get_payload("11", [get_intent_object("study_next_fact")]) headers = { 'Content-type': 'application/json' } response = self.app.post('/', data=json.dumps(payload), headers=headers) self.assertEqual(response.status_code, 200) self.assertNotEqual(RESPONSES, []) self.assertEqual(len(RESPONSES), 3) self.assertEqual(RESPONSES[0]["message"]["text"], "Ok, let's study!\n%s" % fact1.question) self.assertEqual(RESPONSES[1]["message"]["text"], "Here is the answer:\n%s\nHow hard was that on a scale from 0 (impossible) to 5 (trivial)?" % fact1.answer) self.assertEqual(RESPONSES[2]["message"]["text"], "I didn't get a number from that, can you try again on a scale from 0 to 5?")