Example #1
0
    def add_course(self, num_students=5, num_questions=1, num_groups=0, num_answers='#'):
        self.course = CoursesFactory()
        self.instructor = UsersFactory(usertypeforsystem=DefaultFixture.SYS_ROLE_INSTRUCTOR)
        self.course.enroll(self.instructor, UserTypesForCourse.TYPE_INSTRUCTOR)
        self.ta = UsersFactory(usertypeforsystem=DefaultFixture.SYS_ROLE_NORMAL)
        self.course.enroll(self.ta, UserTypesForCourse.TYPE_TA)

        self.add_students(num_students)

        self.add_questions(num_questions)
        # create a shortcut for first question as it is frequently used
        self.question = self.questions[0]

        self.add_groups(num_groups)

        self.add_answers(num_answers)

        return self
Example #2
0
    def __init__(self):
        # initialize with default values in cases of DefaultFixture being unavailable
        DEFAULT_CRITERIA = DefaultFixture.DEFAULT_CRITERIA if DefaultFixture.DEFAULT_CRITERIA else \
            Criteria.query.filter_by(name="Which is better?").first()

        SYS_ROLE_INSTRUCTOR = DefaultFixture.SYS_ROLE_INSTRUCTOR if DefaultFixture.SYS_ROLE_INSTRUCTOR else \
            UserTypesForSystem.query.filter_by(name=UserTypesForSystem.TYPE_INSTRUCTOR).first()
        SYS_ROLE_NORMAL = DefaultFixture.SYS_ROLE_NORMAL if DefaultFixture.SYS_ROLE_NORMAL else \
            UserTypesForSystem.query.filter_by(name=UserTypesForSystem.TYPE_NORMAL).first()

        COURSE_ROLE_INSTRUCTOR = DefaultFixture.COURSE_ROLE_INSTRUCTOR if DefaultFixture.COURSE_ROLE_INSTRUCTOR else \
            UserTypesForCourse.query.filter_by(name=UserTypesForCourse.TYPE_INSTRUCTOR).first()
        COURSE_ROLE_STUDENT = DefaultFixture.COURSE_ROLE_STUDENT if DefaultFixture.COURSE_ROLE_STUDENT else \
            UserTypesForCourse.query.filter_by(name=UserTypesForCourse.TYPE_STUDENT).first()

        # create courses
        self.courses = []
        for course_name in self.COURSE_NAMES:
            course = CoursesFactory(name=course_name,
                                    description=course_name +
                                    " Course Description")
            self.courses.append(course)
        # insert default criteria into each course
        for course in self.courses:
            CriteriaAndCoursesFactory(criterion=DEFAULT_CRITERIA,
                                      course=course)
        # create instructors
        for instructor_name in self.INSTRUCTOR_NAMES:
            self.instructor = UsersFactory(
                username=instructor_name,
                usertypeforsystem=SYS_ROLE_INSTRUCTOR)
        # create students
        self.students = []
        for student_name in self.STUDENT_NAMES:
            student = UsersFactory(username=student_name,
                                   usertypeforsystem=SYS_ROLE_NORMAL)
            self.students.append(student)
        # enrol students and instructor in half of the courses, also create questions and answers
        skip = True
        generator = TechnobabbleGenerator()
        content = "This is some place holder content for this question. Yay!"
        for course in self.courses:
            skip = not skip
            if skip:
                continue
            # enrol instructor
            CoursesAndUsersFactory(user=self.instructor,
                                   course=course,
                                   usertypeforcourse=COURSE_ROLE_INSTRUCTOR)
            # enrol students
            for student in self.students:
                CoursesAndUsersFactory(user=student,
                                       course=course,
                                       usertypeforcourse=COURSE_ROLE_STUDENT)
            # create 5 questions by the instructor
            for i in range(5):
                minutes = random.randint(0, 59)
                created = datetime.utcnow() - timedelta(days=1,
                                                        minutes=minutes)
                post = PostsFactory(course=course,
                                    user=self.instructor,
                                    content=content,
                                    created=created)
                postforquestion = PostsForQuestionsFactory(
                    post=post, title=generator.get_question())
                # insert default criteria into question
                CriteriaAndPostsForQuestionsFactory(criterion=DEFAULT_CRITERIA,
                                                    question=postforquestion)
                # create answers by each student for this question
                for student in self.students:
                    minutes = random.randint(0, 59)
                    created = datetime.utcnow() - timedelta(minutes=minutes)
                    post = PostsFactory(course=course,
                                        user=student,
                                        content=generator.get_answer(),
                                        created=created)
                    PostsForAnswersFactory(post=post, question=postforquestion)
