Ejemplo n.º 1
0
def test_update(app):
    department_to_update = DepartmentServices.get_by_id(1)
    updated = DepartmentServices.update(department_to_update,
                                        dict(name="Updated dep 1"))
    departments = DepartmentServices.get_all()
    assert updated.name == "Updated dep 1"
    assert len(departments) == 2
Ejemplo n.º 2
0
def test_avg_salary_non_empty_departments(app):
    dep_1 = DepartmentServices.get_by_id(1)
    dep_2 = DepartmentServices.get_by_id(2)
    salary_1 = DepartmentServices.get_avg_salary(dep_1)
    salary_2 = DepartmentServices.get_avg_salary(dep_2)
    assert salary_1 == 1000
    assert salary_2 == 2000
Ejemplo n.º 3
0
def test_delete(app):
    department_to_delete = DepartmentServices.get_by_id(1)
    DepartmentServices.delete(department_to_delete)
    departments = DepartmentServices.get_all()
    employees = EmployeeServices.get_all()
    assert department_to_delete not in departments
    assert len(departments) == 1
    # to check if cascade delete happened
    assert len(employees) == 3
Ejemplo n.º 4
0
 def delete(dep_id):
     """
     This method is called when DELETE request is sent to url "/api/v1/departments/id"
     deletes the department entry with specified id from database.
     :return: returns an empty response body and status code 204 if valid id
     specified. If invalid id specified - returns error message and status code 404.
     """
     department = DepartmentServices.get_by_id(dep_id)
     if not department:
         return {"message": f"Department with id {dep_id} not found"}, 404
     DepartmentServices.delete(department)
     return "", 204
Ejemplo n.º 5
0
 def get(self, dep_id=None):
     """
     This method is called when GET request is sent to "/api/v1/departments/[<int:id>]" url
     :return: if "id" not specified the list of all departments in json format,
     status code 200. If id specified -  the department with the specified id
     serialized to json. If invalid id - error message and status code 404.
     """
     if not dep_id:
         departments = DepartmentServices.get_all()
         return self.department_schema.dump(departments, many=True), 200
     department = DepartmentServices.get_by_id(dep_id)
     if not department:
         return {"message": f"Department with id {dep_id} not found"}, 404
     return self.department_schema.dump(department), 200
Ejemplo n.º 6
0
 def post(self, dep_id):
     """
     This method is called when POST request is sent to url
     "/api/v1/departments/<int:dep_id>/employees" with json data.
     Creates a new employee entry in database with department_id=dep_id.
     :return: if valid data provided returns the created entry serialized
     to json, status code 201, if invalid data - returns the error massage
     in json format, status code 400. If invalid department id specified in url -
     returns not-found error message, status code 404.
     """
     department = DepartmentServices.get_by_id(dep_id)
     if not department:
         return {"message": f"Department with id {dep_id} not found"}, 404
     json_data = request.get_json(force=True)
     try:
         data = self.employee_schema.load(json_data,
                                          partial=["department_id"])
     except ValidationError as e:
         return {"message": e.messages}, 400
     data["department_id"] = dep_id
     try:
         new_employee = EmployeeServices.create(data)
     except IntegrityError:
         return {"message": "Not valid department id"}, 400
     return self.employee_schema.dump(new_employee), 201
Ejemplo n.º 7
0
 def patch(self, dep_id):
     """
     This method is called when PATCH request is sent to url "/api/v1/departments/id"
     with json data, updates the department entry with specified id in database.
     :return: if valid data provided returns the updated entry serialized
     to json, status code 200, if invalid data - returns the error massage
     in json format, status code 400. If invalid id specified - returns error
     message and status code 404.
     """
     json_data = request.get_json(force=True)
     department = DepartmentServices.get_by_id(dep_id)
     if not department:
         return {"message": f"Department with id {dep_id} not found"}, 404
     try:
         data = self.department_schema.load(json_data, partial=True)
     except ValidationError as e:
         return e.messages, 400
     try:
         department = DepartmentServices.update(department, data)
     except IntegrityError:
         return {"message": "Department names should be unique."}, 400
     return self.department_schema.dump(department), 200
