Skip to content

Commit

Permalink
refactor(tests): remove test rounding mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
gforsyth authored and cpcloud committed Nov 29, 2023
1 parent ab0a8f6 commit 3b730d9
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 61 deletions.
4 changes: 2 additions & 2 deletions ibis/backends/bigquery/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ibis.backends.bigquery import EXTERNAL_DATA_SCOPES, Backend
from ibis.backends.bigquery.datatypes import BigQuerySchema
from ibis.backends.conftest import TEST_TABLES
from ibis.backends.tests.base import BackendTest, RoundAwayFromZero
from ibis.backends.tests.base import BackendTest
from ibis.backends.tests.data import json_types, non_null_array_types, struct_types, win

DATASET_ID = "ibis_gbq_testing"
Expand All @@ -25,7 +25,7 @@
PROJECT_ID_ENV_VAR = "GOOGLE_BIGQUERY_PROJECT_ID"


class TestConf(BackendTest, RoundAwayFromZero):
class TestConf(BackendTest):
"""Backend-specific class with information for testing."""

supports_divide_by_zero = True
Expand Down
5 changes: 3 additions & 2 deletions ibis/backends/clickhouse/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import ibis
import ibis.expr.types as ir
from ibis import util
from ibis.backends.tests.base import RoundHalfToEven, ServiceBackendTest
from ibis.backends.tests.base import ServiceBackendTest

if TYPE_CHECKING:
from collections.abc import Iterable
Expand All @@ -22,14 +22,15 @@
IBIS_TEST_CLICKHOUSE_DB = os.environ.get("IBIS_TEST_DATA_DB", "ibis_testing")


class TestConf(ServiceBackendTest, RoundHalfToEven):
class TestConf(ServiceBackendTest):
check_dtype = False
supports_window_operations = False
returned_timestamp_unit = "s"
supported_to_timestamp_units = {"s"}
supports_floating_modulus = False
supports_json = False
force_sort_before_comparison = True
rounding_method = "half_to_even"
data_volume = "/var/lib/clickhouse/user_files/ibis"
service_name = "clickhouse"
deps = ("clickhouse_connect",)
Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/datafusion/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

import ibis
from ibis.backends.conftest import TEST_TABLES
from ibis.backends.tests.base import BackendTest, RoundAwayFromZero
from ibis.backends.tests.base import BackendTest
from ibis.backends.tests.data import array_types, win


class TestConf(BackendTest, RoundAwayFromZero):
class TestConf(BackendTest):
# check_names = False
# supports_divide_by_zero = True
# returned_timestamp_unit = 'ns'
Expand Down
5 changes: 3 additions & 2 deletions ibis/backends/druid/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from requests import Session

import ibis
from ibis.backends.tests.base import RoundHalfToEven, ServiceBackendTest
from ibis.backends.tests.base import ServiceBackendTest

if TYPE_CHECKING:
from collections.abc import Iterable
Expand Down Expand Up @@ -89,7 +89,7 @@ def run_query(session: Session, query: str) -> None:
time.sleep(REQUEST_INTERVAL)


class TestConf(ServiceBackendTest, RoundHalfToEven):
class TestConf(ServiceBackendTest):
# druid has the same rounding behavior as postgres
check_dtype = False
supports_window_operations = False
Expand All @@ -99,6 +99,7 @@ class TestConf(ServiceBackendTest, RoundHalfToEven):
native_bool = True
supports_structs = False
supports_json = False # it does, but we haven't implemented it
rounding_method = "half_to_even"
service_name = "druid-middlemanager"
deps = ("pydruid.db.sqlalchemy",)

Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/duckdb/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import ibis
from ibis.backends.conftest import TEST_TABLES
from ibis.backends.tests.base import BackendTest, RoundAwayFromZero
from ibis.backends.tests.base import BackendTest
from ibis.conftest import SANDBOXED

if TYPE_CHECKING:
Expand All @@ -15,7 +15,7 @@
from ibis.backends.base import BaseBackend


class TestConf(BackendTest, RoundAwayFromZero):
class TestConf(BackendTest):
supports_map = True
deps = "duckdb", "duckdb_engine"
stateful = False
Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/flink/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

import ibis
from ibis.backends.conftest import TEST_TABLES
from ibis.backends.tests.base import BackendTest, RoundAwayFromZero
from ibis.backends.tests.base import BackendTest


class TestConf(BackendTest, RoundAwayFromZero):
class TestConf(BackendTest):
supports_structs = False
force_sort_before_comparison = True
deps = "pandas", "pyflink"
Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/impala/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
from ibis import options, util
from ibis.backends.conftest import TEST_TABLES
from ibis.backends.impala.compiler import ImpalaCompiler, ImpalaExprTranslator
from ibis.backends.tests.base import BackendTest, RoundAwayFromZero
from ibis.backends.tests.base import BackendTest
from ibis.backends.tests.data import win
from ibis.tests.expr.mocks import MockBackend


