def test_adding_backend(self): FileStorage.register_backend("example", ExampleFile) self.assertEqual(FileStorage.BACKENDS["example"], ExampleFile) fs = FileStorage("example", "filename") recs = fs.load() self.assertEqual(len(recs), 1) self.assertEqual(recs[0].fields, ["a", "b"])
def test_yaml(self): options = {"table-is-field-0": True} fmt = "yaml" filename = self.tempdir.name + "/" + fmt fs = FileStorage(fmt, filename, options=options) fs.store(self.data) self.check_serialized_data(fs, filename, self.data)
def run_test(self, fmt, lossy=False): options = {"table-is-field-0": True} filename = self.tempdir.name + "/" + fmt fs = FileStorage(fmt, filename, options=options) fs.store(self.data) m = fs.load() self.compare(self.data, m, lossy) self.check_serialized_data(fs, filename, m)
def compression_test(self, format): tempdir = tempfile.TemporaryDirectory() options = {"compression": format} magic = self.magic[format] filename = tempdir.name + "/dtb" fs = FileStorage("json", filename, options=options) fs.store(self.data) m = fs.load() self.compare(self.data, m) with open(filename, "rb") as fp: header = fp.read(len(magic)) self.assertEqual(header, magic) tempdir.cleanup()
def test_default(self): tempdir = tempfile.TemporaryDirectory() filename = tempdir.name + "/dtb" fs = FileStorage("json", filename) fs.store(self.data) m = fs.load() self.compare(self.data, m) with open(filename, "rb") as fp: header = fp.read(20) for k, v in self.magic.items(): self.assertNotEqual(header[0 : len(v)], v) self.assertEqual(header[0:1], b"[") tempdir.cleanup()
def test_autodetection(self): for i in ["sha256", "sha384", "sha512"]: x = self.create_filestorage_hash(i) x.fs.store([Record(["", ""])]) fs = FileStorage("csv", x.tempfile, "hash") recs = fs.load() self.assertEqual(len(recs), 1) self.assertIsInstance(recs[0], Record) self.assertEqual(recs[0].fields[:], ["", ""]) with open(x.tempfile + ".checksum", "w") as fp: # Something other than the correct value. fp.write("%s:2:%s\n" % (i, self.WRONG)) with self.assertRaisesRegex(newfol.exception.FilemanipError, "corrupt"): x.fs.load() x.tempdir.cleanup()
def test_removing_hook(self): FileStorage.register_hook("example", ExampleFile) self.assertEqual(FileStorage.HOOKS["example"], ExampleFile) FileStorage.unregister_hook("example") with self.assertRaises(KeyError): FileStorage.HOOKS["example"]
def test_adding_hook(self): FileStorage.register_hook("example", ExampleHook) self.assertEqual(FileStorage.HOOKS["example"], ExampleHook)
def test_removing_backend(self): FileStorage.register_backend("example", ExampleFile) self.assertEqual(FileStorage.BACKENDS["example"], ExampleFile) FileStorage.unregister_backend("example") with self.assertRaises(KeyError): FileStorage.BACKENDS["example"]
def test_canonicalization(self): for i in ("sha256", "sha384", "sha512"): result = FileStorage._canonicalize_transaction_types(["hash", i]) self.assertEqual(result, [i]) self.assertEqual(FileStorage._canonicalize_transaction_types(None), None)