From ecde10fb9a9e3d69252057acf68a05203fc9a988 Mon Sep 17 00:00:00 2001 From: loki Date: Sun, 9 Nov 2025 20:59:35 +1000 Subject: [PATCH 1/3] backward sql --- migrations/versions/eead387d732e_backward.py | 88 ++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 migrations/versions/eead387d732e_backward.py diff --git a/migrations/versions/eead387d732e_backward.py b/migrations/versions/eead387d732e_backward.py new file mode 100644 index 0000000..2ee8d10 --- /dev/null +++ b/migrations/versions/eead387d732e_backward.py @@ -0,0 +1,88 @@ +"""empty message + +Revision ID: eead387d732e +Revises: f00000000002 +Create Date: 2025-11-09 20:49:44.655909 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'eead387d732e' +down_revision = 'f00000000002' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('categories', schema=None) as batch_op: + batch_op.alter_column('title', + existing_type=sa.VARCHAR(length=255), + nullable=True) + batch_op.alter_column('alias', + existing_type=sa.VARCHAR(length=255), + nullable=True) + + with op.batch_alter_table('posts', schema=None) as batch_op: + batch_op.alter_column('createdon', + existing_type=sa.DATETIME(), + nullable=True) + + with op.batch_alter_table('tags', schema=None) as batch_op: + batch_op.alter_column('title', + existing_type=sa.VARCHAR(length=255), + nullable=True) + batch_op.alter_column('alias', + existing_type=sa.VARCHAR(length=255), + nullable=True) + + with op.batch_alter_table('users', schema=None) as batch_op: + batch_op.alter_column('authenticated', + existing_type=sa.BOOLEAN(), + type_=sa.Integer(), + existing_nullable=True) + batch_op.alter_column('createdon', + existing_type=sa.DATETIME(), + nullable=True, + existing_server_default=sa.text('(CURRENT_TIMESTAMP)')) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('users', schema=None) as batch_op: + batch_op.alter_column('createdon', + existing_type=sa.DATETIME(), + nullable=False, + existing_server_default=sa.text('(CURRENT_TIMESTAMP)')) + batch_op.alter_column('authenticated', + existing_type=sa.Integer(), + type_=sa.BOOLEAN(), + existing_nullable=True) + + with op.batch_alter_table('tags', schema=None) as batch_op: + batch_op.alter_column('alias', + existing_type=sa.VARCHAR(length=255), + nullable=False) + batch_op.alter_column('title', + existing_type=sa.VARCHAR(length=255), + nullable=False) + + with op.batch_alter_table('posts', schema=None) as batch_op: + batch_op.alter_column('createdon', + existing_type=sa.DATETIME(), + nullable=False) + + with op.batch_alter_table('categories', schema=None) as batch_op: + batch_op.alter_column('alias', + existing_type=sa.VARCHAR(length=255), + nullable=False) + batch_op.alter_column('title', + existing_type=sa.VARCHAR(length=255), + nullable=False) + + # ### end Alembic commands ### From af5cb7ec1f697240031e2ae1be32a59ca7821016 Mon Sep 17 00:00:00 2001 From: loki Date: Sun, 9 Nov 2025 21:03:20 +1000 Subject: [PATCH 2/3] basic page in db model --- blog/category/models.py | 1 + blog/infrastructure/database.py | 3 +- .../versions/b7b6e253e6fb_category_page.py | 32 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/b7b6e253e6fb_category_page.py diff --git a/blog/category/models.py b/blog/category/models.py index 29d4649..ddac85b 100644 --- a/blog/category/models.py +++ b/blog/category/models.py @@ -20,6 +20,7 @@ class Category(db.Model): title: Mapped[str | None] alias: Mapped[str | None] template: Mapped[str | None] + page: Mapped[bool | None] posts: Mapped[list["Post"]] = relationship("Post", back_populates="category") diff --git a/blog/infrastructure/database.py b/blog/infrastructure/database.py index 5449b37..615bba9 100644 --- a/blog/infrastructure/database.py +++ b/blog/infrastructure/database.py @@ -5,7 +5,7 @@ is separated from domain models. """ -from sqlalchemy import Table, Column, Integer, String, Text, DateTime, ForeignKey +from sqlalchemy import Table, Column, Integer, String, Text, DateTime, ForeignKey, Boolean from sqlalchemy.sql.schema import MetaData @@ -47,6 +47,7 @@ def get_categories_table(metadata: MetaData) -> Table: Column("title", String(255)), Column("alias", String(255), unique=True), Column("template", String(255), nullable=True), + Column("page", Boolean(), nullable=True, default=False), extend_existing=True, ) diff --git a/migrations/versions/b7b6e253e6fb_category_page.py b/migrations/versions/b7b6e253e6fb_category_page.py new file mode 100644 index 0000000..cdecc5e --- /dev/null +++ b/migrations/versions/b7b6e253e6fb_category_page.py @@ -0,0 +1,32 @@ +"""empty message + +Revision ID: b7b6e253e6fb +Revises: eead387d732e +Create Date: 2025-11-09 20:58:03.081802 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'b7b6e253e6fb' +down_revision = 'eead387d732e' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('categories', schema=None) as batch_op: + batch_op.add_column(sa.Column('page', sa.Boolean(), nullable=True)) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('categories', schema=None) as batch_op: + batch_op.drop_column('page') + + # ### end Alembic commands ### From c5f3cceebed334070b99260c26b7e651c2e488e6 Mon Sep 17 00:00:00 2001 From: loki Date: Sun, 9 Nov 2025 23:32:12 +1000 Subject: [PATCH 3/3] fixed page category --- blog/__init__.py | 1 - blog/config.py | 7 - blog/config_validator.py | 13 +- blog/domain/category.py | 1 + blog/domain/post.py | 1 + blog/infrastructure/database.py | 11 +- blog/post/views.py | 14 +- blog/repos/category.py | 1 + blog/repos/post.py | 22 ++- blog/services/post.py | 4 +- config.example.sh | 5 +- .../versions/b7b6e253e6fb_category_page.py | 13 +- migrations/versions/eead387d732e_backward.py | 129 +++++++++--------- tests/test_admin.py | 1 - tests/test_config_validator.py | 36 ----- tests/test_integration.py | 4 +- tests/test_posts.py | 11 +- tests/test_repositories.py | 8 +- tests/test_services.py | 2 +- tests/test_views.py | 2 +- 20 files changed, 129 insertions(+), 157 deletions(-) diff --git a/blog/__init__.py b/blog/__init__.py index 0f540ca..da8b766 100644 --- a/blog/__init__.py +++ b/blog/__init__.py @@ -74,7 +74,6 @@ def create_app(init_admin: bool = False) -> Flask: configure_extensions(app) if init_admin or env != "testing": - print("init admin") create_admin(admin_ext) app.register_blueprint(post) app.register_blueprint(tags) diff --git a/blog/config.py b/blog/config.py index 6207d74..cfb5a4e 100644 --- a/blog/config.py +++ b/blog/config.py @@ -14,10 +14,6 @@ class Config(object): YANDEX_VERIFICATION: str | None = environ.get("YANDEX_VERIFICATION", None) YANDEX_METRIKA: str = environ.get("YANDEX_METRIKA", "76938046") - PAGE_CATEGORY: list[int] = [ - int(c) for c in environ.get("PAGE_CATEGORY", "0").split(",") - ] - class DevelopmentConfig(Config): DEBUG: bool = True @@ -30,9 +26,6 @@ class DevelopmentConfig(Config): class TestingConfig(Config): TESTING: bool = True SQLALCHEMY_DATABASE_URI: str = "sqlite:///:memory:" - PAGE_CATEGORY: list[int] = [ - 1, - ] class ProductionConfig(Config): diff --git a/blog/config_validator.py b/blog/config_validator.py index 2f58a9f..a7ebbf7 100644 --- a/blog/config_validator.py +++ b/blog/config_validator.py @@ -2,7 +2,7 @@ import logging import os -from typing import Any, cast +from typing import Any logger = logging.getLogger(__name__) @@ -61,17 +61,6 @@ def validate_config(config: dict[str, Any]) -> list[str]: # pyright: ignore[rep + "Supported schemes: sqlite://, postgresql://, mysql://, oracle://" ) - # Validate PAGE_CATEGORY - page_category = config.get("PAGE_CATEGORY") - print(page_category, type(page_category)) - if page_category is not None: - if not isinstance(page_category, list): - raise ConfigValidationError("PAGE_CATEGORY must be a list of integers") - for item in cast("list[Any]", page_category): # pyright: ignore[reportExplicitAny] - if not isinstance(item, int): - raise ConfigValidationError("PAGE_CATEGORY must contain only integers") - - # Validate PORT port = config.get("PORT") if port: try: diff --git a/blog/domain/category.py b/blog/domain/category.py index 5665aa2..09dee9f 100644 --- a/blog/domain/category.py +++ b/blog/domain/category.py @@ -12,6 +12,7 @@ class Category: title: str = "" alias: str = "" template: str | None = None + page: bool | None = False @override def __str__(self): diff --git a/blog/domain/post.py b/blog/domain/post.py index e4b436b..23dd087 100644 --- a/blog/domain/post.py +++ b/blog/domain/post.py @@ -19,6 +19,7 @@ class Post: createdon: datetime.datetime | None = None publishedon: datetime.datetime | None = None category_id: int | None = None + is_page: bool = False user_id: int | None = None def __post_init__(self): diff --git a/blog/infrastructure/database.py b/blog/infrastructure/database.py index 615bba9..4364258 100644 --- a/blog/infrastructure/database.py +++ b/blog/infrastructure/database.py @@ -5,7 +5,16 @@ is separated from domain models. """ -from sqlalchemy import Table, Column, Integer, String, Text, DateTime, ForeignKey, Boolean +from sqlalchemy import ( + Table, + Column, + Integer, + String, + Text, + DateTime, + ForeignKey, + Boolean, +) from sqlalchemy.sql.schema import MetaData diff --git a/blog/post/views.py b/blog/post/views.py index 268b493..8e75e9a 100644 --- a/blog/post/views.py +++ b/blog/post/views.py @@ -1,11 +1,10 @@ import datetime -from typing import cast, ParamSpec, TypeVar +from typing import ParamSpec, TypeVar import markdown from flask import ( Blueprint, Response, - current_app, jsonify, make_response, render_template, @@ -42,9 +41,8 @@ def posts(**kwargs: str) -> Response | str: @post.route("/hx/pages") @cache.cached(timeout=50) # pyright: ignore[reportUntypedFunctionDecorator] def pages_hx() -> Response | str: - page_category = cast("list[int]", current_app.config["PAGE_CATEGORY"]) post_service = ServiceFactory.create_post_service() - pages = post_service.get_page_posts(page_category) + pages = post_service.get_page_posts() return render_template("pages.htmx", pages=pages) @@ -69,11 +67,9 @@ def view(alias: str | None = None, **kwargs: str) -> Response | str: abort(404) # For page categories, we need to check if it's a page or a regular post - page_categories = cast("list[int]", current_app.config["PAGE_CATEGORY"]) - is_page = post.category_id is not None and post.category_id in page_categories is_published = post.publishedon is not None - if not (is_published or is_page): + if not (is_published or post.is_page): abort(404) # Get tags for the post @@ -91,10 +87,8 @@ def view(alias: str | None = None, **kwargs: str) -> Response | str: @flask_sitemap.register_generator def site_map_gen(): - page_category = cast("list[int]", current_app.config["PAGE_CATEGORY"]) - post_service = ServiceFactory.create_post_service() - pages = post_service.get_page_posts(page_category) + pages = post_service.get_page_posts() for page in pages: yield url_for("post.view", alias=page.alias) diff --git a/blog/repos/category.py b/blog/repos/category.py index f18dd60..9fc47c6 100644 --- a/blog/repos/category.py +++ b/blog/repos/category.py @@ -51,6 +51,7 @@ def create(self, entity: CategoryDomain) -> CategoryDomain: category_orm = CategoryORM() category_orm.title = entity.title category_orm.alias = entity.alias + category_orm.page = entity.page # Handle the case where template might be None if entity.template is not None: category_orm.template = entity.template diff --git a/blog/repos/post.py b/blog/repos/post.py index db2f0df..7fd97c7 100644 --- a/blog/repos/post.py +++ b/blog/repos/post.py @@ -3,6 +3,7 @@ import sqlalchemy as sa from typing import Any, override +from blog.category.models import Category as CategoryOrm from blog.extensions import db from blog.post.models import Post as PostORM from blog.tags.models import Tag as TagORM @@ -66,7 +67,11 @@ def get_by_id(self, id: int) -> PostDomain | None: return None def get_by_alias(self, alias: str) -> PostDomain | None: - stmt = sa.select(PostORM).where(PostORM.alias == alias) + stmt = ( + sa.select(PostORM) + .where(PostORM.alias == alias) + .join(CategoryOrm, PostORM.category_id == CategoryOrm.id, isouter=True) + ) post_orm = self.session.scalar(stmt) if post_orm: return self._to_domain_model(post_orm) @@ -94,15 +99,23 @@ def get_all_published_content(self) -> list[PostDomain]: """Get all published content including posts and pages.""" stmt = ( sa.select(PostORM) + .join(CategoryOrm, PostORM.category_id == CategoryOrm.id, isouter=True) .where(PostORM.publishedon.isnot(None)) - .where(PostORM.category_id.isnot(1)) + .where(CategoryOrm.page.isnot(True)) .order_by(PostORM.publishedon.desc()) ) posts_orm = list(self.session.scalars(stmt).all()) return [self._to_domain_model(post_orm) for post_orm in posts_orm] - def get_page_posts(self, page_category_ids: list[int]) -> list[PostDomain]: - stmt = sa.select(PostORM).where(PostORM.category_id.in_(page_category_ids)) + def get_page_posts(self) -> list[PostDomain]: + stmt = ( + sa.select(PostORM) + .join_from( + PostORM, + CategoryOrm, + ) + .where(CategoryOrm.page) # pyright: ignore[reportArgumentType] + ) posts_orm = list(self.session.scalars(stmt).all()) return [self._to_domain_model(post_orm) for post_orm in posts_orm] @@ -167,5 +180,6 @@ def _to_domain_model(self, post_orm: PostORM) -> PostDomain: createdon=post_orm.createdon, publishedon=post_orm.publishedon, category_id=post_orm.category_id, + is_page=bool(post_orm.category.page) if post_orm.category_id else False, user_id=post_orm.user_id, ) diff --git a/blog/services/post.py b/blog/services/post.py index 85fec2b..4c08093 100644 --- a/blog/services/post.py +++ b/blog/services/post.py @@ -52,8 +52,8 @@ def get_published_posts(self) -> list[Post]: def get_all_published_content(self) -> list[Post]: return self.post_repository.get_all_published_content() - def get_page_posts(self, page_category_ids: list[int]) -> list[Post]: - return self.post_repository.get_page_posts(page_category_ids) + def get_page_posts(self) -> list[Post]: + return self.post_repository.get_page_posts() def get_posts_by_tag(self, tag_id: int) -> list[Post]: """Get all posts associated with a specific tag.""" diff --git a/config.example.sh b/config.example.sh index 6a5b772..fbfe84b 100644 --- a/config.example.sh +++ b/config.example.sh @@ -29,9 +29,6 @@ export PORT="5555" # Yandex Metrika ID (optional) # export YANDEX_METRIKA="your-yandex-metrika-id" -# Page categories (comma-separated list of integers) -export PAGE_CATEGORY="0" - # Cache configuration (optional) # export CACHE_TYPE="SimpleCache" -# export CACHE_DEFAULT_TIMEOUT="300" \ No newline at end of file +# export CACHE_DEFAULT_TIMEOUT="300" diff --git a/migrations/versions/b7b6e253e6fb_category_page.py b/migrations/versions/b7b6e253e6fb_category_page.py index cdecc5e..dff1f72 100644 --- a/migrations/versions/b7b6e253e6fb_category_page.py +++ b/migrations/versions/b7b6e253e6fb_category_page.py @@ -5,28 +5,29 @@ Create Date: 2025-11-09 20:58:03.081802 """ + from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = 'b7b6e253e6fb' -down_revision = 'eead387d732e' +revision = "b7b6e253e6fb" +down_revision = "eead387d732e" branch_labels = None depends_on = None def upgrade(): # ### commands auto generated by Alembic - please adjust! ### - with op.batch_alter_table('categories', schema=None) as batch_op: - batch_op.add_column(sa.Column('page', sa.Boolean(), nullable=True)) + with op.batch_alter_table("categories", schema=None) as batch_op: + batch_op.add_column(sa.Column("page", sa.Boolean(), nullable=True)) # ### end Alembic commands ### def downgrade(): # ### commands auto generated by Alembic - please adjust! ### - with op.batch_alter_table('categories', schema=None) as batch_op: - batch_op.drop_column('page') + with op.batch_alter_table("categories", schema=None) as batch_op: + batch_op.drop_column("page") # ### end Alembic commands ### diff --git a/migrations/versions/eead387d732e_backward.py b/migrations/versions/eead387d732e_backward.py index 2ee8d10..2b66c5a 100644 --- a/migrations/versions/eead387d732e_backward.py +++ b/migrations/versions/eead387d732e_backward.py @@ -5,84 +5,89 @@ Create Date: 2025-11-09 20:49:44.655909 """ + from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = 'eead387d732e' -down_revision = 'f00000000002' +revision = "eead387d732e" +down_revision = "f00000000002" branch_labels = None depends_on = None def upgrade(): # ### commands auto generated by Alembic - please adjust! ### - with op.batch_alter_table('categories', schema=None) as batch_op: - batch_op.alter_column('title', - existing_type=sa.VARCHAR(length=255), - nullable=True) - batch_op.alter_column('alias', - existing_type=sa.VARCHAR(length=255), - nullable=True) - - with op.batch_alter_table('posts', schema=None) as batch_op: - batch_op.alter_column('createdon', - existing_type=sa.DATETIME(), - nullable=True) - - with op.batch_alter_table('tags', schema=None) as batch_op: - batch_op.alter_column('title', - existing_type=sa.VARCHAR(length=255), - nullable=True) - batch_op.alter_column('alias', - existing_type=sa.VARCHAR(length=255), - nullable=True) - - with op.batch_alter_table('users', schema=None) as batch_op: - batch_op.alter_column('authenticated', - existing_type=sa.BOOLEAN(), - type_=sa.Integer(), - existing_nullable=True) - batch_op.alter_column('createdon', - existing_type=sa.DATETIME(), - nullable=True, - existing_server_default=sa.text('(CURRENT_TIMESTAMP)')) + with op.batch_alter_table("categories", schema=None) as batch_op: + batch_op.alter_column( + "title", existing_type=sa.VARCHAR(length=255), nullable=True + ) + batch_op.alter_column( + "alias", existing_type=sa.VARCHAR(length=255), nullable=True + ) + + with op.batch_alter_table("posts", schema=None) as batch_op: + batch_op.alter_column("createdon", existing_type=sa.DATETIME(), nullable=True) + + with op.batch_alter_table("tags", schema=None) as batch_op: + batch_op.alter_column( + "title", existing_type=sa.VARCHAR(length=255), nullable=True + ) + batch_op.alter_column( + "alias", existing_type=sa.VARCHAR(length=255), nullable=True + ) + + with op.batch_alter_table("users", schema=None) as batch_op: + batch_op.alter_column( + "authenticated", + existing_type=sa.BOOLEAN(), + type_=sa.Integer(), + existing_nullable=True, + ) + batch_op.alter_column( + "createdon", + existing_type=sa.DATETIME(), + nullable=True, + existing_server_default=sa.text("(CURRENT_TIMESTAMP)"), + ) # ### end Alembic commands ### def downgrade(): # ### commands auto generated by Alembic - please adjust! ### - with op.batch_alter_table('users', schema=None) as batch_op: - batch_op.alter_column('createdon', - existing_type=sa.DATETIME(), - nullable=False, - existing_server_default=sa.text('(CURRENT_TIMESTAMP)')) - batch_op.alter_column('authenticated', - existing_type=sa.Integer(), - type_=sa.BOOLEAN(), - existing_nullable=True) - - with op.batch_alter_table('tags', schema=None) as batch_op: - batch_op.alter_column('alias', - existing_type=sa.VARCHAR(length=255), - nullable=False) - batch_op.alter_column('title', - existing_type=sa.VARCHAR(length=255), - nullable=False) - - with op.batch_alter_table('posts', schema=None) as batch_op: - batch_op.alter_column('createdon', - existing_type=sa.DATETIME(), - nullable=False) - - with op.batch_alter_table('categories', schema=None) as batch_op: - batch_op.alter_column('alias', - existing_type=sa.VARCHAR(length=255), - nullable=False) - batch_op.alter_column('title', - existing_type=sa.VARCHAR(length=255), - nullable=False) + with op.batch_alter_table("users", schema=None) as batch_op: + batch_op.alter_column( + "createdon", + existing_type=sa.DATETIME(), + nullable=False, + existing_server_default=sa.text("(CURRENT_TIMESTAMP)"), + ) + batch_op.alter_column( + "authenticated", + existing_type=sa.Integer(), + type_=sa.BOOLEAN(), + existing_nullable=True, + ) + + with op.batch_alter_table("tags", schema=None) as batch_op: + batch_op.alter_column( + "alias", existing_type=sa.VARCHAR(length=255), nullable=False + ) + batch_op.alter_column( + "title", existing_type=sa.VARCHAR(length=255), nullable=False + ) + + with op.batch_alter_table("posts", schema=None) as batch_op: + batch_op.alter_column("createdon", existing_type=sa.DATETIME(), nullable=False) + + with op.batch_alter_table("categories", schema=None) as batch_op: + batch_op.alter_column( + "alias", existing_type=sa.VARCHAR(length=255), nullable=False + ) + batch_op.alter_column( + "title", existing_type=sa.VARCHAR(length=255), nullable=False + ) # ### end Alembic commands ### diff --git a/tests/test_admin.py b/tests/test_admin.py index 99f56e6..b8df269 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -40,7 +40,6 @@ def temp_user(admin_app, auth_adapter, user_service): # Load the user using the adapter loaded_user = auth_adapter.load_user(created_user.id) - print(loaded_user, type(loaded_user)) assert loaded_user is not None return user_orm diff --git a/tests/test_config_validator.py b/tests/test_config_validator.py index e2bcfbf..c93fa4d 100644 --- a/tests/test_config_validator.py +++ b/tests/test_config_validator.py @@ -113,42 +113,6 @@ def test_validate_config_valid_port(): assert isinstance(warnings, list) -def test_validate_config_invalid_page_category(): - """Test that validate_config handles invalid PAGE_CATEGORY.""" - config = { - "SECRET_KEY": "a-valid-secret-key", - "PAGE_CATEGORY": "not-a-list", # Invalid type - } - - with pytest.raises(ConfigValidationError, match="PAGE_CATEGORY must be a list"): - validate_config(config) - - -def test_validate_config_invalid_page_category_items(): - """Test that validate_config handles invalid PAGE_CATEGORY items.""" - config = { - "SECRET_KEY": "a-valid-secret-key", - "PAGE_CATEGORY": ["not-an-int"], # Invalid items - } - - with pytest.raises( - ConfigValidationError, match="PAGE_CATEGORY must contain only integers" - ): - validate_config(config) - - -def test_validate_config_valid_page_category(): - """Test that validate_config accepts valid PAGE_CATEGORY.""" - config = { - "SECRET_KEY": "a-valid-secret-key", - "PAGE_CATEGORY": [1, 2, 3], - } - - # Should not raise an exception - warnings = validate_config(config) - assert isinstance(warnings, list) - - def test_validate_config_nullcache_in_production(): """Test that validate_config warns about NullCache in production.""" # Temporarily set environment diff --git a/tests/test_integration.py b/tests/test_integration.py index 84cd7e7..b916936 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -33,7 +33,7 @@ def integration_client(): created_user = user_service.create_user(user) # Create categories - page_category = CategoryDomain(title="page", alias="page") + page_category = CategoryDomain(title="page", alias="page", page=True) created_page_category = category_service.create_category(page_category) regular_category = CategoryDomain(title="tech", alias="tech") @@ -60,7 +60,7 @@ def integration_client(): ) created_post1 = post_service.create_post(post1) - # Page post + # Page post2 = PostDomain( pagetitle="Test Page", alias="test-page", diff --git a/tests/test_posts.py b/tests/test_posts.py index ef23f8d..3ed5430 100644 --- a/tests/test_posts.py +++ b/tests/test_posts.py @@ -15,7 +15,7 @@ def test_client(): with app.test_client() as client: with app.app_context(): db.create_all() - page_category = Category(id=1, title="page", alias="page") # type: ignore + page_category = Category(id=1, title="page", alias="page", page=True) # type: ignore db.session.add(page_category) db.session.commit() yield client @@ -32,8 +32,13 @@ def post_helper(prefix="post", page=False): # All posts should be published for tests post.publishedon = db.func.now() post.category_id = None + if page: - post.category_id = 1 + page_category_query = sa.select(Category).where( + Category.page, + ) + category = db.one_or_404(page_category_query) + post.category_id = category.id return post @@ -66,7 +71,6 @@ def test_post_index(test_client): db.session.commit() rv = test_client.get("/posts") assert rv.status == "200 OK" - print(rv.data) assert b"post_title" in rv.data @@ -77,7 +81,6 @@ def test_page_index(test_client): db.session.commit() rv = test_client.get("/hx/pages") assert rv.status == "200 OK" - print(rv.data) with test_client.application.app_context(): post_query = sa.select(Post).where( diff --git a/tests/test_repositories.py b/tests/test_repositories.py index b7e1da5..feeac65 100644 --- a/tests/test_repositories.py +++ b/tests/test_repositories.py @@ -262,7 +262,9 @@ def test_get_page_posts(self, app, post_repository): category_repository = CategoryRepository(db.session) category_domain = CategoryDomain( - title="Page Category", alias="page-category" + title="Page Category", + alias="page-category", + page=True, ) created_category = category_repository.create(category_domain) @@ -289,7 +291,7 @@ def test_get_page_posts(self, app, post_repository): post_repository.create(regular_post_domain) # Get page posts - page_posts = post_repository.get_page_posts([created_category.id]) + page_posts = post_repository.get_page_posts() # Verify we only got the page post assert len(page_posts) >= 1 @@ -305,7 +307,7 @@ def test_get_page_posts_empty_list(self, app, post_repository): """Test getting page posts with an empty category list.""" with app.app_context(): # Get page posts with an empty category list - page_posts = post_repository.get_page_posts([]) + page_posts = post_repository.get_page_posts() # Verify an empty list is returned assert page_posts == [] diff --git a/tests/test_services.py b/tests/test_services.py index 701ef3a..d5280d2 100644 --- a/tests/test_services.py +++ b/tests/test_services.py @@ -241,7 +241,7 @@ def test_get_tags_for_post_nonexistent(self, app, post_service): def test_get_page_posts_empty_list(self, app, post_service): """Test getting page posts with an empty category list.""" # Try to get page posts with an empty category list - posts = post_service.get_page_posts([]) + posts = post_service.get_page_posts() # Verify an empty list is returned assert posts == [] diff --git a/tests/test_views.py b/tests/test_views.py index 75a9f61..55921bd 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -22,7 +22,7 @@ def test_client(): db.create_all() # Create a page category for testing category_service = ServiceFactory.create_category_service() - page_category = CategoryDomain(id=1, title="page", alias="page") + page_category = CategoryDomain(id=1, title="page", alias="page", page=True) category_service.create_category(page_category) yield client with app.app_context():