7
7
8
8
import yaml
9
9
from django .conf import settings
10
+ from django .db .models import Q
10
11
from django .http import HttpResponse
11
12
from django .http .response import JsonResponse
12
13
from django .urls import reverse
@@ -1937,31 +1938,55 @@ def test_create_entry_with_trigger_configuration(self):
1937
1938
user = self .guest_login ()
1938
1939
1939
1940
# 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
+
1940
1945
entity = self .create_entity (
1941
1946
user ,
1942
1947
"Personal Information" ,
1943
1948
attrs = [
1944
- {"name" : "address" , "type" : AttrTypeValue ["string" ] },
1949
+ {"name" : "address" , "type" : AttrTypeValue ["named_object" ], "ref" : entity_pref },
1945
1950
{"name" : "age" , "type" : AttrTypeValue ["string" ]},
1946
1951
],
1947
1952
)
1948
1953
1949
1954
# register TriggerAction configuration before creating an Entry
1950
1955
TriggerCondition .register (
1951
1956
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
+ ],
1954
1979
)
1955
1980
1956
1981
# create an Entry to invoke TriggerAction
1957
1982
params = {
1958
1983
"entry_name" : "Jhon Doe" ,
1959
1984
"attrs" : [
1960
1985
{
1961
- "id" : str (entity .attrs .get (name = "age " ).id ),
1986
+ "id" : str (entity .attrs .get (name = "address " ).id ),
1962
1987
"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 } ],
1965
1990
}
1966
1991
],
1967
1992
}
@@ -1974,12 +1999,13 @@ def test_create_entry_with_trigger_configuration(self):
1974
1999
# check trigger action was worked properly
1975
2000
job_query = Job .objects .filter (operation = JobOperation .MAY_INVOKE_TRIGGER )
1976
2001
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 )
1978
2003
1979
2004
# check created Entry's attributes are set properly by TriggerAction
1980
2005
entry = Entry .objects .get (id = resp .json ().get ("entry_id" ))
1981
2006
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 )
1983
2009
1984
2010
@patch ("entry.tasks.edit_entry_attrs.delay" , Mock (side_effect = tasks .edit_entry_attrs ))
1985
2011
def test_edit_entry_with_role_attributes (self ):
@@ -2035,22 +2061,46 @@ def test_edit_entry_with_role_attributes(self):
2035
2061
def test_edit_entry_with_trigger_configuration (self ):
2036
2062
user = self .guest_login ()
2037
2063
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
+
2039
2069
entity = self .create_entity (
2040
2070
user ,
2041
2071
"Personal Information" ,
2042
2072
attrs = [
2073
+ {"name" : "address" , "type" : AttrTypeValue ["named_object" ], "ref" : entity_pref },
2043
2074
{"name" : "age" , "type" : AttrTypeValue ["string" ]},
2044
- {"name" : "address" , "type" : AttrTypeValue ["string" ]},
2045
2075
],
2046
2076
)
2047
2077
entry = self .add_entry (user , "Jhon Doe" , entity )
2048
2078
2049
2079
# register TriggerAction configuration before creating an Entry
2050
2080
TriggerCondition .register (
2051
2081
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
+ ],
2054
2104
)
2055
2105
2056
2106
# send request for editing Entry to invoke TriggerAction
@@ -2059,8 +2109,9 @@ def test_edit_entry_with_trigger_configuration(self):
2059
2109
"attrs" : [
2060
2110
{
2061
2111
"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 }],
2064
2115
}
2065
2116
],
2066
2117
}
@@ -2074,12 +2125,13 @@ def test_edit_entry_with_trigger_configuration(self):
2074
2125
# check trigger action was worked properly
2075
2126
job_query = Job .objects .filter (operation = JobOperation .MAY_INVOKE_TRIGGER )
2076
2127
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 )
2078
2129
2079
2130
# check updated Entry's attributes are set properly by TriggerAction
2080
2131
self .assertEqual (resp .json ().get ("entry_id" ), entry .id )
2081
2132
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 )
2083
2135
2084
2136
@patch (
2085
2137
"entry.tasks.create_entry_attrs.delay" ,
@@ -4875,45 +4927,61 @@ def test_invoke_trigger_by_revert_attrv(self):
4875
4927
user ,
4876
4928
"Entity" ,
4877
4929
[
4878
- {"name" : "cond" , "type" : AttrTypeValue ["string" ]},
4930
+ {"name" : "cond_str" , "type" : AttrTypeValue ["string" ]},
4931
+ {"name" : "cond_name" , "type" : AttrTypeValue ["named_object" ]},
4879
4932
{"name" : "action" , "type" : AttrTypeValue ["string" ]},
4880
4933
],
4881
4934
)
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" )
4894
4935
4895
4936
# register TriggerAction configuration before creating an Entry
4896
4937
TriggerCondition .register (
4897
4938
entity ,
4898
- [{"attr_id" : entity .attrs .get (name = "cond " ).id , "cond" : "hoge" }],
4939
+ [{"attr_id" : entity .attrs .get (name = "cond_str " ).id , "cond" : "hoge" }],
4899
4940
[{"attr_id" : entity .attrs .get (name = "action" ).id , "value" : "fuga" }],
4900
4941
)
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" }],
4907
4946
)
4908
- self .assertEqual (resp .status_code , 200 )
4909
4947
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 )
4912
4969
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 )
4917
4985
4918
4986
def test_revert_attrv_with_invalid_value (self ):
4919
4987
user = self .guest_login ()
0 commit comments