From 37df051c32cdec171b250e3b1f8e82820103ceb6 Mon Sep 17 00:00:00 2001 From: Ken Tsang Date: Wed, 5 Jul 2017 11:17:03 +0100 Subject: [PATCH] Updated Organisations DAO --- app/dao/organisations_dao.py | 5 ++++ tests/app/conftest.py | 6 ++++- tests/app/dao/test_organisations_dao.py | 36 ++++++++++++++++++++++++- tests/app/db.py | 2 +- tests/app/organisation/test_rest.py | 18 +++++++++++++ 5 files changed, 64 insertions(+), 3 deletions(-) diff --git a/app/dao/organisations_dao.py b/app/dao/organisations_dao.py index b9046816f..f8cb22090 100644 --- a/app/dao/organisations_dao.py +++ b/app/dao/organisations_dao.py @@ -14,3 +14,8 @@ def dao_get_organisation_by_id(org_id): @transactional def dao_create_organisation(organisation): db.session.add(organisation) + + +@transactional +def dao_update_organisation(organisation): + db.session.add(organisation) diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 4fb05d422..290b74f6b 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -39,7 +39,7 @@ from app.dao.invited_user_dao import save_invited_user from app.dao.provider_rates_dao import create_provider_rates from app.clients.sms.firetext import FiretextClient from tests import create_authorization_header -from tests.app.db import create_user, create_template, create_notification +from tests.app.db import create_user, create_template, create_notification, create_organisation @pytest.yield_fixture @@ -974,6 +974,10 @@ def sample_provider_rate(notify_db, notify_db_session, valid_from=None, rate=Non ) +def sample_organisation(notify_db, notify_db_session): + create_organisation() + + @pytest.fixture def restore_provider_details(notify_db, notify_db_session): """ diff --git a/tests/app/dao/test_organisations_dao.py b/tests/app/dao/test_organisations_dao.py index 5192653c5..f5e9ecd4b 100644 --- a/tests/app/dao/test_organisations_dao.py +++ b/tests/app/dao/test_organisations_dao.py @@ -1,6 +1,8 @@ import pytest -from app.dao.organisations_dao import dao_create_organisation +from app.dao.organisations_dao import ( + dao_create_organisation, dao_get_organisations, dao_get_organisation_by_id, dao_update_organisation +) from app.models import Organisation from tests.app.db import create_organisation @@ -12,3 +14,35 @@ def test_create_organisation(notify_db, notify_db_session): assert Organisation.query.count() == 1 organisation_from_db = Organisation.query.first() assert organisation == organisation_from_db + + +def test_get_organisations_gets_all_organisations(notify_db, notify_db_session): + create_organisation(name='test_org_1') + create_organisation(name='test_org_2') + + organisations = dao_get_organisations() + + assert len(organisations) == 2 + + +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' + organisation = create_organisation() + + organisation_from_db = Organisation.query.first() + assert organisation.name != updated_name + + organisation.name = updated_name + + dao_update_organisation(organisation) + organisation_from_db = Organisation.query.first() + + assert organisation_from_db.name == updated_name diff --git a/tests/app/db.py b/tests/app/db.py index 286ba943c..ea9e4cbdd 100644 --- a/tests/app/db.py +++ b/tests/app/db.py @@ -229,7 +229,7 @@ def create_service_inbound_api( return service_inbound_api -def create_organisation(colour='blue', logo='test_x2.png', name='test_logo'): +def create_organisation(colour='blue', logo='test_x2.png', name='test_org_1'): data = { 'colour': colour, 'logo': logo, diff --git a/tests/app/organisation/test_rest.py b/tests/app/organisation/test_rest.py index f024edd31..918be97cb 100644 --- a/tests/app/organisation/test_rest.py +++ b/tests/app/organisation/test_rest.py @@ -37,3 +37,21 @@ def test_get_organisation_by_id(notify_api, notify_db, notify_db_session): assert organisation['logo'] == '/path/image.png' assert organisation['name'] == 'My Org' assert organisation['id'] == str(org.id) + + +def test_create_organisation(client, notify_db, notify_db_session): + data = { + 'name': 'test organisation', + 'colour': '#0000ff', + 'logo': '/images/test_x2.png' + } + auth_header = create_authorization_header() + + response = client.post( + '/organisation', + headers=[('Content-Type', 'application/json'), auth_header], + data=json.dumps(data) + ) + assert response.status_code == 201 + json_resp = json.loads(response.get_data(as_text=True)) + assert data['name'] == json_resp['data']['name']