From f690addefe26bcf70cabab07bd78f605005c525d Mon Sep 17 00:00:00 2001 From: Tom Kedem Date: Sun, 2 Oct 2016 14:17:04 +0300 Subject: [PATCH] Use parametrized test fixture to avoid code duplication. --- tests/test_auto_delete_orphans.py | 57 +++---------------------------- 1 file changed, 5 insertions(+), 52 deletions(-) diff --git a/tests/test_auto_delete_orphans.py b/tests/test_auto_delete_orphans.py index e7ea867..db57bfd 100644 --- a/tests/test_auto_delete_orphans.py +++ b/tests/test_auto_delete_orphans.py @@ -37,17 +37,17 @@ def Tag(Base): return Tag -@pytest.fixture -def Entry(Base, Tag, tagging_tbl): +@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', + Tag, secondary=tagging_tbl, - backref='entries' + backref=request.param ) auto_delete_orphans(Entry.tags) return Entry @@ -61,28 +61,12 @@ def EntryWithoutTagsBackref(Base, Tag, tagging_tbl): id = sa.Column(sa.Integer, primary_key=True) tags = sa.orm.relationship( - 'Tag', + Tag, secondary=tagging_tbl ) return EntryWithoutTagsBackref -@pytest.fixture -def EntryWithBackrefKeywords(Base, Tag, tagging_tbl): - class Entry(Base): - __tablename__ = 'entry' - - id = sa.Column(sa.Integer, primary_key=True) - - tags = sa.orm.relationship( - 'Tag', - secondary=tagging_tbl, - backref=backref('entries', lazy='select') - ) - auto_delete_orphans(Entry.tags) - return Entry - - class TestAutoDeleteOrphans(object): @pytest.fixture @@ -123,34 +107,3 @@ class TestAutoDeleteOrphansWithoutBackref(object): def test_orphan_deletion(self, EntryWithoutTagsBackref): with pytest.raises(ImproperlyConfigured): auto_delete_orphans(EntryWithoutTagsBackref.tags) - - -class TestAutoDeleteOrphansBackrefWithKeywords(object): - - @pytest.fixture - def init_models(self, EntryWithBackrefKeywords, Tag): - pass - - def test_orphan_deletion(self, session, EntryWithBackrefKeywords, Tag): - r1 = EntryWithBackrefKeywords() - r2 = EntryWithBackrefKeywords() - r3 = EntryWithBackrefKeywords() - 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