Exemplo n.º 1
0
    def test_insert(self):
        print('')
        print('Creating an admin user...')
        role = Role.get_by_name('admin')
        self.assertTrue(role is not None)
        user = User(username='******', role=role, password='******')
        print('Admin user successfully created.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')
        print('Adding and committing an admin user into the database...')
        db.session.add(user)
        db.session.commit()
        self.assertTrue(
            User.query.filter_by(username='******').first() is not None)
        print('Admin user successfully added and committed into the database.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')

        print('Creating a course...')
        c = Course(course_code='TEST')
        print('Course successfully created.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')
        print('Trying to add and commit course into database...')
        db.session.add(c)
        db.session.commit()
        self.assertTrue(
            Course.query.filter_by(course_code='TEST').first() is not None)
        print('Course successfully added and committed into database.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')

        print('Creating a survey...')
        survey = Survey.create(description='blah test',
                               times=['1', '2'],
                               owner_id=user.id,
                               course=c.course_code)
        self.assertTrue(
            Survey.query.filter_by(
                description='blah test').first() is not None)
        print('Survey successfully created.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')
        print('Creating a question for the survey...')
        question = Question.create(description="a test question",
                                   owner_id=user.id,
                                   optional=False,
                                   q_type=1)
        self.assertTrue(question is not None)
        print('Question successfully created.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')
        print('Assigning question to survey...')
        survey.set_questions([question.id])
        self.assertTrue(survey.questions.all() is not None)
        print('Question successfully assigned.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')
        print('')
    def test_core_database_functionality(self):
        # admin_create_survey
        print('')
        print('Creating an administrator user...')
        role = Role.get_by_name('admin')
        self.assertTrue(role is not None)
        user = User(username='******', role=role, password='******')
        print('Adding and commiting data to database...')
        db.session.add(user)
        db.session.commit()
        self.assertTrue(User.query.filter_by(
            username='******').first() is not None)
        print('Successfully added and committed data to database.')
        print('Administrator user successfully created.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')

        print('Creating course...')
        c = Course(course_code='TEST')
        print('Adding and commiting data to database...')
        db.session.add(c)
        db.session.commit()
        self.assertTrue(Course.query.filter_by(
            course_code='TEST').first() is not None)
        print('Successfully added and committed data to database.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')

        print('Creating a survey with the course code...')
        survey = Survey.create(description='blah test', times=['1', '2'],
                               owner_id=user.id, course=c.course_code)
        self.assertTrue(Survey.query.filter_by(description='blah test')
                        .first() is not None)
        self.assertTrue(Survey.query.filter_by(description='blah test')
                        .first().status == 'review')
        print('Survey successfully created with the course code.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')
        print('Creating questions for survey...')
        question1 = Question.create(
            description="a test question1", owner_id=user.id, optional=False, q_type=2)
        question2 = Question.create(
            description="a test question2", owner_id=user.id, optional=True, q_type=2)
        self.assertTrue(question1 is not None)
        self.assertTrue(question2 is not None)
        print('Successfully created questions.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')
        print('Setting questions to survey...')
        survey.set_questions([question1.id, question2.id])
        self.assertTrue(survey.questions.all() is not None)
        print('Successfully assigned questions to survey.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')

        # staff_review_survey
        print('Creating staff user...')
        role = Role.get_by_name('staff')
        self.assertTrue(role is not None)
        user = User(username='******', role=role, password='******')
        print('Adding and commiting data to database...')
        db.session.add(user)
        db.session.commit()
        self.assertTrue(User.query.filter_by(username='******')
                        .first() is not None)
        survey = Survey.query.filter_by(description='blah test').first()
        self.assertTrue(survey is not None)
        self.assertTrue(survey.status == 'review')
        print('Successfully added and committed data to database.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')
        print('Setting survey phase to open...')
        survey.status = 'open'
        self.assertTrue(survey.status == 'open')
        db.session.add(survey)
        db.session.commit()
        print('Successfully set survey phase to open and made changes to database.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')

        # student_answer_survey
        print('Creating a student user...')
        role = Role.get_by_name('student')
        self.assertTrue(role is not None)
        user = User(username='******', role=role, password='******')
        db.session.add(user)
        db.session.commit()
        print('Successfully created student user.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')
        print('Creating a survey...')
        survey = Survey.query.filter_by(description='blah test').first()
        self.assertTrue(survey is not None)
        self.assertTrue(survey.status == 'open')
        print('Successfully created a survey.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')
        print('Creating answers for the survey...')
        a = Answer.create(survey_id=survey.id, owner_id=user.id)
        self.assertTrue(a is not None)
        self.assertTrue(a.survey_id == survey.id)
        self.assertTrue(a.owner_id == user.id)
        for question in survey.questions.all():
            ae = AnswerEntity.create(answer_id=a.id,
                                     question_id=question.id,
                                     answer_content='blah'
                                     )
            self.assertTrue(ae is not None)
            self.assertTrue(ae.question_id == question.id)
            self.assertTrue(ae.answer_id == a.id)
        print('Successfully created answers for the survey.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')

        # admin_close_survey
        print('Creating an administrator user...')
        user = User.query.filter_by(username='******').first()
        self.assertTrue(user is not None)
        print('Administrator user successfully created.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')
        print('Creating a survey...')
        survey = Survey.query.filter_by(description='blah test').first()
        self.assertTrue(survey is not None)
        print('Survey successfully created.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')
        print('Setting survey phase to closed...')
        survey.status = 'closed'
        db.session.add(survey)
        db.session.commit()
        self.assertTrue(survey.status == 'closed')
        print('Successfully set survey to closed.')
        print('\x1b[0;32;40m' + 'pass' + '\x1b[0m')
        print('')