Beispiel #1
0
def test_paging_object_text():
    ob = [
        OC(Column("id", Integer, nullable=False)),
        OC(Column("name", String, nullable=False)),
    ]

    p = Paging(T3,
               2,
               ob,
               backwards=False,
               current_marker=None,
               get_marker=getitem)

    assert p.rows

    assert p.next == ((2, "test1"), False)
    assert p.has_next
    general_asserts(p)

    assert not p.has_previous
    assert p.previous == ((1, "test"), True)
    assert p.further == ((2, "test1"), False)

    general_asserts(p)

    assert p.further
Beispiel #2
0
def test_mappedocols():
    a = AppendedColumn(OC(asc("a")))
    b = DirectColumn(OC(desc("b")), 0)
    assert a.oc.is_ascending
    assert not b.oc.is_ascending
    assert b.reversed.oc.is_ascending
    assert b.reversed.oc.is_ascending
Beispiel #3
0
def test_oc():
    a = asc("a")
    b = desc("a")
    c = asc("b")
    n = nullslast(desc("a"))

    a = OC(a)
    b = OC(b)
    c = OC(c)
    n = OC(n)

    assert str(a) == str(OC("a"))
    assert a.is_ascending
    assert not b.is_ascending
    assert not n.reversed.reversed.is_ascending
    assert n.reversed.is_ascending
    assert not n.is_ascending  # make sure reversed doesn't modify in-place
    assert str(a.element) == str(b.element) == str(n.element)
    assert str(a) == str(b.reversed)
    assert str(n.reversed.reversed) == str(n)

    assert a.name == "a"
    assert n.name == "a"
    assert n.quoted_full_name == "a"
    assert repr(n) == "<OC: a DESC NULLS LAST>"
Beispiel #4
0
def custom_value_from_thing(thing, desc, ocol):
    """
    Replacement function for the sqlakeyset value_from_thing method.
    This function supports auto aliasing as is required when sorting by
    columns that appear in subqueries.
    :param thing:
    :param desc:
    :param ocol:
    :return:
    """
    entity = desc['entity']
    expr = desc['expr']

    try:
        is_a_table = entity == expr
    except exc.ArgumentError:
        is_a_table = False

    if isinstance(expr, Mapper) and expr.class_ == entity:
        # Is a table mapper. Just treat as a table.
        is_a_table = True

    if type(is_a_table) == bool and is_a_table:  # is a table
        mapper = class_mapper(desc['type'])
        base_elem = ocol.element.base_columns
        if not base_elem:
            base_elem = ocol.element
        elif len(base_elem) == 1:
            base_elem = next(iter(base_elem))
        else:
            raise ValueError("Multiple columns")

        # If the OCOL is a hybrid attribute it won't belong to one table.
        if not hasattr(base_elem, 'table'):
            raise ValueError

        order_column_table = base_elem.table.name

        if entity.__table__.name == order_column_table:
            prop = mapper.get_property_by_column(base_elem)
            return getattr(thing, prop.key)
        else:
            raise ValueError

    # is an attribute
    if hasattr(expr, 'info'):
        mapper = expr.parent
        tname = mapper.local_table.description

        if ocol.table_name == tname and ocol.name == expr.name:
            return thing
        else:
            raise ValueError
    import re
    ocol_quoted_full_name = re.sub('[()]', '', ocol.quoted_full_name)
    # is an attribute with label
    if ocol_quoted_full_name == OC(expr).full_name:
        return thing
    else:
        raise ValueError
Beispiel #5
0
def test_warn_on_nullslast():
    with warns(UserWarning):
        ob = [OC(nullslast(column("id")))]
        Paging(T1,
               10,
               ob,
               backwards=False,
               current_marker=None,
               get_marker=getitem)
Beispiel #6
0
def test_core(dburl):
    spec = ["b", "d", "book_id", "c"]

    cols = [column(each) for each in spec]
    ob = [OC(x).uo for x in spec]

    selectable = select(
        cols, from_obj=[table("t_Book")], whereclause=column("d") == 99, order_by=ob
    )

    with S(dburl, echo=ECHO) as s:
        check_paging_core(selectable=selectable, s=s)
Beispiel #7
0
def test_core(dburl):
    spec = ['b', 'd', 'book_id', 'c']

    cols = [column(each) for each in spec]
    ob = [OC(x).uo for x in spec]

    selectable = select(cols,
                        from_obj=[table('t_Book')],
                        whereclause=column('d') == 99,
                        order_by=ob)

    with S(dburl, echo=ECHO) as s:
        check_paging_core(selectable=selectable, s=s)
Beispiel #8
0
def test_flask_sqla_compat():
    # test djrobstep#18 for regression
    class T(declarative_base()):
        __tablename__ = "t"
        i = Column(Integer, primary_key=True)

    desc = {
        "name": "T",
        "type": T,
        "aliased": False,
        "expr": class_mapper(T),
        "entity": T,
    }
    mapping = derive_order_key(OC(T.i), desc, 0)
    assert isinstance(mapping, AttributeColumn)
Beispiel #9
0
def test_flask_sqla_compat():
    # test djrobstep#18 for regression
    class T(declarative_base()):
        __tablename__ = 't'
        i = Column(Integer, primary_key=True)

    desc = {
        'name': 'T',
        'type': T,
        'aliased': False,
        'expr': class_mapper(T),
        'entity': T,
    }
    mapping = derive_order_key(OC(T.i), desc, 0)
    assert isinstance(mapping, AttributeColumn)
Beispiel #10
0
def test_paging_object2_per_page_3():
    ob = [OC(x) for x in ["id", "b"]]

    p = Paging(T2,
               3,
               ob,
               backwards=False,
               current_marker=None,
               get_marker=getitem)
    assert p.next == ((3, 3), False)
    assert not p.has_next
    assert not p.has_previous
    assert p.further == ((3, 3), False)
    assert p.previous == ((1, 2), True)
    assert p.is_full
    general_asserts(p)
Beispiel #11
0
def test_paging_object2_per_page_2():
    ob = [OC(x) for x in ['id', 'b']]

    p = Paging(T2,
               2,
               ob,
               backwards=False,
               current_marker=None,
               get_marker=getitem)
    assert p.next == ((2, 1), False)
    assert p.has_next
    general_asserts(p)

    assert not p.has_previous
    assert p.previous == ((1, 2), True)
    assert p.further == ((2, 1), False)

    general_asserts(p)
Beispiel #12
0
def test_paging_objects1():
    p = Page(["abc"])
    assert p.one() == "abc"

    with raises(RuntimeError):
        Page([1, 2]).one()

    with raises(RuntimeError):
        Page([]).one()

    ob = [OC(x) for x in ["id", "b"]]

    p = Paging(T1,
               10,
               ob,
               backwards=False,
               current_marker=None,
               get_marker=getitem)
    assert p.next == (None, False)
    assert p.further == (None, False)
    assert p.previous == (None, True)
    assert not p.is_full
    general_asserts(p)