def test_create_insert_query_data(self) -> None: metadata = ExampleMetadata( test_str="test", test_int=123, test_float=0.123, test_bool=True, test_timestamp=datetime.now(), ) now = datetime.now() insert_store = ExampleStore( metadata=metadata, a=["a", "b", "c"], b=[1, 2, 3], c=[True, False, True], d=[now, now, now], ) conn_conf = self.sql_db.connection_config() data_token = DataToken("test_table", "raw") with Sqlite3Database(conn_conf) as db: db.insert(data_token, insert_store) with Sqlite3Database(conn_conf) as db: query_mask = ExampleStore.b >= 2 actual = db.query(ExampleStore, data_token, query_mask) expected = ExampleStore( metadata=metadata, a=["b", "c"], b=[2, 3], c=[False, True], d=[now, now] ) assert_that(actual.equals(expected), equal_to(True))
def test_upsert_from_link(self) -> None: with self.db_adapter: test1 = ExampleStore(a=["a", "b", "c"], b=[1, 2, 3], c=[True, False, True]) self.db_adapter.create_group(ExampleStore.data_token.data_group) self.db_adapter.create_group_table(ExampleStore.data_token, ExampleStore) self.db_adapter.create_index(ExampleStore.data_token, ExampleStore.a_index) self.db_adapter.insert(ExampleStore.data_token, test1) token2 = DataToken("test2", RAW_GROUP) test2 = ExampleStore(a=["b", "e"], b=[4, 5], c=[True, False]) self.db_adapter.create_group_table(token2, ExampleStore) self.db_adapter.create_index(token2, ExampleStore.a_index) self.db_adapter.insert(token2, test2) linked_store = ExampleStore.from_backend(MockBackend( token2, test2.columns), validate=False) self.db_adapter.upsert(ExampleStore.data_token, linked_store, [ExampleStore.a]) raw1 = self.db_adapter.query(ExampleStore.data_token) queried2 = ExampleStore.from_rows(raw1) test2 = ExampleStore(a=["a", "b", "c", "e"], b=[1, 4, 3, 5], c=[True, True, True, False]) assert_that(queried2.equals(test2), equal_to(True))
def test_append(self) -> None: actual = self.test_store.append(ExampleStore(a="d", b=4, c=False), ignore_index=True) assert_that(actual.a.tolist(), equal_to(["a", "b", "c", "d"])) assert_that(actual.b.tolist(), equal_to([1, 2, 3, 4])) assert_that(actual.c.tolist(), equal_to([True, False, True, False])) expected = ExampleStore(a=["a", "b", "c", "d"], b=[1, 2, 3, 4], c=[True, False, True, False]) assert_that(actual.equals(expected), is_(True))
def test_upsert_from_values(self) -> None: with self.db_adapter: test_metadata = ExampleMetadata( test_str="test", test_int=123, test_float=0.123, test_bool=True, test_timestamp=datetime.now(), ) test1 = ExampleStore( a=["a", "b", "c"], b=[1, 2, 3], c=[True, False, True], metadata=test_metadata, ) self.db_adapter.create_group(ExampleStore.data_token.data_group) self.db_adapter.create_group_table(ExampleStore.data_token, ExampleStore) self.db_adapter.create_index(ExampleStore.data_token, ExampleStore.a_index) self.db_adapter.insert(ExampleStore.data_token, test1) actual_metadata = self.db_adapter.get_group_table_metadata( ExampleStore.data_token, ExampleMetadata) assert_that(actual_metadata, equal_to(test_metadata)) raw1 = self.db_adapter.query(ExampleStore.data_token) queried1 = ExampleStore.from_rows(raw1) assert_that(queried1.equals(test1), equal_to(True)) test2_metadata = ExampleMetadata( test_str="test2", test_int=234, test_float=1.234, test_bool=False, test_timestamp=datetime.now(), ) test2 = ExampleStore(a=["b", "e"], b=[4, 5], c=[True, False], metadata=test2_metadata) self.db_adapter.upsert(ExampleStore.data_token, test2, [ExampleStore.a]) actual_metadata = self.db_adapter.get_group_table_metadata( ExampleStore.data_token, ExampleMetadata) assert_that(actual_metadata, equal_to(test2_metadata)) raw1 = self.db_adapter.query(ExampleStore.data_token) queried2 = ExampleStore.from_rows(raw1) test2 = ExampleStore(a=["a", "b", "c", "e"], b=[1, 4, 3, 5], c=[True, True, True, False]) assert_that(queried2.equals(test2), equal_to(True))
def test_update(self) -> None: data = ExampleStore(a=["a", "b", "c"], b=[1, 2, 3], c=[True, False, True]) self.database.insert(ExampleStore.data_token, data) row_replacement = ExampleStore(a="b", b=2, c=True) self.database.update( ExampleStore.data_token, row_replacement, [ExampleStore.a, ExampleStore.b] ) queried = self.database.query(ExampleStore, ExampleStore.data_token) expected = ExampleStore(a=["a", "b", "c"], b=[1, 2, 3], c=[True, True, True]) assert_that(queried.equals(expected), is_(True))
def setup_method(self) -> None: self.test_store = ExampleStore(a=["a", "b", "c"], b=[1, 2, 3], c=[True, False, True]) conn_config = self.sql_db.connection_config() self.db = Sqlite3Database(conn_config) self.db.insert(ExampleStore.data_token, self.test_store) self.db_store = ExampleStore.link(self.db, ExampleStore.data_token) self.backend = self.db_store._data_backend self.index = self.db_store.index self.a_index = self.db_store.a_index self.ab_index = self.db_store.ab_index
def test_delete(self) -> None: data = ExampleStore(a=["a", "b", "c"], b=[1, 2, 3], c=[True, False, True]) self.database.insert(ExampleStore.data_token, data) self.database.delete(ExampleStore.data_token, ExampleStore.b < 3) queried = self.database.query(ExampleStore, ExampleStore.data_token) expected = ExampleStore( a=["c"], b=[3], c=[True], ) assert_that(queried.equals(expected), is_(True))
def test_single_row(self) -> None: example_row = ExampleStore(a=["a"], b=[1]) assert_that(example_row.a.values, equal_to(["a"])) assert_that(example_row.b.values, equal_to([1])) example_row = ExampleStore(a="a", b=1) assert_that(example_row.a.values, equal_to(["a"])) assert_that(example_row.b.values, equal_to([1])) test_slice = self.test_store.iloc[0] assert_that(test_slice.a.values, equal_to(["a"])) assert_that(test_slice.b.values, equal_to([1])) assert_that(test_slice.b.values, equal_to([True]))
def setup_class(cls) -> None: cls.metadata = ExampleMetadata( test_str="test", test_int=123, test_float=0.123, test_bool=True, test_timestamp=datetime.now(), ) cls.test_store = ExampleStore(metadata=cls.metadata, a=["a", "b", "c"], b=[1, 2, 3], c=[True, False, True]) cls.test_row0 = ExampleStore(a="a", b=1, c=True, index=0) cls.test_row2 = ExampleStore(a="c", b=3, c=True, index=2)
def test_get_mask(self) -> None: mask = cast(Column, self.test_store.b > 1) actual = self.test_store[mask.values] expected = ExampleStore(a=["b", "c"], b=[2, 3], c=[False, True], index=[1, 2]) assert_that(actual.equals(expected), is_(True))
def test_get_query(self) -> None: query = ExampleStore.b > 1 actual = self.test_store[query] expected = ExampleStore(a=["b", "c"], b=[2, 3], c=[False, True], index=[1, 2]) assert_that(actual.equals(expected), is_(True))
def test_get_columns(self) -> None: test1 = self.test_store["a", "b"] test2 = self.test_store[ExampleStore.a, ExampleStore.b] test3 = self.test_store[[ExampleStore.a, ExampleStore.b]] expected = ExampleStore(a=["a", "b", "c"], b=[1, 2, 3]) assert_that(test1.equals(test2), is_(True)) assert_that(test2.equals(test3), is_(True)) assert_that(test3.equals(expected), is_(True))
def test_equals(self) -> None: assert_that(self.test_store.equals(self.test_store), is_(True)) assert_that(self.test_store.equals(1), is_(False)) test = ExampleStore(a=["a", "b", "c"], b=[1, 2, 3], c=[True, False, True]) assert_that(self.test_store.equals(test), is_(False))
def test_invalid_types(self) -> None: try: ExampleStore(a=["a", "b", "c"], b=["a", "b", "c"], c=[True, False, True]) fail("Expected exception") except Exception as e: assert_that("Invalid types provided for: ['b']", is_in(str(e)))
def test_rows_builder(self) -> None: builder = ExampleStore.builder() builder.append_row(a="a", b=1, c=True) builder.append_row(a="b", b=2, c=False) builder.append_row(a="c", b=3, c=True) test_store = builder.build() assert_that(test_store.a.tolist(), equal_to(["a", "b", "c"])) assert_that(test_store.b.tolist(), equal_to([1, 2, 3])) assert_that(test_store.c.tolist(), equal_to([True, False, True]))
def test_columns_builder(self) -> None: builder = ExampleStore.builder() builder["a"] = ["a", "b", "c"] builder.append_column("b", [1, 2, 3]).append_column(ExampleStore.c, [True, False, True]) test_store = builder.build() assert_that(test_store.a.tolist(), equal_to(["a", "b", "c"])) assert_that(test_store.b.tolist(), equal_to([1, 2, 3])) assert_that(test_store.c.tolist(), equal_to([True, False, True]))
def test_empty_store(self) -> None: example_row = ExampleStore(a=[], b=[], c=[]) assert_that(len(example_row.a), equal_to(0)) assert_that(len(example_row.b), equal_to(0)) assert_that(len(example_row.c), equal_to(0)) example_row = ExampleStore(b=[], c=[]) assert_that(example_row.a, equal_to(None)) assert_that(len(example_row.b), equal_to(0)) assert_that(len(example_row.c), equal_to(0)) example_row = ExampleStore(c=[]) assert_that(example_row.a, equal_to(None)) assert_that(example_row.b, equal_to(None)) assert_that(len(example_row.c), equal_to(0)) example_row = ExampleStore() assert_that(example_row.a, equal_to(None)) assert_that(example_row.b, equal_to(None)) assert_that(example_row.c, equal_to(None))
def test_delete(self) -> None: with self.db_adapter: test1 = ExampleStore(a=["a", "b", "c"], b=[1, 2, 3], c=[True, False, True]) self.db_adapter.create_group(ExampleStore.data_token.data_group) self.db_adapter.create_group_table(ExampleStore.data_token, ExampleStore) self.db_adapter.insert(ExampleStore.data_token, test1) raw1 = self.db_adapter.query(ExampleStore.data_token) queried1 = ExampleStore.from_rows(raw1) assert_that(queried1.equals(test1), equal_to(True)) self.db_adapter.delete(ExampleStore.data_token, ExampleStore.b == 2) raw2 = self.db_adapter.query(ExampleStore.data_token) queried2 = ExampleStore.from_rows(raw2) test2 = ExampleStore(a=["a", "c"], b=[1, 3], c=[True, True]) assert_that(queried2.equals(test2), equal_to(True))
def test_store_class(self) -> None: self.registrar.create_table(ExampleStore.data_token, ExampleStore) store_class = self.registrar.store_type(ExampleStore.data_token) actual = store_class(a=1, b=2, c=3) expected = ExampleStore(a=1, b=2, c=3) assert_that(actual.equals(expected), is_(True)) assert_that(store_class.__name__, equal_to("ExampleStore")) assert_that(actual.test_method(), equal_to("test_result")) for actual, expected in zip(store_class.columns, ExampleStore.columns): assert_that(actual.name, equal_to(expected.name)) assert_that(actual.dtype, equal_to(expected.dtype))
def test_insert_from_link(self) -> None: with self.db_adapter: test1 = ExampleStore(a=["a", "b", "c"], b=[1, 2, 3], c=[True, False, True]) self.db_adapter.create_group(ExampleStore.data_token.data_group) self.db_adapter.create_group_table(ExampleStore.data_token, ExampleStore) self.db_adapter.insert(ExampleStore.data_token, test1) token2 = DataToken("test2", RAW_GROUP) self.db_adapter.create_group_table(token2, ExampleStore) linked_store = ExampleStore.from_backend(MockBackend( ExampleStore.data_token, test1.columns), validate=False) self.db_adapter.insert(token2, linked_store) raw1 = self.db_adapter.query(token2) queried1 = ExampleStore.from_rows(raw1) assert_that(test1.equals(queried1), equal_to(True))
def setup_method(self) -> None: self.test_store = ExampleStore(a=["a", "b", "c"], b=[1, 2, 3], c=[True, False, True]) conn_config = self.sql_db.connection_config() self.db = Sqlite3Database(conn_config) self.db.insert(ExampleStore.data_token, self.test_store) self.db_store = ExampleStore.link(self.db, ExampleStore.data_token) self.data_backend = self.db_store._data_backend self.test_series0 = PandasBackend(Series({ "a": "a", "b": 1, "c": True }), index=[0]) self.test_series2 = PandasBackend(Series({ "a": "c", "b": 3, "c": True }), index=[2])
def test_drop_group(self) -> None: assert_that(self.database.has_group(RAW_GROUP), equal_to(False)) assert_that(self.database.has_table(ExampleStore.data_token), equal_to(False)) data = ExampleStore(a="a", b=1, c=True) self.database.insert(ExampleStore.data_token, data) assert_that(self.database.has_group(RAW_GROUP), equal_to(True)) assert_that(self.database.has_table(ExampleStore.data_token), equal_to(True)) self.database.drop_group(RAW_GROUP) assert_that(self.database.has_group(RAW_GROUP), equal_to(False)) assert_that(self.database.has_table(ExampleStore.data_token), equal_to(False))
def test_insert_from_values(self) -> None: with self.db_adapter: test1 = ExampleStore(a=["a", "b", "c"], b=[1, 2, 3], c=[True, False, True]) self.db_adapter.create_group(ExampleStore.data_token.data_group) self.db_adapter.create_group_table(ExampleStore.data_token, ExampleStore) self.db_adapter.insert(ExampleStore.data_token, test1) raw1 = self.db_adapter.query(ExampleStore.data_token) queried1 = ExampleStore.from_rows(raw1) assert_that(queried1.equals(test1), equal_to(True)) test_metadata = ExampleMetadata( test_str="test", test_int=123, test_float=0.123, test_bool=True, test_timestamp=datetime.now(), ) test2 = ExampleStore(a=["d"], b=[4], c=[False], metadata=test_metadata) self.db_adapter.insert(ExampleStore.data_token, test2) actual_metadata = self.db_adapter.get_group_table_metadata( ExampleStore.data_token, ExampleMetadata) assert_that(actual_metadata, equal_to(test_metadata)) test3 = ExampleStore.concat([test1, test2], ignore_index=True) raw2 = self.db_adapter.query(ExampleStore.data_token) queried2 = ExampleStore.from_rows(raw2) assert_that(queried2.equals(test3), equal_to(True))
def test_columns(self) -> None: columns = ["a", "b", "c"] types = [String, Int64, Boolean] for actual_column, expected_name, expected_type in zip( self.test_store.columns, columns, types): assert_that(type(actual_column), equal_to(ColumnAlias)) assert_that(str(actual_column), equal_to(expected_name)) assert_that(actual_column.dtype, equal_to(expected_type)) test_store2 = ExampleStore(b=[1, 2, 3], c=[True, False, True]) columns = ["b", "c"] types = [Int64, Boolean] for actual_column, expected_name, expected_type in zip( test_store2.columns, columns, types): assert_that(type(actual_column), equal_to(ColumnAlias)) assert_that(str(actual_column), equal_to(expected_name)) assert_that(actual_column.dtype, equal_to(expected_type))
def test_missing_column(self) -> None: test_store = ExampleStore(a=["a", "b", "c"], b=[1, 2, 3]) assert_that(test_store["a"].tolist(), equal_to(["a", "b", "c"])) assert_that(test_store["b"].tolist(), equal_to([1, 2, 3])) assert_that(test_store.c, equal_to(None)) assert_that(test_store["c"], equal_to(None)) try: test_store.d fail("Expected exception") except Exception as e: assert_that("Could not match 'd' to ExampleStore column", is_in(str(e))) try: test_store["d"] fail("Expected exception") except Exception as e: assert_that("Could not match 'd' to ExampleStore column", is_in(str(e)))
def test_has_table(self) -> None: test = ExampleStore(a=["1", "2", "3"], b=[1, 2, 3], c=[True, False, True]) self.database.insert(ExampleStore.data_token, test) all_tokens = [ ExampleStore.data_token, DataToken("table_reference", PROTECTED_GROUP), DataToken("store_reference", PROTECTED_GROUP), DataToken("TableReference_v1_definition", PROTECTED_GROUP), DataToken("StoreDefinition_v1_definition", PROTECTED_GROUP), DataToken("StoreReference_v1_definition", PROTECTED_GROUP), ] for token in all_tokens: assert_that(self.database.has_table(token), is_(True)) assert_that( self.database.has_table(DataToken("table_reference", "NOT_GROUP")), is_(False), ) assert_that( self.database.has_table(DataToken("not_table", "public")), is_(False) )
def test_list_group_tables(self) -> None: protected_tokens = [ DataToken("table_reference", PROTECTED_GROUP), DataToken("store_reference", PROTECTED_GROUP), DataToken("index_reference", PROTECTED_GROUP), DataToken("StoreDefinition_v1_definition", PROTECTED_GROUP), DataToken("TableReference_v1_definition", PROTECTED_GROUP), DataToken("StoreReference_v1_definition", PROTECTED_GROUP), DataToken("IndexReference_v1_definition", PROTECTED_GROUP), DataToken("ExampleStore_v1_definition", PROTECTED_GROUP), ] test = ExampleStore(a=["1", "2", "3"], b=[1, 2, 3], c=[True, False, True]) self.database.insert(ExampleStore.data_token, test) assert_that( self.database.list_group_tables(RAW_GROUP), equal_to([ExampleStore.data_token]), ) assert_that( self.database.list_group_tables(PROTECTED_GROUP), equal_to(protected_tokens), )
def test_create_index(self) -> None: with self.db_adapter: test1 = ExampleStore(a=["a", "b", "c"], b=[1, 2, 3], c=[True, False, True]) self.db_adapter.create_group(ExampleStore.data_token.data_group) self.db_adapter.create_group_table(ExampleStore.data_token, ExampleStore) self.db_adapter.create_index(ExampleStore.data_token, ExampleStore.a_index) token2 = DataToken("test2", RAW_GROUP) self.db_adapter.create_group_table(token2, ExampleStore) self.db_adapter.create_index(token2, ExampleStore.a_index) assert_that( self.db_adapter.has_index(ExampleStore.data_token, ExampleStore.a_index), equal_to(True), ) assert_that( self.db_adapter.has_index(token2, ExampleStore.a_index), equal_to(True)) self.db_adapter.insert(ExampleStore.data_token, test1)
class TestDatabaseBackend: sql_db: Sqlite3Container @classmethod def setup_class(cls) -> None: tmp_db_dir = Path(tempfile.gettempdir()) / "tanuki_test" if tmp_db_dir.exists(): shutil.rmtree(tmp_db_dir, ignore_errors=True) tmp_db_dir.mkdir() cls.sql_db = Sqlite3Container(tmp_db_dir) @classmethod def teardown_class(cls) -> None: cls.sql_db.stop() def setup_method(self) -> None: self.test_store = ExampleStore(a=["a", "b", "c"], b=[1, 2, 3], c=[True, False, True]) conn_config = self.sql_db.connection_config() self.db = Sqlite3Database(conn_config) self.db.insert(ExampleStore.data_token, self.test_store) self.db_store = ExampleStore.link(self.db, ExampleStore.data_token) self.data_backend = self.db_store._data_backend self.test_series0 = PandasBackend(Series({ "a": "a", "b": 1, "c": True }), index=[0]) self.test_series2 = PandasBackend(Series({ "a": "c", "b": 3, "c": True }), index=[2]) def teardown_method(self) -> None: self.sql_db.reset() def test_is_link(self) -> None: assert_that(self.data_backend.is_link(), is_(True)) def test_link_token(self) -> None: assert_that(self.data_backend.link_token(), equal_to(ExampleStore.data_token)) def test_to_pandas(self) -> None: assert_that( self.data_backend.to_pandas().equals(self.test_store.to_pandas()), is_(True)) def test_columns(self) -> None: assert_that( [str(col) for col in self.data_backend.columns], equal_to(["a", "b", "c"]), ) def test_values(self) -> None: expected = np.array( [ ["a", 1, True], ["b", 2, False], ["c", 3, True], ], dtype="object", ) assert_that(np.array_equal(self.data_backend.values, expected), is_(True)) def test_dtypes(self) -> None: assert_that( self.data_backend.dtypes, equal_to({ "a": String, "b": Int64, "c": Boolean }), ) def test_cast_columns(self) -> None: try: self.data_backend.cast_columns({"a": Int64}) fail("Expected exception") except Exception as e: assert_that(isinstance(e, NotImplementedError), is_(True)) def test_to_dict(self) -> None: expected = { "a": ["a", "b", "c"], "b": [1, 2, 3], "c": [True, False, True], } assert_that(self.data_backend.to_dict(), equal_to(expected)) def test_index(self) -> None: initial_expected = np.array([0, 1, 2]) assert_that(self.data_backend.index.name, equal_to("index")) assert_that(isinstance(self.data_backend.index, PandasIndex), equal_to(True)) assert_that( np.array_equal(self.data_backend.index.values, initial_expected), equal_to(True), ) new_expected = np.array(["a", "b", "c"]) new_store = self.data_backend.set_index(ExampleStore.a_index) assert_that(new_store.index.name, equal_to("a_index")) assert_that(isinstance(new_store.index, DatabaseIndex), equal_to(True)) assert_that(np.array_equal(new_store.index.values, new_expected), equal_to(True)) reset_store = new_store.reset_index() assert_that(reset_store.index.name, equal_to("index")) assert_that(isinstance(reset_store.index, PandasIndex), equal_to(True)) assert_that(np.array_equal(reset_store.index.values, initial_expected), equal_to(True)) def test_iloc(self) -> None: actual_series = self.data_backend.iloc[0] assert_that(actual_series.equals(self.test_series0), is_(True)) def test_loc(self) -> None: test_slice = self.data_backend.iloc[[0, 2]] actual_series = test_slice.loc[2] assert_that(actual_series.equals(self.test_series2), is_(True)) def test_equals(self) -> None: test = DatabaseBackend(ExampleStore, self.db, ExampleStore.data_token) assert_that(self.data_backend.equals(test), equal_to(True)) newBackend = self.data_backend[ExampleStore.a] test = DatabaseBackend(ExampleStore, self.db, ExampleStore.data_token) assert_that(newBackend.equals(test), equal_to(False)) test = DatabaseBackend(ExampleStore, self.db, ExampleStore.data_token, None, ["a"]) assert_that(self.data_backend.equals(test[ExampleStore.a]), equal_to(False)) test = PandasBackend({ "a": ["a", "b", "d"], "b": [1, 2, 3], "c": [True, False, True] }) assert_that(self.data_backend.equals(test), equal_to(False)) def test_eq(self) -> None: expected = PandasBackend({"a": ["a"], "b": [1], "c": [True]}) query = self.data_backend["a"] == "a" queried = self.data_backend.query(query) assert_that(queried.equals(expected), equal_to(True)) test = PandasBackend({"a": ["a", "c"], "b": [1, 3]}) expected = PandasBackend({ "a": ["a", "c"], "b": [1, 3], "c": [True, True] }) query = self.data_backend == test queried = self.data_backend.query(query) assert_that(queried.equals(expected), equal_to(True)) def test_ne(self) -> None: expected = PandasBackend({ "a": ["b", "c"], "b": [2, 3], "c": [False, True] }) query = self.data_backend["a"] != "a" queried = self.data_backend.query(query) assert_that(queried.equals(expected), equal_to(True)) test = PandasBackend({"a": ["a", "b", "d"], "b": [2, 2, 3]}) expected = PandasBackend({ "a": ["a", "c"], "b": [1, 3], "c": [True, True] }) query = self.data_backend != test queried = self.data_backend.query(query) assert_that(queried.equals(expected), equal_to(True)) def test_gt(self) -> None: expected = PandasBackend({"a": ["c"], "b": [3], "c": [True]}) query = self.data_backend["b"] > 2 queried = self.data_backend.query(query) assert_that(queried.equals(expected), equal_to(True)) test = PandasBackend({"b": [2]}) query = self.data_backend > test queried = self.data_backend.query(query) assert_that(queried.equals(expected), equal_to(True)) def test_ge(self) -> None: expected = PandasBackend({ "a": ["b", "c"], "b": [2, 3], "c": [False, True] }) query = self.data_backend["b"] >= 2 queried = self.data_backend.query(query) assert_that(queried.equals(expected), equal_to(True)) test = PandasBackend({"b": [2]}) query = self.data_backend >= test queried = self.data_backend.query(query) assert_that(queried.equals(expected), equal_to(True)) def test_lt(self) -> None: expected = PandasBackend({"a": ["a"], "b": [1], "c": [True]}) query = self.data_backend["b"] < 2 queried = self.data_backend.query(query) assert_that(queried.equals(expected), equal_to(True)) test = PandasBackend({"b": [2]}) query = self.data_backend < test queried = self.data_backend.query(query) assert_that(queried.equals(expected), equal_to(True)) def test_le(self) -> None: expected = PandasBackend({ "a": ["a", "b"], "b": [1, 2], "c": [True, False] }) query = self.data_backend["b"] <= 2 queried = self.data_backend.query(query) assert_that(queried.equals(expected), equal_to(True)) test = PandasBackend({"b": [2]}) query = self.data_backend <= test queried = self.data_backend.query(query) assert_that(queried.equals(expected), equal_to(True)) def test_len(self) -> None: assert_that(len(self.data_backend), equal_to(3)) def test_iter(self) -> None: columns = ["a", "b", "c"] for actual_col, expected_col in zip(self.data_backend, columns): assert_that(actual_col, equal_to(expected_col)) def test_iterows(self) -> None: for i, row in self.data_backend.iterrows(): iloc_row = self.data_backend.iloc[i] assert_that(row.equals(iloc_row), is_(True)) def test_itertuples(self) -> None: for i, a, b, c in self.data_backend.itertuples(): iloc_row = self.data_backend.iloc[i] assert_that(a, equal_to(iloc_row["a"].values[0])) assert_that(b, equal_to(iloc_row["b"].values[0])) assert_that(c, equal_to(iloc_row["c"].values[0])) def test_getitem(self) -> None: expected = DatabaseBackend(ExampleStore, self.db, ExampleStore.data_token, selected_columns=["b"]) assert_that(self.data_backend["b"].equals(expected), equal_to(True)) def test_getitems(self) -> None: expected = DatabaseBackend(ExampleStore, self.db, ExampleStore.data_token, selected_columns=["a", "b"]) assert_that( self.data_backend.getitems(["a", "b"]).equals(expected), equal_to(True)) def test_getmask(self) -> None: test = self.data_backend.getmask([True, False, True]) expected = PandasBackend( { "a": ["a", "c"], "b": [1, 3], "c": [True, True] }, index=PandasIndex(PIndex([0, 2], name="index"), []), ) assert_that(test.equals(expected), equal_to(True)) def test_query(self) -> None: query = (ExampleStore.a == "a") | (ExampleStore.b == 3) test = self.data_backend.query(query) expected = PandasBackend( { "a": ["a", "c"], "b": [1, 3], "c": [True, True] }, index=PandasIndex(PIndex([0, 1], name="index"), []), ) assert_that(test.equals(expected), equal_to(True)) def test_setitem(self) -> None: try: self.data_backend["a"] = ["d", "e", "f"] fail("Expected exception") except NotImplementedError as e: assert_that( str(e), equal_to( "The current version of Tanuki does not support Store to DB writing" ), ) def test_append(self) -> None: try: postfix = PandasBackend({"a": ["d"], "b": [4], "c": [False]}) self.data_backend.append(postfix, ignore_index=True) fail("Expected exception") except NotImplementedError as e: assert_that( str(e), equal_to( "The current version of Tanuki does not support Store to DB writing" ), ) def test_drop_indices(self) -> None: try: self.data_backend.drop_indices([1]) fail("Expected exception") except NotImplementedError as e: assert_that( str(e), equal_to( "The current version of Tanuki does not support Store to DB writing" ), ) def test_concat(self) -> None: try: postfix = PandasBackend({"a": ["d"], "b": [4], "c": [False]}) DatabaseBackend.concat([self.data_backend, postfix], ignore_index=True) fail("Expected exception") except NotImplementedError as e: assert_that( str(e), equal_to( "The current version of Tanuki does not support Store to DB writing" ), ) def test_str(self) -> None: expected = "Database Link: raw.test\nActive Columns: ['a', 'b', 'c']" assert_that(str(self.data_backend), equal_to(expected)) def test_repr(self) -> None: expected = "Database Link: raw.test\nActive Columns: ['a', 'b', 'c']" assert_that(repr(self.data_backend), equal_to(expected))
def test_insert(self) -> None: data = ExampleStore(a="a", b=1, c=True) self.database.insert(ExampleStore.data_token, data) assert_that(self.database.has_table(ExampleStore.data_token), is_(True)) queried = self.database.query(ExampleStore, ExampleStore.data_token) assert_that(queried.equals(data), is_(True))