예제 #1
0
    def setUp(self):
        """ Prepare test fixture, create application"""
        self.app = create_app('testing')
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()
        # adding departments
        self.test_department_1 = Department(name="TestDepartment1")
        self.test_department_2 = Department(name="TestDepartment2")
        # adding employee
        self.test_employee1 = Employee(first_name="John",
                                       last_name="Doe",
                                       date_of_birth=datetime(1990, 10, 10),
                                       phone_number="0970000000",
                                       email="*****@*****.**",
                                       salary=1000.00,
                                       department_id=1)
        self.test_employee2 = Employee(first_name="Jane",
                                       last_name="Doe",
                                       date_of_birth=datetime(1990, 10, 10),
                                       phone_number="0660000000",
                                       email="*****@*****.**",
                                       salary=2000.00,
                                       department_id=2)

        db.session.add_all([
            self.test_department_1, self.test_department_2,
            self.test_employee1, self.test_employee2
        ])
        db.session.commit()
        self.department_service = DepartmentService()
        self.employee_service = EmployeeService()
        # set up flask test client
        self.client = self.app.test_client()
예제 #2
0
 def fetch_all_of_department(department_uuid: str):
     """
     Returns tuple first element list of dictionaries af all employees
     of given department uuid, second element string name of department
     """
     department_id = Department.get_by_uuid(department_uuid).id
     department_name = Department.get_by_uuid(department_uuid).name
     if department_id:
         result = Employee.query.filter_by(
             department_id=department_id).all()
         return employees_schema.dump(result), department_name
예제 #3
0
    def delete(self, uuid: str):
        """
        Process DELETE request on resource, deletes record with given uuid.
        Returns status code 204 on successful delete in other case - 404
        """
        db_record = Department.get_by_uuid(uuid)
        if uuid and db_record:
            try:
                Department.delete(db_record)
            except IntegrityError:
                return {}, 404

            return {}, 204
        return {}, 404
예제 #4
0
 def add_one(data):
     """
     Takes json data, deserialize it and add to database
     Returns a tuple of added object uuid and dictionary of message
     If object hasn't been added returns None and error message
     """
     # Validate input
     try:
         data = employee_schema.load(data, partial=True)
     except ValidationError as err:
         return None, err.messages
     employee, department_uuid = data
     # Check if data for related department provided
     if not department_uuid:
         return None, {"message:": "department uuid not provided"}
     department_id = Department.get_by_uuid(department_uuid).id
     new_record = Employee(department_id=department_id, **employee)
     # Try to add record to db, if records exists it raise IntegrityError
     db.session.add(new_record)
     try:
         db.session.commit()
     except IntegrityError:
         return None, {"message": "Such record already exists"}
     return new_record.uuid, {
         "message": "Added new employee",
         "uuid": new_record.uuid
     }
예제 #5
0
 def fetch_one(uuid: str):
     """
     Takes uuid of department and returns serialised dictionary for one Department
     object, if uuid not found returns None.
     """
     query_result = Department.get_by_uuid(uuid=uuid)
     if not query_result:
         return None
     return department_schema.dump(query_result)
예제 #6
0
    def add_one(json_data):
        """
        Takes json data, deserialize it and add to database
        Returns a tuple of added object uuid and dictionary of message
        If object hasn't been added returns None and error message
        """
        # Validate and deserialize input
        try:
            data = department_schema.load(json_data, partial=("uuid", ))
        except ValidationError as err:
            return None, err.messages
        # Try to add record to db, if records exists it raise IntegrityError
        new_record = Department(**data)
        try:
            new_record.create()
        except IntegrityError:
            return None, {"message": "Such record already exists"}

        return new_record.uuid, {
            "message": "Added new department",
            "uuid": new_record.uuid
        }
예제 #7
0
    def test_employees(self):
        d1 = Department(name="Test_name_1")
        Department.create(d1)

        e1 = Employee(first_name="John",
                      last_name="Doe",
                      date_of_birth=datetime(1990, 11, 10),
                      phone_number="0970000000",
                      email="*****@*****.**",
                      salary=1000.00,
                      department_id=1)

        e2 = Employee(first_name="Jane",
                      last_name="Does",
                      date_of_birth=datetime(1970, 10, 5),
                      phone_number="0970000000",
                      email="*****@*****.**",
                      salary=2000.00,
                      department_id=1)
        Employee.create(e1)
        Employee.create(e2)

        self.assertIn(e1, db.session)
        self.assertIn(e2, db.session)

        self.assertEqual(
            e1.__repr__(),
            f"[{e1.uuid}] {e1.first_name} {e1.last_name}, D.O.B.: {e1.date_of_birth}"
        )
        self.assertEqual(
            e2.__repr__(),
            f"[{e2.uuid}] {e2.first_name} {e2.last_name}, D.O.B.: {e2.date_of_birth}"
        )

        Employee.delete(e1)
        Employee.delete(e2)

        self.assertNotIn(e1, db.session)
        self.assertNotIn(e2, db.session)
예제 #8
0
    def update_one_department(uuid: str, data: dict):
        """
        Takes uuid of department obj and data
        and updated department obj
        returns tuple of status and message
        """
        try:
            data = department_schema.load(data, partial=("uuid", ))
        except ValidationError as err:
            return "validation error", err.messages

        # query existing record in not found - return 404
        db_record = Department.get_by_uuid(uuid)
        if not db_record:
            return "not found", {"message": "record for update not found"}

        # update record if there is updated field
        if name := data.get("name"):
            db_record.name = name
예제 #9
0
        if not db_record:
            return "not found", {"message": "updated record not found"}

        employee, department_uuid = data

        # update record if there is updated field
        if first_name := employee.get("first_name"):
            db_record.first_name = first_name
        if last_name := employee.get("last_name"):
            db_record.last_name = last_name
        if date_of_birth := employee.get("date_of_birth"):
            db_record.date_of_birth = date_of_birth
        if phone_number := employee.get("phone_number"):
            db_record.phone_number = phone_number
        if email := employee.get("email"):
            db_record.email = email
        if salary := employee.get("salary"):
            db_record.salary = salary

        # update if uuid of department was given
        if department_uuid:
            department_id = Department.get_by_uuid(department_uuid).id
            if department_id:
                db_record.department_id = department_id

        try:
            db.session.commit()
        except IntegrityError:
            return "duplicate", {"message": "Such record already exists"}
        return "OK", {"message": "resource updated"}
예제 #10
0
def delete_department(uuid):
    db_record = Department.get_by_uuid(uuid)
    Department.delete(db_record)
    flash("Department Deleted Successfully")
    return redirect(url_for('main.get_departments'))
예제 #11
0
    def test_departments(self):
        d1 = Department(name="Test_name_1")
        d2 = Department(name="Test_name_2")
        Department.create(d1)
        Department.create(d2)
        self.assertIn(d1, db.session)
        self.assertIn(d2, db.session)
        self.assertEqual(d1.__repr__(), f"[{d1.uuid}] {d1.name}")

        Department.delete(d1)
        Department.delete(d2)

        self.assertNotIn(d1, db.session)
        self.assertNotIn(d2, db.session)