示例#1
0
def testdata_posts(app, test_user):
    with app.app_context():
        # create categories
        categories = []
        for n in range(0, 10):
            category = Category(name='name%d' % n)
            categories.append(category)
            db.session.add(category)

        # create courses
        courses = []
        for n in range(0, 10):
            course = Course(name='name%d' % n,
                            offering_unit='ou%d' % n,
                            subject='subject%d' % n,
                            course_number='cn%d' % n)
            courses.append(course)
            db.session.add(course)

        # create semesters
        semesters = []
        for n in range(0, 10):
            semester = Semester(year=n, season='Fall')
            semesters.append(semester)
            db.session.add(semester)

        # create posts
        posts = []
        for n in range(0, 10):
            post = Post(title='title%d' % n,
                        content='content%d' % n,
                        timestamp=datetime(2018 + n, 1, 1),
                        author_id=test_user.id,
                        course=courses[n],
                        category=categories[n],
                        semester=semesters[n])

            posts.append(post)
            db.session.add(post)

        db.session.commit()

        # create comments
        comments = []
        for n in range(0, 10):
            comment = Comment(author_id=test_user.id,
                              content='content%d' % n,
                              post=posts[n])
            comments.append(comment)
            db.session.add(comment)

        db.session.commit()

        posts_json = marshal(posts, post_marshal_model)
        comments_json = marshal(comments, comment_marshal_model)

        return posts_json, comments_json
示例#2
0
def testdata_user_courses(app, test_user):
    with app.app_context():
        user = User.query.get(test_user.id)

        # courses assigned to test user by default
        user_courses = []
        # other courses in database
        other_courses = []

        for n in range(0, 50):
            course = Course(name='name%d' % n,
                            offering_unit='ou%d' % n,
                            subject='sb%d' % n,
                            course_number='cn%d' % n)

            user_courses.append(course)

            db.session.add(course)

            user.courses.append(course)

        for n in range(50, 100):
            course = Course(name='name%d' % n,
                            offering_unit='ou%d' % n,
                            subject='sb%d' % n,
                            course_number='cn%d' % n)

            other_courses.append(course)

            db.session.add(course)

        db.session.commit()

        user_courses_json = marshal(user_courses, course_marshal_model)

        # courses are sorted by name by default
        user_courses_json = sorted(user_courses_json, key=lambda d: d['name'])

        other_courses_json = marshal(other_courses, course_marshal_model)

        return user_courses_json, other_courses_json
示例#3
0
def test_invalid_course_model(app, name, offering_unit, subject,
                              course_number):
    with app.app_context():
        course = Course(name=name,
                        offering_unit=offering_unit,
                        subject=subject,
                        course_number=course_number)

        with pytest.raises(IntegrityError) as excinfo:
            db.session.add(course)
            db.session.commit()

        db.session.rollback()
        found = Course.query.filter_by(name=name).first()
        assert found is None
示例#4
0
def test_valid_course_model(app, name, offering_unit, subject, course_number):
    with app.app_context():
        course = Course(name=name,
                        offering_unit=offering_unit,
                        subject=subject,
                        course_number=course_number)

        db.session.add(course)
        db.session.commit()

        found = Course.query.filter_by(name=name).first()
        assert found is not None

        assert found.name == name
        assert found.offering_unit == offering_unit
        assert found.subject == subject
        assert found.course_number == course_number
示例#5
0
def test_valid_comment_model(app):
    with app.app_context():
        title = 'example title content'
        content = 'example comment content'

        author = User(email='email', password='******', is_verified=True)
        course = Course(name='name',
                        offering_unit='1',
                        subject='2',
                        course_number='3')
        semester = Semester(year=2018, season='fall')
        category = Category(name='name')

        db.session.add(author)
        db.session.add(course)
        db.session.add(semester)
        db.session.add(category)

        db.session.commit()

        post = Post(title=title,
                    content='example post content',
                    author_id=author.id,
                    semester_id=semester.id,
                    category_id=category.id,
                    course_id=course.id)

        db.session.add(post)
        db.session.commit()

        comment = Comment(content=content,
                          post_id=post.id,
                          author_id=author.id)

        db.session.add(comment)
        db.session.commit()

        found = Comment.query.filter_by(id=comment.id).first()
        assert found is not None

        assert found.content == content
        assert found.timestamp is not None

        assert found.author.email == author.email
示例#6
0
def testdata_courses(app):
    with app.app_context():
        courses = []
        for n in range(0, 100):
            course = Course(name='name%d' % n,
                            offering_unit='ou%d' % n,
                            subject='sb%d' % n,
                            course_number='cn%d' % n)
            courses.append(course)
            db.session.add(course)

        db.session.commit()

        courses_json = marshal(courses, course_marshal_model)

        # courses are sorted by name by default
        courses_json = sorted(courses_json, key=lambda d: d['name'])

        return courses_json
示例#7
0
def test_valid_user_model(app, email, password, is_verified):
    with app.app_context():
        user = User(email=email, password=password, is_verified=is_verified)

        db.session.add(user)
        db.session.commit()

        found = User.query.filter_by(email=email).first()
        assert found is not None

        # test user properties
        assert found.email == email
        assert found.password == password
        assert found.is_verified == is_verified

        # test courses relationship
        # create example courses
        courses = []
        for n in range(5):
            course = Course(name='n%d' % n,
                            offering_unit='ou%d' % n,
                            subject='s%d' % n,
                            course_number='cn%d' % n)
            courses.append(course)
            db.session.add(course)

        db.session.commit()

        # add courses to user
        # thanks to https://stackoverflow.com/a/45047925
        user.courses.extend(courses)

        db.session.commit()

        assert len(user.courses) == len(courses)
        for course in courses:
            assert course in user.courses
示例#8
0
def test_valid_post_model(app):
    with app.app_context():
        title = 'example title content'
        content = 'example post content'
        is_archived = False
        due_date = date(2018, 1, 1)

        author = User(email='email', password='******', is_verified=True)
        course = Course(name='name',
                        offering_unit='1',
                        subject='2',
                        course_number='3')
        semester = Semester(year=2018, season='fall')
        category = Category(name='name')

        db.session.add(author)
        db.session.add(course)
        db.session.add(semester)
        db.session.add(category)

        db.session.commit()

        post = Post(title=title,
                    content=content,
                    is_archived=is_archived,
                    due_date=due_date,
                    author_id=author.id,
                    semester_id=semester.id,
                    category_id=category.id,
                    course_id=course.id)

        db.session.add(post)
        db.session.commit()

        found = Post.query.filter_by(id=post.id).first()
        assert found is not None

        # test Post object properties
        assert found.title == title
        assert found.content == content
        assert found.timestamp is not None
        assert found.is_archived == is_archived
        assert found.due_date == due_date

        # test relationships
        assert found.author.email == author.email
        assert found.course.name == course.name
        assert found.semester.year == semester.year
        assert found.category.name == category.name

        # test comments
        comments = [
            Comment(content='c%d' % n, post_id=post.id, author_id=author.id)
            for n in range(0, 5)
        ]

        for comment in comments:
            db.session.add(comment)

        db.session.commit()

        assert post.comments is not None
        assert len(post.comments) == len(comments)

        for comment in comments:
            assert comment in post.comments

        # test cheers
        users = [User(email='%d' % n, password='******') for n in range(0, 100)]

        for user in users:
            db.session.add(user)
            post.cheers.append(user)

        db.session.commit()

        assert post.cheers is not None
        assert len(post.cheers) == len(users)

        for cheer in users:
            assert cheer in post.cheers