Coverage for app / models / feedback.py: 100%
18 statements
« prev ^ index » next coverage.py v7.12.0, created at 2025-12-06 04:49 +0000
« prev ^ index » next coverage.py v7.12.0, created at 2025-12-06 04:49 +0000
1"""
2This module defines the Feedback model for managing user feedback on books.
4Classes:
5- Feedback: Represents user feedback on books with relationships to User and Book.
6- FeedbackEnum: Enum for feedback types like 'like' and 'dislike'.
8Dependencies:
9- SQLAlchemy for ORM and database integration.
10"""
11from typing import TYPE_CHECKING
12from enum import Enum as PyEnum
14from sqlalchemy import ForeignKey, Enum
15from sqlalchemy.orm import Mapped, mapped_column, relationship, declared_attr
17from app import db
19if TYPE_CHECKING:
20 from app.models.book import Book
21 from app.security.models import User
24class FeedbackEnum(PyEnum):
25 """
26 Enumeration for feedback options.
28 This class defines possible feedback types that can be used to express
29 sentiments. It provides a controlled set of string constants used for
30 determining the type of feedback. This is especially useful in scenarios
31 where only predefined feedback options are allowed, ensuring reliable and
32 predictable inputs.
33 """
34 like = "like" # pylint: disable=invalid-name
35 dislike = "dislike" # pylint: disable=invalid-name
38# pylint: disable=too-few-public-methods
39class Feedback(db.Model):
40 """
41 Representation of user feedback on books.
43 This class is an ORM model representing user feedback within a database.
44 It defines the feedback associated with a specific user and a book,
45 utilizing enumerated feedback types. The class establishes relationships
46 with the `User` and `Book` models and supports lazy loading for optimized
47 database queries. It is mapped to the `feedback` table.
49 :ivar id: Unique identifier for the feedback.
50 :type id: int
51 :ivar user_id: Identifier of the user providing this feedback.
52 :type user_id: int
53 :ivar book_id: Identifier of the book this feedback pertains to.
54 :type book_id: int
55 :ivar feedback: The feedback provided by the user, represented as an
56 enumeration value.
57 :type feedback: FeedbackEnum
58 :ivar book: The book entity associated with this feedback, enabling
59 a joined relationship for efficient access.
60 :type book: Book
61 """
62 __tablename__ = "feedback"
63 id: Mapped[int] = mapped_column(primary_key=True)
64 user_id: Mapped[int] = mapped_column(ForeignKey("user.id"))
65 book_id: Mapped[int] = mapped_column(ForeignKey("books.id"))
66 feedback: Mapped[FeedbackEnum] = mapped_column(Enum(FeedbackEnum))
67 book: Mapped["Book"] = relationship(back_populates="feedbacks", lazy="joined")
69 @declared_attr
70 def user(self) -> Mapped["User"]:
71 """
72 Provides a relationship attribute mapping the feedback entity to the User entity
73 to establish a bidirectional association. The relationship is eager-loaded with
74 a joined strategy.
76 :return: A mapped relationship to the User entity.
77 :rtype: Mapped["User"]
78 """
79 return relationship("User", back_populates="feedbacks", lazy="joined")