Use parametrized test fixture to avoid code duplication.

This commit is contained in:
Tom Kedem 2016-10-02 14:17:04 +03:00
parent 84682326aa
commit f690addefe
1 changed files with 5 additions and 52 deletions

View File

@ -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