def test_unhandled(self): """ Asserts that an unhandled type raises a PicklingError """ # find instance type for type_ in TypesManager.get_types(): if isinstance(type_, InstanceType): # patch can_encode so that no satisfying type is found with patch.object(type_, "can_encode") as can_encode_patch: can_encode_patch.return_value = False with self.assertRaises(PicklingError): dumps(Mock())
def register_event(self, event: Event) -> None: """ Add an event into the database Parameters ---------- event : action.event.Event The event that'll be added to the database """ items = (event.id, event.name, event.sched, pickle.dumps(event.args), pickle.dumps(event.kwargs)) self.cursor.execute("INSERT INTO events VALUES (?, ?, ?, ?, ?)", items) self.db.commit()
def test_converted_keys(self): """ Asserts that keys can be instances of types that are converted """ obj = { (1, 2): 3, datetime.now(): 1, timedelta(seconds=1): "timedelta", datetime.now(): { datetime.now(): datetime.now() } } obj_as_bytes = dumps(obj) from_obj = loads(obj_as_bytes) self.assertDictEqual(obj, from_obj) self.assertIsInstance(obj_as_bytes, bytes) # assert that there is no difference when dumping to file fd, temp_path = mkstemp() try: with open(temp_path, 'w') as fd: dump(obj, fd) with open(temp_path, 'r') as fd: from_file = load(fd) self.assertDictEqual(obj, from_file) finally: os.remove(temp_path)
def test_dump_failures(self): """ Asserts several dump/dumps error conditions """ # TypeError to PicklingError with self.assertRaises(PicklingError): dumps(callable) # TypeError to PicklingError with self.assertRaises(PicklingError): # here an opened file object is expected dump(callable, Mock()) # assert that an error not related to pickling is propagated as such with self.assertRaises(AttributeError): # here an opened file object is expected dump("", 'not an opened file instance')
def test_numeric_keys(self): """ Asserts that keys can be numeric """ obj = {None: 1, 1: 2, 1.1: 1.2} s = dumps(obj) from_s = loads(s) self.assertEqual(obj, from_s) # assert that there is no difference when dumping to file fd, temp_path = mkstemp() try: with open(temp_path, 'w') as fd: dump(obj, fd) with open(temp_path, 'r') as fd: from_file = load(fd) self.assertDictEqual(obj, from_file) finally: os.remove(temp_path)
def test_instance_roundtrip(self): class ClassToPersist(object): def __init__(self): self._list = [1, 2] self._dict = {"one": 1} self._set = {1, 2} self._tuple = (1, 2), self._int = 1 self._float = 1.0 self._datetime = datetime(year=1, month=1, day=1) self._timedelta = timedelta(days=1) self._defaultdict_int = defaultdict(int) self._defaultdict_list = defaultdict(list) self._defaultdict_set = defaultdict(set) instance = ClassToPersist() instance_as_bytes = dumps(instance) from_instance = loads(instance_as_bytes) for key, value in instance.__dict__.items(): self.assertEqual(from_instance[key], value)
def test_dict_roundtrip(self): """ Asserts dumps/loads dealing with bytes """ obj = { "_list": [1, 2], "_dict": { "one": 1 }, "_set": {1, 2}, "_tuple": (1, 2), "_int": 1, "_float": 1.0, "_datetime": datetime(year=1, month=1, day=1), "_timedelta": timedelta(days=1), "_defaultdict_int": defaultdict(int), "_defaultdict_list": defaultdict(list), "_defaultdict_set": defaultdict(set) } obj_as_bytes = dumps(obj) from_obj = loads(obj_as_bytes) self.assertDictEqual(obj, from_obj) self.assertIsInstance(obj_as_bytes, bytes)