From 7df572f2717d71f4f879b8158f4dd096c7860476 Mon Sep 17 00:00:00 2001 From: Nick Crews Date: Sat, 29 Jun 2024 13:11:34 -0800 Subject: [PATCH] test: add some basic cast tests --- ibis/backends/tests/test_generic.py | 69 +++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/ibis/backends/tests/test_generic.py b/ibis/backends/tests/test_generic.py index 3ed4a9db8cc5..8ce1c65fddcc 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, @@ -1758,6 +1759,74 @@ 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), + param("float", "int", 0.0, 0), + param("string", "int", "0", 0), + param("string", "float", "0", 0.0), + param( + "timestamp", + "string", + datetime.datetime(2023, 1, 1), + "2023-01-01 00:00:00", + marks=[ + pytest.mark.broken( + "datafusion", reason="1672531200000000 (micro-seconds)" + ), + pytest.mark.broken(["exasol"], reason="2023-01-01 00:00:00.000000"), + pytest.mark.broken(["flink"], reason="2023-01-01 00:00:00.000000"), + pytest.mark.broken(["impala"], reason="casts to '2023-01-0100:00:00'"), + pytest.mark.broken(["mssql"], reason="2023-01-01 00:00:00.000000"), + pytest.mark.broken("oracle", reason="01-JAN-23 12.00.00.000000000 AM"), + pytest.mark.broken( + "polars", reason="1672531200000000000 (nanoseconds)" + ), + pytest.mark.broken(["trino"], reason="2023-01-01 00:00:00.000"), + ], + ), + param( + "array", + "array", + [0, 1, 2], + ["0", "1", "2"], + marks=[ + pytest.mark.broken(["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", + ), + ], + ), + param( + "struct", + "struct", + {"a": 0, "b": "1"}, + {"a": "0", "b": 1}, + marks=[ + pytest.mark.never( + ["datafusion", "exasol", "impala", "mssql", "mysql", "sqlite"], + reason="backend doesn't support arrays", + ), + 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), + ], + ), + ], + ids=str, +) +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"),