Skip to content

Commit 2125dc5

Browse files
Niloth-ptimabbott
authored andcommitted
test_model: Add user_id test cases for reaction factories.
- reaction_event_factory - reaction_event_index_factory To test the handling of reaction events conforming to the reactions schema post ZFL 2.
1 parent a0233ec commit 2125dc5

File tree

1 file changed

+87
-30
lines changed

1 file changed

+87
-30
lines changed

tests/model/test_model.py

Lines changed: 87 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2862,21 +2862,25 @@ def test__update_rendered_view_change_narrow(
28622862

28632863
@pytest.fixture
28642864
def reaction_event_factory(self):
2865-
def _factory(*, op: str, message_id: int):
2866-
return {
2867-
"emoji_code": "1f44d",
2865+
def _factory(*, op: str, message_id: int, user=None, user_id=None):
2866+
base_event = {
28682867
"id": 2,
2869-
"user": {
2870-
"email": "[email protected]",
2871-
"user_id": 5140,
2872-
"full_name": "Foo Boo",
2873-
},
2868+
"emoji_code": "1f44d",
28742869
"reaction_type": "unicode_emoji",
28752870
"message_id": message_id,
28762871
"emoji_name": "thumbs_up",
28772872
"type": "reaction",
28782873
"op": op,
28792874
}
2875+
if user is not None:
2876+
base_event["user"] = {
2877+
"email": "[email protected]",
2878+
"user_id": 5140,
2879+
"full_name": "Foo Boo",
2880+
}
2881+
if user_id is not None:
2882+
base_event["user_id"] = user_id
2883+
return base_event
28802884

28812885
return _factory
28822886

@@ -2885,28 +2889,45 @@ def reaction_event_index_factory(self):
28852889
"""
28862890
Generate index for reaction tests based on minimal specification
28872891
2888-
Input is a list of pairs, of a message-id and a list of reaction tuples
2892+
Input:
2893+
- msgs: A list of tuples where each tuple contains:
2894+
- A message ID
2895+
- A list of reaction tuples, where each reaction includes:
2896+
- user_id
2897+
- reaction_type
2898+
- emoji_code
2899+
- emoji_name
2900+
- schema: Defines the fields present in the reaction
2901+
(e.g., "with_user", "with_user_id", "with_both")
2902+
28892903
NOTE: reactions as None indicate not indexed, [] indicates no reaction
28902904
"""
28912905
MsgsType = List[Tuple[int, Optional[List[Tuple[int, str, str, str]]]]]
28922906

2893-
def _factory(msgs: MsgsType):
2907+
def _factory(msgs: MsgsType, schema="with_user"):
2908+
def build_reaction(user_id, type, code, name):
2909+
reaction = {
2910+
"reaction_type": type,
2911+
"emoji_code": code,
2912+
"emoji_name": name,
2913+
}
2914+
if schema in {"with_user", "with_both"}:
2915+
reaction["user"] = {
2916+
"email": f"User email #{user_id}",
2917+
"full_name": f"User #{user_id}",
2918+
"id": user_id,
2919+
}
2920+
if schema in {"with_user_id", "with_both"}:
2921+
reaction["user_id"] = user_id
2922+
return reaction
2923+
28942924
return {
28952925
"messages": {
28962926
message_id: {
28972927
"id": message_id,
28982928
"content": f"message content {message_id}",
28992929
"reactions": [
2900-
{
2901-
"user": {
2902-
"email": f"User email #{user_id}",
2903-
"full_name": f"User #{user_id}",
2904-
"id": user_id,
2905-
},
2906-
"reaction_type": type,
2907-
"emoji_code": code,
2908-
"emoji_name": name,
2909-
}
2930+
build_reaction(user_id, type, code, name)
29102931
for user_id, type, code, name in reactions
29112932
],
29122933
}
@@ -2918,25 +2939,43 @@ def _factory(msgs: MsgsType):
29182939
return _factory
29192940

29202941
@pytest.mark.parametrize("op", ["add", "remove"])
2942+
@pytest.mark.parametrize(
2943+
"reaction_event_schema", ["with_user", "with_user_id", "with_both"]
2944+
)
2945+
@pytest.mark.parametrize(
2946+
"reaction_schema", ["with_user", "with_user_id", "with_both"]
2947+
)
29212948
def test__handle_reaction_event_not_in_index(
29222949
self,
29232950
mocker,
29242951
model,
29252952
reaction_event_factory,
29262953
reaction_event_index_factory,
29272954
op,
2955+
reaction_event_schema,
2956+
reaction_schema,
29282957
unindexed_message_id=1,
29292958
):
2930-
reaction_event = reaction_event_factory(
2931-
op=op,
2932-
message_id=unindexed_message_id,
2933-
)
2959+
common_args = {
2960+
"op": op,
2961+
"message_id": unindexed_message_id,
2962+
}
2963+
if reaction_event_schema == "with_user":
2964+
reaction_event = reaction_event_factory(**common_args, user=True)
2965+
elif reaction_event_schema == "with_user_id":
2966+
reaction_event = reaction_event_factory(**common_args, user_id=5140)
2967+
else:
2968+
reaction_event = reaction_event_factory(
2969+
**common_args, user=True, user_id=5140
2970+
)
2971+
29342972
model.index = reaction_event_index_factory(
29352973
[
29362974
(unindexed_message_id, None), # explicitly exclude
29372975
(2, [(1, "unicode_emoji", "1232", "thumbs_up")]),
29382976
(3, []),
2939-
]
2977+
],
2978+
reaction_schema,
29402979
)
29412980
model._update_rendered_view = mocker.Mock()
29422981
previous_index = deepcopy(model.index)
@@ -2954,25 +2993,43 @@ def test__handle_reaction_event_not_in_index(
29542993
("remove", 1), # Removed emoji doesn't match, so length remains 1
29552994
],
29562995
)
2996+
@pytest.mark.parametrize(
2997+
"reaction_event_schema", ["with_user", "with_user_id", "with_both"]
2998+
)
2999+
@pytest.mark.parametrize(
3000+
"reaction_schema", ["with_user", "with_user_id", "with_both"]
3001+
)
29573002
def test__handle_reaction_event_for_msg_in_index(
29583003
self,
29593004
mocker,
29603005
model,
29613006
reaction_event_factory,
29623007
reaction_event_index_factory,
29633008
op,
3009+
reaction_event_schema,
3010+
reaction_schema,
29643011
expected_number_after,
29653012
event_message_id=1,
29663013
):
2967-
reaction_event = reaction_event_factory(
2968-
op=op,
2969-
message_id=event_message_id,
2970-
)
3014+
common_args = {
3015+
"op": op,
3016+
"message_id": event_message_id,
3017+
}
3018+
if reaction_event_schema == "with_user":
3019+
reaction_event = reaction_event_factory(**common_args, user=True)
3020+
elif reaction_event_schema == "with_user_id":
3021+
reaction_event = reaction_event_factory(**common_args, user_id=5140)
3022+
else:
3023+
reaction_event = reaction_event_factory(
3024+
**common_args, user=True, user_id=5140
3025+
)
3026+
29713027
model.index = reaction_event_index_factory(
29723028
[
29733029
(1, [(1, "unicode_emoji", "1232", "thumbs_up")]),
29743030
(2, []),
2975-
]
3031+
],
3032+
reaction_schema,
29763033
)
29773034
model._update_rendered_view = mocker.Mock()
29783035

0 commit comments

Comments
 (0)