From 1aeda84735c0e3850b2be74a237203b422633736 Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Wed, 10 Mar 2021 16:41:28 +0100 Subject: [PATCH] Two db request wrappers --- app/db_mgr.py | 15 +++++++++++++++ tests/backend_tests.py | 14 +++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app/db_mgr.py b/app/db_mgr.py index e69de29..a3bfa2c 100644 --- a/app/db_mgr.py +++ b/app/db_mgr.py @@ -0,0 +1,15 @@ +from app.models import db, Agent, Charge + + +def agents(): + all_agents = [a.firstname for a in Agent.query.all()] + return all_agents + + +def charges_by_agent(agent_id): + # all_charges = db.session.query(Charge).all() + sql_txt = "select sum(charge_rate), p.name from charge inner join period p on charge.period_id = p.id\ + where agent_id={} group by period_id order by p.name"\ + .format(agent_id) + request = db.session.execute(sql_txt) + return request.fetchall() diff --git a/tests/backend_tests.py b/tests/backend_tests.py index 27c1fed..cb9f245 100644 --- a/tests/backend_tests.py +++ b/tests/backend_tests.py @@ -1,6 +1,6 @@ import unittest from pdc_config import TestConfig -from app import create_app +from app import create_app, db_mgr class BaseTestCase(unittest.TestCase): @@ -15,3 +15,15 @@ class BaseTestCase(unittest.TestCase): def test_always_true(self): self.assertTrue(True) + + +class DbMgrTestCase(BaseTestCase): + + def test_agents(self): + all_agents = db_mgr.agents() + print(len(all_agents)) + self.assertEqual(548, len(all_agents)) + + def test_charges_by_agent(self): + all_charges = db_mgr.charges_by_agent(355) + self.assertEqual(6, len(all_charges)) -- libgit2 0.21.2