Exemple #1
0
 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"])
Exemple #2
0
 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)
Exemple #3
0
 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()
Exemple #4
0
 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()
Exemple #5
0
 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()