def test_multiple_tables(df, tmp_hyper, table_name, table_mode): # Write twice; depending on mode this should either overwrite or duplicate entries pantab.frames_to_hyper({ table_name: df, "table2": df }, tmp_hyper, table_mode=table_mode) pantab.frames_to_hyper({ table_name: df, "table2": df }, tmp_hyper, table_mode=table_mode) result = pantab.frames_from_hyper(tmp_hyper) expected = df.copy() if table_mode == "a": expected = pd.concat([expected, expected]).reset_index(drop=True) expected["float32"] = expected["float32"].astype(np.float64) # some test trickery here if not isinstance(table_name, tab_api.TableName) or table_name.schema_name is None: table_name = tab_api.TableName("public", table_name) assert set(result.keys()) == set( (table_name, tab_api.TableName("public", "table2"))) for val in result.values(): tm.assert_frame_equal(val, expected)
def test_bad_table_mode_raises(df, tmp_hyper): msg = "'table_mode' must be either 'w' or 'a'" with pytest.raises(ValueError, match=msg): pantab.frame_to_hyper(df, tmp_hyper, table="test", table_mode="x") with pytest.raises(ValueError, match=msg): pantab.frames_to_hyper({"a": df}, tmp_hyper, table_mode="x")
def test_duplicate_columns_raises(tmp_hyper): df = pd.DataFrame([[1, 1]], columns=[1, 1]) with pytest.raises( tab_api.hyperexception.HyperException, match="column '1' specified more than once", ): pantab.frame_to_hyper(df, tmp_hyper, table="foo") with pytest.raises( tab_api.hyperexception.HyperException, match="column '1' specified more than once", ): pantab.frames_to_hyper({"test": df}, tmp_hyper)
def test_roundtrip_with_external_hyper_connection(df, tmp_hyper): with HyperProcess(Telemetry.DO_NOT_SEND_USAGE_DATA_TO_TABLEAU) as hyper: pantab.frames_to_hyper({ "test": df, "test2": df }, tmp_hyper, hyper_process=hyper) with Connection(hyper.endpoint, tmp_hyper, CreateMode.NONE) as connection: result = pantab.frame_from_hyper(connection, table="test") assert_roundtrip_equal(result, df) result = pantab.frame_from_hyper_query(connection, "SELECT * FROM test") assert result.size == 63 result = pantab.frames_from_hyper(connection) assert set(result.keys()) == set( (TableName("public", "test"), TableName("public", "test2"))) for val in result.values(): assert_roundtrip_equal(val, df)
def test_failed_write_doesnt_overwrite_file(df, tmp_hyper, monkeypatch, table_mode): pantab.frame_to_hyper(df, tmp_hyper, table="test", table_mode=table_mode) last_modified = tmp_hyper.stat().st_mtime # Let's patch the Inserter to fail on creation def failure(*args, **kwargs): raise ValueError("dummy failure") monkeypatch.setattr(pantab._writer.tab_api, "Inserter", failure, raising=True) # Try out our write methods with pytest.raises(ValueError, match="dummy failure"): pantab.frame_to_hyper(df, tmp_hyper, table="test", table_mode=table_mode) pantab.frames_to_hyper({"test": df}, tmp_hyper, table_mode=table_mode) # Neither should not update file stats assert last_modified == tmp_hyper.stat().st_mtime
def test_roundtrip_with_external_hyper_process(df, tmp_hyper): default_log_path = Path.cwd() / "hyperd.log" if default_log_path.exists(): default_log_path.unlink() # By passing in a pre-spawned HyperProcess, one can e.g. avoid creating a log file parameters = {"log_config": ""} with HyperProcess(Telemetry.DO_NOT_SEND_USAGE_DATA_TO_TABLEAU, parameters=parameters) as hyper: # test frame_to_hyper/frame_from_hyper pantab.frame_to_hyper(df, tmp_hyper, table="test", hyper_process=hyper) result = pantab.frame_from_hyper(tmp_hyper, table="test", hyper_process=hyper) assert_roundtrip_equal(result, df) # test frame_from_hyper_query result = pantab.frame_from_hyper_query(tmp_hyper, "SELECT * FROM test", hyper_process=hyper) assert result.size == 63 # test frames_to_hyper/frames_from_hyper pantab.frames_to_hyper({ "test2": df, "test": df }, tmp_hyper, hyper_process=hyper) result = pantab.frames_from_hyper(tmp_hyper, hyper_process=hyper) assert set(result.keys()) == set( (TableName("public", "test"), TableName("public", "test2"))) for val in result.values(): assert_roundtrip_equal(val, df) assert not default_log_path.exists()