Exemplo n.º 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()
Exemplo n.º 2
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
     }
Exemplo n.º 3
0
 def fetch_one(uuid: str):
     """
     Takes uuid of employee and returns serialised dictionary for one Employee
     object, if uuid not found returns empty dictionary.
     """
     query_result = Employee.get_by_uuid(uuid=uuid)
     if not query_result:
         return None
     return employee_schema.dump(query_result)
Exemplo n.º 4
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 = Employee.get_by_uuid(uuid)
        if db_record:
            db.session.delete(db_record)
            db.session.commit()
            return {}, 204

        return {}, 404
Exemplo n.º 5
0
    def update_one(uuid: str, data: dict):
        """
        Takes uuid of Employee and dictionary of values to update employee object
        Returns tuple of status and message
        """
        # Validate and deserialize input
        try:
            data = employee_schema.load(data, partial=True)
        except ValidationError as err:
            return "validation error", err.messages

        # query existing record in not found
        db_record = Employee.get_by_uuid(uuid)
        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
Exemplo n.º 6
0
def delete_employee(uuid):
    db_record = Employee.get_by_uuid(uuid)
    Employee.delete(db_record)
    flash("Employee deleted successfully")
    return redirect(url_for('main.get_employees'))
Exemplo n.º 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)