diff --git a/app/db_mgr.py b/app/db_mgr.py index d4a2b6d..d2a1445 100644 --- a/app/db_mgr.py +++ b/app/db_mgr.py @@ -107,7 +107,7 @@ def charges_by_project_stacked(project_id, category="service"): raise ValueError("Waiting for 'service' or 'capacity'") categories_req = "select name from {} order by id".format(category_table) categories = [c for (c,) in db.session.execute(categories_req)] - headers = ['period']+categories + headers = ['period'] + categories all_charges = [headers] for (period_id, period_name) in db.session.execute("select id, name from period order by id"): # build the request from the string previously set @@ -122,6 +122,34 @@ def charges_by_project_stacked(project_id, category="service"): return all_charges +def charges_by_agent_stacked(agent_id): + """ + Build the list of charges for all projects of one agent, period by period + :param agent_id: + :return: + """ + categories_req = "select name from project order by id" + categories = [c for (c,) in db.session.execute(categories_req)] + headers = ['period'] + categories + all_charges = [headers] + for (period_id, period_name) in db.session.execute("select id, name from period order by id"): + charge_by_project_req = """ + select sum(c.charge_rate) + from project p + left join + charge c on p.id = c.project_id and agent_id = {} and period_id = {} + group by p.id + order by p.id + """.format(agent_id, period_id) + print(charge_by_project_req) + category_charges = [period_name] + for (category_rate,) in db.session.execute(charge_by_project_req): + category_rate = str(category_rate) if category_rate else '0' + category_charges.append(category_rate) + all_charges.append(category_charges) + return all_charges + + def charges_by_agent_tabled(agent_id): """ Build the flatten list of charges for one agent, period by period @@ -155,24 +183,6 @@ def charges_by_agent_tabled(agent_id): return nocomma_results -def charges_by_agent_stacked(agent_id): - """ - Build the list of charges for one agent, period by period - :param agent_id: - :return: - """ - all_charges = {} - for (period_id, period_name) in db.session.execute("select id, name from period order by id"): - charge_by_project_req = """select p.name, c.charge_rate from project p left join - charge c on p.id = c.project_id and agent_id = {} and period_id={} - order by p.id""".format(agent_id, period_id) - period_charge = {} - for project_name, project_rate in db.session.execute(charge_by_project_req): - period_charge[project_name] = project_rate - all_charges[period_name] = period_charge - return all_charges - - def charges_by_agent(agent_id): periods = db.session.execute("select name from period") sql_txt = "select p.name, sum(charge_rate) from charge inner join period p on charge.period_id = p.id\ diff --git a/tests/backend_tests.py b/tests/backend_tests.py index 595051c..8d64c79 100644 --- a/tests/backend_tests.py +++ b/tests/backend_tests.py @@ -1,4 +1,5 @@ import unittest + from pdc_config import TestConfig from app import create_app, db_mgr, db from app.auth.models import User @@ -11,10 +12,10 @@ class BaseTestCase(unittest.TestCase): self.app_context = self.app.app_context() self.app_context.push() # TODO: shall we always copy db from resources ? - #db.create_all() - #admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin', role='admin') - #db.session.add(admin) - #db.session.commit() + # db.create_all() + # admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin', role='admin') + # db.session.add(admin) + # db.session.commit() def tearDown(self): self.app_context.pop() @@ -34,6 +35,16 @@ class DbMgrTestCase(BaseTestCase): all_charges = db_mgr.charges_by_agent(355) self.assertEqual(17, len(all_charges)) + def test_charges_by_agent_stacked(self): + stacked_charges = db_mgr.charges_by_agent_stacked(60) + # Waiting for 17 periods + headers line + self.assertEqual(18, len(stacked_charges)) + + def test_charges_by_project_stacked(self): + stacked_charges = db_mgr.charges_by_project_stacked(60) + # Waiting for 17 periods + headers line + self.assertEqual(18, len(stacked_charges)) + class AuthModelTestCase(BaseTestCase): -- libgit2 0.21.2