Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
from sqlalchemy import TIMESTAMP, Column, ForeignKey, Integer, Text
from sqlalchemy import TIMESTAMP, CheckConstraint, Column, ForeignKey, Integer, Text, func
from sqlalchemy.orm import declarative_base, relationship

Base = declarative_base()

class Account(Base):
__tablename__ = "accounts"
__table_args__ = (
CheckConstraint("account_role IN ('admin', 'author', 'user')", name="accounts_role_check"),
)

account_id = Column("account_id", Integer, primary_key=True, autoincrement=True)
username = Column("username", Text, unique=True, nullable=False)
account_username = Column("account_username", Text, unique=True, nullable=False)
account_password = Column("account_password", Text, nullable=False)
email = Column("email", Text)
account_email = Column("account_email", Text)
account_role = Column("account_role", Text, nullable=False)
created_at = Column("created_at", TIMESTAMP)
account_created_at = Column("account_created_at", TIMESTAMP, server_default=func.now(), nullable=False)

articles = relationship("Article", back_populates="writer", cascade="all, delete-orphan")
feedbacks = relationship("Feedback", back_populates="commenter", cascade="all, delete-orphan")
articles = relationship("Article", back_populates="article_author", cascade="all, delete-orphan")
comments = relationship("Comment", back_populates="comment_author", cascade="all, delete-orphan")


class Article(Base):
__tablename__ = "articles"

article_id = Column("article_id", Integer, primary_key=True, autoincrement=True)
writer_id = Column("writer_id", Integer, ForeignKey("accounts.account_id"), nullable=False)
title = Column("title", Text, nullable=False)
content = Column("content", Text, nullable=False)
published_at = Column("published_at", TIMESTAMP)
article_author_id = Column("article_author_id", Integer, ForeignKey("accounts.account_id", ondelete="CASCADE"), nullable=False)
article_title = Column("article_title", Text, nullable=False)
article_content = Column("article_content", Text, nullable=False)
article_published_at = Column("article_published_at", TIMESTAMP, server_default=func.now(), nullable=False)

writer = relationship("Account", back_populates="articles")
feedbacks = relationship("Feedback", back_populates="article", cascade="all, delete-orphan")
article_author = relationship("Account", back_populates="articles")
article_comments = relationship("Comment", back_populates="comment_article", cascade="all, delete-orphan")


class Feedback(Base):
__tablename__ = "feedback"
class Comment(Base):
__tablename__ = "comments"

feedback_id = Column("feedback_id", Integer, primary_key=True, autoincrement=True)
article_ref = Column("article_ref", Integer, ForeignKey("articles.article_id"), nullable=False)
commenter_id = Column("commenter_id", Integer, ForeignKey("accounts.account_id"), nullable=False)
reply_to = Column("reply_to", Integer, ForeignKey("feedback.feedback_id"))
message = Column("message", Text, nullable=False)
posted_at = Column("posted_at", TIMESTAMP)
comment_id = Column("comment_id", Integer, primary_key=True, autoincrement=True)
comment_article_id = Column("comment_article_id", Integer, ForeignKey("articles.article_id", ondelete="CASCADE"), nullable=False)
comment_written_account_id = Column("comment_written_account_id", Integer, ForeignKey("accounts.account_id", ondelete="CASCADE"), nullable=False)
comment_reply_to = Column("comment_reply_to", Integer, ForeignKey("comments.comment_id"), nullable=True)
comment_content = Column("comment_content", Text, nullable=False)
comment_posted_at = Column("comment_posted_at", TIMESTAMP, server_default=func.now(), nullable=False)

article = relationship("Article", back_populates="feedbacks")
commenter = relationship("Account", back_populates="feedbacks")
replies = relationship("Feedback", remote_side=[feedback_id])
comment_article = relationship("Article", back_populates="article_comments")
comment_author = relationship("Account", back_populates="comments")
replies = relationship("Comment", backref="parent", remote_side=[comment_id])
1 change: 0 additions & 1 deletion migrations/V3__rename_role_colum_in_account.sql

This file was deleted.

2 changes: 2 additions & 0 deletions migrations/V3__rename_role_column_in_account.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE accounts
RENAME COLUMN role TO account_role;
2 changes: 1 addition & 1 deletion migrations/V4__add_password_in_accounts.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ALTER TABLE accounts
ADD COLUMN account_password TEXT NOT NULL;
ADD COLUMN account_password TEXT NOT NULL;
76 changes: 76 additions & 0 deletions migrations/V5__rename_and_update_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
ALTER TABLE accounts
RENAME COLUMN username TO account_username;

ALTER TABLE accounts
RENAME COLUMN email TO account_email;

ALTER TABLE accounts
RENAME COLUMN created_at TO account_created_at;

ALTER TABLE articles
RENAME COLUMN writer_id TO article_author_id;

ALTER TABLE articles
RENAME COLUMN title TO article_title;