Example #3
0
class TestFixture:
    def __init__(self):
        self.course = self.question = None
        self.instructor = self.ta = None
        self.students = []
        self.questions = []
        self.answers = []
        self.groups = []
        self.unauthorized_instructor = UsersFactory(usertypeforsystem=DefaultFixture.SYS_ROLE_INSTRUCTOR)
        self.unauthorized_student = UsersFactory(usertypeforsystem=DefaultFixture.SYS_ROLE_NORMAL)

    def add_course(self, num_students=5, num_questions=1, num_groups=0, num_answers='#'):
        self.course = CoursesFactory()
        self.instructor = UsersFactory(usertypeforsystem=DefaultFixture.SYS_ROLE_INSTRUCTOR)
        self.course.enroll(self.instructor, UserTypesForCourse.TYPE_INSTRUCTOR)
        self.ta = UsersFactory(usertypeforsystem=DefaultFixture.SYS_ROLE_NORMAL)
        self.course.enroll(self.ta, UserTypesForCourse.TYPE_TA)

        self.add_students(num_students)

        self.add_questions(num_questions)
        # create a shortcut for first question as it is frequently used
        self.question = self.questions[0]

        self.add_groups(num_groups)

        self.add_answers(num_answers)

        return self

    def add_answers(self, num_answers):
        if num_answers == '#':
            num_answers = len(self.students) * len(self.questions)
        if len(self.students) * len(self.questions) < num_answers:
            raise ValueError(
                "Number of answers({}) must be equal or smaller than number of students({}) "
                "multiple by number of questions({})".format(num_answers, len(self.students), len(self.questions))
            )
        for i in range(num_answers):
            for question in self.questions:
                post = PostsFactory(course=self.course, user=self.students[i % len(self.students)])
                answer = PostsForAnswersFactory(question=question, post=post)
                # half of the answers have scores
                if i < num_answers/2:
                    ScoreFactory(answer=answer, criteriaandquestions_id=question.criteria[0].id, score=random.random() * 5)
                self.answers.append(answer)

        return self

    def add_groups(self, num_groups):
        student_per_group = int(len(self.students) / num_groups) if num_groups is not 0 else 0
        for idx in range(num_groups):
            group = GroupsFactory(course=self.course)
            self.groups.append(group)
            db.session.commit()
            # slice student list and enroll them into groups
            group.enroll(self.students[idx * student_per_group:min((idx + 1) * student_per_group, len(self.students))])

        return self

    def add_questions(self, num_questions=1, is_answer_period_end=False):
        for _ in range(num_questions):
            post = PostsFactory(course=self.course)
            answer_end = datetime.datetime.now() - datetime.timedelta(
                days=2) if is_answer_period_end else datetime.datetime.now() + datetime.timedelta(days=7)
            question = PostsForQuestionsFactory(post=post, answer_end=answer_end)
            CriteriaAndPostsForQuestionsFactory(criterion=DefaultFixture.DEFAULT_CRITERIA, question=question)
            self.questions.append(question)
        db.session.commit()

        return self

    def add_students(self, num_students):
        students = []
        for _ in range(num_students):
            student = UsersFactory(usertypeforsystem=DefaultFixture.SYS_ROLE_NORMAL)
            students.append(student)
        self.students += students
        self.course.enroll(students)

        return self
Example #4
0
 def create_course(self):
     course = CoursesFactory()
     db.session.commit()
     return course