From 093863b9442756d60f2b61c310f8600f39d49eb3 Mon Sep 17 00:00:00 2001 From: rr- Date: Sun, 24 Apr 2016 09:04:53 +0200 Subject: [PATCH] server/comments: add comment tables --- server/szurubooru/db/__init__.py | 3 ++ server/szurubooru/db/comment.py | 29 +++++++++++ .../46df355634dc_add_comment_tables.py | 41 +++++++++++++++ server/szurubooru/tests/conftest.py | 11 ++++ server/szurubooru/tests/db/test_comment.py | 50 +++++++++++++++++++ server/szurubooru/tests/db/test_post.py | 4 +- 6 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 server/szurubooru/db/comment.py create mode 100644 server/szurubooru/migrations/versions/46df355634dc_add_comment_tables.py create mode 100644 server/szurubooru/tests/db/test_comment.py diff --git a/server/szurubooru/db/__init__.py b/server/szurubooru/db/__init__.py index c08f8b1d..68d04b95 100644 --- a/server/szurubooru/db/__init__.py +++ b/server/szurubooru/db/__init__.py @@ -14,5 +14,8 @@ from szurubooru.db.post import ( PostScore, PostNote, PostFeature) +from szurubooru.db.comment import ( + Comment, + CommentScore) from szurubooru.db.snapshot import Snapshot from szurubooru.db.session import session diff --git a/server/szurubooru/db/comment.py b/server/szurubooru/db/comment.py new file mode 100644 index 00000000..eedf1813 --- /dev/null +++ b/server/szurubooru/db/comment.py @@ -0,0 +1,29 @@ +from sqlalchemy import Column, Integer, DateTime, Text, ForeignKey +from sqlalchemy.orm import relationship +from szurubooru.db.base import Base + +class CommentScore(Base): + __tablename__ = 'comment_score' + + comment_id = Column('comment_id', Integer, ForeignKey('comment.id'), primary_key=True) + user_id = Column('user_id', Integer, ForeignKey('user.id'), primary_key=True) + time = Column('time', DateTime, nullable=False) + score = Column('score', Integer, nullable=False) + + comment = relationship('Comment') + user = relationship('User') + +class Comment(Base): + __tablename__ = 'comment' + + comment_id = Column('id', Integer, primary_key=True) + post_id = Column('post_id', Integer, ForeignKey('post.id'), nullable=False) + user_id = Column('user_id', Integer, ForeignKey('user.id')) + creation_time = Column('creation_time', DateTime, nullable=False) + last_edit_time = Column('last_edit_time', DateTime) + text = Column('text', Text, default=None) + + user = relationship('User') + post = relationship('Post') + scores = relationship( + 'CommentScore', cascade='all, delete-orphan', lazy='joined') diff --git a/server/szurubooru/migrations/versions/46df355634dc_add_comment_tables.py b/server/szurubooru/migrations/versions/46df355634dc_add_comment_tables.py new file mode 100644 index 00000000..0a8965a3 --- /dev/null +++ b/server/szurubooru/migrations/versions/46df355634dc_add_comment_tables.py @@ -0,0 +1,41 @@ +''' +Add comment tables + +Revision ID: 46df355634dc +Created at: 2016-04-24 09:02:05.008648 +''' + +import sqlalchemy as sa +from alembic import op + +revision = '46df355634dc' +down_revision = '84bd402f15f0' +branch_labels = None +depends_on = None + +def upgrade(): + op.create_table( + 'comment', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('user_id', sa.Integer(), nullable=True), + sa.Column('post_id', sa.Integer(), nullable=False), + sa.Column('creation_time', sa.DateTime(), nullable=False), + sa.Column('last_edit_time', sa.DateTime(), nullable=True), + sa.Column('text', sa.Text(), nullable=True), + sa.ForeignKeyConstraint(['user_id'], ['user.id']), + sa.ForeignKeyConstraint(['post_id'], ['post.id']), + sa.PrimaryKeyConstraint('id')) + + op.create_table( + 'comment_score', + sa.Column('comment_id', sa.Integer(), nullable=False), + sa.Column('user_id', sa.Integer(), nullable=False), + sa.Column('time', sa.DateTime(), nullable=False), + sa.Column('score', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['comment_id'], ['comment.id']), + sa.ForeignKeyConstraint(['user_id'], ['user.id']), + sa.PrimaryKeyConstraint('comment_id', 'user_id')) + +def downgrade(): + op.drop_table('comment_score') + op.drop_table('comment') diff --git a/server/szurubooru/tests/conftest.py b/server/szurubooru/tests/conftest.py index 373e5d9b..f99934dc 100644 --- a/server/szurubooru/tests/conftest.py +++ b/server/szurubooru/tests/conftest.py @@ -138,3 +138,14 @@ def post_factory(): post.creation_time = datetime.datetime(1996, 1, 1) return post return factory + +@pytest.fixture +def comment_factory(): + def factory(user=None, post=None, text='dummy'): + comment = db.Comment() + comment.user = user or user_factory() + comment.post = post or post_factory() + comment.text = text + comment.creation_time = datetime.datetime(1996, 1, 1) + return comment + return factory diff --git a/server/szurubooru/tests/db/test_comment.py b/server/szurubooru/tests/db/test_comment.py new file mode 100644 index 00000000..5f3ea708 --- /dev/null +++ b/server/szurubooru/tests/db/test_comment.py @@ -0,0 +1,50 @@ +from datetime import datetime +from szurubooru import db + +def test_saving_comment(user_factory, post_factory): + user = user_factory() + post = post_factory() + comment = db.Comment() + comment.text = 'long text' * 1000 + comment.user = user + comment.post = post + comment.creation_time = datetime(1997, 1, 1) + comment.last_edit_time = datetime(1998, 1, 1) + db.session.add_all([user, post, comment]) + db.session.commit() + + db.session.refresh(comment) + assert not db.session.dirty + assert comment.user is not None and comment.user.user_id is not None + assert comment.text == 'long text' * 1000 + assert comment.creation_time == datetime(1997, 1, 1) + assert comment.last_edit_time == datetime(1998, 1, 1) + +def test_cascade_deletions(comment_factory, user_factory, post_factory): + user = user_factory() + post = post_factory() + comment = comment_factory(user=user, post=post) + db.session.add_all([user, comment]) + db.session.flush() + + score = db.CommentScore() + score.comment = comment + score.user = user + score.time = datetime(1997, 1, 1) + score.score = 1 + db.session.add(score) + db.session.flush() + + assert not db.session.dirty + assert comment.user is not None and comment.user.user_id is not None + assert db.session.query(db.User).count() == 1 + assert db.session.query(db.Comment).count() == 1 + assert db.session.query(db.CommentScore).count() == 1 + + db.session.delete(comment) + db.session.commit() + + assert not db.session.dirty + assert db.session.query(db.User).count() == 1 + assert db.session.query(db.Comment).count() == 0 + assert db.session.query(db.CommentScore).count() == 0 diff --git a/server/szurubooru/tests/db/test_post.py b/server/szurubooru/tests/db/test_post.py index 90125d64..45a89519 100644 --- a/server/szurubooru/tests/db/test_post.py +++ b/server/szurubooru/tests/db/test_post.py @@ -59,7 +59,7 @@ def test_cascade_deletions(post_factory, user_factory, tag_factory): note.post = post note.polygon = '' note.text = '' - db.session.add_all([score]) + db.session.add_all([score, favorite, feature, note]) db.session.flush() post.user = user @@ -74,7 +74,7 @@ def test_cascade_deletions(post_factory, user_factory, tag_factory): db.session.flush() assert not db.session.dirty - assert post.user.user_id is not None + assert post.user is not None and post.user.user_id is not None assert len(post.relations) == 2 assert db.session.query(db.User).count() == 1 assert db.session.query(db.Tag).count() == 2