Esempio n. 1
0
def test_chain_parent_sr():
    qs = Chain(NSR('publisher')).rebuild(Chain(
        SelectRelated('author'))).apply(default_qs)
    assert qs.query.select_related == {'author': {'publisher': {}}}
Esempio n. 2
0
def test_nsr_apply_sr_parent():
    qs = NestedSelectRelated('publisher').rebuild(
        SelectRelated('author')).apply(default_qs)
    assert qs.query.select_related == {'author': {'publisher': {}}}
Esempio n. 3
0
def test_chain_without_parent():
    qs = Chain(SelectRelated('author__publisher'),
               SelectRelated('author')).apply(default_qs)
    assert qs.query.select_related == {'author': {'publisher': {}}}
Esempio n. 4
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'