コード例 #1
0
    def test_abandon_course(self):
        # create a course
        course = Course()
        course.from_dict({'name': 'course', 'description': 'course'})
        course.teacher_id = self.teacher.id
        course.students.append(self.student)
        db.session.add(course)
        db.session.commit()

        # try to access with basic authentication
        rv = self.client.delete(f'/api/v1/student/course/{course.id}',
                                headers=self.basic_auth_header(
                                    'student', 'student'))
        self.assertEquals(rv.status_code, 401)

        # get token
        rv = self.client.post('/api/v1/auth/token',
                              headers=self.basic_auth_header(
                                  'student', 'student'))
        self.assertEquals(rv.status_code, 200)
        token = json.loads(rv.data.decode())['token']

        # access with token
        rv = self.client.delete(f'/api/v1/student/course/{course.id}',
                                headers=self.token_auth_header(token))
        self.assertEquals(rv.status_code, 200)

        # check data
        course = Course.query.filter_by(name='course').first()
        self.assertIsNotNone(course)
        self.assertEquals(course.name, 'course')
        self.assertEquals(course.description, 'course')
        self.assertEquals(course.teacher_id, self.teacher.id)
        self.assertEquals(len(course.students), 0)
コード例 #2
0
def create_course():
    data = request.get_json() or {}
    if not all(field in data for field in ['name', 'description']):
        return '', 400
    course = Course()
    course.teacher_id = g.current_user.id
    course.from_dict(data)
    db.session.add(course)
    db.session.commit()
    return '', 200
コード例 #3
0
    def test_get_homeworks_for_course(self):
        # create a course
        course = Course()
        course.from_dict({'name': 'course', 'description': 'course'})
        course.teacher_id = self.teacher.id
        course.students.append(self.student)
        db.session.add(course)
        db.session.commit()

        # create a homework
        homework = Homework()
        homework.from_dict({
            'name': 'homework',
            'description': 'homework',
            'deadline': '2018-11-08 08:48:11',
            'headcount': 4,
            'self_assignable': False
        })
        homework.course_id = course.id
        db.session.add(homework)
        db.session.commit()

        # try to access with basic authentication
        rv = self.client.get(f'/api/v1/student/course/{course.id}/homeworks',
                             headers=self.basic_auth_header(
                                 'student', 'student'))
        self.assertEquals(rv.status_code, 401)

        # get token
        rv = self.client.post('/api/v1/auth/token',
                              headers=self.basic_auth_header(
                                  'student', 'student'))
        self.assertEquals(rv.status_code, 200)
        token = json.loads(rv.data.decode())['token']

        # access with token
        rv = self.client.get(f'/api/v1/student/course/{course.id}/homeworks',
                             headers=self.token_auth_header(token))
        self.assertEquals(rv.status_code, 200)

        # check returned data
        data = json.loads(rv.data.decode())
        self.assertTrue(
            all(item in data for item in ['homeworks', 'next', 'prev']))
        self.assertEquals(len(data['homeworks']), 1)
        self.assertTrue(all(item in data['homeworks'][0] for item in \
            ['id', 'name', 'description', 'deadline', 'headcount', 'self_assignable', 'course']))
        self.assertIsNotNone(data['homeworks'][0]['id'])
        self.assertEquals(data['homeworks'][0]['name'], 'homework')
        self.assertEquals(data['homeworks'][0]['description'], 'homework')
        self.assertEquals(data['homeworks'][0]['deadline'],
                          '2018-11-08 08:48:11')
        self.assertEquals(data['homeworks'][0]['headcount'], 4)
        self.assertEquals(data['homeworks'][0]['self_assignable'], False)
        self.assertEquals(data['homeworks'][0]['course'], 'course')
