Skip to content

Commit

Permalink
test(backends): fix backend tests that assume a list
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Sep 14, 2024
1 parent 7d89a39 commit ff3550c
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 55 deletions.
2 changes: 1 addition & 1 deletion ibis/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def to_pyarrow(
table = pa.Table.from_batches(reader, schema=arrow_schema)

return expr.__pyarrow_result__(
table.rename_columns(table_expr.columns).cast(arrow_schema)
table.rename_columns(list(table_expr.columns)).cast(arrow_schema)
)

@util.experimental
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/bigquery/tests/system/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def test_parted_column(con, kind):
table_name = f"{kind}_column_parted"
t = con.table(table_name)
expected_column = f"my_{kind}_parted_col"
assert t.columns == [expected_column, "string_col", "int_col"]
assert t.columns == (expected_column, "string_col", "int_col")


def test_cross_project_query(public):
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/impala/tests/test_ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,5 +334,5 @@ def test_varchar_char_support(temp_char_table):


def test_access_kudu_table(kudu_table):
assert kudu_table.columns == ["a"]
assert kudu_table.columns == ("a",)
assert kudu_table["a"].type() == dt.string
2 changes: 1 addition & 1 deletion ibis/backends/impala/tests/test_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ def test_where_with_timestamp(snapshot):

def test_filter_with_analytic(snapshot):
x = ibis.table(ibis.schema([("col", "int32")]), "x")
with_filter_col = x.select(x.columns + [ibis.null().name("filter")])
with_filter_col = x.select(*x.columns, ibis.null().name("filter"))
filtered = with_filter_col.filter(with_filter_col["filter"].isnull())
subquery = filtered.select(filtered.columns)

Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/snowflake/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def test_insert(con):
}
],
)
assert t.columns == ["ID", "NAME", "SCORE_1", "SCORE_2", "SCORE_3", "AGE"]
assert t.columns == ("ID", "NAME", "SCORE_1", "SCORE_2", "SCORE_3", "AGE")
assert t.count().execute() == 1


Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/tests/test_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,7 @@ def test_group_concat_over_window(backend, con):

def test_value_counts_on_expr(backend, alltypes, df):
expr = alltypes.bigint_col.add(1).value_counts()
columns = expr.columns
columns = list(expr.columns)
expr = expr.order_by(columns)
result = expr.execute().sort_values(columns).reset_index(drop=True)
expected = df.bigint_col.add(1).value_counts().reset_index()
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1687,7 +1687,7 @@ def test_cross_database_join(con_create_database, monkeypatch):
)

expr = left.join(right, "a")
assert expr.columns == ["a", "b", "c"]
assert expr.columns == ("a", "b", "c")

result = expr.to_pyarrow()
expected = pa.Table.from_pydict({"a": [1], "b": [2], "c": [3]})
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@
)
def test_load_examples(con, example, columns):
t = getattr(ibis.examples, example).fetch(backend=con)
assert t.columns == columns
assert t.columns == tuple(columns)
assert t.count().execute() > 0
8 changes: 4 additions & 4 deletions ibis/backends/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,17 +722,17 @@ def test_order_by_two_cols_nulls(con, op1, nf1, nf2, op2, expected):
def test_table_info(alltypes):
expr = alltypes.info()
df = expr.execute()
assert alltypes.columns == list(df.name)
assert expr.columns == [
assert alltypes.columns == tuple(df.name)
assert expr.columns == (
"name",
"type",
"nullable",
"nulls",
"non_nulls",
"null_frac",
"pos",
]
assert expr.columns == list(df.columns)
)
assert expr.columns == tuple(df.columns)


@pytest.mark.notyet(
Expand Down
27 changes: 9 additions & 18 deletions ibis/backends/tests/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ def test_mutating_join(backend, batting, awards_players, how):
result_order = ["playerID", "yearID", "lgID", "stint"]

expr = left.join(right, predicate, how=how)
cols = list(left.columns)
if how == "inner":
result = (
expr.execute()
.fillna(np.nan)[left.columns]
.fillna(np.nan)[cols]
.sort_values(result_order)
.reset_index(drop=True)
)
Expand All @@ -86,23 +87,16 @@ def test_mutating_join(backend, batting, awards_players, how):
.fillna(np.nan)
.assign(
playerID=lambda df: df.playerID.where(
df.playerID.notnull(),
df.playerID_right,
df.playerID.notnull(), df.playerID_right
)
)
.drop(["playerID_right"], axis=1)[left.columns]
.drop(["playerID_right"], axis=1)[cols]
.sort_values(result_order)
.reset_index(drop=True)
)

