Beispiel #1
0
def test_from_clause():
    fc = FromClause(users_t)
    assert len(fc) == 1
    fc.add(FromItem(user_names_t, left=True))
    assert len(fc) == 2
    fc.add(FromItem(user_names_t, left=True))
    assert len(fc) == 2
    fc.add(FromItem(users_t.alias('test')))
    assert len(fc) == 3
    for from_item in fc:
        assert isinstance(from_item, FromItem)
    assert isinstance(fc(), Join)
Beispiel #2
0
def test_from_item_2():
    users_a = users_t.alias('test')
    from_item = FromItem(users_a)
    assert from_item.table == users_a
    assert from_item.left is False
    assert from_item.onclause is None
    assert from_item.name == 'test'
Beispiel #3
0
def test_is_from_item():
    users_a = users_t.alias('test')
    assert is_from_item(FromItem(users_t))
    assert is_from_item(users_t)
    assert is_from_item(users_a)
    assert not is_from_item(1)
    assert not is_from_item(False)
    assert not is_from_item(None)
    assert not is_from_item('test')
Beispiel #4
0
    def get_from_items(self, related=False):

        if self.model is None and self.parent is None:
            raise Error('relationship: {!r} not loaded')

        if self.where is not None and not is_clause(self.where):
            raise ModelError('{!r} | invalid "where" clause'.format(self), self.model)

        from_items = list()

        if self.cardinality == Cardinality.ONE_TO_ONE:
            if self.refs:
                from_items.append(FromItem(
                    self.refs[0].table,
                    onclause=get_primary_key(self.refs[0].table) == self.parent.primary_key,
                    left=True))
            from_items.append(FromItem(
                self.model.from_clause(),
                onclause=self.model.primary_key == (self.refs[0] if self.refs else self.parent.primary_key),
                left=True))

        elif self.cardinality == Cardinality.MANY_TO_ONE:
            ref = self.parent.from_clause.get_column(self.refs[0])
            if ref is not None:
                from_items.append(FromItem(
                    self.parent.from_clause() if related else self.model.from_clause(),
                    onclause=self.model.primary_key == ref,
                    left=True))

            else:
                if related:
                    from_items.append(FromItem(
                        self.refs[0].table,
                        onclause=self.model.primary_key == self.refs[0],
                        left=True))
                    from_items.append(FromItem(
                        self.parent.from_clause(),
                        onclause=get_primary_key(self.refs[0].table) == self.parent.primary_key,
                        left=True))
                else:
                    from_items.append(FromItem(
                        self.refs[0].table,
                        onclause=self.parent.primary_key == get_primary_key(self.refs[0].table),
                        left=True))
                    from_items.append(FromItem(
                        self.model.from_clause(),
                        onclause=self.refs[0] == self.model.primary_key,
                        left=True))

        elif self.cardinality == Cardinality.ONE_TO_MANY:
            ref = self.model.from_clause.get_column(self.refs[0])
            if ref is not None:
                from_items.append(FromItem(
                    ref.table,
                    onclause=self.parent.primary_key == ref,
                    left=True))
                if self.model.primary_key.table != ref.table:
                    from_items.append(FromItem(
                        self.model.primary_key.table,
                        onclause=get_primary_key(ref.table) == self.model.primary_key,
                        left=True))
            else:
                from_items.append(FromItem(
                    self.refs[0].table,
                    onclause=self.model.primary_key == get_primary_key(self.refs[0]),
                    left=True))
                from_items.append(FromItem(
                    self.parent.from_clause(),
                    onclause=self.refs[0] == self.parent.primary_key,
                    left=True))

        else:
            if related:
                onclause = self.model.primary_key == self.refs[1]
                if self.where is not None:
                    onclause = and_(onclause, self.where)
                from_items.append(FromItem(self.refs[1].table, onclause=onclause, left=True))
                from_items.append(FromItem(
                    self.parent.from_clause(),
                    onclause=self.parent.primary_key == self.refs[0],
                    left=True))
            else:
                onclause = self.refs[0] == self.parent.primary_key
                if self.where is not None:
                    onclause = and_(onclause, self.where)
                from_items.append(FromItem(self.refs[0].table, onclause=onclause, left=True))
                from_items.append(FromItem(
                    self.model.from_clause(),
                    onclause=self.model.primary_key == self.refs[1],
                    left=True))

        return tuple(from_items)
Beispiel #5
0
def test_from_item_exc_5():
    with pytest.raises(Error):
        FromItem(users_t, onclause='test')
Beispiel #6
0
def test_from_item_exc_2():
    with pytest.raises(Error):
        FromItem('test')
Beispiel #7
0
def test_from_item_exc_1():
    with pytest.raises(Error):
        FromItem(None)
Beispiel #8
0
def test_from_item_5():
    from_item = FromItem(users_t, onclause=users_t.c.id == 1)
    assert from_item.table == users_t
    assert from_item.left is False
    assert from_item.onclause is not None
Beispiel #9
0
def test_from_item_4():
    from_item = FromItem(users_t, onclause=users_t.c.id == 1, left=True)
    assert from_item.table == users_t
    assert from_item.left is True
    assert from_item.onclause is not None
Beispiel #10
0
def test_from_item_3():
    from_item = FromItem(users_t, left=True)
    assert from_item.table == users_t
    assert from_item.left is True
    assert from_item.onclause is None
Beispiel #11
0
def test_from_item_1():
    from_item = FromItem(users_t)
    assert from_item.table == users_t
    assert from_item.left is False
    assert from_item.onclause is None
    assert from_item.name == 'public.users'