示例#1
0
def test_npr_apply_multi():
    qs = NPR(Prefetch('publisher'), 'publisher').rebuild(SelectRelated('author')).apply(default_qs)

    assert len(qs._prefetch_related_lookups) == 2
    assert qs._prefetch_related_lookups[1] == 'author__publisher'

    pr_obj = qs._prefetch_related_lookups[0]
    assert isinstance(pr_obj, Prefetch)
    assert pr_obj.prefetch_through == pr_obj.prefetch_to == 'author__publisher'
class ProductFilters(RQLFilterClass):
    MODEL = Product
    SELECT = True
    FILTERS = (
        {
            'filter': 'id',
            'ordering': True,
        },
        {
            'filter': 'name',
            'search': True,
        },
        {
            'namespace': 'category',
            'filters': ('id', 'name'),
            'qs': SelectRelated('category'),
        },
        {
            'namespace': 'company',
            'filters': ('id', 'name'),
            'hidden': True,
            'qs': SelectRelated('company'),
        },
    )
示例#3
0
def test_sr_apply():
    qs = SelectRelated('author', 'author__publisher').apply(default_qs)
    assert qs.query.select_related == {'author': {'publisher': {}}}
示例#4
0
def test_chain_parent_sr():
    qs = Chain(NSR('publisher')).rebuild(Chain(SelectRelated('author'))).apply(default_qs)
    assert qs.query.select_related == {'author': {'publisher': {}}}
示例#5
0
def test_chain_without_parent():
    qs = Chain(SelectRelated('author__publisher'), SelectRelated('author')).apply(default_qs)
    assert qs.query.select_related == {'author': {'publisher': {}}}
示例#6
0
def test_nsr_apply_sr_parent():
    qs = NestedSelectRelated('publisher').rebuild(SelectRelated('author')).apply(default_qs)
    assert qs.query.select_related == {'author': {'publisher': {}}}
示例#7
0
    assert qs._prefetch_related_lookups == ('pages',)


def test_nested_prefetch_object_in_parent():
    with pytest.raises(AssertionError) as e:
        NestedPrefetchRelated('pages').rebuild(PrefetchRelated(Prefetch('pages')))

    assert str(e.value) == 'Only simple parent relations are supported.'


def test_npr_apply_no_parent():
    qs = NestedPrefetchRelated('pages').apply(default_qs)
    assert qs._prefetch_related_lookups == ('pages',)


@pytest.mark.parametrize('parent', (PrefetchRelated('author'), SelectRelated('author')))
def test_npr_apply_parent(parent):
    qs = NestedPrefetchRelated('publisher').rebuild(parent).apply(default_qs)
    assert qs._prefetch_related_lookups == ('author__publisher',)


def test_npr_apply_multi():
    qs = NPR(Prefetch('publisher'), 'publisher').rebuild(SelectRelated('author')).apply(default_qs)

    assert len(qs._prefetch_related_lookups) == 2
    assert qs._prefetch_related_lookups[1] == 'author__publisher'

    pr_obj = qs._prefetch_related_lookups[0]
    assert isinstance(pr_obj, Prefetch)
    assert pr_obj.prefetch_through == pr_obj.prefetch_to == 'author__publisher'
示例#8
0
def test_nested_prefetch_object_in_parent():
    with pytest.raises(AssertionError) as e:
        NestedPrefetchRelated('pages').rebuild(
            PrefetchRelated(Prefetch('pages')))

    assert str(e.value) == 'Only simple parent relations are supported.'


def test_npr_apply_no_parent():
    qs = NestedPrefetchRelated('pages').apply(default_qs)
    assert qs._prefetch_related_lookups == ('pages', )


@pytest.mark.parametrize('parent',
                         (PrefetchRelated('author'), SelectRelated('author')))
def test_npr_apply_parent(parent):
    qs = NestedPrefetchRelated('publisher').rebuild(parent).apply(default_qs)
    assert qs._prefetch_related_lookups == ('author__publisher', )


def test_npr_apply_multi():
    qs = NPR(Prefetch('publisher'),
             'publisher').rebuild(SelectRelated('author')).apply(default_qs)

    assert len(qs._prefetch_related_lookups) == 2
    assert qs._prefetch_related_lookups[1] == 'author__publisher'

    pr_obj = qs._prefetch_related_lookups[0]
    assert isinstance(pr_obj, Prefetch)
    assert pr_obj.prefetch_through == pr_obj.prefetch_to == 'author__publisher'