server/comments: add comment tables
This commit is contained in:
parent
1476c84a9d
commit
093863b944
6 changed files with 136 additions and 2 deletions
|
@ -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
|
||||
|
|
29
server/szurubooru/db/comment.py
Normal file
29
server/szurubooru/db/comment.py
Normal file
|
@ -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')
|
|
@ -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')
|
|
@ -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
|
||||
|
|
50
server/szurubooru/tests/db/test_comment.py
Normal file
50
server/szurubooru/tests/db/test_comment.py
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue