Skip to content

Commit 7fcdefa

Browse files
authored
Merge pull request #1088 from userlocalhost/feature/trigger/add_named_type
Enable to select named Attributes to Trigger configurations
2 parents ad464ab + 5fe62ac commit 7fcdefa

18 files changed

+912
-298
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## In development
44

55
### Added
6+
* Added Attribute type (`named_object` and `array_named_object`) for TriggerAction
7+
Contributed by @userlocalhost, @hinashi
68

79
### Changed
810

apiclient/typescript-fetch/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dmm-com/airone-apiclient-typescript-fetch",
3-
"version": "0.0.12",
3+
"version": "0.0.13",
44
"description": "AirOne APIv2 client in TypeScript",
55
"main": "src/autogenerated/index.ts",
66
"scripts": {

entry/models.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -2385,9 +2385,12 @@ def _get_value(attrname, attrtype, value):
23852385
return [_get_value(attrname, attrtype, x) for x in value]
23862386

23872387
elif attrtype & AttrTypeValue["named"]:
2388-
[co_value] = list(value.values())
2388+
[name, ref] = list(value.keys()) + list(value.values())
23892389

2390-
return co_value["id"] if co_value else None
2390+
return {
2391+
"id": ref["id"] if ref else None,
2392+
"name": name,
2393+
}
23912394

23922395
elif attrtype & AttrTypeValue["object"]:
23932396
return value["id"] if value else None

entry/tests/test_view.py

+111-43
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import yaml
99
from django.conf import settings
10+
from django.db.models import Q
1011
from django.http import HttpResponse
1112
from django.http.response import JsonResponse
1213
from django.urls import reverse
@@ -1937,31 +1938,55 @@ def test_create_entry_with_trigger_configuration(self):
19371938
user = self.guest_login()
19381939

19391940
# initialize Entity which has Role related Attributes
1941+
entity_pref = self.create_entity(user, "Prefecture")
1942+
self.add_entry(user, "Tokyo", entity_pref)
1943+
pref_info = {"tokyo": self.add_entry(user, "Tokyo", entity_pref)}
1944+
19401945
entity = self.create_entity(
19411946
user,
19421947
"Personal Information",
19431948
attrs=[
1944-
{"name": "address", "type": AttrTypeValue["string"]},
1949+
{"name": "address", "type": AttrTypeValue["named_object"], "ref": entity_pref},
19451950
{"name": "age", "type": AttrTypeValue["string"]},
19461951
],
19471952
)
19481953

19491954
# register TriggerAction configuration before creating an Entry
19501955
TriggerCondition.register(
19511956
entity,
1952-
[{"attr_id": entity.attrs.get(name="age").id, "cond": "0"}],
1953-
[{"attr_id": entity.attrs.get(name="address").id, "value": "Tokyo"}],
1957+
[
1958+
{
1959+
"attr_id": entity.attrs.get(name="address").id,
1960+
"hint": "json",
1961+
"cond": json.dumps(
1962+
{
1963+
"name": "unknown",
1964+
"id": None,
1965+
}
1966+
),
1967+
}
1968+
],
1969+
[
1970+
{"attr_id": entity.attrs.get(name="age").id, "value": "0"},
1971+
{
1972+
"attr_id": entity.attrs.get(name="address").id,
1973+
"value": {
1974+
"name": "Chiyoda ward",
1975+
"id": pref_info["tokyo"],
1976+
},
1977+
},
1978+
],
19541979
)
19551980

19561981
# create an Entry to invoke TriggerAction
19571982
params = {
19581983
"entry_name": "Jhon Doe",
19591984
"attrs": [
19601985
{
1961-
"id": str(entity.attrs.get(name="age").id),
1986+
"id": str(entity.attrs.get(name="address").id),
19621987
"type": str(AttrTypeValue["string"]),
1963-
"value": [{"data": "0", "index": 0}],
1964-
"referral_key": [],
1988+
"value": [{"data": "", "index": 0}],
1989+
"referral_key": [{"data": "unknown", "index": 0}],
19651990
}
19661991
],
19671992
}
@@ -1974,12 +1999,13 @@ def test_create_entry_with_trigger_configuration(self):
19741999
# check trigger action was worked properly
19752000
job_query = Job.objects.filter(operation=JobOperation.MAY_INVOKE_TRIGGER)
19762001
self.assertEqual(job_query.count(), 1)
1977-
self.assertEqual(job_query.first().status, JobStatus.DONE)
2002+
self.assertEqual(job_query.first().status, JobStatus.DONE.value)
19782003

19792004
# check created Entry's attributes are set properly by TriggerAction
19802005
entry = Entry.objects.get(id=resp.json().get("entry_id"))
19812006
self.assertEqual(entry.get_attrv("age").value, "0")
1982-
self.assertEqual(entry.get_attrv("address").value, "Tokyo")
2007+
self.assertEqual(entry.get_attrv("address").value, "Chiyoda ward")
2008+
self.assertEqual(entry.get_attrv("address").referral.id, pref_info["tokyo"].id)
19832009

19842010
@patch("entry.tasks.edit_entry_attrs.delay", Mock(side_effect=tasks.edit_entry_attrs))
19852011
def test_edit_entry_with_role_attributes(self):
@@ -2035,22 +2061,46 @@ def test_edit_entry_with_role_attributes(self):
20352061
def test_edit_entry_with_trigger_configuration(self):
20362062
user = self.guest_login()
20372063

2038-
# initialize Entity and Entry which will be updated by TriggerAction
2064+
# initialize Entity which has Role related Attributes
2065+
entity_pref = self.create_entity(user, "Prefecture")
2066+
self.add_entry(user, "Tokyo", entity_pref)
2067+
pref_info = {"tokyo": self.add_entry(user, "Tokyo", entity_pref)}
2068+
20392069
entity = self.create_entity(
20402070
user,
20412071
"Personal Information",
20422072
attrs=[
2073+
{"name": "address", "type": AttrTypeValue["named_object"], "ref": entity_pref},
20432074
{"name": "age", "type": AttrTypeValue["string"]},
2044-
{"name": "address", "type": AttrTypeValue["string"]},
20452075
],
20462076
)
20472077
entry = self.add_entry(user, "Jhon Doe", entity)
20482078

20492079
# register TriggerAction configuration before creating an Entry
20502080
TriggerCondition.register(
20512081
entity,
2052-
[{"attr_id": entity.attrs.get(name="age").id, "cond": "0"}],
2053-
[{"attr_id": entity.attrs.get(name="address").id, "value": "Tokyo"}],
2082+
[
2083+
{
2084+
"attr_id": entity.attrs.get(name="address").id,
2085+
"hint": "json",
2086+
"cond": json.dumps(
2087+
{
2088+
"name": "unknown",
2089+
"id": None,
2090+
}
2091+
),
2092+
}
2093+
],
2094+
[
2095+
{"attr_id": entity.attrs.get(name="age").id, "value": "0"},
2096+
{
2097+
"attr_id": entity.attrs.get(name="address").id,
2098+
"value": {
2099+
"name": "Chiyoda ward",
2100+
"id": pref_info["tokyo"],
2101+
},
2102+
},
2103+
],
20542104
)
20552105

20562106
# send request for editing Entry to invoke TriggerAction
@@ -2059,8 +2109,9 @@ def test_edit_entry_with_trigger_configuration(self):
20592109
"attrs": [
20602110
{
20612111
"entity_attr_id": "",
2062-
"id": str(entry.attrs.get(schema__name="age").id),
2063-
"value": [{"data": "0", "index": 0}],
2112+
"id": str(entry.attrs.get(schema__name="address").id),
2113+
"value": [{"data": "", "index": 0}],
2114+
"referral_key": [{"data": "unknown", "index": 0}],
20642115
}
20652116
],
20662117
}
@@ -2074,12 +2125,13 @@ def test_edit_entry_with_trigger_configuration(self):
20742125
# check trigger action was worked properly
20752126
job_query = Job.objects.filter(operation=JobOperation.MAY_INVOKE_TRIGGER)
20762127
self.assertEqual(job_query.count(), 1)
2077-
self.assertEqual(job_query.first().status, JobStatus.DONE)
2128+
self.assertEqual(job_query.first().status, JobStatus.DONE.value)
20782129

20792130
# check updated Entry's attributes are set properly by TriggerAction
20802131
self.assertEqual(resp.json().get("entry_id"), entry.id)
20812132
self.assertEqual(entry.get_attrv("age").value, "0")
2082-
self.assertEqual(entry.get_attrv("address").value, "Tokyo")
2133+
self.assertEqual(entry.get_attrv("address").value, "Chiyoda ward")
2134+
self.assertEqual(entry.get_attrv("address").referral.id, pref_info["tokyo"].id)
20832135

20842136
@patch(
20852137
"entry.tasks.create_entry_attrs.delay",
@@ -4875,45 +4927,61 @@ def test_invoke_trigger_by_revert_attrv(self):
48754927
user,
48764928
"Entity",
48774929
[
4878-
{"name": "cond", "type": AttrTypeValue["string"]},
4930+
{"name": "cond_str", "type": AttrTypeValue["string"]},
4931+
{"name": "cond_name", "type": AttrTypeValue["named_object"]},
48794932
{"name": "action", "type": AttrTypeValue["string"]},
48804933
],
48814934
)
4882-
entry = self.add_entry(
4883-
user,
4884-
"TestEntry",
4885-
entity,
4886-
values={
4887-
"cond": "hoge",
4888-
},
4889-
)
4890-
4891-
# changed value to retrieve
4892-
changing_attr = entry.attrs.get(name="cond")
4893-
changing_attr.add_value(user, "changed")
48944935

48954936
# register TriggerAction configuration before creating an Entry
48964937
TriggerCondition.register(
48974938
entity,
4898-
[{"attr_id": entity.attrs.get(name="cond").id, "cond": "hoge"}],
4939+
[{"attr_id": entity.attrs.get(name="cond_str").id, "cond": "hoge"}],
48994940
[{"attr_id": entity.attrs.get(name="action").id, "value": "fuga"}],
49004941
)
4901-
4902-
# send request to revert attribute value of "cond"
4903-
revert_attrv = changing_attr.values.filter(value="hoge").last()
4904-
params = {"attr_id": str(changing_attr.id), "attrv_id": str(revert_attrv.id)}
4905-
resp = self.client.post(
4906-
reverse("entry:revert_attrv"), json.dumps(params), "application/json"
4942+
TriggerCondition.register(
4943+
entity,
4944+
[{"attr_id": entity.attrs.get(name="cond_name").id, "cond": "foo"}],
4945+
[{"attr_id": entity.attrs.get(name="action").id, "value": "fuga"}],
49074946
)
4908-
self.assertEqual(resp.status_code, 200)
49094947

4910-
# This check that Attribute value of "action" would be updated by TriggerAction
4911-
self.assertEqual(entry.get_attrv("action").value, "fuga")
4948+
# changed value to retrieve
4949+
testing_params = [
4950+
("cond_str", "hoge", "changed", Q(value="hoge")),
4951+
(
4952+
"cond_name",
4953+
{"name": "foo", "id": None},
4954+
{"name": "changed", "id": None},
4955+
Q(value="foo"),
4956+
),
4957+
]
4958+
for index, (attrname, initial_value, changed_value, query) in enumerate(testing_params):
4959+
entry = self.add_entry(
4960+
user,
4961+
"TestEntry",
4962+
entity,
4963+
values={
4964+
attrname: initial_value,
4965+
},
4966+
)
4967+
changing_attr = entry.attrs.get(name=attrname)
4968+
changing_attr.add_value(user, changed_value)
49124969

4913-
# check trigger action was worked properly
4914-
job_query = Job.objects.filter(operation=JobOperation.MAY_INVOKE_TRIGGER)
4915-
self.assertEqual(job_query.count(), 1)
4916-
self.assertEqual(job_query.first().status, JobStatus.DONE)
4970+
# send request to revert attribute value of "cond"
4971+
revert_attrv = changing_attr.values.filter(query).last()
4972+
params = {"attr_id": str(changing_attr.id), "attrv_id": str(revert_attrv.id)}
4973+
resp = self.client.post(
4974+
reverse("entry:revert_attrv"), json.dumps(params), "application/json"
4975+
)
4976+
self.assertEqual(resp.status_code, 200)
4977+
4978+
# This check that Attribute value of "action" would be updated by TriggerAction
4979+
self.assertEqual(entry.get_attrv("action").value, "fuga")
4980+
4981+
# check trigger action was worked properly
4982+
job_query = Job.objects.filter(operation=JobOperation.MAY_INVOKE_TRIGGER)
4983+
self.assertEqual(job_query.count(), 1 + index)
4984+
self.assertEqual(job_query.last().status, JobStatus.DONE)
49174985

49184986
def test_revert_attrv_with_invalid_value(self):
49194987
user = self.guest_login()

frontend/src/components/entry/entryForm/AttributeValueField.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const server = setupServer(
3434
}),
3535
// getGroups
3636
http.get("http://localhost/group/api/v2/groups", () => {
37-
return HttpResponse.json([]);
37+
return HttpResponse.json({ count: 0, results: [] });
3838
}),
3939
// getRoles
4040
http.get("http://localhost/role/api/v2/", () => {

frontend/src/components/entry/entryForm/GroupAttributeValueField.test.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ describe("GroupAttributeValueField", () => {
6565
};
6666

6767
const groups: PaginatedGroupList = {
68+
count: 0,
6869
results: [
6970
{
7071
id: 1,

0 commit comments

Comments
 (0)