From 6a79eedbcec5bf9a68bc1954c59ee47dbd79bc83 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Wed, 7 Feb 2018 14:43:09 +0000 Subject: [PATCH] Add Organisation DAO --- app/dao/organisation_dao.py | 25 ++++++++++++ tests/app/dao/test_organisation_dao.py | 53 ++++++++++++++++++++++++++ tests/app/db.py | 13 +++++++ 3 files changed, 91 insertions(+) create mode 100644 app/dao/organisation_dao.py create mode 100644 tests/app/dao/test_organisation_dao.py diff --git a/app/dao/organisation_dao.py b/app/dao/organisation_dao.py new file mode 100644 index 000000000..fd5b863b2 --- /dev/null +++ b/app/dao/organisation_dao.py @@ -0,0 +1,25 @@ +from app import db +from app.dao.dao_utils import transactional +from app.models import Organisation + + +def dao_get_organisations(): + return Organisation.query.order_by( + Organisation.active.desc(), Organisation.name.asc() + ).all() + + +def dao_get_organisation_by_id(organisation_id): + return Organisation.query.filter_by(id=organisation_id).one() + + +@transactional +def dao_create_organisation(organisation): + db.session.add(organisation) + + +@transactional +def dao_update_organisation(organisation, **kwargs): + for key, value in kwargs.items(): + setattr(organisation, key, value) + db.session.add(organisation) diff --git a/tests/app/dao/test_organisation_dao.py b/tests/app/dao/test_organisation_dao.py new file mode 100644 index 000000000..0ab662e31 --- /dev/null +++ b/tests/app/dao/test_organisation_dao.py @@ -0,0 +1,53 @@ +from app.dao.organisation_dao import ( + dao_get_organisations, + dao_get_organisation_by_id, + dao_update_organisation, +) +from app.models import Organisation + +from tests.app.db import create_organisation + + +def test_get_organisations_gets_all_organisations_alphabetically_with_active_organisations_first( + notify_db, + notify_db_session +): + m_active_org = create_organisation(name='m_active_organisation') + z_inactive_org = create_organisation(name='z_inactive_organisation', active=False) + a_inactive_org = create_organisation(name='a_inactive_organisation', active=False) + z_active_org = create_organisation(name='z_active_organisation') + a_active_org = create_organisation(name='a_active_organisation') + + organisations = dao_get_organisations() + + assert len(organisations) == 5 + assert organisations[0] == a_active_org + assert organisations[1] == m_active_org + assert organisations[2] == z_active_org + assert organisations[3] == a_inactive_org + assert organisations[4] == z_inactive_org + + +def test_get_organisation_by_id_gets_correct_organisation(notify_db, notify_db_session): + organisation = create_organisation() + + organisation_from_db = dao_get_organisation_by_id(organisation.id) + + assert organisation_from_db == organisation + + +def test_update_organisation(notify_db, notify_db_session): + updated_name = 'new name' + create_organisation() + + organisation = Organisation.query.all() + + assert len(organisation) == 1 + assert organisation[0].name != updated_name + + dao_update_organisation(organisation[0], name=updated_name) + + organisation = Organisation.query.all() + + assert len(organisation) == 1 + assert organisation[0].name == updated_name diff --git a/tests/app/db.py b/tests/app/db.py index d35d2b275..bd55e40a3 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -14,6 +14,7 @@ from app.models import ( MonthlyBilling, Notification, EmailBranding, + Organisation, Rate, Service, ServiceEmailReplyTo, @@ -41,6 +42,7 @@ from app.dao.services_dao import dao_create_service from app.dao.service_permissions_dao import dao_add_service_permission from app.dao.inbound_sms_dao import dao_create_inbound_sms from app.dao.email_branding_dao import dao_create_email_branding +from app.dao.organisation_dao import dao_create_organisation def create_user(mobile_number="+447700900986", email="notify@digital.cabinet-office.gov.uk", state='active'): @@ -479,3 +481,14 @@ def create_letter_rate( db.session.commit() return rate + + +def create_organisation(name='test_org_1', active=True): + data = { + 'name': name, + 'active': active + } + organisation = Organisation(**data) + dao_create_organisation(organisation) + + return organisation