Ejemplo n.º 8
0
 def get(self, dep_id):
     """
     This method is called when GET request is sent to
     "/api/v1/departments/<int:dep_id>/employees" url.
     :return: return the json-serialized list of employees working in
     department with id specified in url, status code 200. If invalid id -
     error message and status code 404.
     """
     department = DepartmentServices.get_by_id(dep_id)
     if not department:
         return {"message": f"Department with id {dep_id} not found"}, 404
     employees = EmployeeServices.get_all_for_department(dep_id)
     return self.employee_schema.dump(employees, many=True), 200
Ejemplo n.º 9
0
 def post(self):
     """
     This method is called when POST request is sent to url "/api/v1/departments"
      with json data. Creates a new department entry in database.
     :return: if valid data provided returns the created entry serialized
     to json, status code 201, if invalid data - returns the error massage
     in json format, status code 400.
     """
     json_data = request.get_json(force=True)
     try:
         data = self.department_schema.load(json_data)
     except ValidationError as e:
         return e.messages, 400
     try:
         new_department = DepartmentServices.create(data)
     except IntegrityError:
         return {"message": "Department names should be unique."}, 400
     return self.department_schema.dump(new_department), 201
Ejemplo n.º 10
0
 def get(self, dep_id=None):
     """
     This method is called when GET request is sent to
     "/api/v1/employees/search" or
     "/api/v1//departments/<dep_id>/employees/search" urls.
     The method parses the "date_of_birth" and(optional) "date_for_interval"
     query parameters from the GET request, and based on them filters out
     the employees born on a specified date or in an interval between dates.
     Depending on url fetches employees for a specific department or all.
     :return: if sent to "/api/v1/employees/search" returns the list all
     employees born on a specified date or in an interval between dates in json format,
     status code 200. if sent to "/api/v1//departments/<dep_id>/employees/search" -
     the same but only for employees from specified department.
     If no "date_of_birth" parameter provided - returns error message, status code 400.
     If invalid department id provided - returns error message, status code 404.
     """
     date_of_birth = request.args.get("date_of_birth")
     if not date_of_birth:
         return {"message": "Enter search data"}, 400
     date_of_birth = datetime.strptime(date_of_birth, "%Y-%m-%d").date()
     date_for_interval = request.args.get("date_for_interval")
     if date_for_interval:
         date_for_interval = datetime.strptime(date_for_interval,
                                               "%Y-%m-%d").date()
     if not dep_id:
         employees = EmployeeServices.get_by_date_of_birth(
             date_of_birth, date_for_interval)
     else:
         department = DepartmentServices.get_by_id(dep_id)
         if not department:
             return {
                 "message": f"Department with id {dep_id} not found"
             }, 404
         employees = EmployeeServices.get_by_date_of_birth_from_department(
             dep_id, date_of_birth, date_for_interval)
     return self.employee_schema.dump(employees, many=True), 200
Ejemplo n.º 11
0
 def get_avg_salary(self, obj):
     """
     A method to add the calculated average salary data
     to the serialized data on the fly.
     """
     return DepartmentServices.get_avg_salary(obj)
Ejemplo n.º 12
0
def test_get_by_wrong_id(app):
    department = DepartmentServices.get_by_id(42)
    assert department is None
Ejemplo n.º 13
0
def test_avg_salary_empty_department(app):
    dep_3 = DepartmentServices.create(dict(name="Dep 3"))
    salary_3 = DepartmentServices.get_avg_salary(dep_3)
    assert salary_3 == 0
Ejemplo n.º 14
0
def test_create_wrong_data(app):
    with pytest.raises(TypeError):
        DepartmentServices.create(dict(title="Dep 3"))
Ejemplo n.º 15
0
def test_create(app):
    department = DepartmentServices.create(dict(name="Dep 3"))
    departments = DepartmentServices.get_all()
    assert department.name == "Dep 3"
    assert len(departments) == 3
    assert department in departments
Ejemplo n.º 16
0
def test_get_all(app):
    departments = DepartmentServices.get_all()
    assert len(departments) == 2
Ejemplo n.º 17
0
def test_get_by_id(app):
    department = DepartmentServices.get_by_id(1)
    assert department is not None
    assert department.name == "Dep 1"