Skip to content
Open
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
2 changes: 2 additions & 0 deletions backend/src/cms_backend/db/title.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 23 additions & 40 deletions backend/tests/db/test_title.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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
Loading