expected = (
check_eq(
left_df,
right_df,
how=how,
on=predicate,
suffixes=("_x", "_y"),
)[left.columns]
check_eq(left_df, right_df, how=how, on=predicate, suffixes=("_x", "_y"))[cols]
.sort_values(result_order)
.reset_index(drop=True)
)
Expand All @@ -123,20 +117,17 @@ def test_filtering_join(backend, batting, awards_players, how):
result_order = ["playerID", "yearID", "lgID", "stint"]

expr = left.join(right, predicate, how=how)
cols = list(left.columns)
result = (
expr.execute()
.fillna(np.nan)
.sort_values(result_order)[left.columns]
.sort_values(result_order)[cols]
.reset_index(drop=True)
)

expected = check_eq(
left_df,
right_df,
how=how,
on=predicate,
suffixes=("", "_y"),
).sort_values(result_order)[list(left.columns)]
left_df, right_df, how=how, on=predicate, suffixes=("", "_y")
).sort_values(result_order)[cols]

backend.assert_frame_equal(result, expected, check_like=True)

Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/tests/test_set_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

@pytest.fixture
def union_subsets(alltypes, df):
cols_a, cols_b, cols_c = (alltypes.columns.copy() for _ in range(3))
cols_a, cols_b, cols_c = (list(alltypes.columns) for _ in range(3))

random.seed(89)
random.shuffle(cols_a)
Expand Down
2 changes: 1 addition & 1 deletion ibis/expr/types/dataframe_interchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def num_columns(self):
return len(self._table.columns)

def column_names(self):
return self._table.columns
return list(self._table.columns)

