diff --git a/tildes/tests/conftest.py b/tildes/tests/conftest.py index d13b693..9e65784 100644 --- a/tildes/tests/conftest.py +++ b/tildes/tests/conftest.py @@ -17,6 +17,10 @@ from tildes.models.group import Group from tildes.models.user import User +# include the fixtures defined in fixtures.py +pytest_plugins = ['tests.fixtures'] + + class NestedSessionWrapper(Session): """Wrapper that starts a new nested transaction on commit/rollback.""" diff --git a/tildes/tests/fixtures.py b/tildes/tests/fixtures.py new file mode 100644 index 0000000..26cb8f8 --- /dev/null +++ b/tildes/tests/fixtures.py @@ -0,0 +1,53 @@ +from pytest import fixture + +from tildes.models.comment import Comment +from tildes.models.topic import Topic + + +@fixture +def text_topic(db, session_group, session_user): + """Create a text topic, delete it as teardown (including comments).""" + new_topic = Topic.create_text_topic( + session_group, session_user, 'A Text Topic', 'the text') + db.add(new_topic) + db.commit() + + yield new_topic + + db.query(Comment).filter_by(topic_id=new_topic.topic_id).delete() + db.delete(new_topic) + db.commit() + + +@fixture +def link_topic(db, session_group, session_user): + """Create a link topic, delete it as teardown (including comments).""" + new_topic = Topic.create_link_topic( + session_group, session_user, 'A Link Topic', 'http://example.com') + db.add(new_topic) + db.commit() + + yield new_topic + + db.query(Comment).filter_by(topic_id=new_topic.topic_id).delete() + db.delete(new_topic) + db.commit() + + +@fixture +def topic(text_topic): + """Create a topic, test doesn't care which type.""" + return text_topic + + +@fixture +def comment(db, session_user, topic): + """Create a comment in the database, delete it as teardown.""" + new_comment = Comment(topic, session_user, 'A comment') + db.add(new_comment) + db.commit() + + yield new_comment + + db.delete(new_comment) + db.commit() diff --git a/tildes/tests/test_comment.py b/tildes/tests/test_comment.py index 76bbc0c..f1ffae4 100644 --- a/tildes/tests/test_comment.py +++ b/tildes/tests/test_comment.py @@ -6,7 +6,6 @@ from pyramid.security import ( Everyone, principals_allowed_by_permission, ) -from pytest import fixture from tildes.enums import CommentSortOption from tildes.lib.datetime import utc_now @@ -15,39 +14,10 @@ from tildes.models.comment import ( CommentTree, EDIT_GRACE_PERIOD, ) -from tildes.models.topic import Topic from tildes.schemas.comment import CommentSchema from tildes.schemas.fields import Markdown -@fixture -def topic(db, session_group, session_user): - """Create a topic in the db, delete it as teardown (including comments).""" - new_topic = Topic.create_text_topic( - session_group, session_user, 'Some title', 'some text') - db.add(new_topic) - db.commit() - - yield new_topic - - db.query(Comment).filter_by(topic_id=new_topic.topic_id).delete() - db.delete(new_topic) - db.commit() - - -@fixture -def comment(db, session_user, topic): - """Create a comment in the database, delete it as teardown.""" - new_comment = Comment(topic, session_user, 'A comment') - db.add(new_comment) - db.commit() - - yield new_comment - - db.delete(new_comment) - db.commit() - - def test_comment_creation_validates_schema(mocker, session_user, topic): """Ensure that comment creation goes through schema validation.""" mocker.spy(CommentSchema, 'load') diff --git a/tildes/tests/test_comment_user_mentions.py b/tildes/tests/test_comment_user_mentions.py index ec9ec15..51ec837 100644 --- a/tildes/tests/test_comment_user_mentions.py +++ b/tildes/tests/test_comment_user_mentions.py @@ -11,27 +11,6 @@ from tildes.models.topic import Topic from tildes.models.user import User -@fixture -def topic(db, session_group, session_user): - """Create a topic in the db, delete it as teardown (including comments).""" - new_topic = Topic.create_text_topic( - session_group, session_user, 'Some title', 'some text') - db.add(new_topic) - db.commit() - - yield new_topic - - db.query(Comment).filter_by(topic_id=new_topic.topic_id).delete() - db.delete(new_topic) - db.commit() - - -@fixture -def comment(topic, session_user): - """Return an unsaved comment.""" - return Comment(topic, session_user, 'Comment content.') - - @fixture def user_list(db): """Create several users.""" diff --git a/tildes/tests/test_topic.py b/tildes/tests/test_topic.py index 2a3f472..2427624 100644 --- a/tildes/tests/test_topic.py +++ b/tildes/tests/test_topic.py @@ -7,7 +7,7 @@ from pyramid.security import ( Everyone, principals_allowed_by_permission, ) -from pytest import fixture, raises +from pytest import raises from tildes.lib.datetime import utc_now from tildes.models.topic import EDIT_GRACE_PERIOD, Topic @@ -15,34 +15,6 @@ from tildes.schemas.fields import Markdown, SimpleString from tildes.schemas.topic import TopicSchema -@fixture -def text_topic(db, session_group, session_user): - """Create a text topic in the db and delete it as teardown.""" - new_topic = Topic.create_text_topic( - session_group, session_user, 'A Text Topic', 'the text') - db.add(new_topic) - db.commit() - - yield new_topic - - db.delete(new_topic) - db.commit() - - -@fixture -def link_topic(db, session_group, session_user): - """Create a link topic in the db and delete it as teardown.""" - new_topic = Topic.create_link_topic( - session_group, session_user, 'A Link Topic', 'http://example.com') - db.add(new_topic) - db.commit() - - yield new_topic - - db.delete(new_topic) - db.commit() - - def test_text_creation_validations(mocker, session_user, session_group): """Ensure that text topic creation goes through expected validation.""" mocker.spy(TopicSchema, 'load') diff --git a/tildes/tests/test_topic_tags.py b/tildes/tests/test_topic_tags.py index cc00c43..e14d98a 100644 --- a/tildes/tests/test_topic_tags.py +++ b/tildes/tests/test_topic_tags.py @@ -1,22 +1,3 @@ -from pytest import fixture - -from tildes.models.topic import Topic - - -@fixture -def text_topic(db, session_group, session_user): - """Create a text topic in the db and delete it as teardown.""" - new_topic = Topic.create_text_topic( - session_group, session_user, 'A Text Topic', 'the text') - db.add(new_topic) - db.commit() - - yield new_topic - - db.delete(new_topic) - db.commit() - - def test_tags_whitespace_stripped(text_topic): """Ensure excess whitespace around tags gets stripped.""" text_topic.tags = [' one', 'two ', ' three ']