def test_from_dict_empty(self): """ Asserts a Params class is correctly instantiated from an empty dict """ params = Params.from_dict({}) self.assertTrue(params, msg='Params could not be instantiated from dict.') keys = Params.__dataclass_fields__.keys() default_params = Params() for k in keys: self.assertEqual(default_params.__getattribute__(k), params.__getattribute__(k), msg=f'Key {k} does not match in Params class.')
def read_entities( input_dir: str ) -> Tuple[Dict[str, Rider], Dict[str, Vehicle], Dict[str, Depot], Params]: """Method to parse the Riders, Vehicles and Depots from JSON to Dict""" riders_file = RIDERS_FILE.format(input_dir=input_dir) with open(riders_file) as f: logging.info(f'Read riders from {riders_file}.') riders_dicts = json.load(f) riders = { r_dict['rider_id']: Rider.from_dict(r_dict) for r_dict in riders_dicts } logging.info(f'Successfully parsed {len(riders)} riders.') vehicles_file = VEHICLES_FILE.format(input_dir=input_dir) with open(vehicles_file) as f: vehicles_dicts = json.load(f) logging.info(f'Read vehicles from {vehicles_file}.') vehicles = { v_dict['vehicle_id']: Vehicle.from_dict(v_dict) for v_dict in vehicles_dicts } logging.info(f'Successfully parsed {len(vehicles)} vehicles.') depots_file = DEPOTS_FILE.format(input_dir=input_dir) with open(depots_file) as f: depots_dicts = json.load(f) logging.info(f'Read depots from {depots_file}.') depots = { d_dict['depot_id']: Depot.from_dict(d_dict) for d_dict in depots_dicts } logging.info(f'Successfully parsed {len(depots)} depots.') params_file = PARAMS_FILE.format(input_dir=input_dir) with open(params_file) as f: logging.info(f'Read params from {params_file}.') params_dict = json.load(f) params = Params.from_dict(params_dict) logging.info(f'Successfully parsed {len(params_dict)} params.') return riders, vehicles, depots, params
def test_from_dict_complete(self): """ Asserts a Params class is correctly instantiated from a complete dict """ params_dict = { 'GEOHASH_PRECISION_GROUPING': 8, 'FIRST_SOLUTION_STRATEGY': 'AUTOMATIC', 'SEARCH_METAHEURISTIC': 'AUTOMATIC', 'SEARCH_TIME_LIMIT': 4, 'SEARCH_SOLUTIONS_LIMIT': 3000, } params = Params.from_dict(params_dict) self.assertTrue(params, msg='Params could not be instantiated from dict.') keys = Params.__dataclass_fields__.keys() for k in keys: self.assertEqual(params_dict[k], params.__getattribute__(k), msg=f'Key {k} does not match in Params class.')
def test_from_dict_partial(self): """ Asserts a Params class is correctly instantiated from a partial dict """ params_dict = {'GEOHASH_PRECISION_GROUPING': 4} params = Params.from_dict(params_dict) self.assertTrue(params, msg='Params could not be instantiated from dict.') keys = Params.__dataclass_fields__.keys() default_params = Params() for k in keys: if k in params_dict.keys(): self.assertEqual( params_dict[k], params.__getattribute__(k), msg=f'Key {k} does not match in Params class.') else: self.assertEqual( default_params.__getattribute__(k), params.__getattribute__(k), msg=f'Key {k} does not match in Params class.')