def get_column(self, i: int) -> IbisColumn:
name = self._table.columns[i]
Expand Down
6 changes: 3 additions & 3 deletions ibis/expr/types/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def cast(self, schema: SchemaLike) -> Table:
Columns not present in the input schema will be passed through unchanged
>>> t.columns
['species', 'island', 'bill_length_mm', 'bill_depth_mm', 'flipper_length_mm', 'body_mass_g', 'sex', 'year']
('species', 'island', 'bill_length_mm', 'bill_depth_mm', 'flipper_length_mm', 'body_mass_g', 'sex', 'year')
>>> expr = t.cast({"body_mass_g": "float64", "bill_length_mm": "int"})
>>> expr.select(*cols).head()
┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┓
Expand Down Expand Up @@ -3263,7 +3263,7 @@ def cross_join(
│ … │ … │ … │ … │ … │ … │
└─────────┴───────────┴────────────────┴───────────────┴───────────────────┴───┘
>>> expr.columns
['species',
('species',
'island',
'bill_length_mm',
'bill_depth_mm',
Expand All @@ -3274,7 +3274,7 @@ def cross_join(
'bill_length_mm_right',
'bill_depth_mm_right',
'flipper_length_mm_right',
'body_mass_g_right']
'body_mass_g_right')
>>> expr.count()
┌─────┐
│ 344 │
Expand Down
30 changes: 15 additions & 15 deletions ibis/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
>>> t = ibis.table(dict(a="int", b="string", c="array<int>", abcd="float"))
>>> expr = t.select([t[c] for c in t.columns if t[c].type().is_numeric()])
>>> expr.columns
['a', 'abcd']
('a', 'abcd')
Compare that to the [`numeric`](#ibis.selectors.numeric) selector:
>>> import ibis.selectors as s
>>> expr = t.select(s.numeric())
>>> expr.columns
['a', 'abcd']
('a', 'abcd')
When there are multiple properties to check it gets worse:
Expand All @@ -39,13 +39,13 @@
... ]
... )
>>> expr.columns
['a', 'b', 'abcd']
('a', 'b', 'abcd')
Using a composition of selectors this is much less tiresome:
>>> expr = t.select((s.numeric() | s.of_type("string")) & s.contains(("a", "b", "cd")))
>>> expr.columns
['a', 'b', 'abcd']
('a', 'b', 'abcd')
"""

from __future__ import annotations
Expand Down Expand Up @@ -112,7 +112,7 @@ def where(predicate: Callable[[ir.Value], bool]) -> Selector:
>>> t = ibis.table(dict(a="float32"), name="t")
>>> expr = t.select(s.where(lambda col: col.get_name() == "a"))
>>> expr.columns
['a']
('a',)
"""
return Where(predicate)
Expand All @@ -128,10 +128,10 @@ def numeric() -> Selector:
>>> import ibis.selectors as s
>>> t = ibis.table(dict(a="int", b="string", c="array<string>"), name="t")
>>> t.columns
['a', 'b', 'c']
('a', 'b', 'c')
>>> expr = t.select(s.numeric()) # `a` has integer type, so it's numeric
>>> expr.columns
['a']
('a',)
See Also
--------
Expand Down Expand Up @@ -168,13 +168,13 @@ def of_type(dtype: dt.DataType | str | type[dt.DataType]) -> Selector:
>>> t = ibis.table(dict(name="string", siblings="array<string>", parents="array<int64>"))
>>> expr = t.select(s.of_type(dt.Array(dt.string)))
>>> expr.columns
['siblings']
('siblings',)
Strings are also accepted
>>> expr = t.select(s.of_type("array<string>"))
>>> expr.columns
['siblings']
('siblings',)
Abstract/unparametrized types may also be specified by their string name
(e.g. "integer" for any integer type), or by passing in a `DataType` class
Expand All @@ -185,7 +185,7 @@ def of_type(dtype: dt.DataType | str | type[dt.DataType]) -> Selector:
>>> expr1.equals(expr2)
True
>>> expr2.columns
['siblings', 'parents']
('siblings', 'parents')
See Also
--------
Expand Down Expand Up @@ -247,7 +247,7 @@ def startswith(prefixes: str | tuple[str, ...]) -> Selector:
>>> t = ibis.table(dict(apples="int", oranges="float", bananas="bool"), name="t")
>>> expr = t.select(s.startswith(("a", "b")))
>>> expr.columns
['apples', 'bananas']
('apples', 'bananas')
See Also
--------
Expand Down Expand Up @@ -319,14 +319,14 @@ def contains(
... )
>>> expr = t.select(s.contains(("a", "b")))
>>> expr.columns
['a', 'b', 'ab']
('a', 'b', 'ab')
Select columns that contain all of `"a"` and `"b"`, that is, both `"a"` and
`"b"` must be in each column's name to match.
>>> expr = t.select(s.contains(("a", "b"), how=all))
>>> expr.columns
['ab']
('ab',)
See Also
--------
Expand Down Expand Up @@ -359,7 +359,7 @@ def matches(regex: str | re.Pattern) -> Selector:
>>> t = ibis.table(dict(ab="string", abd="int", be="array<string>"))
>>> expr = t.select(s.matches(r"ab+"))
>>> expr.columns
['ab', 'abd']
('ab', 'abd')
See Also
--------
Expand Down Expand Up @@ -410,7 +410,7 @@ def cols(*names: str | ir.Column) -> Selector:
>>> t = ibis.table({"a": "int", "b": "int", "c": "int"})
>>> expr = t.select(s.cols("a", "b"))
>>> expr.columns
['a', 'b']
('a', 'b')
See Also
--------
Expand Down
4 changes: 2 additions & 2 deletions ibis/tests/expr/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ def test_topk_function_late_bind(airlines):
def test_topk_name(airlines):
expr1 = airlines.dest.topk(5, name="mycol")
expr2 = airlines.dest.topk(5, by=_.count().name("mycol"))
assert expr1.columns == ["dest", "mycol"]
assert expr1.columns == ("dest", "mycol")
assert_equal(expr1, expr2)

expr3 = airlines.dest.topk(5, by=_.arrdelay.mean(), name="mycol")
expr4 = airlines.dest.topk(5, by=_.arrdelay.mean().name("mycol"))
assert expr3.columns == ["dest", "mycol"]
assert expr3.columns == ("dest", "mycol")
assert_equal(expr3, expr4)
6 changes: 3 additions & 3 deletions ibis/tests/expr/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,18 +874,18 @@ def test_group_by_column_select_api(table):
def test_value_counts(table):
expr1 = table.g.value_counts()
expr2 = table[["g"]].group_by("g").aggregate(g_count=_.count())
assert expr1.columns == ["g", "g_count"]
assert expr1.columns == ("g", "g_count")
assert_equal(expr1, expr2)

expr3 = table.g.value_counts(name="freq")
expr4 = table[["g"]].group_by("g").aggregate(freq=_.count())
assert expr3.columns == ["g", "freq"]
assert expr3.columns == ("g", "freq")
assert_equal(expr3, expr4)


def test_value_counts_on_window_function(table):
expr = (table.a - table.a.mean()).name("x").value_counts(name="count")
assert expr.columns == ["x", "count"]
assert expr.columns == ("x", "count")


def test_value_counts_unnamed_expr(con):
Expand Down

0 comments on commit ff3550c

Please sign in to comment.