Skip to content

Commit

Permalink
add -br suffix to broadcast item guid (#219)
Browse files Browse the repository at this point in the history
so those are different than on print versions of the same story

SDCP-747
  • Loading branch information
petrjasek authored Mar 20, 2024
1 parent 695d663 commit f8e003e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
7 changes: 7 additions & 0 deletions server/cp/output/formatter/cp_ninjs_newsroom_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

from superdesk.publish.formatters import NewsroomNinjsFormatter

from cp import is_broadcast


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -118,5 +120,10 @@ def update_ninjs_subjects(self, ninjs, language="en-CA"):

def _transform_to_ninjs(self, article, subscriber, recursive=True):
ninjs = super()._transform_to_ninjs(article, subscriber, recursive)

self.update_ninjs_subjects(ninjs, "en-CA")

if is_broadcast(article) and ninjs["guid"] == article.get("ingest_id"):
ninjs["guid"] = ninjs["guid"] + "-br"

return ninjs
70 changes: 70 additions & 0 deletions server/tests/output/formatter/cp_ninjs_newsroom_formatter_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import cp
import flask
import unittest

from unittest.mock import patch

from cp.output.formatter.cp_ninjs_newsroom_formatter import CPNewsroomNinjsFormatter


class TestCPNewsroomNinjsFormatter(unittest.TestCase):
def setUp(self) -> None:
self.formatter = CPNewsroomNinjsFormatter()
self.app = flask.Flask(__name__)
self.ctx = self.app.test_request_context()
self.ctx.push()

def tearDown(self) -> None:
self.ctx.pop()

@patch("superdesk.get_resource_service")
def test_transform_to_ninjs(self, mock_get_resource_service):
# Create a sample article and subscriber
article = {"ingest_id": "123", "type": "text", "auto_publish": True}
subscriber = {
# Add necessary fields for the test
}

# Call the _transform_to_ninjs method
result = self.formatter._transform_to_ninjs(article, subscriber)

# Assert that the result is as expected
self.assertEqual(result["guid"], "123")

# Add more assertions for other fields if needed

@patch("superdesk.get_resource_service")
def test_transform_to_ninjs_with_broadcast(self, mock_get_resource_service):
# Create a sample article and subscriber
article = {
"ingest_id": "123",
"type": "text",
"auto_publish": True,
"subject": [
{"name": "Broadcast", "qcode": cp.BROADCAST, "scheme": cp.DISTRIBUTION},
],
}
subscriber = {
# Add necessary fields for the test
}

# Call the _transform_to_ninjs method with is_broadcast=True
result = self.formatter._transform_to_ninjs(article, subscriber, recursive=True)

# Assert that the result is as expected
self.assertEqual(result["guid"], "123-br")

@patch("superdesk.get_resource_service")
def test_update_ninjs_subjects_exception(self, mock_get_resource_service):
mock_get_resource_service.side_effect = Exception("Test exception")

ninjs = {"subject": [{"name": "test", "scheme": "subject"}]}
with self.assertLogs(
"cp.output.formatter.cp_ninjs_newsroom_formatter", level="ERROR"
) as cm:
self.formatter.update_ninjs_subjects(ninjs, "en-CA")

self.assertIn(
"An error occurred. We are in CP NewsRoom Ninjs Formatter Ninjs Subjects exception: Test exception",
cm.output[0],
)

0 comments on commit f8e003e

Please sign in to comment.