def create_fixture(cls): temp_file = cls.fixture_path + '.tmp' if os.path.exists(temp_file): os.unlink(temp_file) connection = sqlite3.connect(temp_file) connection.executescript( """ CREATE TABLE data (key TEXT, value BLOB); CREATE TABLE environment_metadata (key TEXT, value TEXT); """ ) for key, value in cls.get_expected_values(): if '.uncompressed' in key: data = serialize(value) elif '.compressed' in key: data = serialize_compressed(value) else: raise RuntimeError('Invalid key') connection.execute( 'INSERT INTO data VALUES (?, ?)', [key, sqlite3.Binary(data)] ) for key, value in cls.get_environment_metadata(): connection.execute( 'INSERT INTO environment_metadata VALUES (?, ?)', [key, value] ) connection.commit() connection.close() os.rename(temp_file, cls.fixture_path)
def create_fixture(cls): temp_db_path = cls.fixture_db_path + ".tmp" if os.path.exists(temp_db_path): os.unlink(temp_db_path) connection = sqlite3.connect(temp_db_path) connection.executescript(""" CREATE TABLE data (key TEXT, value BLOB); CREATE TABLE environment_metadata (key TEXT, value TEXT); """) json_data = {} for key, value in cls.generate_test_values(): json_value = value if hasattr(value, "todense"): json_value = json_value.todense() json_data[key] = { "type": type(value).__name__, "value": json_value.tolist(), } if ".uncompressed" in key: data = serialize(value) elif ".compressed" in key: data = serialize_compressed(value) else: raise RuntimeError("Invalid key") connection.execute("INSERT INTO data VALUES (?, ?)", [key, sqlite3.Binary(data)]) for key, value in cls.get_environment_metadata(): connection.execute( "INSERT INTO environment_metadata VALUES (?, ?)", [key, value]) connection.commit() connection.close() os.rename(temp_db_path, cls.fixture_db_path) with open(cls.fixture_json_path, "w") as f: json.dump(json_data, f, indent=2)
def test_sqlite_roundtrip(self): obj = {"hello": 123} data = serialize(obj) new_data = roundtrip_through_sqlite(sqlite3.Binary(data)) new_obj = deserialize(new_data) self.assertEqual(new_obj, obj)
def test_dtype_is_preserved(self): obj = scipy.sparse.csc_matrix((5, 4), dtype=numpy.uint16) new_obj = deserialize(serialize(obj)) self.assertEqual(obj.dtype, new_obj.dtype)
def test_matrix_serialisation(self): obj = scipy.sparse.csc_matrix((5, 4)) new_obj = deserialize(serialize(obj)) self.assertTrue(numpy.array_equal(obj.todense(), new_obj.todense()))
def test_simple_serialisation_with_compression(self): obj = {"hello": "world" * 256} data = serialize(obj) compressed_data = serialize_compressed(obj) self.assertLess(len(compressed_data), len(data)) self.assertEqual(deserialize(compressed_data), obj)
def test_simple_serialisation(self): obj = {"hello": 123} self.assertEqual(deserialize(serialize(obj)), obj)