コード例 #4
0
    def test_abandon_homework(self):
        # create a course
        course = Course()
        course.from_dict({'name': 'course', 'description': 'course'})
        course.teacher_id = self.teacher.id
        course.students.append(self.student)
        db.session.add(course)
        db.session.commit()

        # create a homework
        homework = Homework()
        homework.from_dict({
            'name': 'homework',
            'description': 'homework',
            'deadline': '2018-11-08 08:48:11',
            'headcount': 4,
            'self_assignable': False
        })
        homework.course_id = course.id
        homework.students.append(self.student)
        db.session.add(homework)
        db.session.commit()

        # try to access with basic authentication
        rv = self.client.delete(f'/api/v1/student/homework/{homework.id}',
                                headers=self.basic_auth_header(
                                    'student', 'student'))
        self.assertEquals(rv.status_code, 401)

        # get token
        rv = self.client.post('/api/v1/auth/token',
                              headers=self.basic_auth_header(
                                  'student', 'student'))
        self.assertEquals(rv.status_code, 200)
        token = json.loads(rv.data.decode())['token']

        # access with token
        rv = self.client.delete(f'/api/v1/student/homework/{homework.id}',
                                headers=self.token_auth_header(token))
        self.assertEquals(rv.status_code, 200)

        # check data
        homework = Homework.query.filter_by(name='homework').first()
        self.assertIsNotNone(homework)
        self.assertEquals(homework.name, 'homework')
        self.assertEquals(homework.description, 'homework')
        self.assertEquals(
            homework.deadline,
            datetime.strptime('2018-11-08 08:48:11', '%Y-%m-%d %H:%M:%S'))
        self.assertEquals(homework.headcount, 4)
        self.assertEquals(homework.self_assignable, False)
        self.assertEquals(homework.course_id, course.id)
        self.assertEquals(len(homework.students), 0)
コード例 #5
0
    def test_create_homework(self):
        # create a course
        course = Course()
        course.from_dict({'name': 'course', 'description': 'course'})
        course.teacher_id = self.teacher.id
        db.session.add(course)
        db.session.commit()

        # try to access with basic authentication
        rv = self.client.post(f'/api/v1/teacher/course/{course.id}/homeworks', headers=self.basic_auth_header('teacher', 'teacher'), \
                              data=json.dumps({'name': 'f1', 'description': 'f2', 'deadline': '2018-11-08 08:48:11', \
                                               'headcount': 4, 'self_assignable': False, 'students': [self.student.id,]}))
        self.assertEquals(rv.status_code, 401)

        # get token
        rv = self.client.post('/api/v1/auth/token',
                              headers=self.basic_auth_header(
                                  'teacher', 'teacher'))
        self.assertEquals(rv.status_code, 200)
        token = json.loads(rv.data.decode())['token']

        # access with token
        rv = self.client.post(f'/api/v1/teacher/course/{course.id}/homeworks', headers=self.token_auth_header(token), \
                              data=json.dumps({'name': 'f1', 'description': 'f2', 'deadline': '2018-11-08 08:48:11', \
                                               'headcount': 4, 'self_assignable': False, 'students': [self.student.id,]}))
        self.assertEquals(rv.status_code, 200)

        # check if homework was created
        homework = Homework.query.filter_by(name='f1').first()
        self.assertIsNotNone(homework)
        self.assertEquals(homework.name, 'f1')
        self.assertEquals(homework.description, 'f2')
        self.assertEquals(
            homework.deadline,
            datetime.strptime('2018-11-08 08:48:11', '%Y-%m-%d %H:%M:%S'))
        self.assertEquals(homework.headcount, 4)
        self.assertEquals(homework.self_assignable, False)
        self.assertEquals(homework.course_id, course.id)
        self.assertEquals(len(homework.students), 1)

        # try to create homework without all the necessary fields
        data = {
            'name': 'f1',
            'description': 'f2',
            'deadline': '2018-11-08 08:48:11',
            'headcount': 4,
            'self_assignable': False
        }
        request_datas = self.insufficient_datas(data)
        for d in request_datas:
            rv = self.client.post(f'/api/v1/teacher/course/{course.id}/homeworks', \
                                  headers=self.token_auth_header(token), data=json.dumps(d))
            self.assertEquals(rv.status_code, 400)
