Skip to content

Commit

Permalink
invlidate products cache when updating navigation products (#720)
Browse files Browse the repository at this point in the history
CPCN-469
  • Loading branch information
petrjasek authored Jan 4, 2024
1 parent db18f50 commit 24056ea
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions newsroom/navigations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from flask import jsonify, current_app as app
from flask_babel import gettext
from superdesk import get_resource_service
from superdesk.cache import cache

from newsroom.decorator import admin_only
from newsroom.navigations import blueprint
Expand Down Expand Up @@ -116,3 +117,4 @@ def add_remove_products_for_navigation(nav_id: ObjectId, product_ids: List[str])
db.update_one({"_id": product["_id"]}, {"$addToSet": {"navigations": nav_id}})
else:
db.update_one({"_id": product["_id"]}, {"$pull": {"navigations": nav_id}})
cache.clean(["products"])
41 changes: 41 additions & 0 deletions tests/core/test_navigations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from bson import ObjectId
from flask import json
from pytest import fixture
from newsroom.navigations.views import add_remove_products_for_navigation
from newsroom.products.products import get_products_by_navigation

from newsroom.tests.users import test_login_succeeds_for_admin # noqa
from newsroom.tests.fixtures import COMPANY_1_ID
Expand Down Expand Up @@ -230,3 +232,42 @@ def test_get_agenda_navigations_by_company_returns_ordered(client, app):
assert navigations[0].get("name") == "Uber"
navigations = get_navigations_by_company({"_id": COMPANY_1_ID}, "wire")
assert navigations[0].get("name") == "Sport"


def test_get_products_by_navigation_caching(app):
nav_id = ObjectId()
app.data.insert(
"navigations",
[
{
"_id": nav_id,
"name": "Uber",
"is_enabled": True,
"product_type": "agenda",
}
],
)

app.data.insert(
"products",
[
{
"_id": "p-2",
"name": "A News",
"navigations": [nav_id],
"description": "news product",
"is_enabled": True,
"product_type": "wire",
"query": "latest",
},
],
)

# using new context to avoid caching via flask.g
with app.app_context():
assert 1 == len(get_products_by_navigation([nav_id], "wire"))

add_remove_products_for_navigation(nav_id, [])

with app.app_context():
assert 0 == len(get_products_by_navigation([nav_id], "wire"))

0 comments on commit 24056ea

Please sign in to comment.