ALTER TABLE articles
RENAME COLUMN content TO article_content;

ALTER TABLE articles
RENAME COLUMN published_at TO article_published_at;

ALTER TABLE articles
DROP CONSTRAINT articles_writer_id_fkey;

ALTER TABLE articles
ADD CONSTRAINT articles_author_fk
FOREIGN KEY (article_author_id)
REFERENCES accounts(account_id)
ON DELETE CASCADE;

ALTER TABLE feedback
RENAME TO comments;

ALTER TABLE comments
RENAME COLUMN feedback_id TO comment_id;

ALTER TABLE comments
RENAME COLUMN article_ref TO comment_article_id;

ALTER TABLE comments
RENAME COLUMN commenter_id TO comment_written_account_id;

ALTER TABLE comments
RENAME COLUMN reply_to TO comment_reply_to;

ALTER TABLE comments
RENAME COLUMN message TO comment_content;

ALTER TABLE comments
RENAME COLUMN posted_at TO comment_posted_at;

ALTER TABLE comments
DROP CONSTRAINT feedback_article_ref_fkey;

ALTER TABLE comments
ADD CONSTRAINT comments_article_fk
FOREIGN KEY (comment_article_id)
REFERENCES articles(article_id)
ON DELETE CASCADE;

ALTER TABLE comments
DROP CONSTRAINT feedback_commenter_id_fkey;

ALTER TABLE comments
ADD CONSTRAINT comments_author_fk
FOREIGN KEY (comment_written_account_id)
REFERENCES accounts(account_id)
ON DELETE CASCADE;

ALTER TABLE comments
DROP CONSTRAINT feedback_reply_to_fkey;

ALTER TABLE comments
ADD CONSTRAINT comments_reply_fk
FOREIGN KEY (comment_reply_to)
REFERENCES comments(comment_id);
38 changes: 19 additions & 19 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
from app.models import Account, Article, Feedback
from app.models import Account, Article, Comment


def test_create_account(db_session):
account = Account(username="pytest_user_account", account_password="123456789", email="test_account@example.com", account_role="user")
account = Account(account_username="pytest_user_account", account_password="123456789", account_email="test_account@example.com", account_role="user")
db_session.add(account)
db_session.commit()
result = db_session.query(Account).filter_by(username="pytest_user_account").first()
result = db_session.query(Account).filter_by(account_username="pytest_user_account").first()
assert result is not None
assert result.account_password is not None
assert result.account_role == "user"

def test_create_article(db_session):
author = Account(username="pytest_author_article", account_password="123456789", email="author_article@test.com", account_role="author")
author = Account(account_username="pytest_author_article", account_password="123456789", account_email="author_article@test.com", account_role="author")
db_session.add(author)
db_session.commit()
article = Article(writer_id=author.account_id, title="Titre article", content="Contenu article")
article = Article(article_author_id=author.account_id, article_title="Titre article", article_content="Contenu article")
db_session.add(article)
db_session.commit()
result = db_session.query(Article).filter_by(title="Titre article").first()
result = db_session.query(Article).filter_by(article_title="Titre article").first()
assert result is not None
assert result.writer.username == "pytest_author_article"
assert result.writer.account_password is not None
assert result.article_author.account_username == "pytest_author_article"
assert result.article_author.account_password is not None

def test_create_feedback(db_session):
author = Account(username="pytest_author_feedback", account_password="123456789", email="author_feedback@test.com", account_role="author")
user = Account(username="pytest_user_feedback", account_password="123456789", email="user_feedback@test.com", account_role="user")
def test_create_comment(db_session):
author = Account(account_username="pytest_author_comment", account_password="123456789", account_email="author_comment@test.com", account_role="author")
user = Account(account_username="pytest_user_comment", account_password="123456789", account_email="user_comment@test.com", account_role="user")
db_session.add_all([author, user])
db_session.commit()
article = Article(writer_id=author.account_id, title="Titre feedback", content="Contenu feedback")
article = Article(article_author_id=author.account_id, article_title="Titre comment", article_content="Contenu comment")
db_session.add(article)
db_session.commit()
feedback = Feedback(article_ref=article.article_id, commenter_id=user.account_id, message="Bravo !")
db_session.add(feedback)
comment = Comment(comment_article_id=article.article_id, comment_written_account_id=user.account_id, comment_content="Bravo !")
db_session.add(comment)
db_session.commit()
result = db_session.query(Feedback).filter_by(message="Bravo !").first()
result = db_session.query(Comment).filter_by(comment_content="Bravo !").first()
assert result is not None
assert result.commenter.username == "pytest_user_feedback"
assert result.commenter.account_password is not None
assert result.article.title == "Titre feedback"
assert result.article.writer.account_password is not None
assert result.comment_author.account_username == "pytest_user_comment"
assert result.comment_author.account_password is not None
assert result.comment_article.article_title == "Titre comment"
assert result.comment_article.article_author.account_password is not None