def test_debug_true(): with c.OptionsCtx() as options: options.debug = True assert c.this().gen_converter(debug=True)(1) == 1 with pytest.raises(TypeError): assert c.item(0).gen_converter(debug=True)(1) == 1
def test_table_inner_join(): result = list( Table.from_rows([(1, 2), (2, 3)], ["a", "b"]).join( Table.from_rows([(1, 3), (2, 4)], ["a", "c"]), how="inner", on=["a"], ).into_iter_rows(dict)) assert result == [ { "a": 1, "b": 2, "c": 3 }, { "a": 2, "b": 3, "c": 4 }, ] result = list( Table.from_rows([(1, 2), (2, 3)], ["a", "b"]).embed_conversions().update().drop().join( Table.from_rows([(1, 3), (2, 4)], ["a", "c"]), how="inner", on=["a"], ).into_iter_rows(include_header=True)) assert result == [("a", "b", "c"), (1, 2, 3), (2, 3, 4)] with c.OptionsCtx() as o: o.debug = True result = list( Table.from_rows([(1, 2), (2, 3)], ["a", "b"]).join( Table.from_rows([(0, -1), (3, 4)], ["a", "c"]), how="inner", on=c.LEFT.col("a") < c.RIGHT.col("a"), ).into_iter_rows(include_header=True)) assert result == [ ("a_LEFT", "b", "a_RIGHT", "c"), (1, 2, 3, 4), (2, 3, 3, 4), ]
def test_table_left_join(): with c.OptionsCtx() as options: options.debug = False result = list( Table.from_rows([(1, 2), (2, 3)], ["a", "b"]).join( Table.from_rows([(1, 3), (2, 4)], ["a", "c"]), how="left", on=["a"], ).join( Table.from_rows( [ (2, 4, 7), ], ["a", "c", "d"], ), how="left", on=["a", "c"], ).into_iter_rows(include_header=True)) assert result == [ ("a", "b", "c", "d"), (1, 2, 3, None), (2, 3, 4, 7), ]
def test_table_chain(): with c.OptionsCtx() as o: o.debug = True result = list( Table.from_rows([["a", "b"], [1, 2]], header=True).chain( Table.from_rows([["b", "a", "c"], [4, 3, 5]], header=True)).into_iter_rows( tuple, include_header=True)) assert result == [ ("a", "b", "c"), (1, 2, None), (3, 4, 5), ] result = list( Table.from_rows([["a", "b"], [1, 2], [5, 6]], header=True).chain( Table.from_rows([["c", "a"], [4, 3]], header=True)).into_iter_rows(tuple, include_header=True)) assert result == [ ("a", "b", "c"), (1, 2, None), (5, 6, None), (3, None, 4), ] result = list( Table.from_rows( [["a"], ["1"]], header=True).update(a=c.col("a").as_type(int) + 10).chain( Table.from_rows([["b"], ["2"]], header=True).update_all(int), fill_value=False, ).into_iter_rows(tuple, include_header=True)) assert result == [ ("a", "b"), (11, False), (False, 2), ] result = list( Table.from_rows([["a"], ["1"]], header=True).chain( Table.from_rows([["a"], ["2"]], header=True)).into_iter_rows(include_header=True)) assert result == [ ("a", ), ("1", ), ("2", ), ] result = list( Table.from_rows([["a"], ["1"]], header=True).update(a=c.col("a").as_type(int)).chain( Table.from_rows([["a"], ["2"]], header=True)).into_iter_rows()) assert result == [(1, ), ("2", )] result = list( Table.from_rows([["a"], ["1"]], header=True).chain( Table.from_rows([["a"], ["2"]], header=True).update( a=c.col("a").as_type(int))).into_iter_rows()) assert result == [("1", ), (2, )] result = list( Table.from_rows([["a"], ["1"]], header=True).update( a=c.col("a").as_type(int)).embed_conversions().chain( Table.from_rows([["a"], ["2"]], header=True)).into_iter_rows()) assert result == [(1, ), ("2", )]
def test_pipes(): assert c.list_comp(c.inline_expr("{0} ** 2").pass_args(c.this())).pipe( c.call_func(sum, c.this())).pipe( c.call_func( lambda x, a: x + a, c.this(), c.naive({ "abc": 10 }).item(c.input_arg("key_name")), )).pipe([c.this(), c.this()]).execute([1, 2, 3], key_name="abc", debug=False) == [ 24, 24, ] assert c.item(0).pipe(datetime.strptime, "%Y-%m-%d").pipe( c.call_func(lambda dt: dt.date(), c.this())).execute(["2019-01-01"], debug=False) == date(2019, 1, 1) assert c.item(0).pipe(datetime.strptime, "%Y-%m-%d").pipe( c.this().call_method("date")).execute(["2019-01-01"], debug=False) == date(2019, 1, 1) with c.OptionsCtx() as options: max_pipe_length = options.max_pipe_length = 10 with pytest.raises(c.ConversionException): conv = c.this() for i in range(max_pipe_length + 1): conv = c.this().pipe(conv) with c.OptionsCtx() as options2, pytest.raises(c.ConversionException): options2.max_pipe_length = 5 conv.clone() conv = c.dict_comp( c.item("name"), c.item("transactions").pipe( c.list_comp({ "id": c.item(0).as_type(str), "amount": c.item(1).pipe(c.if_(c.this(), c.this().as_type(Decimal), None)), })), ).gen_converter(debug=True) assert conv([{ "name": "test", "transactions": [(0, 0), (1, 10)] }]) == { "test": [ { "id": "0", "amount": None }, { "id": "1", "amount": Decimal("10") }, ] } with c.OptionsCtx() as options: max_pipe_length = options.max_pipe_length = 10 conv1 = c.item(0).pipe(c.item(1).pipe(c.item(2))) def measure_pipe_length(conv): length = 0 for i in range(max_pipe_length): if conv._predefined_input is not None: length += 1 conv = conv._predefined_input else: break return length pipe_length_before = measure_pipe_length(conv1) for i in range(max_pipe_length + 20): c.generator_comp(c.this().pipe(conv1)) pipe_length_after = measure_pipe_length(conv1) assert pipe_length_after == pipe_length_before