def test_yaml_serialize(self): filename = 'cesar.yaml' cesar_from_yaml = Cesar.from_yaml_file(filename) with open(get_dump_path(filename)) as f: cesar_json = yaml.load(f.read(), Loader=yaml.FullLoader) garages = [] for garage_json in cesar_json.get('garages'): cars = [] for car_json in garage_json.get('cars'): car = Car(price=car_json.get('price'), car_type=car_json.get('car_type'), producer=car_json.get('producer'), mileage=car_json.get('mileage'), number=car_json.get('number')) cars.append(car) garage = Garage(town=garage_json.get('town'), places=garage_json.get('places'), owner=garage_json.get('owner'), cars=cars, number=garage_json.get('number')) garages.append(garage) cesar = Cesar(name=cesar_json.get('name'), garages=garages, register_id=cesar_json.get('register_id')) self.assertEqual(cesar, cesar_from_yaml)
def test_pickle_serialize(self): filename = 'garage.pickle' garage_from_pickle = Garage.from_pickle_file(filename) with open(get_dump_path(filename), "rb") as f: garage = pickle.loads(f.read()) self.assertEqual(garage, garage_from_pickle)
def test_json_serialize(self): filename = 'car.json' car_from_json = Car.from_json_file(filename) with open(get_dump_path(filename)) as f: car_json = json.loads(f.read()) car = Car(price=car_json.get('price'), car_type=car_json.get('car_type'), producer=car_json.get('producer'), mileage=car_json.get('mileage'), number=car_json.get('number')) self.assertEqual(car, car_from_json)
def from_pickle_file(file_name): with open(get_dump_path(file_name), 'rb') as f: data = pickle.load(f) return data
def to_pickle_file(self, file_name): with open(get_dump_path(file_name), 'wb') as f: pickle.dump(self, f)
def from_yaml_file(cls, file_name): with open(get_dump_path(file_name), 'r') as f: data = yaml.load(f, Loader=yaml.FullLoader) return cls.from_yaml(data)
def to_yaml_file(self, file_name): with open(get_dump_path(file_name), 'w') as f: yaml.dump(self.to_dict(), f)
def from_json_file(cls, file_name): with open(get_dump_path(file_name), 'r') as f: data = json.load(f) return cls.from_json(data)
def to_json_file(self, file_name): with open(get_dump_path(file_name), 'w') as f: json.dump(self.to_dict(), f, indent=4)