async def test_get_vehicle_by_id(self, app: FastAPI, client: AsyncClient, test_vehicle: VehiclesInDB) -> None: res = await client.get( app.url_path_for("vehicles:get-vehicle-by-id", id=test_vehicle.id)) assert res.status_code == HTTP_200_OK vehicle = VehiclesInDB(**res.json()) assert vehicle == test_vehicle
async def test_get_all_vehicles_returns_valid_response( self, app: FastAPI, client: AsyncClient, test_vehicle: VehiclesInDB) -> None: res = await client.get(app.url_path_for("vehicles:get-all-vehicles")) assert res.status_code == HTTP_200_OK assert isinstance(res.json(), list) assert len(res.json()) > 0 vehicles = [VehiclesInDB(**l) for l in res.json()] assert test_vehicle in vehicles
async def populate_vehicle(self, *, vehicle: VehiclesInDB, user_id: int) -> VehiclesInDB: return VehiclesPublic( **vehicle.dict(), insurance=await self.insurances_repo.get_last_created_insurance_by_vehicle_id( vehicle_id=vehicle.id), roles=await self.roles_repo.get_user_role_by_vehicle_user_id( vehicle_id=vehicle.id, user_id=user_id), )
async def create_vehicle(self, *, new_vehicle: VehiclesCreate, id) -> VehiclesInDB: query_values = new_vehicle.dict() new_vehicle = await self.db.fetch_one(query=CREATE_VEHICLE_QUERY, values=query_values) already_added = await self.get_vehicle_by_sign(sign=new_vehicle["sign"] ) if already_added: await self.roles_repo.create_role_of_user_for_vehicle( vehicle_id=new_vehicle["id"], user_id=id) return await self.populate_vehicle( vehicle=VehiclesInDB(**new_vehicle), user_id=id) else: await self.insurances_repo.create_first_insurance_for_vehicle( vehicle_id=new_vehicle["id"]) await self.roles_repo.create_role_of_user_for_vehicle( vehicle_id=new_vehicle["id"], user_id=id) return await self.populate_vehicle( vehicle=VehiclesInDB(**new_vehicle), user_id=id)
async def get_vehicle_by_sign(self, *, sign: str, populate: bool = False) -> VehiclesInDB: vehicle_record = await self.db.fetch_one( query=GET_VEHICLE_BY_SIGN_QUERY, values={"sign": sign}) if not vehicle_record: return None else: vehicle = VehiclesInDB(**vehicle_record) return vehicle
async def get_vehicle_by_id(self, *, id: int, user_id: int, populate: bool = True): vehicle_record = await self.db.fetch_one(query=GET_VEHICLE_BY_ID_QUERY, values={ "id": id, "user_id": user_id }) if not vehicle_record: return None else: vehicle = VehiclesInDB(**vehicle_record) if populate: return await self.populate_vehicle(vehicle=vehicle, user_id=user_id) return vehicle
async def get_vehicle_by_user_id_digit(self, *, sign: str, user_id: int, populate: bool = True): sign_digit = int(''.join(filter(str.isdigit, sign))) print(sign_digit) vehicle = '' vehicle_records = await self.get_all_vehicles_by_user_id(id=user_id) print(len(vehicle_records)) for vehicle_record in vehicle_records: print(1) check = int(''.join(filter(str.isdigit, vehicle_record['sign']))) if check == sign_digit: vehicle = VehiclesInDB(**vehicle_record) if populate: return await self.populate_vehicle(vehicle=vehicle, user_id=user_id) if vehicle == '': return None else: return vehicle