コード例 #6
0
    def test_remove_homework(self):
        # create a homework
        course = Course()
        course.from_dict({'name': 'course', 'description': 'course'})
        course.teacher_id = self.teacher.id
        db.session.add(course)
        db.session.commit()

        # create a homework
        homework = Homework()
        homework.from_dict({
            'name': 'homework',
            'description': 'homework',
            'deadline': '2018-11-08 08:48:11',
            'headcount': 4,
            'self_assignable': False
        })
        homework.course_id = course.id
        db.session.add(homework)
        db.session.commit()

        number_of_homeworks_before = len(Homework.query.all())

        # try to access with basic authentication
        rv = self.client.delete(f'/api/v1/teacher/homework/{homework.id}',
                                headers=self.basic_auth_header(
                                    'teacher', 'teacher'))
        self.assertEquals(rv.status_code, 401)

        # get token
        rv = self.client.post('/api/v1/auth/token',
                              headers=self.basic_auth_header(
                                  'teacher', 'teacher'))
        self.assertEquals(rv.status_code, 200)
        token = json.loads(rv.data.decode())['token']

        # access with token
        rv = self.client.delete(f'/api/v1/teacher/homework/{homework.id}',
                                headers=self.token_auth_header(token))
        self.assertEquals(rv.status_code, 200)

        number_of_homeworks_after = len(Homework.query.all())
        self.assertEquals(
            number_of_homeworks_before - number_of_homeworks_after, 1)

        # try to remove a nonexistent homework
        rv = self.client.delete(f'/api/v1/teacher/homework/{homework.id}',
                                headers=self.token_auth_header(token))
        self.assertEquals(rv.status_code, 410)
コード例 #7
0
    def test_get_applied_courses(self):
        # create a course
        course = Course()
        course.from_dict({'name': 'course', 'description': 'course'})
        course.teacher_id = self.teacher.id
        course.students.append(self.student)
        db.session.add(course)
        db.session.commit()

        # try to access with basic authentication
        rv = self.client.get('/api/v1/student/courses',
                             headers=self.basic_auth_header(
                                 'student', 'student'))
        self.assertEquals(rv.status_code, 401)

        # get token
        rv = self.client.post('/api/v1/auth/token',
                              headers=self.basic_auth_header(
                                  'student', 'student'))
        self.assertEquals(rv.status_code, 200)
        token = json.loads(rv.data.decode())['token']

        # access with token
        rv = self.client.get('/api/v1/student/courses',
                             headers=self.token_auth_header(token))
        self.assertEquals(rv.status_code, 200)

        # check returned data
        data = json.loads(rv.data.decode())
        self.assertTrue(
            all(item in data for item in ['courses', 'next', 'prev']))
        self.assertEquals(len(data['courses']), 1)
        self.assertTrue(
            all(item in data['courses'][0]
                for item in ['id', 'name', 'description', 'teacher']))
        self.assertIsNotNone(data['courses'][0]['id'])
        self.assertEquals(data['courses'][0]['name'], 'course')
        self.assertEquals(data['courses'][0]['description'], 'course')
        self.assertEquals(data['courses'][0]['teacher'], 'teacher')
