Conditions | 1 |
Total Lines | 60 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # coding=utf-8 |
||
43 | @pytest.mark.parametrize("air_miles, land_miles, age, vip, expected", [ |
||
44 | (5000, 1000, 37, 'yes', True), |
||
45 | (1500, 1000, 37, 'yes', False), |
||
46 | (5000, 5001, 37, 'yes', False), |
||
47 | (5000, 1000, 16, 'yes', False), |
||
48 | (5000, 1000, 70, 'yes', False), |
||
49 | (5000, 1000, 37, 'no', False), |
||
50 | (100, 50, 15, 'no', False) |
||
51 | ]) |
||
52 | def test_multiple_rules_engine(air_miles, land_miles, age, vip, expected): |
||
53 | air_miles_source = DictSource('air_miles') |
||
54 | minimum_miles_source = FixedValueSource(3500) |
||
55 | minimum_air_miles_rule = SimpleComparisonRule(air_miles_source, |
||
56 | minimum_miles_source, |
||
57 | GreaterThanOrEqual()) |
||
58 | |||
59 | land_miles_source = DictSource('land_miles') |
||
60 | less_land_than_air_miles_rule = SimpleComparisonRule(land_miles_source, |
||
61 | air_miles_source, |
||
62 | LessThanOrEqual()) |
||
63 | |||
64 | air_miles_percentage = PercentageSource(0.05, air_miles_source) |
||
65 | air_miles_percentage_rule = SimpleComparisonRule(land_miles_source, |
||
66 | air_miles_percentage, |
||
67 | GreaterThanOrEqual()) |
||
68 | |||
69 | air_and_land_miles_rule = BooleanAndRule([minimum_air_miles_rule, |
||
70 | less_land_than_air_miles_rule, |
||
71 | air_miles_percentage_rule]) |
||
72 | |||
73 | age_source = DictSource('age') |
||
74 | minimum_age_source = FixedValueSource(21) |
||
75 | minimum_age_rule = SimpleComparisonRule(age_source, minimum_age_source, |
||
76 | GreaterThanOrEqual()) |
||
77 | |||
78 | maximum_age_source = FixedValueSource(65) |
||
79 | maximum_age_rule = SimpleComparisonRule(age_source, maximum_age_source, |
||
80 | LessThanOrEqual()) |
||
81 | |||
82 | vip_status_source = DictSource('vip') |
||
83 | positive_vip_status = FixedValueSource('yes') |
||
84 | vip_status_rule = SimpleComparisonRule(vip_status_source, |
||
85 | positive_vip_status, |
||
86 | Equal()) |
||
87 | |||
88 | engine = Engine([ |
||
89 | air_and_land_miles_rule, |
||
90 | minimum_age_rule, |
||
91 | maximum_age_rule, |
||
92 | vip_status_rule |
||
93 | ]) |
||
94 | |||
95 | data = { |
||
96 | 'air_miles': air_miles, |
||
97 | 'land_miles': land_miles, |
||
98 | 'age': age, |
||
99 | 'vip': vip |
||
100 | } |
||
101 | |||
102 | assert engine.decide(data) == expected |
||
103 |