def test_get_student_by_id(self):
        """Test case for get_student_by_id

        Find student by ID
        """
        #arrange
        body = Student()
        body.first_name = names.get_first_name()
        body.last_name = names.get_last_name()
        body.grades = {'math': 8, 'history': 9}
        response = self.client.open(
            '/service-api/student',
            method='POST',
            data=json.dumps(body),
            content_type='application/json')
        student_id = (response.json)
        #act
        response = self.client.open(
            '/service-api/student/{student_id}'.format(student_id=student_id),
            method='GET')
        #assert
        self.assert200(response,
                        'Response body is : ' + response.data.decode('utf-8'))
        #clean up
        response = self.client.open(
            '/service-api/student/{student_id}'.format(student_id=student_id),
            method='DELETE')
    def test_list_students(self):
        """Test case for list_students

        List all students
        """
        body = Student()
        body.first_name = names.get_first_name()
        last_name = names.get_last_name()
        body.last_name = last_name
        body.grades = {'math': 8, 'history': 9}
        response = self.client.open(
            '/service-api/student',
            method='POST',
            data=json.dumps(body),
            content_type='application/json')
        query_string = [('last_name', last_name)]
        #arrange
        response = self.client.open(
            #'/service-api/student/?last_name={last_name}'.format(last_name=last_name),
            '/service-api/student/',
            method='GET',
            query_string=query_string)
        #assert
        self.assert200(response,
                        'Response body is : ' + response.data.decode('utf-8'))
示例#3
0
    def test_get_student_by_id(self):
        """Test case for get_student_by_id

        Find student by ID
        """
        body = Student()
        body.first_name = names.get_first_name()
        body.last_name = names.get_last_name()
        body.grades = {'math': 8, 'history': 9}
        response = self.client.open(
            '/service-api/student/',
            method='POST',
            data=json.dumps(body),
            content_type='application/json')
        student_id = (response.json)

        query_string = [('math', 9)]
        response = self.client.open(
            '/service-api/student/{student_id}'.format(student_id=student_id),
            method='GET',
            query_string=query_string)
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))
        self.assertTrue(response.is_json)
        self.assertIsInstance(response.json, dict)
 def test_delete_student(self):
     """Test case for delete_student
     """
     body = Student()
     body.first_name = names.get_first_name()
     body.last_name = names.get_last_name()
     body.grades = {'math': 8, 'history': 9}
     response = self.client.open('/service-api/student',
                                 method='POST',
                                 data=json.dumps(body),
                                 content_type='application/json')
     student_id = (response.json)
