def test_save_link(app): with app.app_context(): hardware = UsedHardwareModel(hardware_id=1, employee_id=1) hardware.save_link() hardware_db = UsedHardwareModel.find_by_hardware_id(hardware.id) assert hardware_db is not None
def test_delete_link(app): with app.app_context(): hardware = UsedHardwareModel(hardware_id=1, employee_id=1) db.session.add(hardware) db.session.commit() hardware.delete_link() hardware_db = UsedHardwareModel.find_by_hardware_id(hardware.id) assert hardware_db is None
def test_get_all(app): with app.app_context(): hardware = UsedHardwareModel(hardware_id=1, employee_id=1) db.session.add(hardware) db.session.commit() hardware_db = UsedHardwareModel.get_all() assert isinstance(hardware_db, list) assert len(hardware_db) == 1
def test_find_by_hardware_id(app): with app.app_context(): hardware = UsedHardwareModel(hardware_id=1, employee_id=1) db.session.add(hardware) db.session.commit() hardware_db = UsedHardwareModel.find_by_hardware_id(hardware.id) assert hardware_db.hardware_id == hardware.hardware_id assert hardware_db.employee_id == hardware.employee_id assert hardware_db.id == hardware.id
def test_find_by_employee_id(app): with app.app_context(): hardware = UsedHardwareModel(hardware_id=1, employee_id=1) db.session.add(hardware) db.session.commit() hardware_db = UsedHardwareModel.find_by_employee_id(hardware.id) assert isinstance(hardware_db, list) assert len(hardware_db) == 1 assert hardware_db[0].hardware_id == hardware.hardware_id assert hardware_db[0].employee_id == hardware.employee_id
def delete(employee_id): """Delete employee""" employee = EmployeeModel.find_by_id(employee_id) used_hardware = UsedHardwareModel.find_by_employee_id(employee_id) # First unlink all hardware, then delete if used_hardware: UsedHardwareModel.delete_links(employee_id) if employee is not None: employee.delete_employee() return employee api.abort(404, f'Employee {employee_id} does not exist')
def delete(hardware_id): """Unlink hardware""" hardware = UsedHardwareModel.find_by_hardware_id(hardware_id) if hardware is not None: hardware.delete_link() return hardware api.abort(404, f'Hardware {hardware_id} does not exist')
def test_delete_links(app): with app.app_context(): hardware_1 = UsedHardwareModel(hardware_id=1, employee_id=1) db.session.add(hardware_1) db.session.commit() hardware_2 = UsedHardwareModel(hardware_id=2, employee_id=1) db.session.add(hardware_2) db.session.commit() hardware_count = UsedHardwareModel.delete_links(hardware_1.employee_id) hardware_db_1 = UsedHardwareModel.find_by_hardware_id(hardware_1.id) hardware_db_2 = UsedHardwareModel.find_by_hardware_id(hardware_2.id) assert hardware_db_1 is None assert hardware_db_2 is None assert hardware_count == 2
def delete(hardware_id): """Delete hardware""" hardware = HardwareModel.find_by_id(hardware_id) used_hardware = UsedHardwareModel.find_by_hardware_id(hardware_id) # First unlink, then delete if used_hardware is not None: used_hardware.delete_link() if hardware is not None: hardware.delete_hardware() return hardware api.abort(404, f'Hardware {hardware_id} does not exist')
def post(hardware_id): """Link hardware with employee""" data = parser_used.parse_args() data['hardware_id'] = hardware_id employee_id = data['employee_id'] if HardwareModel.find_by_id(hardware_id) is None: api.abort(400, f'Hardware {hardware_id} does not exist') if EmployeeModel.find_by_id(employee_id) is None: api.abort(400, f'Employee {employee_id} does not exist') if UsedHardwareModel.find_by_hardware_id(hardware_id) is not None: api.abort(400, f'Hardware {hardware_id} already in use') hardware = UsedHardwareModel(**data) try: hardware.save_link() except: api.abort(500, 'Failed to link hardware') return hardware
def put(hardware_id): """Update employee linked to hardware""" data = parser_used.parse_args() employee_id = data['employee_id'] employee = EmployeeModel.find_by_id(employee_id) if employee is None: api.abort(400, f'Employee {employee_id} does not exist') hardware = UsedHardwareModel.find_by_hardware_id(hardware_id) if hardware is not None: hardware.employee_id = employee_id try: hardware.save_link() except: api.abort(500, 'Failed to update employee') return hardware api.abort( 400, f'Hardware {hardware_id} is not in use and cannot by modified')
def get(hardware_id): """Get used hardware""" hardware = UsedHardwareModel.find_by_hardware_id(hardware_id) if hardware is not None: return hardware api.abort(404, f'Hardware {hardware_id} is not in use')