File: test_auto_delete_orphans.py

package info (click to toggle)
python-sqlalchemy-utils 0.41.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,252 kB
  • sloc: python: 13,566; makefile: 141
file content (112 lines) | stat: -rw-r--r-- 2,742 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import pytest
import sqlalchemy as sa
from sqlalchemy.orm import backref

from sqlalchemy_utils import auto_delete_orphans, ImproperlyConfigured


@pytest.fixture
def tagging_tbl(Base):
    return sa.Table(
        'tagging',
        Base.metadata,
        sa.Column(
            'tag_id',
            sa.Integer,
            sa.ForeignKey('tag.id', ondelete='cascade'),
            primary_key=True
        ),
        sa.Column(
            'entry_id',
            sa.Integer,
            sa.ForeignKey('entry.id', ondelete='cascade'),
            primary_key=True
        )
    )


@pytest.fixture
def Tag(Base):
    class Tag(Base):
        __tablename__ = 'tag'
        id = sa.Column(sa.Integer, primary_key=True)
        name = sa.Column(sa.String(100), unique=True, nullable=False)

        def __init__(self, name=None):
            self.name = name
    return Tag


@pytest.fixture(
    params=['entries', backref('entries', lazy='select')],
    ids=['backref_string', 'backref_with_keywords']
)
def Entry(Base, Tag, tagging_tbl, request):
    class Entry(Base):
        __tablename__ = 'entry'

        id = sa.Column(sa.Integer, primary_key=True)

        tags = sa.orm.relationship(
            Tag,
            secondary=tagging_tbl,
            backref=request.param
        )
    auto_delete_orphans(Entry.tags)
    return Entry


@pytest.fixture
def EntryWithoutTagsBackref(Base, Tag, tagging_tbl):
    class EntryWithoutTagsBackref(Base):
        __tablename__ = 'entry'

        id = sa.Column(sa.Integer, primary_key=True)

        tags = sa.orm.relationship(
            Tag,
            secondary=tagging_tbl
        )
    return EntryWithoutTagsBackref


class TestAutoDeleteOrphans:

    @pytest.fixture
    def init_models(self, Entry, Tag):
        pass

    def test_orphan_deletion(self, session, Entry, Tag):
        r1 = Entry()
        r2 = Entry()
        r3 = Entry()
        t1, t2, t3, t4 = (
            Tag('t1'),
            Tag('t2'),
            Tag('t3'),
            Tag('t4')
        )

        r1.tags.extend([t1, t2])
        r2.tags.extend([t2, t3])
        r3.tags.extend([t4])
        session.add_all([r1, r2, r3])

        assert session.query(Tag).count() == 4
        r2.tags.remove(t2)
        assert session.query(Tag).count() == 4
        r1.tags.remove(t2)
        assert session.query(Tag).count() == 3
        r1.tags.remove(t1)
        assert session.query(Tag).count() == 2


class TestAutoDeleteOrphansWithoutBackref:

    @pytest.fixture
    def init_models(self, EntryWithoutTagsBackref, Tag):
        pass

    def test_orphan_deletion(self, EntryWithoutTagsBackref):
        with pytest.raises(ImproperlyConfigured):
            auto_delete_orphans(EntryWithoutTagsBackref.tags)