Commit b48d8e69a1189939e93a21048685457bb01bd68e

Authored by hitier
1 parent 7d095c38

Changed charges_by_agent_stacked returned format

Showing 2 changed files with 44 additions and 23 deletions   Show diff stats
app/db_mgr.py
... ... @@ -107,7 +107,7 @@ def charges_by_project_stacked(project_id, category="service"):
107 107 raise ValueError("Waiting for 'service' or 'capacity'")
108 108 categories_req = "select name from {} order by id".format(category_table)
109 109 categories = [c for (c,) in db.session.execute(categories_req)]
110   - headers = ['period']+categories
  110 + headers = ['period'] + categories
111 111 all_charges = [headers]
112 112 for (period_id, period_name) in db.session.execute("select id, name from period order by id"):
113 113 # build the request from the string previously set
... ... @@ -122,6 +122,34 @@ def charges_by_project_stacked(project_id, category="service"):
122 122 return all_charges
123 123  
124 124  
  125 +def charges_by_agent_stacked(agent_id):
  126 + """
  127 + Build the list of charges for all projects of one agent, period by period
  128 + :param agent_id:
  129 + :return:
  130 + """
  131 + categories_req = "select name from project order by id"
  132 + categories = [c for (c,) in db.session.execute(categories_req)]
  133 + headers = ['period'] + categories
  134 + all_charges = [headers]
  135 + for (period_id, period_name) in db.session.execute("select id, name from period order by id"):
  136 + charge_by_project_req = """
  137 + select sum(c.charge_rate)
  138 + from project p
  139 + left join
  140 + charge c on p.id = c.project_id and agent_id = {} and period_id = {}
  141 + group by p.id
  142 + order by p.id
  143 + """.format(agent_id, period_id)
  144 + print(charge_by_project_req)
  145 + category_charges = [period_name]
  146 + for (category_rate,) in db.session.execute(charge_by_project_req):
  147 + category_rate = str(category_rate) if category_rate else '0'
  148 + category_charges.append(category_rate)
  149 + all_charges.append(category_charges)
  150 + return all_charges
  151 +
  152 +
125 153 def charges_by_agent_tabled(agent_id):
126 154 """
127 155 Build the flatten list of charges for one agent, period by period
... ... @@ -155,24 +183,6 @@ def charges_by_agent_tabled(agent_id):
155 183 return nocomma_results
156 184  
157 185  
158   -def charges_by_agent_stacked(agent_id):
159   - """
160   - Build the list of charges for one agent, period by period
161   - :param agent_id:
162   - :return:
163   - """
164   - all_charges = {}
165   - for (period_id, period_name) in db.session.execute("select id, name from period order by id"):
166   - charge_by_project_req = """select p.name, c.charge_rate from project p left join
167   - charge c on p.id = c.project_id and agent_id = {} and period_id={}
168   - order by p.id""".format(agent_id, period_id)
169   - period_charge = {}
170   - for project_name, project_rate in db.session.execute(charge_by_project_req):
171   - period_charge[project_name] = project_rate
172   - all_charges[period_name] = period_charge
173   - return all_charges
174   -
175   -
176 186 def charges_by_agent(agent_id):
177 187 periods = db.session.execute("select name from period")
178 188 sql_txt = "select p.name, sum(charge_rate) from charge inner join period p on charge.period_id = p.id\
... ...
tests/backend_tests.py
1 1 import unittest
  2 +
2 3 from pdc_config import TestConfig
3 4 from app import create_app, db_mgr, db
4 5 from app.auth.models import User
... ... @@ -11,10 +12,10 @@ class BaseTestCase(unittest.TestCase):
11 12 self.app_context = self.app.app_context()
12 13 self.app_context.push()
13 14 # TODO: shall we always copy db from resources ?
14   - #db.create_all()
15   - #admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin', role='admin')
16   - #db.session.add(admin)
17   - #db.session.commit()
  15 + # db.create_all()
  16 + # admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin', role='admin')
  17 + # db.session.add(admin)
  18 + # db.session.commit()
18 19  
19 20 def tearDown(self):
20 21 self.app_context.pop()
... ... @@ -34,6 +35,16 @@ class DbMgrTestCase(BaseTestCase):
34 35 all_charges = db_mgr.charges_by_agent(355)
35 36 self.assertEqual(17, len(all_charges))
36 37  
  38 + def test_charges_by_agent_stacked(self):
  39 + stacked_charges = db_mgr.charges_by_agent_stacked(60)
  40 + # Waiting for 17 periods + headers line
  41 + self.assertEqual(18, len(stacked_charges))
  42 +
  43 + def test_charges_by_project_stacked(self):
  44 + stacked_charges = db_mgr.charges_by_project_stacked(60)
  45 + # Waiting for 17 periods + headers line
  46 + self.assertEqual(18, len(stacked_charges))
  47 +
37 48  
38 49 class AuthModelTestCase(BaseTestCase):
39 50  
... ...