コード例 #8
0
    def test_remove_course(self):
        # create a course
        course = Course()
        course.from_dict({'name': 'course', 'description': 'course'})
        course.teacher_id = self.teacher.id
        db.session.add(course)
        db.session.commit()

        number_of_courses_before = len(Course.query.all())

        # try to access with basic authentication
        rv = self.client.delete(f'/api/v1/teacher/course/{course.id}',
                                headers=self.basic_auth_header(
                                    'teacher', 'teacher'))
        self.assertEquals(rv.status_code, 401)

        # get token
        rv = self.client.post('/api/v1/auth/token',
                              headers=self.basic_auth_header(
                                  'teacher', 'teacher'))
        self.assertEquals(rv.status_code, 200)
        token = json.loads(rv.data.decode())['token']

        # access with token
        rv = self.client.delete(f'/api/v1/teacher/course/{course.id}',
                                headers=self.token_auth_header(token))
        self.assertEquals(rv.status_code, 200)

        number_of_courses_after = len(Course.query.all())
        self.assertEquals(number_of_courses_before - number_of_courses_after,
                          1)

        # try to remove a nonexistent course
        rv = self.client.delete(f'/api/v1/teacher/course/{course.id}',
                                headers=self.token_auth_header(token))
        self.assertEquals(rv.status_code, 410)
コード例 #9
0
    t.name = 't'
    t.username = '******'
    t.set_password('t')
    db.session.add(t)
    db.session.commit()

    from homework_server.models import Student
    s = Student()
    s.name = 's'
    s.username = '******'
    s.set_password('s')
    db.session.add(s)
    db.session.commit()

    from homework_server.models import Course
    c = Course()
    c.name = 'c'
    c.description = 'c'
    c.teacher_id = t.id
    c.students.append(s)
    db.session.add(c)
    db.session.commit()

    from homework_server.models import Homework
    from datetime import datetime
    h = Homework()
    h.name = 'h'
    h.description = 'h'
    h.deadline = datetime.utcnow()
    h.headcount = 4
    h.self_assignable = False
コード例 #10
0
    def test_get_solution(self):
        # create a homework
        course = Course()
        course.from_dict({'name': 'course', 'description': 'course'})
        course.teacher_id = self.teacher.id
        db.session.add(course)
        db.session.commit()

        # create a homework
        homework = Homework()
        homework.from_dict({
            'name': 'homework',
            'description': 'homework',
            'deadline': '2018-11-08 08:48:11',
            'headcount': 4,
            'self_assignable': False
        })
        homework.course_id = course.id
        homework.students.append(self.student)
        db.session.add(homework)
        db.session.commit()

        # create a solution
        solution = Solution()
        solution.from_dict({'status': 'status'})
        solution.file_path = 'f'
        solution.homework_id = homework.id
        db.session.add(solution)
        db.session.commit()

        submitted_at = datetime.utcnow()

        # try to access with basic authentication
        rv = self.client.get(f'/api/v1/student/solution/{solution.id}',
                             headers=self.basic_auth_header(
                                 'student', 'student'))
        self.assertEquals(rv.status_code, 401)

        # get token
        rv = self.client.post('/api/v1/auth/token',
                              headers=self.basic_auth_header(
                                  'student', 'student'))
        self.assertEquals(rv.status_code, 200)
        token = json.loads(rv.data.decode())['token']

        # access with token
        rv = self.client.get(f'/api/v1/student/solution/{solution.id}',
                             headers=self.token_auth_header(token))
        self.assertEquals(rv.status_code, 200)

        # check returned data
        data = json.loads(rv.data.decode())
        self.assertTrue('solution' in data)
        self.assertTrue(
            all(item in data['solution']
                for item in ['id', 'status', 'submitted_at']))
        self.assertIsNotNone(data['solution']['id'])
        self.assertEquals(data['solution']['status'], 'status')
        self.assertTrue(
            abs((submitted_at -
                 datetime.strptime(data['solution']['submitted_at'],
                                   '%Y-%m-%d %H:%M:%S')).seconds) < 1)
