|
| 1 | +import sys |
| 2 | +import os |
| 3 | +from os.path import dirname, join, expanduser, normpath, realpath |
| 4 | + |
| 5 | +import pytest |
| 6 | +from graphene.test import Client |
| 7 | + |
| 8 | +from unittest import TestCase |
| 9 | + |
| 10 | +import model_enums |
| 11 | +model_enums._called_from_test = True |
| 12 | + |
| 13 | +from app import app |
| 14 | +from db import db |
| 15 | +from models import Sectors, Groups, Organizations, Domains |
| 16 | +from queries import schema |
| 17 | + |
| 18 | + |
| 19 | +# This is the only way I could get imports to work for unit testing. |
| 20 | +PACKAGE_PARENT = '..' |
| 21 | +SCRIPT_DIR = dirname(realpath(join(os.getcwd(), expanduser(__file__)))) |
| 22 | +sys.path.append(normpath(join(SCRIPT_DIR, PACKAGE_PARENT))) |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(scope='class') |
| 26 | +def domain_test_resolver_db_init(): |
| 27 | + """Build database for domain resolver testing""" |
| 28 | + db.init_app(app) |
| 29 | + |
| 30 | + sectors_added = False |
| 31 | + groups_added = False |
| 32 | + org_added = False |
| 33 | + domain_added = False |
| 34 | + |
| 35 | + with app.app_context(): |
| 36 | + if Sectors.query.first() is None: |
| 37 | + sector = Sectors( |
| 38 | + id=2, |
| 39 | + zone="GC", |
| 40 | + sector="GC_BF", |
| 41 | + description="Banking and Finance" |
| 42 | + ) |
| 43 | + db.session.add(sector) |
| 44 | + db.session.commit() |
| 45 | + sectors_added = True |
| 46 | + |
| 47 | + if Groups.query.first() is None: |
| 48 | + group = Groups( |
| 49 | + id=2, |
| 50 | + s_group='GC_BF', |
| 51 | + description='Banking and Finance', |
| 52 | + sector_id=2 |
| 53 | + ) |
| 54 | + db.session.add(group) |
| 55 | + db.session.commit() |
| 56 | + groups_added = True |
| 57 | + |
| 58 | + if Organizations.query.first() is None: |
| 59 | + org = Organizations( |
| 60 | + id=6, |
| 61 | + organization='BOC', |
| 62 | + description='BOC - Bank of Canada', |
| 63 | + group_id=2 |
| 64 | + ) |
| 65 | + db.session.add(org) |
| 66 | + db.session.commit() |
| 67 | + org_added = True |
| 68 | + |
| 69 | + if Domains.query.first() is None: |
| 70 | + domain = Domains( |
| 71 | + id=15, |
| 72 | + domain='bankofcanada.ca', |
| 73 | + organization_id=6 |
| 74 | + ) |
| 75 | + db.session.add(domain) |
| 76 | + db.session.commit() |
| 77 | + domain_added = True |
| 78 | + |
| 79 | + yield |
| 80 | + |
| 81 | + with app.app_context(): |
| 82 | + if domain_added: |
| 83 | + Domains.query.filter(Domains.id == 15).delete() |
| 84 | + db.session.commit() |
| 85 | + if org_added: |
| 86 | + Organizations.query.filter(Organizations.id == 6).delete() |
| 87 | + db.session.commit() |
| 88 | + if groups_added: |
| 89 | + Groups.query.filter(Groups.id == 2).delete() |
| 90 | + db.session.commit() |
| 91 | + if sectors_added: |
| 92 | + Sectors.query.filter(Sectors.id == 2).delete() |
| 93 | + db.session.commit() |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.usefixtures('domain_test_resolver_db_init') |
| 97 | +class TestOrgResolver(TestCase): |
| 98 | + def test_get_domain_resolvers_by_id(self): |
| 99 | + """Test get_domain_by_id resolver""" |
| 100 | + with app.app_context(): |
| 101 | + client = Client(schema) |
| 102 | + query = """ |
| 103 | + { |
| 104 | + getDomainById(id: 15){ |
| 105 | + domain |
| 106 | + } |
| 107 | + }""" |
| 108 | + |
| 109 | + result_refr = { |
| 110 | + "data": { |
| 111 | + "getDomainById": [ |
| 112 | + { |
| 113 | + "domain": "bankofcanada.ca" |
| 114 | + } |
| 115 | + ] |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + result_eval = client.execute(query) |
| 120 | + self.assertDictEqual(result_refr, result_eval) |
| 121 | + |
| 122 | + def test_get_domain_resolvers_by_domain(self): |
| 123 | + """"Test get_domain_by_domain resolver""" |
| 124 | + with app.app_context(): |
| 125 | + client = Client(schema) |
| 126 | + query = """ |
| 127 | + { |
| 128 | + getDomainByDomain(url: "bankofcanada.ca"){ |
| 129 | + domain |
| 130 | + } |
| 131 | + }""" |
| 132 | + |
| 133 | + result_refr = { |
| 134 | + "data": { |
| 135 | + "getDomainByDomain": [ |
| 136 | + { |
| 137 | + "domain": "bankofcanada.ca" |
| 138 | + } |
| 139 | + ] |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + result_eval = client.execute(query) |
| 144 | + self.assertDictEqual(result_refr, result_eval) |
| 145 | + |
| 146 | + def test_get_domain_resolvers_by_org(self): |
| 147 | + """Test get_domain_by_org_enum resolver""" |
| 148 | + with app.app_context(): |
| 149 | + client = Client(schema) |
| 150 | + query = """ |
| 151 | + { |
| 152 | + getDomainByOrganization(org: BOC){ |
| 153 | + domain |
| 154 | + } |
| 155 | + }""" |
| 156 | + result_refr = { |
| 157 | + "data": { |
| 158 | + "getDomainByOrganization": [ |
| 159 | + { |
| 160 | + "domain": "bankofcanada.ca" |
| 161 | + } |
| 162 | + ] |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + result_eval = client.execute(query) |
| 167 | + self.assertDictEqual(result_refr, result_eval) |
| 168 | + |
| 169 | + def test_domain_resolver_by_id_invalid(self): |
| 170 | + """Test get_domain_by_id invalid ID error handling""" |
| 171 | + with app.app_context(): |
| 172 | + client = Client(schema) |
| 173 | + query = """ |
| 174 | + { |
| 175 | + getDomainById(id: 9999){ |
| 176 | + domain |
| 177 | + } |
| 178 | + } |
| 179 | + """ |
| 180 | + executed = client.execute(query) |
| 181 | + |
| 182 | + assert executed['errors'] |
| 183 | + assert executed['errors'][0] |
| 184 | + assert executed['errors'][0]['message'] == "Error, Invalid ID" |
| 185 | + |
| 186 | + def test_domain_resolver_by_org_invalid(self): |
| 187 | + """Test get_domain_by_domain invalid sector error handling""" |
| 188 | + with app.app_context(): |
| 189 | + client = Client(schema) |
| 190 | + query = """ |
| 191 | + { |
| 192 | + getDomainByDomain(url: "google.ca"){ |
| 193 | + domain |
| 194 | + } |
| 195 | + } |
| 196 | + """ |
| 197 | + executed = client.execute(query) |
| 198 | + |
| 199 | + assert executed['errors'] |
| 200 | + assert executed['errors'][0] |
| 201 | + assert executed['errors'][0]['message'] == 'Error, domain does not exist' |
| 202 | + |
| 203 | + def test_domain_resolver_by_org_invalid(self): |
| 204 | + """Test get_domain_by_org invalid Zone error handling""" |
| 205 | + with app.app_context(): |
| 206 | + client = Client(schema) |
| 207 | + query = """ |
| 208 | + { |
| 209 | + getDomainByOrganization(org: fds){ |
| 210 | + domain |
| 211 | + } |
| 212 | + } |
| 213 | + """ |
| 214 | + executed = client.execute(query) |
| 215 | + |
| 216 | + assert executed['errors'] |
| 217 | + assert executed['errors'][0] |
| 218 | + assert executed['errors'][0]['message'] == f'Argument "org" has invalid value fds.\nExpected type "OrganizationsEnum", found fds.' |
0 commit comments