Browse Source

Tests: move common fixtures into a module

This moves some of the commonly-used fixtures (creating a topic, etc.)
into a separate module, which gets included into conftest.py (so the
fixtures are available everywhere) by treating it as a "plugin".
merge-requests/25/head
Deimos 6 years ago
parent
commit
eb1e4c6eca
  1. 4
      tildes/tests/conftest.py
  2. 53
      tildes/tests/fixtures.py
  3. 30
      tildes/tests/test_comment.py
  4. 21
      tildes/tests/test_comment_user_mentions.py
  5. 30
      tildes/tests/test_topic.py
  6. 19
      tildes/tests/test_topic_tags.py

4
tildes/tests/conftest.py

@ -17,6 +17,10 @@ from tildes.models.group import Group
from tildes.models.user import User from tildes.models.user import User
# include the fixtures defined in fixtures.py
pytest_plugins = ['tests.fixtures']
class NestedSessionWrapper(Session): class NestedSessionWrapper(Session):
"""Wrapper that starts a new nested transaction on commit/rollback.""" """Wrapper that starts a new nested transaction on commit/rollback."""

53
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()

30
tildes/tests/test_comment.py

@ -6,7 +6,6 @@ from pyramid.security import (
Everyone, Everyone,
principals_allowed_by_permission, principals_allowed_by_permission,
) )
from pytest import fixture
from tildes.enums import CommentSortOption from tildes.enums import CommentSortOption
from tildes.lib.datetime import utc_now from tildes.lib.datetime import utc_now
@ -15,39 +14,10 @@ from tildes.models.comment import (
CommentTree, CommentTree,
EDIT_GRACE_PERIOD, EDIT_GRACE_PERIOD,
) )
from tildes.models.topic import Topic
from tildes.schemas.comment import CommentSchema from tildes.schemas.comment import CommentSchema
from tildes.schemas.fields import Markdown 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): def test_comment_creation_validates_schema(mocker, session_user, topic):
"""Ensure that comment creation goes through schema validation.""" """Ensure that comment creation goes through schema validation."""
mocker.spy(CommentSchema, 'load') mocker.spy(CommentSchema, 'load')

21
tildes/tests/test_comment_user_mentions.py

@ -11,27 +11,6 @@ from tildes.models.topic import Topic
from tildes.models.user import User 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 @fixture
def user_list(db): def user_list(db):
"""Create several users.""" """Create several users."""

30
tildes/tests/test_topic.py

@ -7,7 +7,7 @@ from pyramid.security import (
Everyone, Everyone,
principals_allowed_by_permission, principals_allowed_by_permission,
) )
from pytest import fixture, raises
from pytest import raises
from tildes.lib.datetime import utc_now from tildes.lib.datetime import utc_now
from tildes.models.topic import EDIT_GRACE_PERIOD, Topic 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 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): def test_text_creation_validations(mocker, session_user, session_group):
"""Ensure that text topic creation goes through expected validation.""" """Ensure that text topic creation goes through expected validation."""
mocker.spy(TopicSchema, 'load') mocker.spy(TopicSchema, 'load')

19
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): def test_tags_whitespace_stripped(text_topic):
"""Ensure excess whitespace around tags gets stripped.""" """Ensure excess whitespace around tags gets stripped."""
text_topic.tags = [' one', 'two ', ' three '] text_topic.tags = [' one', 'two ', ' three ']

Loading…
Cancel
Save