def _populate_db(): """ Pre-populate database with 2 questionnaires.One of the questionnaire has 3 questions and 1 answer for each question. The other questionnaire has no question. """ _questionnaire = Questionnaire( title="test-questionnaire-1", description="test-questionnaire" ) db.session.add(_questionnaire) db.session.add(Questionnaire( title="test-questionnaire-2", description="test-questionnaire" )) for i in range(1, 4): q = Question( title="test-question-{}".format(i), description="test-question", questionnaire=_questionnaire ) a = Answer( userName="******".format(i), content="test-answer", question=q ) db.session.add(q) db.session.add(a) db.session.commit()
def _get_answer(_question, _userName): """ An example of answer to a specific question """ answer = Answer(content="Everyday is okay!", question=_question, userName=_userName) return answer
def test_null_answer(db_handle): """ Tests whether an answer can be created without its content or userName. """ questionnaire = _get_questionnaire() question = _get_question(questionnaire) answer1 = Answer(question=question, userName="******") answer2 = Answer(content="This is a non-question test", userName="******") answer3 = Answer(question=question, content="This is a non-userName test") db.session.add(questionnaire) db.session.add(question) db.session.add(answer1) db.session.add(answer2) db.session.add(answer3) with pytest.raises(exc.IntegrityError): db.session.commit()
def create_answer(_content, _question, _userName): """ Create an answer to one question. Content of the answer and the username of the user are required. It's also mandatory to put it into a specific questionnaire. """ answer = Answer(content=_content, question=_question, userName=_userName) db.session.add(answer) print("A new answer is created by", _userName) print("Content: ", _content) print("--------------------") return answer
def search_ingest(query, question_id) -> Union[Answer, None]: knowledge_pdf_content = current_app.ingest_connector.search(query) if not knowledge_pdf_content: return None answer = Answer( text=knowledge_pdf_content.content, question_fk=question_id, predictor_fk=Predictor.query.filter_by(name="ingest").first().id) db.session.add(answer) db.session.commit() return answer
def search_elastic(query, question_id, mode="question") -> Union[Answer, None]: if mode == "question": knowledge_question: KnowledgeQuestion = KnowledgeQuestion.search(query) if not knowledge_question: return None answer_text = knowledge_question.knowledge_answer.text else: knowledge_answer: KnowledgeAnswer = KnowledgeAnswer.search(query) if not knowledge_answer: return None answer_text = knowledge_answer.text answer = Answer( text=answer_text, question_fk=question_id, predictor_fk=Predictor.query.filter_by(name=f"qa_{mode}").first().id) db.session.add(answer) db.session.commit() return answer
import csv from app import db, Question, Answer with open("data.csv", 'rU') as csvfile: reader = csv.DictReader(csvfile) for row in reader: question = row['Question'] choices = row['Choices'] answer = row['Answer'] qid = row['QID'] q = Question(question, choices, qid) db.session.add_all([q]) db.session.commit() a = Answer(answer, qid) db.session.add_all([a]) db.session.commit()