def test_update_student_valid_id(self):
        student_id = 3
        first_name = "Steve"
        last_name = "Smith"
        student_class = "5 C"
        nationality = "USA"

        expected_output = [{
            "id": student_id,
            "first_name": first_name,
            "lastName": last_name,
            "class": student_class,
            "nationality": nationality
        }]

        response = requests.put(base_url, {
            "id": student_id,
            "class": student_class
        })
        if response.status_code == 200:
            cnx = dbu.db_connect()
            student_details = dbu.db_fetch_students(cnx, student_id=student_id)
            dbu.db_close_cnx(cnx)
            assert student_details == expected_output
        else:
            assert False
    def test_fetch_students_valid_id_class(self):
        student_id = 3
        first_name = "Steve"
        last_name = "Smith"
        student_class = "5 C"
        nationality = "USA"

        expected_output = [{
            "id": student_id,
            "first_name": first_name,
            "lastName": last_name,
            "class": student_class,
            "nationality": nationality
        }]

        response = requests.get(base_url + "fetchStudents", {
            "id": student_id,
            "class": "5 C"
        })
        if response.status_code == 200:
            cnx = dbu.db_connect()
            student_json = dbu.db_fetch_students(cnx,
                                                 student_id=student_id,
                                                 student_class=student_class)
            dbu.db_close_cnx(cnx)
            assert student_json == expected_output
        else:
            assert False
    def put(self):
        # Use this for update student record
        # update if id is unique and other args are valid
        # at least 1 arg is required
        student_details = student_put_args.parse_args()
        student_id = student_details["id"]
        first_name = student_details["firstName"]
        last_name = student_details["lastName"]
        student_class = student_details["class"]
        nationality = student_details["nationality"]

        cnx = dbu.db_connect()

        is_valid_id = dbu.db_fetch_students(cnx, student_id=student_id)

        if not is_valid_id:
            dbu.db_close_cnx(cnx)
            abort(
                404,
                message=
                f"Student with id {student_id} does not exist. Unable to update."
            )

        upd = dbu.db_update_student(cnx,
                                    student_id=student_id,
                                    first_name=first_name,
                                    last_name=last_name,
                                    student_class=student_class,
                                    nationality=nationality)
        dbu.db_close_cnx(cnx)
        if upd:
            return f"Successfully updated record for student with id {student_id}", 200
        else:
            abort(400,
                  message=f"Not able to update student with id {student_id}")
    def post(self):
        student_details = student_post_args.parse_args()
        student_id = student_details["id"]
        first_name = student_details["firstName"]
        last_name = student_details["lastName"]
        student_class = student_details["class"]
        nationality = student_details["nationality"]

        # TODO: Validate Class and Nationality

        # Add data to db
        cnx = dbu.db_connect()
        if cnx:
            is_exists_student = dbu.db_fetch_students(cnx,
                                                      student_id=student_id)
            if is_exists_student:
                abort(409,
                      message=f"Student with id {student_id} already exists!")

            ins = dbu.db_insert_student_details(cnx, student_id, first_name,
                                                last_name, student_class,
                                                nationality)
            dbu.db_close_cnx(cnx)
            if ins:
                return student_details, 201
        abort(400,
              message="Student creation failed. Please provide valid data.")
    def delete(self, student_id):
        # Use this for delete student record
        # Delete if id is unique
        cnx = dbu.db_connect()

        # Check if student_id exists and is unique
        student_list = dbu.db_fetch_students(cnx, student_id=student_id)

        if student_list:
            if len(student_list) > 1:
                dbu.db_close_cnx(cnx)
                abort(
                    400,
                    message=
                    f"Student id {student_id} is not unique. Unable to delete."
                )
        else:
            dbu.db_close_cnx(cnx)
            abort(
                404,
                message=
                f"Student with id {student_id} does not exist. Unable to delete."
            )

        is_delete = dbu.db_delete_student(cnx, student_id=student_id)
        dbu.db_close_cnx(cnx)
        if is_delete:
            return f"Successfully deleted record for student with id {student_id}", 204
        else:
            abort(400,
                  message=f"Not able to delete student with id {student_id}")
 def test_delete_student_valid_id(self):
     student_id = 3
     response = requests.delete(base_url + f"{student_id}")
     if response.status_code == 204:
         cnx = dbu.db_connect()
         student_details = dbu.db_fetch_students(cnx, student_id=student_id)
         dbu.db_close_cnx(cnx)
         assert not student_details
     else:
         assert False
Exemplo n.º 7
0
    def test_db_fetch_students_by_invalid_id_class_combo(self):
        cnx = dbu.db_connect()
        student_id = 1
        student_class = '5 C'

        students_text = dbu.db_fetch_students(cnx,
                                              student_id=student_id,
                                              student_class=student_class)
        dbu.db_close_cnx(cnx)
        assert not students_text
Exemplo n.º 8
0
    def test_db_fetch_students_by_id(self):
        cnx = dbu.db_connect()
        expected_student_json = [{
            "id": 1,
            "first_name": "John",
            "lastName": "Smith",
            "class": "4 C",
            "nationality": "USA"
        }]

        students_json = dbu.db_fetch_students(cnx, student_id=1)
        dbu.db_close_cnx(cnx)
        assert students_json == expected_student_json
Exemplo n.º 9
0
    def test_db_fetch_students_by_class(self):
        cnx = dbu.db_connect()
        expected_student_json = [{
            "id": 1,
            "first_name": "John",
            "lastName": "Smith",
            "class": "4 C",
            "nationality": "USA"
        }, {
            "id": 2,
            "first_name": "Jane",
            "lastName": "Doe",
            "class": "4 C",
            "nationality": "Canada"
        }]

        students_json = dbu.db_fetch_students(cnx, student_class='4 C')
        assert students_json == expected_student_json
    def get(self):
        # id or class is required
        fetch_students = fetch_students_args.parse_args()

        student_id = fetch_students["id"]
        student_class = fetch_students["class"]

        cnx = dbu.db_connect()
        student_json = dbu.db_fetch_students(cnx,
                                             student_id=student_id,
                                             student_class=student_class)

        dbu.db_close_cnx(cnx)

        if not student_json:
            abort(404, message="valid id and/or class is required")
        else:
            return student_json, 200
    def test_create_student_valid_id(self):
        # input
        student_id = 3
        first_name = "Steve"
        last_name = "Smith"
        student_class = "5 D"
        nationality = "USA"

        # expected output
        expected_output = [{
            "id": student_id,
            "first_name": first_name,
            "lastName": last_name,
            "class": student_class,
            "nationality": nationality
        }]

        # Make API call
        response = requests.post(
            base_url, {
                "id": student_id,
                "firstName": first_name,
                "lastName": last_name,
                "class": student_class,
                "nationality": nationality
            })

        if response.status_code == 201:
            # Verify in db if student creation was successful
            # Caution: Test will be marked pass for existing record
            #          else is required to fail the test
            cnx = dbu.db_connect()
            student_details = dbu.db_fetch_students(cnx, student_id=student_id)
            dbu.db_close_cnx(cnx)
            assert student_details == expected_output
        else:
            assert False