def test_values_can_be_read_correctly(self): connection = self.get_fixture_connection() with open(self.fixture_json_path) as f: expected_values = json.load(f) for key, expected_value in expected_values.items(): result = connection.execute("SELECT value FROM data WHERE key=?", [key]) value = deserialize(result.fetchone()[0]) self.assertEqual(type(value).__name__, expected_value["type"]) if hasattr(value, "todense"): value = value.todense() self.assertEqual(value.tolist(), expected_value["value"])
def test_values_can_be_read_correctly(self): connection = self.get_fixture_connection() for key, expected_value in self.get_expected_values(): result = connection.execute( 'SELECT value FROM data WHERE key=?', [key] ) value = deserialize(result.fetchone()[0]) self.assertIsInstance(value, type(expected_value)) if hasattr(value, 'todense'): value = value.todense() expected_value = expected_value.todense() self.assertEqual(value.tolist(), expected_value.tolist())
def get_values_for_bnf_code(cursor, code): """ Return a list of matrices which are the prescribing values for the supplied BNF code, or None if there is no prescribing data """ result = cursor.execute( """ SELECT items, quantity, actual_cost, net_cost FROM presentation WHERE bnf_code=? AND items IS NOT NULL """, [code] ) rows = list(result) if rows: return [deserialize(value) for value in rows[0]]
def __init__(self, connection, table, key_field, value_field): results = connection.execute( "SELECT {}, {} FROM {}".format(key_field, value_field, table) ) self.matrices = {} self.nonzero_values = 0 for key, value in results: matrix = deserialize(value) self.matrices[key] = matrix if hasattr(matrix, "count_nonzero"): self.nonzero_values += matrix.count_nonzero() else: self.nonzero_values += numpy.count_nonzero(matrix) self.practices = dict(connection.execute("SELECT code, offset FROM practice")) self.dates = dict(connection.execute("SELECT date, offset FROM date")) for date, offset in list(self.dates.items()): self.dates[date + " 00:00:00 UTC"] = offset
def test_sqlite_roundtrip_with_compression(self): obj = {"hello": "world" * 256} data = serialize_compressed(obj) new_data = roundtrip_through_sqlite(sqlite3.Binary(data)) new_obj = deserialize(new_data) self.assertEqual(new_obj, obj)
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)