class TestConf(BackendTest, RoundAwayFromZero):
class TestConf(BackendTest):
supports_arrays = True
supports_arrays_outside_of_select = False
check_dtype = False
Expand Down
5 changes: 3 additions & 2 deletions ibis/backends/mssql/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import ibis
from ibis.backends.conftest import init_database
from ibis.backends.tests.base import RoundHalfToEven, ServiceBackendTest
from ibis.backends.tests.base import ServiceBackendTest

if TYPE_CHECKING:
from collections.abc import Iterable
Expand All @@ -21,7 +21,7 @@
IBIS_TEST_MSSQL_DB = os.environ.get("IBIS_TEST_MSSQL_DATABASE", "ibis_testing")


class TestConf(ServiceBackendTest, RoundHalfToEven):
class TestConf(ServiceBackendTest):
# MSSQL has the same rounding behavior as postgres
check_dtype = False
supports_window_operations = False
Expand All @@ -30,6 +30,7 @@ class TestConf(ServiceBackendTest, RoundHalfToEven):
supports_arrays_outside_of_select = supports_arrays
supports_structs = False
supports_json = False
rounding_method = "half_to_even"
service_name = "mssql"
deps = "pymssql", "sqlalchemy"

Expand Down
5 changes: 3 additions & 2 deletions ibis/backends/mysql/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import ibis
from ibis.backends.conftest import TEST_TABLES, init_database
from ibis.backends.tests.base import RoundHalfToEven, ServiceBackendTest
from ibis.backends.tests.base import ServiceBackendTest

if TYPE_CHECKING:
from collections.abc import Iterable
Expand All @@ -22,14 +22,15 @@
IBIS_TEST_MYSQL_DB = os.environ.get("IBIS_TEST_MYSQL_DATABASE", "ibis_testing")


class TestConf(ServiceBackendTest, RoundHalfToEven):
class TestConf(ServiceBackendTest):
# mysql has the same rounding behavior as postgres
check_dtype = False
returned_timestamp_unit = "s"
supports_arrays = False
supports_arrays_outside_of_select = supports_arrays
native_bool = False
supports_structs = False
rounding_method = "half_to_even"
service_name = "mysql"
deps = "pymysql", "sqlalchemy"

Expand Down
5 changes: 3 additions & 2 deletions ibis/backends/oracle/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import sqlalchemy as sa

import ibis
from ibis.backends.tests.base import RoundHalfToEven, ServiceBackendTest
from ibis.backends.tests.base import ServiceBackendTest

if TYPE_CHECKING:
from collections.abc import Iterable
Expand All @@ -29,7 +29,7 @@
# where ORACLE_DB is the same name you used in the docker-compose file.


class TestConf(ServiceBackendTest, RoundHalfToEven):
class TestConf(ServiceBackendTest):
check_dtype = False
supports_window_operations = False
returned_timestamp_unit = "s"
Expand All @@ -40,6 +40,7 @@ class TestConf(ServiceBackendTest, RoundHalfToEven):
native_bool = False
supports_structs = False
supports_json = False
rounding_method = "half_to_even"
data_volume = "/opt/oracle/data"
service_name = "oracle"
deps = "oracledb", "sqlalchemy"
Expand Down
5 changes: 3 additions & 2 deletions ibis/backends/pandas/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

import ibis
from ibis.backends.conftest import TEST_TABLES
from ibis.backends.tests.base import BackendTest, RoundHalfToEven
from ibis.backends.tests.base import BackendTest
from ibis.backends.tests.data import array_types, json_types, struct_types, win


class TestConf(BackendTest, RoundHalfToEven):
class TestConf(BackendTest):
check_names = False
supported_to_timestamp_units = BackendTest.supported_to_timestamp_units | {"ns"}
supports_divide_by_zero = True
returned_timestamp_unit = "ns"
stateful = False
rounding_method = "half_to_even"
deps = ("pandas",)

def _load_data(self, **_: Any) -> None:
Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/polars/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import ibis
from ibis.backends.conftest import TEST_TABLES
from ibis.backends.tests.base import BackendTest, RoundAwayFromZero
from ibis.backends.tests.base import BackendTest
from ibis.backends.tests.data import array_types, struct_types, win


class TestConf(BackendTest, RoundAwayFromZero):
class TestConf(BackendTest):
supports_structs = True
supports_json = False
reduction_tolerance = 1e-3
Expand Down
5 changes: 3 additions & 2 deletions ibis/backends/postgres/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import ibis
from ibis.backends.conftest import init_database
from ibis.backends.tests.base import RoundHalfToEven, ServiceBackendTest
from ibis.backends.tests.base import ServiceBackendTest

if TYPE_CHECKING:
from collections.abc import Iterable
Expand All @@ -42,12 +42,13 @@
)


class TestConf(ServiceBackendTest, RoundHalfToEven):
class TestConf(ServiceBackendTest):
# postgres rounds half to even for double precision and half away from zero
# for numeric and decimal

