Skip to content

Commit c3cea40

Browse files
authored
Merge pull request #16 from gisce/improve-max-min-zero
Return 0 for max/min when an empty sequence
2 parents 53b90fc + 22fc5a2 commit c3cea40

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

Diff for: ooui/graph/fields.py

+4
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,12 @@ def get_value_for_operator(operator, values):
8484
avg = total_sum / len(values)
8585
return round_number(avg)
8686
elif operator == "min":
87+
if not values:
88+
return 0
8789
return min(values)
8890
elif operator == "max":
91+
if not values:
92+
return 0
8993
return max(values)
9094
else:
9195
raise ValueError("Unsupported operator: {}".format(operator))

Diff for: spec/graph/fields_spec.py

+10
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,22 @@
122122
values = [10, 3, 45, 7]
123123
result = get_value_for_operator('min', values)
124124
expect(result).to(equal(3))
125+
with context('if sequence is empty'):
126+
with it('should return 0'):
127+
values = []
128+
result = get_value_for_operator('min', values)
129+
expect(result).to(equal(0))
125130

126131
with context('when operator is "max"'):
127132
with it('should return the maximum value'):
128133
values = [10, 3, 45, 7]
129134
result = get_value_for_operator('max', values)
130135
expect(result).to(equal(45))
136+
with context('if sequence is empty'):
137+
with it('should return 0'):
138+
values = []
139+
result = get_value_for_operator('max', values)
140+
expect(result).to(equal(0))
131141

132142
with context('when an unsupported operator is provided'):
133143
with it('should raise a ValueError'):

0 commit comments

Comments
 (0)