class TestHandler(unittest.TestCase): def setUp(self): self.cargo = Cargo(handler=handler) def test_incorrect_command(self): cmd = { 'action': 'create_person', } response = self.cargo._handler(cmd) expected_response = { 'action': 'create_person', 'data': { 'message': 'Command is incorrect' }, 'response': 400, 'time': None } self.assertEqual(response, expected_response) def test_unsupported_action(self): cmd = { 'action': 'unsupported_action', 'time': 1.1, } response = self.cargo._handler(cmd) expected_response = { 'action': 'unsupported_action', 'data': { 'message': 'Action with name unsupported_action not supported' }, 'response': 404, 'time': 1.1 } self.assertEqual(response, expected_response)
class TestProtocolCreateResponse(unittest.TestCase): def setUp(self): self.cargo = Cargo(handler=handler) def test_action_response(self): cmd = { 'time': 1000.10, 'data': {}, } response = self.cargo._handler(cmd) expected_response = { 'action': None, 'data': { 'message': 'Command is incorrect' }, 'response': 400, 'time': 1000.1 } self.assertEqual(response, expected_response) def test_time_response(self): cmd = { 'action': 'create_person', } response = self.cargo._handler(cmd) expected_response = { 'action': 'create_person', 'data': { 'message': 'Command is incorrect' }, 'response': 400, 'time': None } self.assertEqual(response, expected_response)
class TestEcho(unittest.TestCase): def setUp(self): self.cargo = Cargo(handler=handler) def test_echo(self): cmd = { 'action': 'echo', 'time': 1.1, } response = self.cargo._handler(cmd) expected_response = { 'action': 'echo', 'data': None, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response)
def setUp(self): self.cargo = Cargo(handler=handler)
def setUp(self): Base.metadata.create_all() self.cargo = Cargo(handler=handler)
class TestRoute(unittest.TestCase): def setUp(self): Base.metadata.create_all() self.cargo = Cargo(handler=handler) def tearDown(self): os.remove(os.path.basename(DATABASE)) def test_create_route(self): self.cargo._handler(CREATE_WAREHOUSE1_COMMAND) self.cargo._handler(CREATE_WAREHOUSE2_COMMAND) response = self.cargo._handler(CREATE_ROUTE1_COMMAND) expected_response = { 'action': 'create_route', 'data': { 'message': 'Route added', }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_route(self): self.cargo._handler(CREATE_WAREHOUSE1_COMMAND) self.cargo._handler(CREATE_WAREHOUSE2_COMMAND) self.cargo._handler(CREATE_ROUTE1_COMMAND) cmd = { 'action': 'read_route', 'time': 1.1, 'data': { 'route': { 'id': 1 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_route', 'data': { 'message': 'Route read', 'route': { 'id': 1, 'from_warehouse_id': 1, 'to_warehouse_id': 2, 'load_id': None, 'person_id': None, 'vehicle_id': None, }, }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_route_wrong_data(self): self.cargo._handler(CREATE_ROUTE1_COMMAND) cmd = { 'action': 'read_route', 'time': 1.1, 'data': { 'route': { 'id': 999 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_route', 'data': { 'message': 'Route with id=999 not found', }, 'response': 404, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_route_wrong_request(self): self.cargo._handler(CREATE_ROUTE1_COMMAND) cmd = { 'action': 'read_route', 'time': 1.1, 'data': { 'route': {} }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_route', 'data': { 'message': 'No id or data specified', }, 'response': 400, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_all_routes_empty(self): cmd = { 'action': 'read_all_routes', 'time': 1.1, 'data': dict(), } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_all_routes', 'data': { 'message': 'Routes read', 'routes': [] }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_all_routes(self): self.cargo._handler(CREATE_CITY1_COMMAND) self.cargo._handler(CREATE_CITY2_COMMAND) self.cargo._handler(CREATE_CITY3_COMMAND) self.cargo._handler(CREATE_WAREHOUSE1_COMMAND) self.cargo._handler(CREATE_WAREHOUSE2_COMMAND) self.cargo._handler(CREATE_WAREHOUSE3_COMMAND) self.cargo._handler(CREATE_ROUTE1_COMMAND) self.cargo._handler(CREATE_ROUTE2_COMMAND) cmd = { 'action': 'read_all_routes', 'time': 1.1, 'data': dict(), } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_all_routes', 'data': { 'message': 'Routes read', 'routes': [ { 'id': 1, 'from_warehouse_id': 1, 'to_warehouse_id': 2, 'load_id': None, 'person_id': None, 'vehicle_id': None, }, { 'id': 2, 'from_warehouse_id': 3, 'to_warehouse_id': 2, 'load_id': None, 'person_id': None, 'vehicle_id': None, }, ] }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_update_route(self): self.cargo._handler(CREATE_CITY3_COMMAND) self.cargo._handler(CREATE_WAREHOUSE3_COMMAND) self.cargo._handler(CREATE_ROUTE1_COMMAND) cmd = { 'action': 'update_route', 'time': 1.1, 'data': { 'route': { 'id': 1, 'from_warehouse_id': 3, } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'update_route', 'data': { 'message': 'Route updated', }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) cmd = { 'action': 'read_route', 'time': 1.1, 'data': { 'route': { 'id': 1 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_route', 'data': { 'message': 'Route read', 'route': { 'id': 1, 'from_warehouse_id': 3, 'to_warehouse_id': 2, 'load_id': None, 'person_id': None, 'vehicle_id': None, }, }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_update_route_wrong_data(self): self.cargo._handler(CREATE_ROUTE1_COMMAND) cmd = { 'action': 'update_route', 'time': 1.1, 'data': { 'route': { 'id': 999, 'from_warehouse_id': 3, } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'update_route', 'data': { 'message': 'Route with id=999 not found', }, 'response': 404, 'time': 1.1 } self.assertEqual(response, expected_response) def test_delete_route(self): self.cargo._handler(CREATE_ROUTE1_COMMAND) cmd = { 'action': 'delete_route', 'time': 1.1, 'data': { 'route': { 'id': 1 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'delete_route', 'data': { 'message': 'Route deleted', }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response)
class TestWarehouse(unittest.TestCase): def setUp(self): Base.metadata.create_all() self.cargo = Cargo(handler=handler) def tearDown(self): os.remove(os.path.basename(DATABASE)) def test_create_warehouse(self): self.cargo._handler(CREATE_CITY1_COMMAND) self.cargo._handler(CREATE_CITY2_COMMAND) response = self.cargo._handler(CREATE_WAREHOUSE1_COMMAND) expected_response = { 'action': 'create_warehouse', 'data': { 'message': 'Warehouse added', }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_warehouse(self): self.cargo._handler(CREATE_CITY1_COMMAND) self.cargo._handler(CREATE_CITY2_COMMAND) self.cargo._handler(CREATE_WAREHOUSE1_COMMAND) cmd = { 'action': 'read_warehouse', 'time': 1.1, 'data': { 'warehouse': { 'id': 1 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_warehouse', 'data': { 'message': 'Warehouse read', 'warehouse': { 'id': 1, 'address': 'пр. Ленина 51', 'city_id': 1, }, }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_warehouse_wrong_data(self): self.cargo._handler(CREATE_CITY1_COMMAND) self.cargo._handler(CREATE_CITY2_COMMAND) self.cargo._handler(CREATE_WAREHOUSE1_COMMAND) cmd = { 'action': 'read_warehouse', 'time': 1.1, 'data': { 'warehouse': { 'id': 999 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_warehouse', 'data': { 'message': 'Warehouse with id=999 not found', }, 'response': 404, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_warehouse_wrong_request(self): self.cargo._handler(CREATE_CITY1_COMMAND) self.cargo._handler(CREATE_CITY2_COMMAND) self.cargo._handler(CREATE_WAREHOUSE1_COMMAND) cmd = { 'action': 'read_warehouse', 'time': 1.1, 'data': { 'warehouse': {} }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_warehouse', 'data': { 'message': 'No id or data specified', }, 'response': 400, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_all_warehouses_empty(self): cmd = { 'action': 'read_all_warehouses', 'time': 1.1, 'data': dict(), } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_all_warehouses', 'data': { 'message': 'Warehouses read', 'warehouses': [] }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_all_warehouses(self): self.cargo._handler(CREATE_CITY1_COMMAND) self.cargo._handler(CREATE_CITY2_COMMAND) self.cargo._handler(CREATE_WAREHOUSE1_COMMAND) self.cargo._handler(CREATE_WAREHOUSE2_COMMAND) cmd = { 'action': 'read_all_warehouses', 'time': 1.1, 'data': dict(), } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_all_warehouses', 'data': { 'message': 'Warehouses read', 'warehouses': [ { 'id': 1, 'address': 'пр. Ленина 51', 'city_id': 1, }, { 'id': 2, 'address': 'пр. К.Маркса 1', 'city_id': 2, }, ] }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_update_warehouse(self): self.cargo._handler(CREATE_CITY1_COMMAND) self.cargo._handler(CREATE_WAREHOUSE1_COMMAND) cmd = { 'action': 'update_warehouse', 'time': 1.1, 'data': { 'warehouse': { 'id': 1, 'address': 'ул. Советская 2', } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'update_warehouse', 'data': { 'message': 'Warehouse updated', }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) cmd = { 'action': 'read_warehouse', 'time': 1.1, 'data': { 'warehouse': { 'id': 1 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_warehouse', 'data': { 'message': 'Warehouse read', 'warehouse': { 'id': 1, 'address': 'ул. Советская 2', 'city_id': 1, }, }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_update_warehouse_wrong_data(self): self.cargo._handler(CREATE_CITY1_COMMAND) self.cargo._handler(CREATE_WAREHOUSE1_COMMAND) cmd = { 'action': 'update_warehouse', 'time': 1.1, 'data': { 'warehouse': { 'id': 999, 'address': 'ул. Советская 2', } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'update_warehouse', 'data': { 'message': 'Warehouse with id=999 not found', }, 'response': 404, 'time': 1.1 } self.assertEqual(response, expected_response) def test_delete_warehouse(self): self.cargo._handler(CREATE_CITY1_COMMAND) self.cargo._handler(CREATE_WAREHOUSE1_COMMAND) cmd = { 'action': 'delete_warehouse', 'time': 1.1, 'data': { 'warehouse': { 'id': 1 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'delete_warehouse', 'data': { 'message': 'Warehouse deleted', }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response)
class TestCity(unittest.TestCase): def setUp(self): Base.metadata.create_all() self.cargo = Cargo(handler=handler) def tearDown(self): os.remove(os.path.basename(DATABASE)) def test_create_city(self): response = self.cargo._handler(CREATE_CITY1_COMMAND) expected_response = { 'action': 'create_city', 'data': { 'message': 'City added', }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_city(self): self.cargo._handler(CREATE_CITY1_COMMAND) cmd = { 'action': 'read_city', 'time': 1.1, 'data': { 'city': { 'id': 1 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_city', 'data': { 'message': 'City read', 'city': { 'id': 1, 'name': 'Moscow', }, }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_city_wrong_data(self): self.cargo._handler(CREATE_CITY1_COMMAND) cmd = { 'action': 'read_city', 'time': 1.1, 'data': { 'city': { 'id': 999 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_city', 'data': { 'message': 'City with id=999 not found', }, 'response': 404, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_city_wrong_request(self): self.cargo._handler(CREATE_CITY1_COMMAND) cmd = { 'action': 'read_city', 'time': 1.1, 'data': { 'city': {} }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_city', 'data': { 'message': 'No id or data specified', }, 'response': 400, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_all_cities_empty(self): cmd = { 'action': 'read_all_cities', 'time': 1.1, 'data': dict(), } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_all_cities', 'data': { 'message': 'Cities read', 'cities': [] }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_all_cities(self): self.cargo._handler(CREATE_CITY1_COMMAND) self.cargo._handler(CREATE_CITY2_COMMAND) cmd = { 'action': 'read_all_cities', 'time': 1.1, 'data': dict(), } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_all_cities', 'data': { 'message': 'Cities read', 'cities': [ { 'id': 1, 'name': 'Moscow' }, { 'id': 2, 'name': 'SPb' }, ] }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_update_city(self): self.cargo._handler(CREATE_CITY1_COMMAND) cmd = { 'action': 'update_city', 'time': 1.1, 'data': { 'city': { 'id': 1, 'name': 'Barnaul', } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'update_city', 'data': { 'message': 'City updated', }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) cmd = { 'action': 'read_city', 'time': 1.1, 'data': { 'city': { 'id': 1 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_city', 'data': { 'message': 'City read', 'city': { 'id': 1, 'name': 'Barnaul', }, }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_update_city_wrong_data(self): self.cargo._handler(CREATE_CITY1_COMMAND) cmd = { 'action': 'update_city', 'time': 1.1, 'data': { 'city': { 'id': 999, 'name': 'Barnaul', } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'update_city', 'data': { 'message': 'City with id=999 not found', }, 'response': 404, 'time': 1.1 } self.assertEqual(response, expected_response) def test_delete_city(self): self.cargo._handler(CREATE_CITY1_COMMAND) cmd = { 'action': 'delete_city', 'time': 1.1, 'data': { 'city': { 'id': 1 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'delete_city', 'data': { 'message': 'City deleted', }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response)
def test_instant_creation_no_handle(self): with self.assertRaises(TypeError): Cargo()
parser = create_parser() def _create_controller(handler): window = MainView() # dialogs = Dialogs(window, 'CompanyStatistics') # return MainController(window, dialogs) return MainController(window, handler) if parser.parse_args().migrate: module_name_list = [f'cargo.{item}.models' for item in INSTALLED_MODULES] module_path_list = (os.path.join(BASE_DIR, item, 'models.py') for item in INSTALLED_MODULES) for index, path in enumerate(module_path_list): if os.path.exists(path): __import__(module_name_list[index]) Base.metadata.create_all() elif parser.parse_args().schema: generate_schema() else: app = QApplication(sys.argv) with Cargo(handler) as cargo: # cargo.run() controller = _create_controller(handler) controller.show() sys.exit(app.exec_())
class TestStaff(unittest.TestCase): def setUp(self): Base.metadata.create_all() self.cargo = Cargo(handler=handler) def tearDown(self): os.remove(os.path.basename(DATABASE)) def test_read_all_persons(self): cmd = { 'action': 'read_all_persons', 'time': 1.1, 'data': dict(), } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_all_persons', 'data': { 'message': 'Persons read', 'persons': [] }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_create_person(self): cmd = { 'action': 'create_person', 'time': 1.1, 'data': {'person': {'name': 'Duncan', 'surname': 'MacLeod'}}, } response = self.cargo._handler(cmd) expected_response = { 'action': 'create_person', 'data': { 'message': 'Person added', }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_person(self): cmd = { 'action': 'create_person', 'time': 1.1, 'data': {'person': {'name': 'Duncan', 'surname': 'MacLeod'}}, } self.cargo._handler(cmd) cmd = { 'action': 'read_person', 'time': 1.1, 'data': {'person': {'id': 1}}, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_person', 'data': { 'message': 'Person read', 'person': { 'id': 1, 'name': 'Duncan', 'surname': 'MacLeod', 'patronymic': None, 'phone': None, 'info': None, }, }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_person_wrong_data(self): cmd = { 'action': 'create_person', 'time': 1.1, 'data': {'person': {'name': 'Duncan', 'surname': 'MacLeod'}}, } self.cargo._handler(cmd) cmd = { 'action': 'read_person', 'time': 1.1, 'data': {'person': {'id': 999}}, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_person', 'data': { 'message': 'Person with id=999 not found', }, 'response': 404, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_person_wrong_request(self): cmd = { 'action': 'create_person', 'time': 1.1, 'data': {'person': {'name': 'Duncan', 'surname': 'MacLeod'}}, } self.cargo._handler(cmd) cmd = { 'action': 'read_person', 'time': 1.1, 'data': {'person': {}}, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_person', 'data': { 'message': 'No id or data specified', }, 'response': 400, 'time': 1.1 } self.assertEqual(response, expected_response) def test_update_person(self): cmd = { 'action': 'create_person', 'time': 1.1, 'data': {'person': {'name': 'Duncan', 'surname': 'MacLeod'}}, } self.cargo._handler(cmd) cmd = { 'action': 'update_person', 'time': 1.1, 'data': {'person': {'id': 1, 'name': 'Scotch', 'surname': 'MacLeod'}}, } self.cargo._handler(cmd) response = self.cargo._handler(cmd) expected_response = { 'action': 'update_person', 'data': { 'message': 'Person updated', }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) cmd = { 'action': 'read_person', 'time': 1.1, 'data': {'person': {'id': 1}}, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_person', 'data': { 'message': 'Person read', 'person': { 'id': 1, 'name': 'Scotch', 'surname': 'MacLeod', 'patronymic': None, 'phone': None, 'info': None, }, }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_update_person_wrong_data(self): cmd = { 'action': 'create_person', 'time': 1.1, 'data': {'person': {'name': 'Duncan', 'surname': 'MacLeod'}}, } self.cargo._handler(cmd) cmd = { 'action': 'update_person', 'time': 1.1, 'data': {'person': {'id': 999, 'name': 'Scotch', 'surname': 'MacLeod'}}, } self.cargo._handler(cmd) response = self.cargo._handler(cmd) expected_response = { 'action': 'update_person', 'data': { 'message': 'Person with id=999 not found', }, 'response': 404, 'time': 1.1 } self.assertEqual(response, expected_response) def test_delete_person(self): cmd = { 'action': 'create_person', 'time': 1.1, 'data': {'person': {'name': 'Duncan', 'surname': 'MacLeod'}}, } self.cargo._handler(cmd) cmd = { 'action': 'delete_person', 'time': 1.1, 'data': {'person': {'id': 1}}, } response = self.cargo._handler(cmd) expected_response = { 'action': 'delete_person', 'data': { 'message': 'Person deleted', }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_all_routes_by_person(self): self.cargo._handler(CREATE_CITY1_COMMAND) self.cargo._handler(CREATE_CITY2_COMMAND) self.cargo._handler(CREATE_CITY3_COMMAND) self.cargo._handler(CREATE_WAREHOUSE1_COMMAND) self.cargo._handler(CREATE_WAREHOUSE2_COMMAND) self.cargo._handler(CREATE_WAREHOUSE3_COMMAND) self.cargo._handler(CREATE_ROUTE1_COMMAND) self.cargo._handler(CREATE_ROUTE2_COMMAND) self.cargo._handler(CREATE_PERSON1_COMMAND) cmd = { 'action': 'read_routes_of_person', 'time': 1.1, 'data': {'person': {'id': 1}}, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_routes_of_person', 'data': { 'message': 'Routes read', 'routes': [ { 'id': 1, 'from_warehouse_id': 1, 'to_warehouse_id': 2, 'load_id': None, 'person_id': 1, 'vehicle_id': None, }, { 'id': 2, 'from_warehouse_id': 3, 'to_warehouse_id': 2, 'load_id': None, 'person_id': 1, 'vehicle_id': None, }, ] }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response)
class TestVehicle(unittest.TestCase): def setUp(self): Base.metadata.create_all() self.cargo = Cargo(handler=handler) def tearDown(self): os.remove(os.path.basename(DATABASE)) def test_read_all_vehicles(self): cmd = { 'action': 'read_all_vehicles', 'time': 1.1, 'data': dict(), } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_all_vehicles', 'data': { 'message': 'Vehicles read', 'vehicles': [] }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_create_vehicle(self): response = self.cargo._handler(CREATE_VEHICLE_COMMAND) expected_response = { 'action': 'create_vehicle', 'data': { 'message': 'Vehicle added', }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_vehicle(self): self.cargo._handler(CREATE_VEHICLE_COMMAND) cmd = { 'action': 'read_vehicle', 'time': 1.1, 'data': { 'vehicle': { 'id': 1 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_vehicle', 'data': { 'message': 'Vehicle read', 'vehicle': { 'id': 1, 'model': 'KAMAZ-65207-87', 'plate': 'a000aa00', 'year': None, 'payload': 14725, 'run': None, 'fuel_consumption': None, 'volume': 48.3, }, }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_vehicle_wrong_data(self): self.cargo._handler(CREATE_VEHICLE_COMMAND) cmd = { 'action': 'read_vehicle', 'time': 1.1, 'data': { 'vehicle': { 'id': 999 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_vehicle', 'data': { 'message': 'Vehicle with id=999 not found', }, 'response': 404, 'time': 1.1 } self.assertEqual(response, expected_response) def test_read_vehicle_wrong_request(self): self.cargo._handler(CREATE_VEHICLE_COMMAND) cmd = { 'action': 'read_vehicle', 'time': 1.1, 'data': { 'vehicle': {} }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_vehicle', 'data': { 'message': 'No id or data specified', }, 'response': 400, 'time': 1.1 } self.assertEqual(response, expected_response) def test_update_vehicle(self): self.cargo._handler(CREATE_VEHICLE_COMMAND) cmd = { 'action': 'update_vehicle', 'time': 1.1, 'data': { 'vehicle': { 'id': 1, 'plate': 'a0001aa00' } }, } self.cargo._handler(cmd) response = self.cargo._handler(cmd) expected_response = { 'action': 'update_vehicle', 'data': { 'message': 'Vehicle updated', }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) cmd = { 'action': 'read_vehicle', 'time': 1.1, 'data': { 'vehicle': { 'id': 1 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'read_vehicle', 'data': { 'message': 'Vehicle read', 'vehicle': { 'id': 1, 'model': 'KAMAZ-65207-87', 'plate': 'a0001aa00', 'year': None, 'payload': 14725, 'run': None, 'fuel_consumption': None, 'volume': 48.3, }, }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response) def test_update_vehicle_wrong_data(self): self.cargo._handler(CREATE_VEHICLE_COMMAND) cmd = { 'action': 'update_vehicle', 'time': 1.1, 'data': { 'vehicle': { 'id': 999, 'plate': 'a0001aa00' } }, } self.cargo._handler(cmd) response = self.cargo._handler(cmd) expected_response = { 'action': 'update_vehicle', 'data': { 'message': 'Vehicle with id=999 not found', }, 'response': 404, 'time': 1.1 } self.assertEqual(response, expected_response) def test_delete_vehicle(self): self.cargo._handler(CREATE_VEHICLE_COMMAND) cmd = { 'action': 'delete_vehicle', 'time': 1.1, 'data': { 'vehicle': { 'id': 1 } }, } response = self.cargo._handler(cmd) expected_response = { 'action': 'delete_vehicle', 'data': { 'message': 'Vehicle deleted', }, 'response': 200, 'time': 1.1 } self.assertEqual(response, expected_response)