コード例 #11
0
    def test_submit_solution(self):
        # create a course
        course = Course()
        course.from_dict({'name': 'course', 'description': 'course'})
        course.teacher_id = self.teacher.id
        course.students.append(self.student)
        db.session.add(course)
        db.session.commit()

        # create a homework
        homework = Homework()
        homework.from_dict({
            'name': 'homework',
            'description': 'homework',
            'deadline': '2018-11-08 08:48:11',
            'headcount': 4,
            'self_assignable': False
        })
        homework.course_id = course.id
        homework.students.append(self.student)
        db.session.add(homework)
        db.session.commit()

        # create another student
        student2 = Student()
        student2.from_dict({'name': 'student2', 'username': '******'})
        student2.set_password('student2')
        db.session.add(student2)
        db.session.commit()

        # try to access with basic authentication
        rv = self.client.post(f'/api/v1/student/homework/{homework.id}/submit', headers=self.basic_auth_header('student', 'student'), \
                              content_type='multipart/form-data', data={'file': (BytesIO(b'tmp'), 'tmp.txt')})
        self.assertEquals(rv.status_code, 401)

        # get token
        rv = self.client.post('/api/v1/auth/token',
                              headers=self.basic_auth_header(
                                  'student', 'student'))
        self.assertEquals(rv.status_code, 200)
        token = json.loads(rv.data.decode())['token']

        # access with token
        rv = self.client.post(f'/api/v1/student/homework/{homework.id}/submit', headers=self.token_auth_header(token), \
                              content_type='multipart/form-data', data={'file': (BytesIO(b'tmp'), 'tmp.txt')})
        self.assertEquals(rv.status_code, 200)

        submitted_at = datetime.utcnow()

        # check data
        solutions = Solution.query.all()
        self.assertEquals(len(solutions), 1)
        solution = Solution.query.filter_by(status='').first()
        self.assertTrue(
            all(item in solution.to_dict()
                for item in ['status', 'submitted_at']))
        self.assertEquals(solution.status, '')
        self.assertTrue(
            abs((submitted_at - solution.submitted_at).seconds) < 1)
        self.assertIsNotNone(solution.file_path)

        # get token for student2
        rv = self.client.post('/api/v1/auth/token',
                              headers=self.basic_auth_header(
                                  'student2', 'student2'))
        self.assertEquals(rv.status_code, 200)
        token = json.loads(rv.data.decode())['token']

        # access with student2's token
        rv = self.client.post(f'/api/v1/student/homework/{homework.id}/submit', headers=self.token_auth_header(token), \
                              content_type='multipart/form-data', data={'file': (BytesIO(b'tmp'), 'tmp.txt')})
        self.assertEquals(rv.status_code, 409)

        # clean up the upload folder
        shutil.rmtree(self.app.config['UPLOAD_FOLDER'])
コード例 #12
0
    def test_modify_solution(self):
        # create a homework
        course = Course()
        course.from_dict({'name': 'course', 'description': 'course'})
        course.teacher_id = self.teacher.id
        db.session.add(course)
        db.session.commit()

        # create a homework
        homework = Homework()
        homework.from_dict({
            'name': 'homework',
            'description': 'homework',
            'deadline': '2018-11-08 08:48:11',
            'headcount': 4,
            'self_assignable': False
        })
        homework.course_id = course.id
        db.session.add(homework)
        db.session.commit()

        # create a solution
        solution = Solution()
        solution.from_dict({'status': 'status'})
        solution.file_path = 'f'
        solution.homework_id = homework.id
        db.session.add(solution)
        db.session.commit()

        submitted_at = datetime.utcnow()

        # try to access with basic authentication
        rv = self.client.put(f'/api/v1/teacher/solution/{solution.id}', headers=self.basic_auth_header('teacher', 'teacher'), \
                             data=json.dumps({'status': 'f1'}))
        self.assertEquals(rv.status_code, 401)

        # get token
        rv = self.client.post('/api/v1/auth/token',
                              headers=self.basic_auth_header(
                                  'teacher', 'teacher'))
        self.assertEquals(rv.status_code, 200)
        token = json.loads(rv.data.decode())['token']

        # access with token
        rv = self.client.put(f'/api/v1/teacher/solution/{solution.id}', headers=self.token_auth_header(token), \
                             data=json.dumps({'status': 'f1'}))
        self.assertEquals(rv.status_code, 200)

        # check if solution was modified
        solution = Solution.query.filter_by(status='f1').first()
        self.assertIsNotNone(solution)
        self.assertEquals(solution.status, 'f1')
        self.assertEquals(solution.file_path, 'f')

        # try to modify homework without all the necessary fields
        data = {'status': 'f1'}
        request_datas = self.insufficient_datas(data)
        for d in request_datas:
            rv = self.client.put(f'/api/v1/teacher/solution/{solution.id}', \
                                 headers=self.token_auth_header(token), data=json.dumps(d))
            self.assertEquals(rv.status_code, 400)
