Skip to content

Commit

Permalink
Added test for Trigger and Action at the entry APIv2 test
Browse files Browse the repository at this point in the history
  • Loading branch information
userlocalhost committed Nov 28, 2023
1 parent 4fdca33 commit b512eed
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 78 deletions.
24 changes: 16 additions & 8 deletions entry/tests/test_api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4546,9 +4546,9 @@ def test_destroy_entries_notify(self, mock_task):

self.assertTrue(mock_task.called)


@mock.patch(
"trigger.tasks.may_invoke_trigger.delay", mock.Mock(side_effect=trigger_tasks.may_invoke_trigger)
"trigger.tasks.may_invoke_trigger.delay",
mock.Mock(side_effect=trigger_tasks.may_invoke_trigger),
)
def test_update_entry_when_trigger_is_set(self):
# create Entry to be updated in this test
Expand All @@ -4560,12 +4560,17 @@ def test_update_entry_when_trigger_is_set(self):

# register Trigger and Action that specify "fuga" at text attribute
# when value "hoge" is set to the Attribute "val".
parent_condition = TriggerCondition.register(self.entity, [
{"attr_id": self.entity.attrs.get(name="val").id, "str_cond": "hoge"},
], [
{"attr_id": self.entity.attrs.get(name="text").id, "value": "fuga"},
])
parent_condition = TriggerCondition.register(
self.entity,
[
{"attr_id": self.entity.attrs.get(name="val").id, "str_cond": "hoge"},
],
[
{"attr_id": self.entity.attrs.get(name="text").id, "value": "fuga"},
],
)

# send request to update Entry
params = {
"name": "entry-change",
"attrs": [
Expand All @@ -4575,4 +4580,7 @@ def test_update_entry_when_trigger_is_set(self):
resp = self.client.put(
"/entry/api/v2/%s/" % entry.id, json.dumps(params), "application/json"
)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.status_code, 200)

# check Attribute "text", which is specified by TriggerCondition, was changed to "fuga"
self.assertEqual(entry.get_attrv("text").value, "fuga")
31 changes: 20 additions & 11 deletions trigger/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ def get_serializer_acceptable_value(self, value=None, attr_type=None):

value = value or self.values.first()
if attr_type & AttrTypeValue["array"]:
return [self.get_serializer_acceptable_value(x, attr_type ^ AttrTypeValue["array"]) for x in self.values.all()]
return [
self.get_serializer_acceptable_value(x, attr_type ^ AttrTypeValue["array"])
for x in self.values.all()
]
elif attr_type == AttrTypeValue["boolean"]:
return value.bool_cond
elif attr_type == AttrTypeValue["named_object"]:
Expand Down Expand Up @@ -135,13 +138,13 @@ def run(self, user, entry, call_stacks=[]):
"delay_trigger": False,
"call_stacks": [*call_stacks, self.id],
}
serializer = EntryUpdateSerializer(instance=entry,
data=setting_data,
context={"request": DRFRequest(user)})
serializer = EntryUpdateSerializer(
instance=entry, data=setting_data, context={"request": DRFRequest(user)}
)
if serializer:
serializer.is_valid(raise_exception=True)
serializer.save()


class TriggerActionValue(models.Model):
action = models.ForeignKey(TriggerAction, on_delete=models.CASCADE, related_name="values")
Expand Down Expand Up @@ -181,6 +184,7 @@ def get_actions(self, recv_attrs: list) -> list[TriggerAction]:
It's necessary to expand APIv2 implementation to pass EntityAttr ID in the recv_attrs
context to reduce DB query to get it from Attribute instance.
"""

def _is_match(condition):
for attr_info in [x for x in recv_attrs if x["attr_id"] == condition.attr.id]:
if condition.is_match_condition(attr_info["value"]):
Expand Down Expand Up @@ -227,6 +231,7 @@ def is_match_condition(self, recv_value, attr_type=None) -> bool:
"""
This checks specified value, which is compatible with APIv2 standard, matches with this condition.
"""

# This is a helper method when AttrType is "object" or "named_object"
def _is_match_object(val):
if isinstance(val, int) or isinstance(val, str):
Expand All @@ -239,7 +244,12 @@ def _is_match_object(val):
attr_type = self.attr.type

if attr_type & AttrTypeValue["array"]:
return any([self.is_match_condition(x, self.attr.type ^ AttrTypeValue["array"]) for x in recv_value])
return any(
[
self.is_match_condition(x, self.attr.type ^ AttrTypeValue["array"])
for x in recv_value
]
)

elif attr_type == AttrTypeValue["named_object"]:
return _is_match_object(recv_value["id"]) and self.str_cond == recv_value["name"]
Expand All @@ -253,7 +263,6 @@ def _is_match_object(val):
elif attr_type == AttrTypeValue["boolean"]:
return self.bool_cond == recv_value


@classmethod
def register(cls, entity: Entity, conditions: list, actions: list) -> TriggerParentCondition:
# convert input to InputTriggerCondition
Expand Down Expand Up @@ -285,8 +294,8 @@ def register(cls, entity: Entity, conditions: list, actions: list) -> TriggerPar
def get_invoked_actions(cls, entity: Entity, recv_data: list):
actions = []
for parent_condition in TriggerParentCondition.objects.filter(entity=entity):
actions += parent_condition.get_actions([
{"attr_id": int(x["id"]), "value": x["value"]} for x in recv_data
])
actions += parent_condition.get_actions(
[{"attr_id": int(x["id"]), "value": x["value"]} for x in recv_data]
)

return actions
return actions
9 changes: 7 additions & 2 deletions trigger/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
from job.models import Job
from user.models import User
from entry.models import Entry
from trigger.models import TriggerParentCondition, TriggerCondition, TriggerAction, TriggerActionValue
from trigger.models import (
TriggerParentCondition,
TriggerCondition,
TriggerAction,
TriggerActionValue,
)


@app.task(bind=True)
Expand All @@ -24,4 +29,4 @@ def may_invoke_trigger(self, job_id):
for action in TriggerCondition.get_invoked_actions(entry.schema, recv_data):
action.run(user, entry)

job.update(Job.STATUS["DONE"])
job.update(Job.STATUS["DONE"])
Loading

0 comments on commit b512eed

Please sign in to comment.