|  | 
| 1 | 1 | from dataclasses import dataclass | 
| 2 | 2 | 
 | 
|  | 3 | +import pytest | 
|  | 4 | + | 
| 3 | 5 | import greenplumpython as gp | 
| 4 | 6 | from tests import db | 
| 5 | 7 | 
 | 
| 6 | 8 | 
 | 
| 7 |  | -def test_simple_func(db: gp.Database): | 
| 8 |  | -    @dataclass | 
| 9 |  | -    class Int: | 
| 10 |  | -        i: int | 
|  | 9 | +@dataclass | 
|  | 10 | +class Int: | 
|  | 11 | +    i: int | 
|  | 12 | + | 
|  | 13 | + | 
|  | 14 | +@dataclass | 
|  | 15 | +class Pair: | 
|  | 16 | +    i: int | 
|  | 17 | +    j: int | 
|  | 18 | + | 
|  | 19 | + | 
|  | 20 | +@pytest.fixture | 
|  | 21 | +def t(db: gp.Database): | 
|  | 22 | +    rows = [(i, i) for i in range(10)] | 
|  | 23 | +    return db.create_dataframe(rows=rows, column_names=["a", "b"]) | 
|  | 24 | + | 
|  | 25 | + | 
|  | 26 | +@gp.create_function(language_handler="plcontainer", runtime="plc_python_example") | 
|  | 27 | +def add_one(x: list[Int]) -> list[Int]: | 
|  | 28 | +    return [{"i": arg["i"] + 1} for arg in x] | 
| 11 | 29 | 
 | 
| 12 |  | -    @gp.create_function(language_handler="plcontainer", runtime="plc_python_example") | 
| 13 |  | -    def add_one(x: list[Int]) -> list[Int]: | 
| 14 |  | -        return [{"i": arg["i"] + 1} for arg in x] | 
| 15 | 30 | 
 | 
|  | 31 | +def test_simple_func(db: gp.Database): | 
| 16 | 32 |     assert ( | 
| 17 | 33 |         len( | 
| 18 | 34 |             list( | 
| 19 | 35 |                 db.create_dataframe(columns={"i": range(10)}).apply( | 
| 20 |  | -                    lambda _: add_one(), expand=True | 
|  | 36 | +                    lambda t: add_one(t), expand=True | 
| 21 | 37 |                 ) | 
| 22 | 38 |             ) | 
| 23 | 39 |         ) | 
| 24 | 40 |         == 10 | 
| 25 | 41 |     ) | 
|  | 42 | + | 
|  | 43 | + | 
|  | 44 | +def test_func_no_input(db: gp.Database): | 
|  | 45 | + | 
|  | 46 | +    with pytest.raises(Exception) as exc_info:  # no input data for func raises Exception | 
|  | 47 | +        db.create_dataframe(columns={"i": range(10)}).apply(lambda _: add_one(), expand=True) | 
|  | 48 | +    assert "No input data specified, please specify a DataFrame or Columns" in str(exc_info.value) | 
|  | 49 | + | 
|  | 50 | + | 
|  | 51 | +def test_func_column(db: gp.Database, t: gp.DataFrame): | 
|  | 52 | +    @gp.create_function(language_handler="plcontainer", runtime="plc_python_example") | 
|  | 53 | +    def add(x: list[Pair]) -> list[Int]: | 
|  | 54 | +        return [{"i": arg["i"] + arg["j"]} for arg in x] | 
|  | 55 | + | 
|  | 56 | +    assert len(list(t.apply(lambda t: add(t["a"], t["b"]), expand=True))) == 10 | 
0 commit comments