From 8839733c0054d232ae3f4e19593f71f8f4ddfd41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Ja=C5=A1ek?= Date: Thu, 15 Feb 2024 09:59:56 +0100 Subject: [PATCH] reformat server code using black (#200) * add black * update github actions * fix typo --- .github/workflows/tests.yml | 14 +- .../fix_events_moment_timezone_2023.py | 10 +- server/cp/ingest/parser/ap.py | 43 +++-- server/cp/ingest/parser/businesswire.py | 1 - server/cp/ingest/parser/cp_onclusive.py | 7 +- server/cp/ingest/parser/globenewswire.py | 1 - server/cp/macros/auto_routing.py | 2 +- .../cp/macros/translate_to_desk_language.py | 2 +- server/cp/orangelogic.py | 1 - server/cp/planning_exports/__init__.py | 4 +- server/cp/planning_exports/common.py | 57 ++++-- .../french_news_events_list.py | 32 ++-- server/cp/planning_exports/news_event_list.py | 6 +- server/cp/usage_metrics.py | 1 - server/dev-requirements.txt | 3 +- server/prod_api_app.py | 4 +- server/settings.py | 8 +- server/tests/ingest/parser/__init__.py | 1 - server/tests/ingest/parser/ap_test.py | 52 +++-- .../tests/ingest/parser/businesswire_test.py | 1 - .../tests/ingest/parser/cp_onclusive_test.py | 5 +- server/tests/ingest/parser/cp_transcripts.py | 10 +- .../tests/ingest/parser/globenewswire_test.py | 1 - server/tests/io/ap2jimi_test.py | 1 - .../update_translation_metadata_macro_test.py | 178 +++++++++--------- server/tests/orangelogic_test.py | 1 - server/tests/output/formatter/__init__.py | 1 - server/tests/output/formatter/jimi_test.py | 13 +- .../tests/output/formatter/newsmlg2_test.py | 1 - server/tests/output/publish_service_test.py | 1 - 30 files changed, 251 insertions(+), 211 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c85ff7ca..1cb31f57 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -14,9 +14,9 @@ jobs: working-directory: server steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - - uses: actions/setup-python@v4 + - uses: actions/setup-python@v5 with: python-version: '3.8' @@ -34,7 +34,9 @@ jobs: - run: mypy . - - run: nosetests tests + - run: black --check . + + - run: pytest tests - run: python manage.py app:initialize_data @@ -48,12 +50,12 @@ jobs: working-directory: client steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - - uses: actions/setup-node@v3 + - uses: actions/setup-node@v4 with: node-version: '14.x' - - run: npm install --no-audit + - run: npm ci - run: npm run build diff --git a/server/cp/commands/fix_events_moment_timezone_2023.py b/server/cp/commands/fix_events_moment_timezone_2023.py index 687cbd00..bf75a777 100644 --- a/server/cp/commands/fix_events_moment_timezone_2023.py +++ b/server/cp/commands/fix_events_moment_timezone_2023.py @@ -48,10 +48,12 @@ def run(self, start, end, offset): }, ], "ingest_provider": {"$exists": 0}, # ignore ingested events - "dates.tz": {"$nin": [ - "America/Regina", - "America/Whitehorse", - ]}, + "dates.tz": { + "$nin": [ + "America/Regina", + "America/Whitehorse", + ] + }, } updated_count = 0 diff --git a/server/cp/ingest/parser/ap.py b/server/cp/ingest/parser/ap.py index 64ed5622..ed1e466c 100644 --- a/server/cp/ingest/parser/ap.py +++ b/server/cp/ingest/parser/ap.py @@ -293,8 +293,13 @@ def parse(self, data, provider=None): if item["type"] == "text": try: - prev_item = superdesk.get_resource_service("archive").find_one(req=None, ingest_id=item["guid"]) - if prev_item is not None and prev_item["extra"]["ap_version"] != ap_item["version"]: + prev_item = superdesk.get_resource_service("archive").find_one( + req=None, ingest_id=item["guid"] + ) + if ( + prev_item is not None + and prev_item["extra"]["ap_version"] != ap_item["version"] + ): item["rewrite_of"] = prev_item["guid"] except KeyError: pass @@ -518,9 +523,13 @@ def _parse_subject(self, subject, item): if subj.get("ap_subject"): codes = [code.strip() for code in subj["ap_subject"].split(",")] for ap_subj in subject: - if ap_subj.get('creator') == 'Editorial' and any( - [code for code in codes if ap_subj["code"].startswith(code)] - ) and subj["qcode"] not in added: + if ( + ap_subj.get("creator") == "Editorial" + and any( + [code for code in codes if ap_subj["code"].startswith(code)] + ) + and subj["qcode"] not in added + ): added.add(subj["qcode"]) item["subject"].append( { @@ -735,7 +744,7 @@ def set_cat(cat): "name": cat["name"], "qcode": cat["qcode"], "scheme": CATEGORY_SCHEME, - "translations": cat.get("translations") + "translations": cat.get("translations"), } ) @@ -860,14 +869,16 @@ def _parse_ap_products(self, data, item): products = data["meta"]["products"] except KeyError: return - item.setdefault("subject", []).extend([ - { - "name": p["name"], - "qcode": str(p["id"]), - "scheme": cp.AP_PRODUCT, - } - for p in products - ]) + item.setdefault("subject", []).extend( + [ + { + "name": p["name"], + "qcode": str(p["id"]), + "scheme": cp.AP_PRODUCT, + } + for p in products + ] + ) def categorisation_mapping(self, in_item, item): """Avoid extra mapping.""" @@ -900,8 +911,8 @@ def clean_html(html): for elem in root.iter(): elem.attrib.pop("id", None) elem.attrib.pop("class", None) - if elem.tag in ('hl2', 'pre', 'note'): - elem.tag = 'p' + if elem.tag in ("hl2", "pre", "note"): + elem.tag = "p" root = cleaner.clean_html(root) return sd_etree.to_string(root, method="html") diff --git a/server/cp/ingest/parser/businesswire.py b/server/cp/ingest/parser/businesswire.py index 0343261c..022105df 100644 --- a/server/cp/ingest/parser/businesswire.py +++ b/server/cp/ingest/parser/businesswire.py @@ -9,7 +9,6 @@ class BusinessWireParser(NewsMLOneFeedParser): - label = "BusinessWire" NAME = label.lower() diff --git a/server/cp/ingest/parser/cp_onclusive.py b/server/cp/ingest/parser/cp_onclusive.py index 882135b8..89eb2ffb 100644 --- a/server/cp/ingest/parser/cp_onclusive.py +++ b/server/cp/ingest/parser/cp_onclusive.py @@ -121,7 +121,12 @@ def find_subject(self, subjects, name): if ( subject.get("translations") and subject["translations"].get("name") - and name.lower() in [value.lower() for value in subject["translations"]["name"].values() if value] + and name.lower() + in [ + value.lower() + for value in subject["translations"]["name"].values() + if value + ] ): return subject if subject["name"].lower() == name.lower(): diff --git a/server/cp/ingest/parser/globenewswire.py b/server/cp/ingest/parser/globenewswire.py index a8311762..7a191046 100644 --- a/server/cp/ingest/parser/globenewswire.py +++ b/server/cp/ingest/parser/globenewswire.py @@ -29,7 +29,6 @@ class GlobeNewswireParser(NewsMLTwoFeedParser): - NAME = "globenewswire" label = SOURCE ALLOWED_EXT = {".newsml", ".xml"} diff --git a/server/cp/macros/auto_routing.py b/server/cp/macros/auto_routing.py index c7de24e4..36202a56 100644 --- a/server/cp/macros/auto_routing.py +++ b/server/cp/macros/auto_routing.py @@ -30,7 +30,7 @@ def find_name_item(cv_id, name): def callback(item, **kwargs): - """ This macro will set the language of the articles to the Desk language. """ + """This macro will set the language of the articles to the Desk language.""" rule = kwargs.get("rule") item["profile"] = "autorouting" if rule and ":" in rule["name"]: diff --git a/server/cp/macros/translate_to_desk_language.py b/server/cp/macros/translate_to_desk_language.py index fcabf81c..770c8be9 100644 --- a/server/cp/macros/translate_to_desk_language.py +++ b/server/cp/macros/translate_to_desk_language.py @@ -17,7 +17,7 @@ def translate_to_desk_language(item, **kwargs): - """ This macro will set the language of the articles to the Desk language. """ + """This macro will set the language of the articles to the Desk language.""" dest_desk = kwargs.get("dest_desk_id") diff --git a/server/cp/orangelogic.py b/server/cp/orangelogic.py index 91679918..d66162a0 100644 --- a/server/cp/orangelogic.py +++ b/server/cp/orangelogic.py @@ -67,7 +67,6 @@ def count(self, **kwargs): class OrangelogicSearchProvider(SearchProvider): - label = "Orange Logic" TZ = "America/Toronto" diff --git a/server/cp/planning_exports/__init__.py b/server/cp/planning_exports/__init__.py index e4d71943..6e6bf00b 100644 --- a/server/cp/planning_exports/__init__.py +++ b/server/cp/planning_exports/__init__.py @@ -4,4 +4,6 @@ def init_app(app): app.jinja_env.globals.update(group_items_by_state=group_items_by_state) - app.jinja_env.globals.update(group_items_by_french_topics=group_items_by_french_topics) + app.jinja_env.globals.update( + group_items_by_french_topics=group_items_by_french_topics + ) diff --git a/server/cp/planning_exports/common.py b/server/cp/planning_exports/common.py index f094bd97..a77b216b 100644 --- a/server/cp/planning_exports/common.py +++ b/server/cp/planning_exports/common.py @@ -26,9 +26,13 @@ def set_item_title(item, event): Prioritise the Event's slugline/name before Planning item's """ - item["title"] = event.get("name") or item.get("name") or \ - event.get("slugline") or item.get("slugline") or \ - "" + item["title"] = ( + event.get("name") + or item.get("name") + or event.get("slugline") + or item.get("slugline") + or "" + ) def set_item_description(item, event): @@ -38,18 +42,18 @@ def set_item_description(item, event): """ description = ( - event.get("definition_long") or - item.get("definition_long") or - event.get("definition_short") or - item.get("definition_short") or - item.get("description_text") or - "" + event.get("definition_long") + or item.get("definition_long") + or event.get("definition_short") + or item.get("definition_short") + or item.get("description_text") + or "" ).rstrip() short_description = ( - event.get("definition_short") or - item.get("definition_short") or - item.get("description_text") or - "" + event.get("definition_short") + or item.get("definition_short") + or item.get("description_text") + or "" ).rstrip() if description: @@ -73,7 +77,7 @@ def set_item_dates(item, event): else: item["dates"] = { "start": item["planning_date"], - "tz": app.config["DEFAULT_TIMEZONE"] + "tz": app.config["DEFAULT_TIMEZONE"], } # Construct the date string here so we don't have to use @@ -81,7 +85,9 @@ def set_item_dates(item, event): tz = item["dates"].get("tz") or app.config["DEFAULT_TIMEZONE"] start_local = utc_to_local(tz, item["dates"]["start"]) start_local_str = start_local.strftime("%I:%M %P") - end_local = utc_to_local(tz, item["dates"]["end"]) if item["dates"].get("end") else None + end_local = ( + utc_to_local(tz, item["dates"]["end"]) if item["dates"].get("end") else None + ) end_local_str = end_local.strftime("%I:%M %P") if end_local else None tz_name = start_local.tzname() @@ -117,7 +123,12 @@ def set_item_location(item, event): try: address_qcode = (item["location"][0] or {}).get("qcode") if address_qcode: - address_item = get_resource_service("locations").find_one(req=None, guid=address_qcode) or {} + address_item = ( + get_resource_service("locations").find_one( + req=None, guid=address_qcode + ) + or {} + ) address = address_item.get("address") or {} try: @@ -127,13 +138,17 @@ def set_item_location(item, event): item["address"] = { "country": address["country"] if address.get("country") else None, - "locality": address["locality"] if address.get("locality") else None, + "locality": address["locality"] + if address.get("locality") + else None, "city": address["city"] if address.get("city") else "", "state": address["state"] if address.get("state") else None, "name": address.get("city") or address_item.get("name") or "", - "full": address_item.get("unique_name") or address_item.get("formatted_address") or "", + "full": address_item.get("unique_name") + or address_item.get("formatted_address") + or "", "title": address_item.get("name") or "", - "address": address_line + "address": address_line, } except (IndexError, KeyError): pass @@ -143,7 +158,9 @@ def set_item_location(item, event): item["address"]["name"] = item["address"]["name"].upper() if item["address"]["title"] and item["address"]["address"]: - item["address"]["short"] = item["address"]["title"] + ", " + item["address"]["address"] + item["address"]["short"] = ( + item["address"]["title"] + ", " + item["address"]["address"] + ) else: item["address"]["short"] = item["address"]["full"] diff --git a/server/cp/planning_exports/french_news_events_list.py b/server/cp/planning_exports/french_news_events_list.py index 87c8732e..02445b5d 100644 --- a/server/cp/planning_exports/french_news_events_list.py +++ b/server/cp/planning_exports/french_news_events_list.py @@ -29,7 +29,9 @@ def get_french_name(item) -> str: - return ((item.get("translations") or {}).get("name") or {}).get("fr-CA") or item.get("name") + return ((item.get("translations") or {}).get("name") or {}).get( + "fr-CA" + ) or item.get("name") def set_item_group(item): @@ -37,16 +39,10 @@ def set_item_group(item): qcodes = [] if len(item.get("calendars") or []): - qcodes = [ - calendar.get("qcode") - for calendar in item["calendars"] - ] + qcodes = [calendar.get("qcode") for calendar in item["calendars"]] elif len(item.get("agendas") or []): # Agendas are converted from `_id` to Agenda item in `planning_article_export` endpoint - qcodes = [ - agenda.get("_id") - for agenda in item["agendas"] - ] + qcodes = [agenda.get("_id") for agenda in item["agendas"]] if not len(qcodes): return @@ -112,11 +108,17 @@ def group_items_by_french_topics(items): item_translations = { "calendars": { calendar.get("qcode"): get_french_name(calendar).upper() - for calendar in vocabs_service.find_one(req=None, _id="event_calendars").get("items") or [] + for calendar in vocabs_service.find_one( + req=None, _id="event_calendars" + ).get("items") + or [] }, "coverage_types": { coverage_type.get("name"): get_french_name(coverage_type).lower() - for coverage_type in vocabs_service.find_one(req=None, _id="g2_content_type").get("items") or [] + for coverage_type in vocabs_service.find_one( + req=None, _id="g2_content_type" + ).get("items") + or [] }, } @@ -140,7 +142,9 @@ def group_items_by_french_topics(items): date_groups[local_date_str] = { # Format name for heading, in Canadian French i.e. # jeudi 24 juin - "name": format_date(item["local_date"], "EEEE d MMMM", locale="fr_CA").capitalize(), + "name": format_date( + item["local_date"], "EEEE d MMMM", locale="fr_CA" + ).capitalize(), "groups": { item_translations["calendars"].get(qcode): [] for qcode in GROUP_QCODE_ORDER @@ -151,6 +155,8 @@ def group_items_by_french_topics(items): date_groups[local_date_str]["groups"][group_name].append(item) if not date_groups: - raise SuperdeskApiError.badRequestError(_("No items matched the required calendar/agenda")) + raise SuperdeskApiError.badRequestError( + _("No items matched the required calendar/agenda") + ) return date_groups.items() diff --git a/server/cp/planning_exports/news_event_list.py b/server/cp/planning_exports/news_event_list.py index 02210596..38daae7b 100644 --- a/server/cp/planning_exports/news_event_list.py +++ b/server/cp/planning_exports/news_event_list.py @@ -56,7 +56,7 @@ class SortGroups(NamedTuple): "nunavut", "northwest territories", "yukon", - ] + ], } @@ -125,8 +125,8 @@ def group_items_by_state(items): SORT_GROUPS.PRAIRIES: [], SORT_GROUPS.BRITISH_COLUMBIA: [], SORT_GROUPS.NORTH: [], - SORT_GROUPS.UNDATED: [] - } + SORT_GROUPS.UNDATED: [], + }, } date_groups[local_date_str]["groups"][item["group"]].append(item) diff --git a/server/cp/usage_metrics.py b/server/cp/usage_metrics.py index 13cad681..3773976f 100644 --- a/server/cp/usage_metrics.py +++ b/server/cp/usage_metrics.py @@ -31,7 +31,6 @@ class UsageResource(superdesk.Resource): class UsageService(superdesk.Service): - def on_fetched(self, doc): super().on_fetched(doc) diff --git a/server/dev-requirements.txt b/server/dev-requirements.txt index 6b9af0c7..f9bed9fa 100644 --- a/server/dev-requirements.txt +++ b/server/dev-requirements.txt @@ -1,13 +1,14 @@ -r requirements.txt +pep8 pytest flake8 -pep8 nose mypy<=1.8.0 httmock==1.4.0 wooper==0.4.4 requests_mock +black~=23.0 types-python-dateutil types-pytz diff --git a/server/prod_api_app.py b/server/prod_api_app.py index 61ae9007..7ad299d7 100644 --- a/server/prod_api_app.py +++ b/server/prod_api_app.py @@ -10,4 +10,6 @@ # fix pagination limit application.config["PAGINATION_LIMIT"] = 200 -superdesk.register_resource("usage_metrics", UsageResource, UsageService, _app=application) +superdesk.register_resource( + "usage_metrics", UsageResource, UsageService, _app=application +) diff --git a/server/settings.py b/server/settings.py index 96ce80be..f169450b 100644 --- a/server/settings.py +++ b/server/settings.py @@ -229,13 +229,13 @@ SAML_BASE_PATH = env("SAML_PATH", os.path.join(ABS_PATH, "saml")) if SERVER_URL == "http://localhost:5000/api": SAML_PATH = os.path.join(SAML_BASE_PATH, "localhost") -elif 'scp-master' in SERVER_URL: +elif "scp-master" in SERVER_URL: SAML_PATH = os.path.join(SAML_BASE_PATH, "test") -elif 'cp-uat-api' in SERVER_URL: +elif "cp-uat-api" in SERVER_URL: SAML_PATH = os.path.join(SAML_BASE_PATH, "uat-old") -elif 'cp-uat' in SERVER_URL: +elif "cp-uat" in SERVER_URL: SAML_PATH = os.path.join(SAML_BASE_PATH, "uat") -elif 'cms-api' in SERVER_URL: +elif "cms-api" in SERVER_URL: SAML_PATH = os.path.join(SAML_BASE_PATH, "prod-old") else: SAML_PATH = os.path.join(SAML_BASE_PATH, "prod") diff --git a/server/tests/ingest/parser/__init__.py b/server/tests/ingest/parser/__init__.py index b8333cbc..fcf1ce55 100644 --- a/server/tests/ingest/parser/__init__.py +++ b/server/tests/ingest/parser/__init__.py @@ -16,7 +16,6 @@ def get_fixture_path(filename, provider): class ParserTestCase(unittest.TestCase): - parser: FeedParser provider: str diff --git a/server/tests/ingest/parser/ap_test.py b/server/tests/ingest/parser/ap_test.py index e29e39e5..ccd0dd85 100644 --- a/server/tests/ingest/parser/ap_test.py +++ b/server/tests/ingest/parser/ap_test.py @@ -33,7 +33,6 @@ class CP_AP_ParseTestCase(unittest.TestCase): - app = flask.Flask(__name__) app.locators = MagicMock() app.config.update({"AP_TAGS_MAPPING": settings.AP_TAGS_MAPPING}) @@ -91,11 +90,8 @@ def test_parse(self): "qcode": "w", "scheme": "categories", "translations": { - "name": { - "en-CA": "International", - "fr-CA": "International" - } - } + "name": {"en-CA": "International", "fr-CA": "International"} + }, }, item["anpa_category"], ) @@ -111,7 +107,9 @@ def test_parse(self): self.assertIn("APV", tags) self.assertIn("TSX", tags) - products = [s["qcode"] for s in item["subject"] if s.get("scheme") == cp.AP_PRODUCT] + products = [ + s["qcode"] for s in item["subject"] if s.get("scheme") == cp.AP_PRODUCT + ] self.assertEqual(6, len(products)) self.assertIn("33381", products) @@ -228,11 +226,8 @@ def test_category_politics_international(self): "qcode": "w", "scheme": CATEGORY_SCHEME, "translations": { - "name": { - "en-CA": "International", - "fr-CA": "International" - } - } + "name": {"en-CA": "International", "fr-CA": "International"} + }, } ], item["anpa_category"], @@ -252,11 +247,8 @@ def test_category_apv(self): "qcode": "w", "scheme": CATEGORY_SCHEME, "translations": { - "name": { - "en-CA": "International", - "fr-CA": "International" - } - } + "name": {"en-CA": "International", "fr-CA": "International"} + }, } ], item["anpa_category"], @@ -276,16 +268,15 @@ def test_category_tennis(self): "qcode": "r", "scheme": CATEGORY_SCHEME, "translations": { - "name": { - "en-CA": "Agate", - "fr-CA": "Statistiques" - } - } + "name": {"en-CA": "Agate", "fr-CA": "Statistiques"} + }, } ], item["anpa_category"], ) - self.assertEqual([], [s["name"] for s in item["subject"] if s.get("scheme") == AP_SUBJECT_CV]) + self.assertEqual( + [], [s["name"] for s in item["subject"] if s.get("scheme") == AP_SUBJECT_CV] + ) output = self.format(item) self.assertIn("Agate", output) self.assertIn("Agate", output) @@ -295,7 +286,9 @@ def test_ignore_slugline_to_subject_map(self): _data = json.load(fp) # Prefix slugline with `BC` so slugline -> subject mapping works # in this case, slugline -> "BC-TEN-" -> "15065000" - _data["data"]["item"]["slugline"] = "BC" + _data["data"]["item"]["slugline"][2:] + _data["data"]["item"]["slugline"] = ( + "BC" + _data["data"]["item"]["slugline"][2:] + ) with self.app.app_context(): with patch.dict(superdesk.resources, resources): @@ -308,16 +301,15 @@ def test_ignore_slugline_to_subject_map(self): "qcode": "r", "scheme": CATEGORY_SCHEME, "translations": { - "name": { - "en-CA": "Agate", - "fr-CA": "Statistiques" - } - } + "name": {"en-CA": "Agate", "fr-CA": "Statistiques"} + }, } ], item["anpa_category"], ) - self.assertEqual([], [s["name"] for s in item["subject"] if s.get("scheme") == AP_SUBJECT_CV]) + self.assertEqual( + [], [s["name"] for s in item["subject"] if s.get("scheme") == AP_SUBJECT_CV] + ) output = self.format(item) self.assertIn("Agate", output) diff --git a/server/tests/ingest/parser/businesswire_test.py b/server/tests/ingest/parser/businesswire_test.py index 534cd0ec..46179fc8 100644 --- a/server/tests/ingest/parser/businesswire_test.py +++ b/server/tests/ingest/parser/businesswire_test.py @@ -6,7 +6,6 @@ class BusinessWireTestCase(ParserTestCase): - parser = BusinessWireParser() provider = "businesswire" diff --git a/server/tests/ingest/parser/cp_onclusive_test.py b/server/tests/ingest/parser/cp_onclusive_test.py index 3e331a96..3cc64ed3 100644 --- a/server/tests/ingest/parser/cp_onclusive_test.py +++ b/server/tests/ingest/parser/cp_onclusive_test.py @@ -111,7 +111,10 @@ def test_content(self): "scheme": "subject_custom", }, ] - self.assertEqual(sorted(map(qcode, item["subject"])), sorted(map(qcode, expected_subjects))) + self.assertEqual( + sorted(map(qcode, item["subject"])), + sorted(map(qcode, expected_subjects)), + ) item["anpa_category"].sort(key=lambda i: i["name"]) expected_categories = [ { diff --git a/server/tests/ingest/parser/cp_transcripts.py b/server/tests/ingest/parser/cp_transcripts.py index 7a322cdd..2a8655b5 100644 --- a/server/tests/ingest/parser/cp_transcripts.py +++ b/server/tests/ingest/parser/cp_transcripts.py @@ -20,9 +20,15 @@ class CP_Transcripts_ParseTestCase(unittest.TestCase): def test_parse(self): with self.app.app_context(), patch.dict(superdesk.resources, resources): superdesk.resources["archive"].service.find_one.side_effect = [ - {"ingest_id": "d3c8487a-1757-4dde-8bb5-22ca166c1e67.0", "version": 0, "extra": {"ap_version": 999}}, + { + "ingest_id": "d3c8487a-1757-4dde-8bb5-22ca166c1e67.0", + "version": 0, + "extra": {"ap_version": 999}, + }, ] - items = parser.parse(get_fixture_path("cp_transcripts.json", "cp_transcripts"), provider) + items = parser.parse( + get_fixture_path("cp_transcripts.json", "cp_transcripts"), provider + ) superdesk.resources["archive"].service.find_one.side_effect = None item = items[0] diff --git a/server/tests/ingest/parser/globenewswire_test.py b/server/tests/ingest/parser/globenewswire_test.py index 5e9b1a5f..42fbe957 100644 --- a/server/tests/ingest/parser/globenewswire_test.py +++ b/server/tests/ingest/parser/globenewswire_test.py @@ -11,7 +11,6 @@ class GlobeNewswireParserTestCase(ParserTestCase): - parser = GlobeNewswireParser() provider = "globenewswire" diff --git a/server/tests/io/ap2jimi_test.py b/server/tests/io/ap2jimi_test.py index 820e8d53..915d1807 100644 --- a/server/tests/io/ap2jimi_test.py +++ b/server/tests/io/ap2jimi_test.py @@ -31,7 +31,6 @@ def fixture(filename): class AP2JimiTestCase(unittest.TestCase): - app = flask.Flask(__name__) app.locators = MagicMock() app.config.update({"AP_TAGS_MAPPING": settings.AP_TAGS_MAPPING}) diff --git a/server/tests/macros/update_translation_metadata_macro_test.py b/server/tests/macros/update_translation_metadata_macro_test.py index ed0a5e28..5877b82b 100644 --- a/server/tests/macros/update_translation_metadata_macro_test.py +++ b/server/tests/macros/update_translation_metadata_macro_test.py @@ -5,7 +5,9 @@ from unittest.mock import patch from tests.mock import resources -from cp.macros.update_translation_metadata_macro import update_translation_metadata_macro as macro +from cp.macros.update_translation_metadata_macro import ( + update_translation_metadata_macro as macro, +) import pytz import settings @@ -24,67 +26,67 @@ def test_remove_destination_and_add_presse_canadienne_staff_as_destination(self) """ item = { - '_id': 'urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564', - 'guid': 'urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564', - 'headline': 'test headline', - 'slugine': 'test slugline', - 'state': 'in_progress', - 'type': 'text', - 'language': 'en', - 'anpa_take_key': 'update', - 'subject': [{ - 'name': 'Command News', - 'qcode': 'CMPD1', - 'scheme': 'destinations' - }] + "_id": "urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564", + "guid": "urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564", + "headline": "test headline", + "slugine": "test slugline", + "state": "in_progress", + "type": "text", + "language": "en", + "anpa_take_key": "update", + "subject": [ + {"name": "Command News", "qcode": "CMPD1", "scheme": "destinations"} + ], } with self.app.app_context(): with patch.dict(superdesk.resources, resources): macro(item) - self.assertIn('subject', item) + self.assertIn("subject", item) self.assertIn( { - 'name': 'Presse Canadienne staff', - 'qcode': 'sfstf', - 'scheme': 'destinations', + "name": "Presse Canadienne staff", + "qcode": "sfstf", + "scheme": "destinations", }, - item['subject'], + item["subject"], ) - self.assertEqual(item.get('anpa_take_key'), '') + self.assertEqual(item.get("anpa_take_key"), "") def test_override_destination_canadian_press_staff_to_presse_canadienne_staff(self): """ If Canadian Press Staff destination is present override it with Presse Canadienne staff """ item = { - '_id': 'urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564', - 'guid': 'urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564', - 'headline': 'test headline', - 'slugine': 'test slugline', - 'state': 'in_progress', - 'type': 'text', - 'language': 'en', - 'subject': [{ - 'name': 'Canadian Press Staff', - 'qcode': 'cpstf', - 'scheme': 'destinations' - }] + "_id": "urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564", + "guid": "urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564", + "headline": "test headline", + "slugine": "test slugline", + "state": "in_progress", + "type": "text", + "language": "en", + "subject": [ + { + "name": "Canadian Press Staff", + "qcode": "cpstf", + "scheme": "destinations", + } + ], } with self.app.app_context(): with patch.dict(superdesk.resources, resources): macro(item) - self.assertIn('subject', item) + self.assertIn("subject", item) self.assertIn( { - 'name': 'Presse Canadienne staff', - 'qcode': 'sfstf', - 'scheme': 'destinations', + "name": "Presse Canadienne staff", + "qcode": "sfstf", + "scheme": "destinations", }, - item['subject'], + item["subject"], ) def test_override_destination_the_associated_press_to_l_associated_press(self): @@ -92,32 +94,34 @@ def test_override_destination_the_associated_press_to_l_associated_press(self): If The Associated Press destination is present override it with L'Associated Press """ item = { - '_id': 'urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564', - 'guid': 'urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564', - 'headline': 'test headline', - 'slugine': 'test slugline', - 'state': 'in_progress', - 'type': 'text', - 'language': 'en', - 'subject': [{ - 'name': 'The Associated Press', - 'qcode': 'ap---', - 'scheme': 'destinations' - }] + "_id": "urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564", + "guid": "urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564", + "headline": "test headline", + "slugine": "test slugline", + "state": "in_progress", + "type": "text", + "language": "en", + "subject": [ + { + "name": "The Associated Press", + "qcode": "ap---", + "scheme": "destinations", + } + ], } with self.app.app_context(): with patch.dict(superdesk.resources, resources): macro(item) - self.assertIn('subject', item) + self.assertIn("subject", item) self.assertIn( { - 'name': "L'Associated Press", - 'qcode': 'apfra', - 'scheme': 'destinations', + "name": "L'Associated Press", + "qcode": "apfra", + "scheme": "destinations", }, - item['subject'], + item["subject"], ) def test_destination_is_empty_add_presse_canadienne_staff(self): @@ -125,48 +129,49 @@ def test_destination_is_empty_add_presse_canadienne_staff(self): If the destination is empty add Presse Canadienne staff """ item = { - '_id': 'urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564', - 'guid': 'urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564', - 'headline': 'test headline', - 'slugine': 'test slugline', - 'state': 'in_progress', - 'type': 'text', - 'keywords': ['foo', 'bar'], - 'language': 'en' + "_id": "urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564", + "guid": "urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564", + "headline": "test headline", + "slugine": "test slugline", + "state": "in_progress", + "type": "text", + "keywords": ["foo", "bar"], + "language": "en", } with self.app.app_context(): with patch.dict(superdesk.resources, resources): macro(item) - self.assertIn('subject', item) + self.assertIn("subject", item) self.assertIn( { - 'name': 'Presse Canadienne staff', - 'qcode': 'sfstf', - 'scheme': 'destinations', + "name": "Presse Canadienne staff", + "qcode": "sfstf", + "scheme": "destinations", }, - item['subject'], + item["subject"], ) def test_dateline(self): - - self.app.config.update({ - "GEONAMES_SEARCH_STYLE": settings.GEONAMES_SEARCH_STYLE, - "GEONAMES_FEATURE_CLASSES": settings.GEONAMES_FEATURE_CLASSES, - "GEONAMES_USERNAME": settings.GEONAMES_USERNAME, - "GEONAMES_URL": default_settings.GEONAMES_URL, - "GEONAMES_TOKEN": default_settings.GEONAMES_TOKEN, - }) + self.app.config.update( + { + "GEONAMES_SEARCH_STYLE": settings.GEONAMES_SEARCH_STYLE, + "GEONAMES_FEATURE_CLASSES": settings.GEONAMES_FEATURE_CLASSES, + "GEONAMES_USERNAME": settings.GEONAMES_USERNAME, + "GEONAMES_URL": default_settings.GEONAMES_URL, + "GEONAMES_TOKEN": default_settings.GEONAMES_TOKEN, + } + ) item = { - '_id': 'urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564', - 'guid': 'urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564', - 'headline': 'test headline', - 'slugine': 'test slugline', - 'state': 'in_progress', - 'type': 'text', - 'dateline': { + "_id": "urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564", + "guid": "urn:newsml:localhost:5000:2019-12-10T14:43:46.224107:d13ac5ae-7f43-4b7f-89a5-2c6835389564", + "headline": "test headline", + "slugine": "test slugline", + "state": "in_progress", + "type": "text", + "dateline": { "date": datetime(2021, 7, 22, 00, 00, tzinfo=pytz.UTC), "text": "LONDON, Jun 22 testing source -", "source": "The Associated Press", @@ -179,12 +184,9 @@ def test_dateline(self): "country": "United Kingdom", "country_code": "GB", "dateline": "city", - "location": { - "lat": 51.50853, - "lon": -0.12574 - } - } - } + "location": {"lat": 51.50853, "lon": -0.12574}, + }, + }, } with self.app.app_context(): diff --git a/server/tests/orangelogic_test.py b/server/tests/orangelogic_test.py index 901dd60b..23a3ba89 100644 --- a/server/tests/orangelogic_test.py +++ b/server/tests/orangelogic_test.py @@ -58,7 +58,6 @@ def fetch_ok(url, request): class OrangelogicTestCase(unittest.TestCase): - provider = {"config": {"username": "foo", "password": "bar"}} def setUp(self): diff --git a/server/tests/output/formatter/__init__.py b/server/tests/output/formatter/__init__.py index 34eb0021..b0325f15 100644 --- a/server/tests/output/formatter/__init__.py +++ b/server/tests/output/formatter/__init__.py @@ -8,7 +8,6 @@ class BaseXmlFormatterTestCase(unittest.TestCase): - subscriber = {} formatter = None article = None diff --git a/server/tests/output/formatter/jimi_test.py b/server/tests/output/formatter/jimi_test.py index 6b6913c1..790bfc6d 100644 --- a/server/tests/output/formatter/jimi_test.py +++ b/server/tests/output/formatter/jimi_test.py @@ -14,7 +14,6 @@ class JimiFormatterTestCase(BaseXmlFormatterTestCase): - formatter = JimiFormatter() article = { "_id": "id", @@ -470,9 +469,11 @@ def test_writethru_keeps_newscompid(self): self.assertEqual("00000001", item.find("NewsCompID").text) def test_ap_update_keeps_newscomip(self): - resources["ingest"].service.find_one.side_effect = [{ - "unique_id": 1, - }] + resources["ingest"].service.find_one.side_effect = [ + { + "unique_id": 1, + } + ] item = self.format_item( { @@ -553,8 +554,8 @@ def test_ap_translated(self): item = self.format_item( { "language": "fr-CA", - "extra": {cp.ORIG_ID: 'a' * 32}, + "extra": {cp.ORIG_ID: "a" * 32}, } ) - self.assertEqual('a' * 30 + 'fa', item.find('SystemSlug').text) + self.assertEqual("a" * 30 + "fa", item.find("SystemSlug").text) diff --git a/server/tests/output/formatter/newsmlg2_test.py b/server/tests/output/formatter/newsmlg2_test.py index 959bbece..f5f0f4e7 100644 --- a/server/tests/output/formatter/newsmlg2_test.py +++ b/server/tests/output/formatter/newsmlg2_test.py @@ -11,7 +11,6 @@ class NewsmlG2TestCase(BaseXmlFormatterTestCase): - now = utcnow() formatter = CPNewsMLG2Formatter() article = { diff --git a/server/tests/output/publish_service_test.py b/server/tests/output/publish_service_test.py index a22cc2ca..bd36ffd0 100644 --- a/server/tests/output/publish_service_test.py +++ b/server/tests/output/publish_service_test.py @@ -13,7 +13,6 @@ class CPPublishServiceTestCase(unittest.TestCase): - formatter = JimiFormatter() def format_queue_item(self, item):