Esempio n. 1
0
    def test_create_question(self):

        question = Question()

        data = {
            'questionText': 'Example text',
            'multi': 'false',
            'options': ['yes', 'no'],
            'text': 'false',
            'mandatory': 'true'
        }

        question.load_from_dict(data)

        write_id = question.write_to_db(DATABASE_FILENAME)

        question = None

        question = Question()
        question.load_from_db(DATABASE_FILENAME, write_id)

        self.assertEqual(question.get_id(), write_id)
        self.assertEqual(question.get_question_text(), 'Example text')
        self.assertEqual(question.get_type(), 'single')
        self.assertEqual(question.get_mandatory(), True)
        self.assertEqual(question.get_options()[0], 'yes')
def write_question(form):
    question = Question()
    question.load_from_dict(form)
    if int(form['saved_id']) != -1:
        return question.update_db(QUESTIONS_FILENAME, int(form['saved_id']))
    else:
        return question.write_to_db(QUESTIONS_FILENAME)
Esempio n. 3
0
    def write_to_db(self, filename):
        existing_questions = []
        for qid in [
                x[0] for x in db_select(filename, "SELECT ID FROM QUESTIONS")
        ]:
            question = Question()
            question.load_from_db(filename, qid)
            existing_questions.append(question)

        survey_ids = [
            i[0] for i in db_select(filename, "SELECT ID FROM SURVEYS")
        ]
        if survey_ids == []:
            max_survey_id = 0
        else:
            max_survey_id = max(survey_ids)
        db_execute(
            filename,
            """INSERT INTO SURVEYS (ID, START, END, COURSE, SEMESTER, STATE) 
								VALUES ("{0}", "{1}", "{2}", "{3}", "{4}", "{5}")
								""".format(str(max_survey_id + 1), str(self._start), str(self._end),
                   self._course.name, self._course.semester, str(self._state)))

        i = 1
        for question in self._questions:
            exists = False
            for existing_question in existing_questions:
                if not exists and question.matches(existing_question):
                    db_execute(
                        filename,
                        """INSERT INTO INCLUDE (SURVEYID, QUESTIONID, POSITION)
											VALUES ("{0}", "{1}", "{2}")""".format(
                            str(max_survey_id + 1),
                            str(existing_question.get_id()), i + 1))
                    exists = True
            if not exists:
                write_id = question.write_to_db(filename)
                db_execute(
                    filename,
                    """INSERT INTO INCLUDE (SURVEYID, QUESTIONID, POSITION)
										VALUES ("{0}", "{1}", "{2}")""".format(str(max_survey_id + 1),
                                                 str(write_id), i + 1))
            i += 1

        return max_survey_id + 1
Esempio n. 4
0
    def test_edit_saved_question(self):
        question = Question()

        data = {
            'questionText': 'Example text',
            'multi': 'false',
            'options': ['yes', 'no'],
            'text': 'false',
            'mandatory': 'false'
        }

        question.load_from_dict(data)

        write_id = question.write_to_db(DATABASE_FILENAME)

        question = None

        question = Question()

        data = {
            'questionNum': write_id,
            'questionText': 'Example other text',
            'multi': 'true',
            'options': ['maybe', 'not'],
            'text': 'false',
            'mandatory': 'true'
        }

        question.load_from_dict(data)
        question.update_db(DATABASE_FILENAME, write_id)

        question = None

        question = Question()

        question.load_from_db(DATABASE_FILENAME, write_id)

        self.assertNotEqual(question.get_question_text(), 'Example text')
        self.assertEqual(question.get_question_text(), 'Example other text')
        self.assertNotEqual(question.get_type(), 'single')
        self.assertEqual(question.get_type(), 'multi')
        self.assertNotEqual(question.get_options(), ['yes', 'no'])
        self.assertEqual(question.get_options(), ['maybe', 'not'])
        self.assertNotEqual(question.get_mandatory(), False)
        self.assertEqual(question.get_mandatory(), True)
Esempio n. 5
0
    def test_create_optional_question(self):
        question = Question()

        data = {
            'questionText': 'Example text',
            'multi': 'false',
            'options': ['yes', 'no'],
            'text': 'false',
            'mandatory': 'false'
        }

        question.load_from_dict(data)

        write_id = question.write_to_db(DATABASE_FILENAME)

        question = None

        question = Question()
        question.load_from_db(DATABASE_FILENAME, write_id)

        self.assertEqual(question.get_mandatory(), False)
Esempio n. 6
0
    def test_create_text_question_no_options(self):
        question = Question()

        data = {
            'questionText': 'Example text',
            'multi': 'false',
            'options': [],
            'text': 'true',
            'mandatory': 'false'
        }

        question.load_from_dict(data)

        write_id = question.write_to_db(DATABASE_FILENAME)

        question = None

        question = Question()
        question.load_from_db(DATABASE_FILENAME, write_id)

        self.assertEqual(question.get_type(), 'text')
        self.assertEqual(question.get_options(), [])