From 7d2a343364f9737ff09fbeba24c8a5d41a42116b Mon Sep 17 00:00:00 2001 From: Nick Crews Date: Sat, 13 Jul 2024 13:09:32 -0800 Subject: [PATCH] test(redo): add some basic cast tests (#9483) redo of #9461 --- ibis/backends/tests/test_generic.py | 50 +++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/ibis/backends/tests/test_generic.py b/ibis/backends/tests/test_generic.py index c4670fd69a97..25fcb95b0533 100644 --- a/ibis/backends/tests/test_generic.py +++ b/ibis/backends/tests/test_generic.py @@ -28,6 +28,7 @@ OracleDatabaseError, PolarsSchemaError, PsycoPg2InternalError, + PsycoPg2SyntaxError, Py4JJavaError, PyDruidProgrammingError, PyODBCDataError, @@ -1763,6 +1764,55 @@ def hash_256(col): backend.assert_series_equal(h1, h2) +@pytest.mark.parametrize( + ("from_type", "to_type", "from_val", "expected"), + [ + param("int", "float", 0, 0.0, id="int_to_float"), + param("float", "int", 0.0, 0, id="float_to_int"), + param("string", "int", "0", 0, id="string_to_int"), + param("string", "float", "0", 0.0, id="string_to_float"), + param( + "array", + "array", + [0, 1, 2], + ["0", "1", "2"], + marks=[ + pytest.mark.notimpl(["pandas"], reason="casts to ['0']"), + pytest.mark.notimpl(["druid"], raises=PyDruidProgrammingError), + pytest.mark.notimpl(["oracle"], raises=OracleDatabaseError), + pytest.mark.never( + ["exasol", "impala", "mssql", "mysql", "sqlite"], + reason="backend doesn't support arrays", + ), + ], + id="array", + ), + param( + "struct", + "struct", + {"a": 0, "b": "1"}, + {"a": "0", "b": 1}, + marks=[ + pytest.mark.notimpl(["flink"], raises=Py4JJavaError), + pytest.mark.notimpl(["druid"], raises=PyDruidProgrammingError), + pytest.mark.notimpl(["oracle"], raises=OracleDatabaseError), + pytest.mark.notimpl(["postgres"], raises=PsycoPg2SyntaxError), + pytest.mark.notimpl(["risingwave"], raises=PsycoPg2InternalError), + pytest.mark.never( + ["datafusion", "exasol", "impala", "mssql", "mysql", "sqlite"], + reason="backend doesn't support structs", + ), + ], + id="struct", + ), + ], +) +def test_cast(con, from_type, to_type, from_val, expected): + expr = ibis.literal(from_val, type=from_type).cast(to_type) + result = con.execute(expr) + assert result == expected + + @pytest.mark.notimpl(["pandas", "dask", "oracle", "sqlite"]) @pytest.mark.parametrize( ("from_val", "to_type", "expected"),