From 7f495bd1b6f3409e9aad5c913e5fb28f3c917da0 Mon Sep 17 00:00:00 2001 From: Uchechukwu Orji Date: Tue, 12 May 2026 10:34:32 +0100 Subject: [PATCH] clear collection titles before updating --- backend/src/cms_backend/db/title.py | 2 + backend/tests/db/test_title.py | 63 +++++++++++------------------ 2 files changed, 25 insertions(+), 40 deletions(-) diff --git a/backend/src/cms_backend/db/title.py b/backend/src/cms_backend/db/title.py index 205f25ec..b1bca56d 100644 --- a/backend/src/cms_backend/db/title.py +++ b/backend/src/cms_backend/db/title.py @@ -271,6 +271,8 @@ def update_title( for tc in title.collections: session.delete(tc) + title.collections.clear() + for entry in collection_titles: collection = get_collection_by_name( session, collection_name=entry.collection_name diff --git a/backend/tests/db/test_title.py b/backend/tests/db/test_title.py index b79be8e8..63744a93 100644 --- a/backend/tests/db/test_title.py +++ b/backend/tests/db/test_title.py @@ -4,7 +4,14 @@ from sqlalchemy import select from sqlalchemy.orm import Session as OrmSession -from cms_backend.db.models import Book, BookLocation, Collection, Event, Title +from cms_backend.db.models import ( + Book, + BookLocation, + Collection, + CollectionTitle, + Event, + Title, +) from cms_backend.db.title import get_title_by_name_or_none, get_titles, update_title from cms_backend.schemas.orms import BaseTitleCollectionSchema @@ -117,41 +124,16 @@ def test_update_title_name( def test_update_title_collection_titles( dbsession: OrmSession, create_title: Callable[..., Title], + create_book_location: Callable[..., BookLocation], + create_book: Callable[..., Book], create_collection: Callable[..., Collection], + create_collection_title: Callable[..., CollectionTitle], ): """Test updating a title's collection_titles""" - create_collection(name="wikipedia") - create_collection(name="gutenberg") - - title = create_title(name="wikipedia_en_test") - - update_title( - dbsession, - title_id=title.id, - maturity=None, - collection_titles=[ - BaseTitleCollectionSchema(collection_name="wikipedia", path="wikis"), - BaseTitleCollectionSchema(collection_name="gutenberg", path="books"), - ], - ) - - dbsession.refresh(title) - assert len(title.collections) == 2 - collection_names = {tc.collection.name for tc in title.collections} - assert collection_names == {"wikipedia", "gutenberg"} - - -def test_update_title_collection_titles_updates_prod_books( - dbsession: OrmSession, - create_title: Callable[..., Title], - create_collection: Callable[..., Collection], - create_book: Callable[..., Book], - create_book_location: Callable[..., BookLocation], -): - """Test that updating collection_titles updates locations for prod books""" - collection1 = create_collection(name="wikipedia") - + collection = create_collection(name="wikipedia") title = create_title(name="wikipedia_en_test") + create_collection_title(title, collection, path="wikis") + create_collection(name="gutenberg") # Create a book in prod book = create_book( @@ -163,29 +145,30 @@ def test_update_title_collection_titles_updates_prod_books( # Create current location for the book create_book_location( book=book, - warehouse_id=collection1.warehouse_id, + warehouse_id=collection.warehouse_id, path="old_path", filename="test.zim", status="current", ) dbsession.flush() - # Update collection_titles - create_collection(name="gutenberg") - update_title( dbsession, title_id=title.id, maturity=None, collection_titles=[ - BaseTitleCollectionSchema(collection_name="gutenberg", path="new_path"), + BaseTitleCollectionSchema(collection_name="wikipedia", path="wikis"), + BaseTitleCollectionSchema(collection_name="gutenberg", path="books"), ], ) + dbsession.refresh(title) + assert len(title.collections) == 2 + collection_names = {tc.collection.name for tc in title.collections} + assert collection_names == {"wikipedia", "gutenberg"} + dbsession.refresh(book) assert book.needs_file_operation is True target_locations = [loc for loc in book.locations if loc.status == "target"] - assert len(target_locations) == 1 - assert str(target_locations[0].path) == "new_path" - assert target_locations[0].filename == "test.zim" + assert len(target_locations) == 2