Skip to content

Commit f8b0ba5

Browse files
committed
Add showTotal attribute to GraphIndicator
- Add showTotal attribute to GraphIndicator class with default value True - Include showTotal in response when processing indicators - Add comprehensive tests for showTotal functionality in both graph_spec.py and processor_spec.py - Maintain backward compatibility with default showTotal=True
1 parent 047ee36 commit f8b0ba5

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

ooui/graph/indicator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def __init__(self, graph_type, element):
2424
) or None
2525
self._show_percent = parse_bool_attribute(
2626
element.get('showPercent')) if element.get('showPercent') else False
27+
self._show_total = parse_bool_attribute(
28+
element.get('showTotal')) if element.get('showTotal') else True
2729
self._progressbar = parse_bool_attribute(
2830
element.get('progressbar')) if element.get('progressbar') else False
2931
self.domain_parse_values = {}
@@ -44,6 +46,10 @@ def total_domain(self):
4446
def show_percent(self):
4547
return self._show_percent
4648

49+
@property
50+
def show_total(self):
51+
return self._show_total
52+
4753
@property
4854
def progressbar(self):
4955
return self._progressbar
@@ -71,6 +77,7 @@ def process(self, value, total=0):
7177
res['progressbar'] = self.progressbar
7278
if self.show_percent:
7379
res['showPercent'] = self.show_percent
80+
res['showTotal'] = self.show_total
7481
if not self.show_percent and not self.progressbar:
7582
res.pop('percent', None)
7683
return res

spec/graph/graph_spec.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
expect(graph.fields).to(contain_only('potencia'))
3434
expect(graph.total_domain).to(be_none)
3535
expect(graph.show_percent).to(be_true)
36+
expect(graph.show_total).to(be_true)
3637
expect(graph.progressbar).to(be_false)
3738
expect(graph.suffix).to(equal('kW'))
3839

@@ -73,6 +74,50 @@
7374
expect(result).not_to(have_key('progressbar'))
7475
expect(result).not_to(have_key('showPercent'))
7576

77+
with it('should include showTotal by default'):
78+
xml = """<?xml version="1.0"?>
79+
<graph string="My indicator" type="indicator" />
80+
"""
81+
graph = parse_graph(xml)
82+
expect(graph.show_total).to(be_true)
83+
result = graph.process(50, 100)
84+
expect(result).to(have_key('showTotal', True))
85+
86+
with it('should support showTotal="0" to disable'):
87+
xml = """<?xml version="1.0"?>
88+
<graph string="My indicator" showTotal="0" type="indicator" />
89+
"""
90+
graph = parse_graph(xml)
91+
expect(graph.show_total).to(be_false)
92+
result = graph.process(50, 100)
93+
expect(result).to(have_key('showTotal', False))
94+
95+
with it('should support showTotal="1" explicitly'):
96+
xml = """<?xml version="1.0"?>
97+
<graph string="My indicator" showTotal="1" type="indicator" />
98+
"""
99+
graph = parse_graph(xml)
100+
expect(graph.show_total).to(be_true)
101+
result = graph.process(50, 100)
102+
expect(result).to(have_key('showTotal', True))
103+
104+
with it('should always include showTotal in response'):
105+
xml = """<?xml version="1.0"?>
106+
<graph string="My indicator" type="indicator" />
107+
"""
108+
graph = parse_graph(xml)
109+
result = graph.process(50, 100)
110+
expect(result).to(have_key('showTotal'))
111+
expect(result['showTotal']).to(be_true)
112+
113+
xml_false = """<?xml version="1.0"?>
114+
<graph string="My indicator" showTotal="0" type="indicator" />
115+
"""
116+
graph_false = parse_graph(xml_false)
117+
result_false = graph_false.process(50, 100)
118+
expect(result_false).to(have_key('showTotal'))
119+
expect(result_false['showTotal']).to(be_false)
120+
76121
with it("should parse a chart graph XML with type line"):
77122
xml = """<?xml version="1.0"?>
78123
<graph type="line" y_range="auto">

spec/graph/processor_spec.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def get_graph_data(xml, model):
6868
icon='slack',
6969
suffix='kW',
7070
type='indicatorField',
71+
showTotal=True,
7172
))
7273

7374
with it('should process indicator graph'):
@@ -91,6 +92,31 @@ def get_graph_data(xml, model):
9192
type='indicator',
9293
))
9394

95+
with it('should process indicatorField graph with showTotal="0"'):
96+
xml = """<?xml version="1.0"?>
97+
<graph string="My indicator" showPercent="1" showTotal="0" type="indicatorField" color="red:value>0;green:value==0" totalDomain="[]" icon="slack" suffix="kW">
98+
<field name="potencia" operator="+" />
99+
</graph>
100+
"""
101+
total_values = models['polissa'].data
102+
t20A_values = [v for v in total_values if v['tarifa'][1] == "2.0A"]
103+
g = parse_graph(xml)
104+
result = g.process(
105+
t20A_values,
106+
fields=models['polissa'].fields,
107+
total_values=total_values
108+
)
109+
expect(result).to(have_keys(
110+
value=77.72,
111+
percent=28.19,
112+
total=275.72,
113+
color='red',
114+
icon='slack',
115+
suffix='kW',
116+
type='indicatorField',
117+
showTotal=False,
118+
))
119+
94120
# Un test per quan el total sigui 0 no falli al calcular el percentatge
95121
with it('should evaluate percent when total is 0'):
96122
xml = """<?xml version="1.0"?>

0 commit comments

Comments
 (0)