コード例 #13
0
    def test_student(self):
        # create invalid student
        student = Student()
        db.session.add(student)
        self.assertRaises(IntegrityError, db.session.commit)
        db.session.rollback()

        # create valid student
        student.from_dict({
            'name': 'student',
            'username': '******',
            'password': '******'
        })
        db.session.add(student)
        db.session.commit()

        # check data
        self.assertEquals(len(Student.query.all()), 1)
        self.assertIsNotNone(student.id)
        self.assertNotEquals(student.password_hash, 'student')
        self.assertEquals(student.type, 'students')
        d = student.to_dict()
        self.assertIsNotNone(d['id'])
        self.assertEquals(d['username'], 'student')
        self.assertEquals(d['name'], 'student')

        # try to create student with the same username
        student2 = Student()
        student2.from_dict({
            'name': 'student2',
            'username': '******',
            'password': '******'
        })
        db.session.add(student2)
        self.assertRaises(IntegrityError, db.session.commit)
        db.session.rollback()

        # create a teacher
        teacher = Teacher()
        teacher.from_dict({
            'name': 'teacher',
            'username': '******',
            'password': '******'
        })
        db.session.add(teacher)
        db.session.commit()

        # create a course and add student to it
        course = Course()
        course.from_dict({'name': 'course', 'description': 'course'})
        course.teacher_id = teacher.id
        course.students.append(student)
        db.session.add(course)
        db.session.commit()

        # check if student has one course
        self.assertEquals(len(student.courses), 1)

        # create a homework and add student to it
        homework = Homework()
        homework.from_dict({
            'name': 'homework',
            'description': 'homework',
            'deadline': datetime.utcnow(),
            'headcount': 2,
            'self_assignable': True
        })
        homework.course_id = course.id
        homework.students.append(student)
        db.session.add(homework)
        db.session.commit()

        # check if student has one homework
        self.assertEquals(len(student.homeworks), 1)
コード例 #14
0
    def test_solution(self):
        # create invalid solution
        solution = Solution()
        db.session.add(solution)
        self.assertRaises(IntegrityError, db.session.commit)
        db.session.rollback()

        # try to create solution without file_path
        solution.from_dict({'status': 'status'})
        db.session.add(solution)
        self.assertRaises(IntegrityError, db.session.commit)
        db.session.rollback()

        # try to create solution without a homework
        solution.file_path = 'file_path'
        db.session.add(solution)
        self.assertRaises(IntegrityError, db.session.commit)
        db.session.rollback()

        # create a teacher
        teacher = Teacher()
        teacher.from_dict({
            'name': 'teacher',
            'username': '******',
            'password': '******'
        })
        db.session.add(teacher)
        db.session.commit()

        # create a course
        course = Course()
        course.from_dict({'name': 'course', 'description': 'course'})
        course.teacher_id = teacher.id
        db.session.add(course)
        db.session.commit()

        # create a homework
        homework = Homework()
        homework.from_dict({
            'name': 'homework',
            'description': 'homework',
            'deadline': '2018-10-30 10:20:32',
            'headcount': 2,
            'self_assignable': True
        })
        homework.course_id = course.id
        db.session.add(homework)
        db.session.commit()

        # add homework to solution
        solution.homework_id = homework.id
        db.session.add(solution)
        db.session.commit()

        # check data
        submitted_at = datetime.utcnow()
        self.assertEquals(len(Solution.query.all()), 1)
        self.assertIsNotNone(solution.id)
        self.assertEquals(solution.homework_id, homework.id)
        d = solution.to_dict()
        self.assertIsNotNone(d['id'])
        self.assertEquals(sorted(d.keys()), ['id', 'status', 'submitted_at'])
        self.assertEquals(d['status'], 'status')
        self.assertTrue(
            abs((submitted_at -
                 datetime.strptime(d['submitted_at'], '%Y-%m-%d %H:%M:%S')
                 ).seconds) < 1)