returned_timestamp_unit = "s"
supports_structs = False
rounding_method = "half_to_even"
service_name = "postgres"
deps = "psycopg2", "sqlalchemy"

Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/pyspark/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
import ibis
from ibis import util
from ibis.backends.conftest import TEST_TABLES
from ibis.backends.tests.base import BackendTest, RoundAwayFromZero
from ibis.backends.tests.base import BackendTest
from ibis.backends.tests.data import json_types, win


def set_pyspark_database(con, database):
con._session.catalog.setCurrentDatabase(database)


class TestConf(BackendTest, RoundAwayFromZero):
class TestConf(BackendTest):
supported_to_timestamp_units = {"s"}
deps = ("pyspark",)

Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/snowflake/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import ibis
from ibis.backends.conftest import TEST_TABLES
from ibis.backends.snowflake.datatypes import SnowflakeType
from ibis.backends.tests.base import BackendTest, RoundAwayFromZero
from ibis.backends.tests.base import BackendTest
from ibis.formats.pyarrow import PyArrowSchema

if TYPE_CHECKING:
Expand Down Expand Up @@ -63,7 +63,7 @@ def copy_into(con, data_dir: Path, table: str) -> None:
)


class TestConf(BackendTest, RoundAwayFromZero):
class TestConf(BackendTest):
supports_map = True
default_identifier_case_fn = staticmethod(str.upper)
deps = ("snowflake.connector", "snowflake.sqlalchemy")
Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/sqlite/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import ibis
import ibis.expr.types as ir
from ibis.backends.conftest import TEST_TABLES
from ibis.backends.tests.base import BackendTest, RoundAwayFromZero
from ibis.backends.tests.base import BackendTest


class TestConf(BackendTest, RoundAwayFromZero):
class TestConf(BackendTest):
supports_arrays = False
supports_arrays_outside_of_select = supports_arrays
supports_window_operations = True
Expand Down
45 changes: 18 additions & 27 deletions ibis/backends/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,6 @@
import ibis.expr.types as ir


# TODO: Merge into BackendTest, #2564
class RoundingConvention:
@staticmethod
@abc.abstractmethod
def round(series: pd.Series, decimals: int = 0) -> pd.Series:
"""Round a series to `decimals` number of decimal values."""


# TODO: Merge into BackendTest, #2564
class RoundAwayFromZero(RoundingConvention):
@staticmethod
def round(series: pd.Series, decimals: int = 0) -> pd.Series:
if not decimals:
return (-(np.sign(series)) * np.ceil(-(series.abs()) - 0.5)).astype(
np.int64
)
return series.round(decimals=decimals)


# TODO: Merge into BackendTest, #2564
class RoundHalfToEven(RoundingConvention):
@staticmethod
def round(series: pd.Series, decimals: int = 0) -> pd.Series:
result = series.round(decimals=decimals)
return result if decimals else result.astype(np.int64)


class BackendTest(abc.ABC):
check_dtype = True
check_names = True
Expand All @@ -67,6 +40,7 @@ class BackendTest(abc.ABC):
service_name = None
supports_tpch = False
force_sort_before_comparison = False
rounding_method = "away_from_zero"

@property
@abc.abstractmethod
Expand Down Expand Up @@ -186,6 +160,23 @@ def assert_frame_equal(
kwargs.setdefault("check_dtype", cls.check_dtype)
tm.assert_frame_equal(left, right, *args, **kwargs)

@classmethod
def round(cls, series: pd.Series, decimals: int = 0) -> pd.Series:
return getattr(cls, cls.rounding_method)(series, decimals)

@staticmethod
def away_from_zero(series: pd.Series, decimals: int = 0) -> pd.Series:
if not decimals:
return (-(np.sign(series)) * np.ceil(-(series.abs()) - 0.5)).astype(
np.int64
)
return series.round(decimals=decimals)

@staticmethod
def half_to_even(series: pd.Series, decimals: int = 0) -> pd.Series:
result = series.round(decimals=decimals)
return result if decimals else result.astype(np.int64)

@staticmethod
def default_series_rename(series: pd.Series, name: str = "tmp") -> pd.Series:
return series.rename(name)
Expand Down
4 changes: 2 additions & 2 deletions ibis/backends/trino/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import ibis.expr.datatypes as dt
import ibis.selectors as s
from ibis.backends.conftest import TEST_TABLES
from ibis.backends.tests.base import BackendTest, RoundAwayFromZero
from ibis.backends.tests.base import BackendTest

if TYPE_CHECKING:
from collections.abc import Iterable
Expand All @@ -32,7 +32,7 @@
)


class TestConf(BackendTest, RoundAwayFromZero):
class TestConf(BackendTest):
# trino rounds half to even for double precision and half away from zero
# for numeric and decimal

Expand Down

0 comments on commit 3b730d9

Please sign in to comment.