Changeset View
Changeset View
Standalone View
Standalone View
swh/counters/tests/test_journal_client.py
| Show All 17 Lines | from swh.model.model import ( | ||||
| ObjectType, | ObjectType, | ||||
| Person, | Person, | ||||
| Release, | Release, | ||||
| Revision, | Revision, | ||||
| RevisionType, | RevisionType, | ||||
| Timestamp, | Timestamp, | ||||
| TimestampWithTimezone, | TimestampWithTimezone, | ||||
| ) | ) | ||||
| PROCESSING_METHODS = { | |||||
| "release": "swh.counters.journal_client.process_releases", | |||||
| "revision": "swh.counters.journal_client.process_revisions", | |||||
| } | |||||
| DATE = TimestampWithTimezone( | DATE = TimestampWithTimezone( | ||||
vsellier: this constant is not used anymore if I'm not wrong | |||||
Done Inline ActionsRight, I forgot to remove it. Let's do the rebase dance again then ;-) anlambert: Right, I forgot to remove it. Let's do the rebase dance again then ;-) | |||||
| timestamp=Timestamp(seconds=0, microseconds=0), offset=0, negative_utc=False | timestamp=Timestamp(seconds=0, microseconds=0), offset=0, negative_utc=False | ||||
| ) | ) | ||||
| def _get_processing_method_mocks(mocker): | |||||
| return { | |||||
| message_type: mocker.patch(PROCESSING_METHODS[message_type]) | |||||
| for message_type in PROCESSING_METHODS.keys() | |||||
| } | |||||
| def _create_release(author_fullname: Optional[str]) -> Dict: | def _create_release(author_fullname: Optional[str]) -> Dict: | ||||
| """Use Release.to_dict to be sure the field's name used to retrieve | """Use Release.to_dict to be sure the field's name used to retrieve | ||||
| the author is correct""" | the author is correct""" | ||||
| author = None | author = None | ||||
| if author_fullname: | if author_fullname: | ||||
| author = Person(fullname=bytes(author_fullname, "utf-8"), name=None, email=None) | author = Person(fullname=bytes(author_fullname, "utf-8"), name=None, email=None) | ||||
| ▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| REVISIONS_AUTHOR_FULLNAMES = {b"author 1", b"author 2"} | REVISIONS_AUTHOR_FULLNAMES = {b"author 1", b"author 2"} | ||||
| REVISIONS_COMMITTER_FULLNAMES = {b"committer 1", b"committer 2"} | REVISIONS_COMMITTER_FULLNAMES = {b"committer 1", b"committer 2"} | ||||
| REVISIONS_PERSON_FULLNAMES = REVISIONS_AUTHOR_FULLNAMES | REVISIONS_COMMITTER_FULLNAMES | REVISIONS_PERSON_FULLNAMES = REVISIONS_AUTHOR_FULLNAMES | REVISIONS_COMMITTER_FULLNAMES | ||||
| def test__journal_client__all_keys(mocker): | def test_journal_client_all_keys(local_redis_host): | ||||
| redis = Redis(host=local_redis_host) | |||||
| mock = mocker.patch("swh.counters.redis.Redis.add") | |||||
| redis = Redis(host="localhost") | |||||
| keys = { | keys = { | ||||
| "coll1": {b"key1": b"value1", b"key2": b"value2"}, | "coll1": {b"key1": b"value1", b"key2": b"value2"}, | ||||
| "coll2": {b"key3": b"value3", b"key4": b"value4", b"key5": b"value5"}, | "coll2": {b"key3": b"value3", b"key4": b"value4", b"key5": b"value5"}, | ||||
| } | } | ||||
| process_journal_messages(messages=keys, counters=redis) | process_journal_messages(messages=keys, counters=redis) | ||||
| assert mock.call_count == 2 | assert redis.get_counts(redis.get_counters()) == {b"coll1": 2, b"coll2": 3} | ||||
| first_call_args = mock.call_args_list[0] | |||||
| assert first_call_args[0][0] == "coll1" | |||||
| assert first_call_args[0][1] == keys["coll1"] | |||||
| second_call_args = mock.call_args_list[1] | def test_journal_client_process_revisions(local_redis_host): | ||||
| assert second_call_args[0][0] == "coll2" | redis = Redis(host=local_redis_host) | ||||
| assert second_call_args[0][1] == keys["coll2"] | |||||
| def test__journal_client_process_revisions(mocker): | |||||
| mock = mocker.patch("swh.counters.redis.Redis.add") | |||||
| redis = Redis(host="localhost") | |||||
| process_revisions(REVISIONS, redis) | process_revisions(REVISIONS, redis) | ||||
| assert mock.call_count == 1 | assert redis.get_counts(redis.get_counters()) == { | ||||
| first_call_args = mock.call_args_list[0] | b"person": len(REVISIONS_PERSON_FULLNAMES) | ||||
| assert first_call_args[0][0] == "person" | } | ||||
| assert sorted(first_call_args[0][1]) == sorted(REVISIONS_PERSON_FULLNAMES) | |||||
| def test__journal_client_process_releases(mocker): | |||||
| mock = mocker.patch("swh.counters.redis.Redis.add") | |||||
| redis = Redis(host="localhost") | def test_journal_client_process_releases(local_redis_host): | ||||
| redis = Redis(host=local_redis_host) | |||||
| process_releases(RELEASES, redis) | process_releases(RELEASES, redis) | ||||
| assert mock.call_count == 1 | assert redis.get_counts(redis.get_counters()) == { | ||||
| first_call_args = mock.call_args_list[0] | b"person": len(RELEASES_AUTHOR_FULLNAMES) | ||||
| assert first_call_args[0][0] == "person" | } | ||||
| assert first_call_args[0][1] == list(RELEASES_AUTHOR_FULLNAMES) | |||||
| def test__journal_client_process_releases_without_authors(mocker): | |||||
| mock = mocker.patch("swh.counters.redis.Redis.add") | |||||
| def test_journal_client_process_releases_without_authors(local_redis_host): | |||||
| releases = { | releases = { | ||||
| rel["id"]: msgpack.dumps(rel) | rel["id"]: msgpack.dumps(rel) | ||||
| for rel in [ | for rel in [ | ||||
| _create_release(author_fullname=None), | _create_release(author_fullname=None), | ||||
| _create_release(author_fullname=None), | _create_release(author_fullname=None), | ||||
| ] | ] | ||||
| } | } | ||||
| redis = Redis(host="localhost") | redis = Redis(host=local_redis_host) | ||||
| process_releases(releases, redis) | process_releases(releases, redis) | ||||
| assert mock.called == 1 | assert redis.get_counts(redis.get_counters()) == {} | ||||
| first_call_args = mock.call_args_list[0] | |||||
| assert first_call_args[0][0] == "person" | |||||
| assert first_call_args[0][1] == [] | |||||
this constant is not used anymore if I'm not wrong