param( lambda x: x.day_of_week.full_name(), 'Tuesday', id='day_of_week_full_name', ), ], ) def test_simple_datetime_operations(con, func, expected, translate): value = ibis.timestamp('2015-09-01 14:48:05.359') assert con.execute(func(value)) == expected @pytest.mark.parametrize( ('func', 'left', 'right', 'expected'), [ param(operator.add, L(3), L(4), 7, id='add'), param(operator.sub, L(3), L(4), -1, id='sub'), param(operator.mul, L(3), L(4), 12, id='mul'), param(operator.truediv, L(12), L(4), 3, id='truediv_no_remainder'), param(operator.pow, L(12), L(2), 144, id='pow'), param(operator.mod, L(12), L(5), 2, id='mod'), param(operator.truediv, L(7), L(2), 3.5, id='truediv_remainder'), param(operator.floordiv, L(7), L(2), 3, id='floordiv'), param(lambda x, y: x.floordiv(y), L(7), 2, 3, id='floordiv_no_literal'), param( lambda x, y: x.rfloordiv(y), L(2), 7, 3, id='rfloordiv_no_literal'), ], ) def test_binary_arithmetic(con, func, left, right, expected):
con.execute(func(value)) == expected @pytest.mark.parametrize(('value', 'expected'), [(0, None), (5.5, 5.5)]) def test_nullifzero(con, value, expected): result = con.execute(L(value).nullifzero()) if expected is None: assert pd.isnull(result) else: assert result == expected @pytest.mark.parametrize( ('expr', 'expected'), [ (L(None).isnull(), True), (L(1).isnull(), False), (L(None).notnull(), False), (L(1).notnull(), True), ], ) def test_isnull_notnull(con, expr, expected): assert con.execute(expr) == expected @pytest.mark.parametrize( ('expr', 'expected'), [ (ibis.coalesce(5, None, 4), 5), (ibis.coalesce(ibis.NA, 4, ibis.NA), 4), (ibis.coalesce(ibis.NA, ibis.NA, 3.14), 3.14),
def test_string_length(con, value, expected): assert con.execute(L(value).length()) == expected
def test_re_replace(con, translate): expr1 = L('Hello, World!').re_replace('.', '\\\\0\\\\0') expr2 = L('Hello, World!').re_replace('^', 'here: ') assert con.execute(expr1) == 'HHeelllloo,, WWoorrlldd!!' assert con.execute(expr2) == 'here: Hello, World!'
def test_str_replace(con): expr = L('foobarfoo').replace('foo', 'H') expected = 'HbarH' assert con.execute(expr) == expected
def test_string_reverse(con): assert con.execute(L('foo').reverse()) == 'oof'
def test_string_lower(con): assert con.execute(L('FOO').lower()) == 'foo'
def test_isnull_notnull(con, raw_value, opname, expected): lit = L(raw_value) op = operator.methodcaller(opname) expr = op(lit) assert con.execute(expr) == expected
def test_reduction_invalid_where(con, alltypes, reduction): condbad_literal = L('T') with pytest.raises(TypeError): fn = methodcaller(reduction, where=condbad_literal) fn(alltypes.double_col)
def test_repeat(con): expr = L('bar ').repeat(3) assert con.execute(expr) == 'bar bar bar '
def test_translate(con): expr = L('faab').translate('a', 'b') assert con.execute(expr) == 'fbbb'
def test_string_contains(con, haystack, needle, expected): value = L(haystack) expr = value.contains(needle) assert con.execute(expr) == expected
def test_string_strip(con, opname, expected): op = operator.methodcaller(opname) value = L(' foo ') assert con.execute(op(value)) == expected
def test_nullifzero(con, value, expected): assert con.execute(L(value).nullifzero()) == expected
def test_string_join(self): cases = [ (L(',').join(['a', 'b']), "concat_ws(',', 'a', 'b')") ] self._check_expr_cases(cases)
def test_nullifzero(self): cases = [ (L(0).nullifzero(), None), (L(5.5).nullifzero(), 5.5), ] self._check_e2e_cases(cases)
result = con.execute(expr) if isinstance(expected, pd.Series): expected = backend.default_series_rename(expected) backend.assert_series_equal(result, expected) else: try: assert result == expected except ValueError: backend.assert_series_equal(result, expected) @pytest.mark.parametrize( ('expr', 'expected'), [ param(L(-5).abs(), 5, id='abs-neg'), param(L(5).abs(), 5, id='abs'), param(ibis.least(L(10), L(1)), 1, id='least'), param(ibis.greatest(L(10), L(1)), 10, id='greatest'), param(L(5.5).round(), 6.0, id='round'), param(L(5.556).round(2), 5.56, id='round-digits'), param(L(5.556).ceil(), 6.0, id='ceil'), param(L(5.556).floor(), 5.0, id='floor'), param(L(5.556).exp(), math.exp(5.556), id='expr'), param(L(5.556).sign(), 1, id='sign-pos'), param(L(-5.556).sign(), -1, id='sign-neg'), param(L(0).sign(), 0, id='sign-zero'), param(L(5.556).sqrt(), math.sqrt(5.556), id='sqrt'), param(L(5.556).log(2), math.log(5.556, 2), id='log-base'), param(L(5.556).ln(), math.log(5.556), id='ln'), param(L(5.556).log2(), math.log(5.556, 2), id='log2'),
def test_string_length(self): cases = [ (L('foo_bar').length(), 7), (L('').length(), 0), ] self._check_e2e_cases(cases)
def test_string_upper(con): assert con.execute(L('foo').upper()) == 'FOO'
def test_string_upper_lower(self): cases = [ (L('foo').upper(), 'FOO'), (L('FOO').lower(), 'foo'), ] self._check_e2e_cases(cases)
def test_string_lenght(con): assert con.execute(L('FOO').length()) == 3
def test_string_find(self): cases = [ (L('foobar').find('bar'), 3), (L('foobar').find('baz'), -1), ] self._check_e2e_cases(cases)
) def test_timestamp_functions(con, expr, expected): assert con.execute(expr) == expected def test_now(con): expr = ibis.now().strftime('%Y%m%d %H') result = con.execute(expr) expected = datetime.datetime.utcnow().strftime('%Y%m%d %H') assert result == expected @pytest.mark.parametrize( ('expr', 'expected'), [ (L(3) + L(4), 7), (L(3) - L(4), -1), (L(3) * L(4), 12), (L(12) / L(4), 3), (L(12)**L(2), 144), (L(12) % L(5), 2), ], ) def test_binary_arithmetic(con, expr, expected): assert con.execute(expr) == expected @pytest.mark.parametrize( ('expr', 'expected'), [ (L(7) / L(2), 3.5),
def test_str_replace(self): cases = [ (L('foobarfoo').replace('foo', 'H'), 'HbarH'), ] self._check_e2e_cases(cases)
def _log10(translator, expr): op = expr.op() arg = op.args return _log_common(translator, arg, L(10))
def test_reduction_invalid_where(self): condbad_literal = L('T') c = self.table.double_col for reduction in [c.sum, c.count, c.mean, c.max, c.min]: with self.assertRaises(TypeError): reduction(where=condbad_literal)
def test_nullifzero(con, value, expected): result = con.execute(L(value).nullifzero()) if expected is None: assert pd.isnull(result) else: assert result == expected
def _check_literals(self, cases): for value, expected in cases: lit_expr = L(value) result = self._translate(lit_expr) assert result == expected
def test_string_substring(con, op, expected): value = L('foo_bar') assert con.execute(op(value)) == expected
def test_find_in_set(con, raw_value, expected): value = L(raw_value, dt.string) haystack = ['a', 'b', 'c', None] expr = value.find_in_set(haystack) assert con.execute(expr) == expected