@@ -18,29 +18,6 @@ def test_instanciation(base_config_path):
1818 eng_2 = RulesEngine (config_dict = config_dict )
1919 assert isinstance (eng_2 , RulesEngine )
2020
21- # Dummy action function
22- set_value = lambda value , ** kwargs : {"value" : value }
23-
24- raw_rules = {
25- "type" : {
26- "rule_str" : {
27- "condition" : lambda x : isinstance (x , str ),
28- "condition_parameters" : {"x" : "input.to_check" },
29- "action" : set_value ,
30- "action_parameters" : {"value" : "String" },
31- },
32- "rule_other" : {
33- "condition" : None ,
34- "condition_parameters" : None ,
35- "action" : set_value ,
36- "action_parameters" : {"value" : "other type" },
37- },
38- }
39- }
40- # Dictionary instanciation
41- eng_3 = RulesEngine (rules_dict = raw_rules )
42- assert isinstance (eng_3 , RulesEngine )
43-
4421
4522@pytest .mark .parametrize (
4623 "input_data, config_dir, rule_set, verbose, good_results" ,
@@ -280,6 +257,22 @@ def test_instanciation(base_config_path):
280257 "email" : True ,
281258 },
282259 ),
260+ (
261+ {
262+ "age" : None ,
263+ "language" : "french" ,
264+ "powers" : ["strength" , "fly" ],
265+ "favorite_meal" : "Spinach" ,
266+ },
267+ "ignored_rules" ,
268+ "default_rule_set" ,
269+ False ,
270+ {
271+ "admission" : {"admission" : True },
272+ "course" : {"course_id" : "senior" },
273+ "email" : True ,
274+ },
275+ ),
283276 ],
284277)
285278def test_conf_apply_rules (input_data , config_dir , rule_set , verbose , good_results , base_config_path ):
@@ -295,115 +288,6 @@ def test_conf_apply_rules(input_data, config_dir, rule_set, verbose, good_result
295288 assert res_1 == res_2 == good_results
296289
297290
298- @pytest .mark .parametrize (
299- "input_data, verbose, good_results" ,
300- [
301- (
302- {
303- "age" : 5000 ,
304- "language" : "english" ,
305- "power" : "immortality" ,
306- "favorite_meal" : None ,
307- "weapons" : ["Magic lasso" , "Bulletproof bracelets" , "Sword" , "Shield" ],
308- },
309- True ,
310- {
311- "verbosity" : {
312- "rule_set" : "default_rule_set" ,
313- "results" : [
314- {
315- "rule_group" : "power_level" ,
316- "verified_conditions" : {
317- "condition" : {"expression" : "USER_CONDITION" , "values" : {"USER_CONDITION" : True }}
318- },
319- "activated_rule" : "super_power" ,
320- "action_result" : "super" ,
321- },
322- {
323- "rule_group" : "admission" ,
324- "verified_conditions" : {
325- "condition" : {"expression" : "USER_CONDITION" , "values" : {"USER_CONDITION" : True }}
326- },
327- "activated_rule" : "admitted" ,
328- "action_result" : "Admitted!" ,
329- },
330- ],
331- },
332- "power_level" : "super" ,
333- "admission" : "Admitted!" ,
334- },
335- ),
336- (
337- {
338- "age" : 5000 ,
339- "language" : "english" ,
340- "power" : "immortality" ,
341- "favorite_meal" : None ,
342- "weapons" : ["Magic lasso" , "Bulletproof bracelets" , "Sword" , "Shield" ],
343- },
344- False ,
345- {"power_level" : "super" , "admission" : "Admitted!" },
346- ),
347- ],
348- )
349- def test_dict_apply_rules (input_data , verbose , good_results ):
350- """Unit test of the method RulesEngine.apply_rules() when init was done using rule_dict"""
351- # Dummy action function
352-
353- def is_a_super_power (level , ** kwargs ):
354- """Dummy validation function."""
355- return level == "super"
356-
357- def admit (** kwargs ):
358- """Dummy action function."""
359- return "Admitted!"
360-
361- def set_value (value , ** kwargs ):
362- """Dummy action function."""
363- return value
364-
365- rules_raw = {
366- "power_level" : {
367- "super_power" : {
368- "condition" : lambda p : p in ["immortality" , "time_travelling" , "invisibility" ],
369- "condition_parameters" : {"p" : "input.power" },
370- "action" : lambda x , ** kwargs : x ,
371- "action_parameters" : {"x" : "super" },
372- },
373- "minor_power" : {
374- "condition" : lambda p : p in ["juggle" , "sing" , "sleep" ],
375- "condition_parameters" : {"p" : "input.power" },
376- "action" : lambda x , ** kwargs : x ,
377- "action_parameters" : {"x" : "minor" },
378- },
379- "no_power" : {
380- "condition" : None ,
381- "condition_parameters" : None ,
382- "action" : lambda x , ** kwargs : x ,
383- "action_parameters" : {"x" : "no_power" },
384- },
385- },
386- "admission" : {
387- "admitted" : {
388- "condition" : is_a_super_power ,
389- "condition_parameters" : {"level" : "output.power_level" },
390- "action" : admit ,
391- "action_parameters" : None ,
392- },
393- "not_admitted" : {
394- "condition" : None ,
395- "condition_parameters" : None ,
396- "action" : set_value ,
397- "action_parameters" : {"value" : "Not admitted :-(" },
398- },
399- },
400- }
401-
402- eng_2 = RulesEngine (rules_dict = rules_raw )
403- res = eng_2 .apply_rules (input_data , verbose = verbose )
404- assert res == good_results
405-
406-
407291def test_ignore_global_strategy (base_config_path ):
408292 """Unit test of the class RulesEngine"""
409293 # Config. instanciation
@@ -440,3 +324,33 @@ def test_kwargs_in_apply_rules(input_data, good_results, base_config_path):
440324 res = eng .apply_rules (input_data , rule_set = "fourth_rule_set" , my_parameter = "super@connection" )
441325
442326 assert res == good_results
327+
328+
329+ @pytest .mark .parametrize (
330+ "input_data, config_dir, rule_set, ignored_rules, good_results" ,
331+ [
332+ (
333+ {
334+ "age" : None ,
335+ "language" : "french" ,
336+ "powers" : ["strength" , "fly" ],
337+ "favorite_meal" : "Spinach" ,
338+ },
339+ "ignored_rules" ,
340+ "default_rule_set" ,
341+ {"ADM_OK" },
342+ {
343+ "admission" : {"admission" : False },
344+ "course" : {"course_id" : "senior" },
345+ "email" : True ,
346+ },
347+ ),
348+ ],
349+ )
350+ def test_conf_ignored_rules (input_data , config_dir , rule_set , ignored_rules , good_results , base_config_path ):
351+ """UT of ignored rules."""
352+ path = os .path .join (base_config_path , config_dir )
353+ eng = RulesEngine (config_path = path )
354+ res = eng .apply_rules (input_data = input_data , rule_set = rule_set , ignored_rules = ignored_rules )
355+
356+ assert res == good_results
0 commit comments