コード例 #15
0
    def test_homework(self):
        # create invalid homework
        homework = Homework()
        db.session.add(homework)
        self.assertRaises(IntegrityError, db.session.commit)
        db.session.rollback()

        # try to create homework without a course
        homework.from_dict({
            'name': 'homework',
            'description': 'homework',
            'deadline': '2018-10-30 10:20:32',
            'headcount': 2,
            'self_assignable': True
        })
        db.session.add(homework)
        self.assertRaises(IntegrityError, db.session.commit)
        db.session.rollback()

        # create a teacher
        teacher = Teacher()
        teacher.from_dict({
            'name': 'teacher',
            'username': '******',
            'password': '******'
        })
        db.session.add(teacher)
        db.session.commit()

        # create a course
        course = Course()
        course.from_dict({'name': 'course', 'description': 'course'})
        course.teacher_id = teacher.id
        db.session.add(course)
        db.session.commit()

        # add course to homework
        homework.course_id = course.id
        db.session.add(homework)
        db.session.commit()

        # check data
        self.assertEquals(len(Homework.query.all()), 1)
        self.assertIsNotNone(homework.id)
        self.assertEquals(homework.course_id, course.id)
        d = homework.to_dict()
        self.assertIsNotNone(d['id'])
        self.assertEquals(d['name'], 'homework')
        self.assertEquals(d['description'], 'homework')
        self.assertEquals(d['deadline'], '2018-10-30 10:20:32')
        self.assertEquals(d['headcount'], 2)
        self.assertTrue(d['self_assignable'])
        self.assertEquals(d['course'], 'course')

        # try to create homework with the same name
        homework2 = Homework()
        homework2.from_dict({
            'name': 'homework',
            'description': 'homework2',
            'deadline': datetime.utcnow(),
            'headcount': 3,
            'self_assignable': False
        })
        homework2.course_id = course.id
        db.session.add(homework2)
        self.assertRaises(IntegrityError, db.session.commit)
        db.session.rollback()
コード例 #16
0
    def test_course(self):
        # create invalid course
        course = Course()
        db.session.add(course)
        self.assertRaises(IntegrityError, db.session.commit)
        db.session.rollback()

        # try to create course without a teacher
        course.from_dict({'name': 'course', 'description': 'course'})
        db.session.add(course)

        self.assertIsNone(course.teacher_id)

        # create a teacher
        teacher = Teacher()
        teacher.from_dict({
            'name': 'teacher',
            'username': '******',
            'password': '******'
        })
        db.session.add(teacher)
        db.session.commit()

        # add teacher to course
        course.teacher_id = teacher.id
        db.session.add(course)
        db.session.commit()

        # check data
        self.assertEquals(len(Course.query.all()), 1)
        self.assertIsNotNone(course.id)
        self.assertEquals(course.teacher_id, teacher.id)
        d = course.to_dict()
        self.assertIsNotNone(d['id'])
        self.assertEquals(d['name'], 'course')
        self.assertEquals(d['description'], 'course')
        self.assertEquals(d['teacher'], 'teacher')

        # try to create course with the same name
        course2 = Course()
        course2.from_dict({'name': 'course', 'description': 'course2'})
        course2.teacher_id = teacher.id
        db.session.add(course2)
        self.assertRaises(IntegrityError, db.session.commit)
        db.session.rollback()