def update_student(body):  # noqa: E501
    """Updated student

    This can only be done by the logged in student. # noqa: E501

    :param nuid: nuid that need to be updated
    :type nuid: int
    :param body: Updated student object
    :type body: dict | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        body = Student.from_dict(connexion.request.get_json())  # noqa: E501
        update_string = """
            UPDATE student
            SET pass = "******"
            WHERE nuid = {0};
            """.format(body.nuid, body.password)
        try:
            session_cookie = connexion.request.cookies.get("session")
            session_NUID = connexion.JWT_verify(session_cookie)
            db_conn = connexion.DB(connexion.DB_ENG)
            db_conn.execute(update_string)
            db_conn.close()
            return "Accepted", 201
        except exc.IntegrityError:
            return "Already Exists", 202
        except KeyError:
            return "Forbidden", 403
    return "Bad Request", 400
def get_student_by_nuid(nuid):  # noqa: E501
    """Get student by nuid

     # noqa: E501

    :param nuid: The name that needs to be fetched. Use student1 for testing.
    :type nuid: int

    :rtype: Student
    """
    select_string = """
        SELECT
            name,
            email,
            nuid
        FROM
            student
        WHERE
            nuid = {}
        """.format(nuid)
    try:
        db_conn = connexion.DB(connexion.DB_ENG)
        result = db_conn.execute(select_string)
        db_conn.close()
        for row in result:
            res = {
                'name': row["name"],
                'nuid': row["nuid"],
                'email': row["email"]
            }
            res = Student.from_dict(res)
            return res, 200
        return "Object not found", 404
    except exc.IntegrityError:
        return "Internal Server Error", 500
 def test_add_student(self):
     """Test case for add_student
     Add a new student
     """
     body = Student()
     body.first_name = names.get_first_name()
     body.last_name = names.get_last_name()
     body.grades = {'math': 8, 'history': 9}
     response = self.client.open('/service-api/student',
                                 method='POST',
                                 data=json.dumps(body),
                                 content_type='application/json')
     self.assert200(response,
                    'Response body is : ' + response.data.decode('utf-8'))
     self.assertTrue(response.is_json)
     self.assertIsInstance(response.json, int)
def add_student(body, subject=None):  # noqa: E501
    """Add a new student

     # noqa: E501

    :param body: Student object that needs to be added
    :type body: dict | bytes
    :param subject: The subject name
    :type subject: str

    :rtype: int
    """
    if connexion.request.is_json:
        
        student_json = connexion.request.get_json()
        if 'first_name' not in student_json.keys() or student_json['first_name'] == '':
            return 'invalid input', 405

        if 'last_name' not in student_json.keys() or student_json['last_name'] == '':
            return 'invalid input', 405
        
        student = Student.from_dict(student_json)  # noqa: E501

        try:
            return swagger_server.service.student_service.add_student(student)
        except ValueError:
            return 'already exists', 409
    def test_delete_student(self, mock_delete_student):
        """
        Test case for delete_student
        """

        mock_student = Student(789, "first_name", "last_name", grades={})

        mock_delete_student.return_value = mock_student

        response = self.client.open(
            '/service-api/student/{student_id}'.format(student_id=789),
            method='DELETE')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))

        response_student = Student.from_dict(json.loads(response.data))

        self.assertEqual(response_student, mock_student)
示例#10
0
def add_student(body):  # noqa: E501
    """Add a new student
     # noqa: E501
    :param body: Student object that needs to be added
    :type body: dict | bytes
    :rtype: str
    """
    if connexion.request.is_json:
        body = Student.from_dict(connexion.request.get_json())  # noqa: E501
    return student_service.add_student(body)
示例#11
0
    def test_add_student(self):
        """Test case for add_student

        Add a new student
        """
        body = Student()
        body.first_name = names.get_first_name()
        body.last_name = names.get_last_name()
        body.grades = {'The_Mathematics_of_Quantum_Neutrino_Fields': 0, '20th_Century_History': 0}
        response = self.client.open(
            '/service-api/student',
            method='POST',
            data=json.dumps(body),
            content_type='application/json')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))
        self.assertTrue(response.is_json)
        # self.assertIsInstance(response.json, int)   # 返回学生id
        self.assertIsInstance(response.json, dict)           
示例#12
0
 def _make_student(student_code: str) -> Student:
     student_data = dbq.get_student_and_group(student_code)
     if student_data is not None:
         return Student(student_code=student_data["studentCode"],
                        sex=student_data["sex"],
                        age=student_data["age"],
                        country=student_data["country"],
                        city=student_data["city"],
                        group_code=student_data["groupCode"])
     else:
         return None
    def test_get_student_by_last_name(self):
        """Test case for get_student_by_last_name

        Find student by last name
        """
        body = Student()
        body.first_name = names.get_first_name()
        body.last_name = 'last_name_example'
        body.grades = {'math': 8, 'history': 9}
        response = self.client.open('/service-api/student/',
                                    method='POST',
                                    data=json.dumps(body),
                                    content_type='application/json')

        query_string = [('last_name', 'last_name_example')]
        response = self.client.open('/service-api/student/',
                                    method='GET',
                                    query_string=query_string)
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))
示例#14
0
    def test_create_student(self):
        """Test case for create_student

        Create student
        """
        body = Student()
        response = self.client.open('/pablokvitca/classdeck-api/1.0.0/student',
                                    method='POST',
                                    data=json.dumps(body),
                                    content_type='application/json')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))
示例#15
0
    def test_add_student(self):
        """Test case for add_student

        Add a new student
        """
        body = Student()
        response = self.client.open('/service-api/student',
                                    method='POST',
                                    data=json.dumps(body),
                                    content_type='application/json')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))
示例#16
0
    def test_delete_student(self):
        """Test case for delete_student

        Delete student by ID
        """
        body = Student()
        body.first_name = names.get_first_name()
        body.last_name = names.get_last_name()
        body.grades = {'math': 8, 'history': 9}
        response = self.client.open(
            '/service-api/student',
            method='POST',
            data=json.dumps(body),
            content_type='application/json')
        student_id = (response.json)['student_id']    # 学生id
        # student_id = (response.json)
        response = self.client.open(
            '/service-api/student/{student_id}'.format(student_id=student_id),
            method='DELETE')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))
示例#17
0
    def test_update_student(self):
        """Test case for update_student

        Updated student
        """
        body = Student()
        response = self.client.open(
            '/pablokvitca/classdeck-api/1.0.0/student/{nuid}'.format(nuid=2),
            method='PUT',
            data=json.dumps(body),
            content_type='application/json')
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))
示例#18
0
def add_student(body):  # noqa: E501
    """Add a new student

     # noqa: E501

    :param body: Student object that needs to be added
    :type body: dict | bytes

    :rtype: str
    """
    if connexion.request.is_json:
        body = Student.from_dict(connexion.request.get_json())  # noqa: E501
        return student_service.add_student(body)
    else:
        return "Your request must include JSON, with at least one of the following: 'first_name', 'last_name'", 200
示例#19
0
    def test_get_student_by_last_name(self):
        """Test case for get_student_by_last_name

        Find student by last name
        """
        body = Student()
        body.first_name = names.get_first_name()
        body.last_name = names.get_last_name()
        body.grades = {'Chinese': 8, 'history': 9}
        response = self.client.open(
            '/service-api/student',
            method='POST',
            data=json.dumps(body),
            content_type='application/json')
        
        query_string = [('last_name', (response.json)['last_name'])] # 学生的last_name
        response = self.client.open(
            '/service-api/student/',
            method='GET',
            query_string=query_string)
        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))
        self.assertTrue(response.is_json)
        self.assertIsInstance(response.json, dict)
示例#20
0
def add_student(body):  # noqa: E501
    """Add a new student

     # noqa: E501

    :param body: Student object that needs to be added
    :type body: dict | bytes

    :rtype: str
    """
    if connexion.request.is_json:
        body = Student.from_dict(connexion.request.get_json())  # noqa: E501
        if not body or not body.last_name or not body.first_name:
            return 'Invalid input', 405
    return student_service.add_student(body)
    def test_add_student(self):
        """Test case for add_student

        Add a new student
        """
        body = Student(first_name=str(uuid.uuid4()), last_name='beratna', grades={"subject_example": 8})
        response = self.client.open(
            '/service-api/student',
            method='POST',
            data=json.dumps(body),
            content_type='application/json')
        data = response.data.decode('utf-8')
        self.assert200(response,
                       'Response body is : ' + data)
        TestDefaultController.student_id = data
def add_student(body):  # noqa: E501
    """Add a new student

     # noqa: E501

    :param body: Student object that needs to be added
    :type body: dict | bytes

    :rtype: int
    """
    if connexion.request.is_json:
        student = Student.from_dict(connexion.request.get_json())  # noqa: E501
        # print(student)
        result = student_service.add_student(student)
        return result

    return 'input no bueono', 400
def add_student(body):  # noqa: E501
    """Add a new student

     # noqa: E501

    :param body: Student object that needs to be added
    :type body: dict | bytes

    :rtype: str
    """
    if connexion.request.is_json:
        try:
            body = Student.from_dict(
                connexion.request.get_json())  # noqa: E501
        except ValueError:
            return 'Invalid Input', 405
    return student_service.add_student(body)
def add_student(body):  # noqa: E501
    """Add a new student

     # noqa: E501

    :param body: Student object that needs to be added
    :type body: dict | bytes

    :rtype: int
    """
    if connexion.request.is_json:
        body = Student.from_dict(connexion.request.get_json())  # noqa: E501
        # print('default controller body', body)
        if (not body.first_name) or (not body.last_name):
            return "Not content", 405
    # return 'do some magic!'
    return student_service.add_student(body)
    def test_add_student_already_exists(self, mock_add_student):
        """
        return 409 if student already exists
        """
        body = Student(first_name="test", last_name="test")

        mock_add_student.side_effect = ValueError

        query_string = [('subject', 'subject_example')]
        response = self.client.open('/service-api/student',
                                    method='POST',
                                    data=json.dumps(body),
                                    content_type='application/json',
                                    query_string=query_string)

        self.assert_status(
            response, 409,
            'Response body is : ' + response.data.decode('utf-8'))
    def test_get_student_by_id(self, mock_get_student_by_id):
        """Test case for get_student_by_id

        Find student by ID
        """

        mock_get_student_by_id.return_value = Student(1, "first1", "last1")

        response = self.client.open(
            '/service-api/student/{student_id}'.format(student_id=1),
            method='GET')

        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))

        json_data = json.loads(response.data)
        print(json_data)

        self.assertEqual(json_data['student_id'], 1)
        self.assertEqual(json_data['first_name'], "first1")
        self.assertEqual(json_data['last_name'], "last1")
示例#27
0
def add_student(body):  # noqa: E501
    """Add a new student

     # noqa: E501

    :param body: Student object that needs to be added
    :type body: dict | bytes

    :rtype: int
    """
    # try:
    #     res = connexion.request.is_json.get_json()
    #     if "first_name" not in res.keys() or "last_name" not in res.keys():
    #         return 'Full name required', 405
    # except ValueError:
    #     'invalid entry', 405

    if connexion.request.is_json:
        body = Student.from_dict(connexion.request.get_json()) # noqa: E501
        if body.first_name == None or body.last_name == None:
            return 'Full name required', 405
    return student_service.add_student(body)
    def test_get_student_by_last_name(self, mock_get_student_by_last_name):
        """
        find student by last name
        """

        mock_get_student_by_last_name.return_value = Student(
            1, "first1", "last1")

        response = self.client.open(
            '/service-api/student/?last_name={last_name}'.format(
                last_name='last1'),
            method='GET')

        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))

        json_data = json.loads(response.data)
        print(json_data)

        self.assertEqual(json_data['student_id'], 1)
        self.assertEqual(json_data['first_name'], "first1")
        self.assertEqual(json_data['last_name'], "last1")
    def test_add_student(self, mock_add_student):
        """Test case for add_student

        Add a new student
        """
        body = Student(first_name="test", last_name="test")

        mock_add_student.return_value = 99

        query_string = [('subject', 'subject_example')]
        response = self.client.open('/service-api/student',
                                    method='POST',
                                    data=json.dumps(body),
                                    content_type='application/json',
                                    query_string=query_string)

        self.assert200(response,
                       'Response body is : ' + response.data.decode('utf-8'))

        mock_add_student.assert_called_with(body)

        jsondata = json.loads(response.data)

        self.assertEqual(jsondata, 99)
def create_student(body):  # noqa: E501
    """Create student

    This can only be done by the logged in student. # noqa: E501

    :param body: Created student object
    :type body: dict | bytes

    :rtype: None
    """
    if connexion.request.is_json:
        body = Student.from_dict(connexion.request.get_json())  # noqa: E501
        insert_string = """
            INSERT INTO student (name, email, nuid, pass)
            VALUES ("{0}", "{1}", {2}, "{3}");
            """.format(body.name, body.email, body.nuid, body.password)
        try:
            db_conn = connexion.DB(connexion.DB_ENG)
            db_conn.execute(insert_string)
            db_conn.close()
            return "Accepted", 201
        except exc.IntegrityError:
            return "User for that NUID and/or email already exists", 404
    return "Bad Request", 400