Skip to content
Merged
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
5 changes: 4 additions & 1 deletion scripts/collect_hashtags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sseclient import SSEClient as EventSource
import sys

from common import valid_hashtag, valid_edit, hashtag_match
from common import valid_hashtag, valid_edit, hashtag_match, is_canary
import db

import mwapi
Expand Down Expand Up @@ -151,6 +151,9 @@ def populate_media_information(change):
except ValueError:
continue

if is_canary(change):
continue

if "comment" in change:
hashtag_matches = hashtag_match(change["comment"])
if hashtag_matches and valid_edit(change):
Expand Down
5 changes: 5 additions & 0 deletions scripts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def valid_hashtag(hashtag):
return True


def is_canary(change):
# Artificial heartbeat events, which must be discarded.
return change.get("meta", {}).get("domain") == "canary"


def valid_edit(change):

# Exclude Wikidata for now, just far too much data
Expand Down
18 changes: 18 additions & 0 deletions scripts/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import unittest

from common import is_canary

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to do some research, but it looks like if we add an empty __init__.py to the scripts/ directory, it will then be treated as a Python package and Django will look for the tests. However because the tests are ran from the root directory, it will think common is a top-level package when it's only part of scripts. The fix there seems to be to add a parent-relative import:

Suggested change
from common import is_canary
from .common import is_canary

Then docker compose exec app python manage.py test (and CI) should run the test. I think however the other files in scripts/ need the same treatment to import locally, which doesn't look a huge change:

  • collect_hashtags.py – from common → from .common, import db → import .db
  • load_old_db.py – from common → from .common

Should we go ahead and try that?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note again I'm not very experienced with Python so this may not be the right way to go about it. If there's any disagreement, I'm happy to defer to @jsnshrmn and sort out running the tests in a separate PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The collector is started directly as python collect_hashtags.py, so Python doesn't treat it as part of a package, and from .common import ... only works inside a package so things would break if we did that :) getting the test working isn't a big change but might be worth following up with instead I think

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed; let's merge for now and consider this a coverage concern for future ci work.



class IsCanaryTest(unittest.TestCase):
def test_canary_event(self):
self.assertTrue(is_canary({"meta": {"domain": "canary"}}))

def test_normal_event(self):
self.assertFalse(is_canary({"meta": {"domain": "en.wikipedia.org"}}))

def test_missing_meta(self):
self.assertFalse(is_canary({}))


if __name__ == "__main__":
unittest.main()
Loading