Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from workforce-data-initiative/expose_geo
Expose geography
- Loading branch information
Showing
6 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from app import app | ||
import unittest | ||
from flask_migrate import upgrade | ||
|
||
|
||
class ApiTestCase(unittest.TestCase): | ||
def setUp(self): | ||
app.app.config.from_object('config.test_config.Config') | ||
self.app = app.app.test_client() | ||
self.app.testing = True | ||
with app.app.app_context(): | ||
upgrade() | ||
app.db.session.begin(subtransactions=True) | ||
|
||
def tearDown(self): | ||
app.db.session.rollback() | ||
app.db.session.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from app import app | ||
from factory import alchemy, LazyAttribute, Sequence | ||
from api.v1 import Geography, Quarter, JobImportance, JobMaster | ||
from faker import Faker | ||
|
||
fake = Faker() | ||
|
||
|
||
class JobMasterFactory(alchemy.SQLAlchemyModelFactory): | ||
class Meta(object): | ||
model = JobMaster | ||
sqlalchemy_session = app.db.session | ||
|
||
uuid = Sequence(lambda n: str(n)) | ||
onet_soc_code = LazyAttribute(lambda x: fake.phone_number()) | ||
title = LazyAttribute(lambda x: fake.job()) | ||
original_title = LazyAttribute(lambda x: fake.job()) | ||
description = LazyAttribute(lambda x: fake.job()) | ||
nlp_a = LazyAttribute(lambda x: fake.job()) | ||
|
||
|
||
class GeographyFactory(alchemy.SQLAlchemyModelFactory): | ||
class Meta(object): | ||
model = Geography | ||
sqlalchemy_session = app.db.session | ||
|
||
geography_id = Sequence(lambda x: x) | ||
geography_type = 'CBSA' | ||
geography_name = LazyAttribute(lambda x: fake.phone_number()) | ||
|
||
|
||
class QuarterFactory(alchemy.SQLAlchemyModelFactory): | ||
class Meta(object): | ||
model = Quarter | ||
sqlalchemy_session = app.db.session | ||
|
||
quarter_id = Sequence(lambda x: x) | ||
quarter = '3' | ||
year = '2016' | ||
|
||
|
||
class JobImportanceFactory(alchemy.SQLAlchemyModelFactory): | ||
class Meta(object): | ||
model = JobImportance | ||
sqlalchemy_session = app.db.session | ||
|
||
importance = 0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from app import app | ||
import json | ||
import unittest | ||
from tests.api_test_case import ApiTestCase | ||
|
||
from tests.factories import GeographyFactory, \ | ||
JobImportanceFactory, \ | ||
JobMasterFactory, \ | ||
QuarterFactory | ||
|
||
|
||
class JobGetTestCase(ApiTestCase): | ||
|
||
def test_with_uuid(self): | ||
job = JobMasterFactory() | ||
app.db.session.add(job) | ||
response = self.app.get('v1/jobs/{}'.format(job.uuid)) | ||
self.assertEqual(response.status_code, 200) | ||
response_data = json.loads(response.get_data()) | ||
self.assertEqual(response_data['title'], job.title) | ||
|
||
def test_with_soc_code(self): | ||
job = JobMasterFactory() | ||
app.db.session.add(job) | ||
response = self.app.get('v1/jobs/{}'.format(job.onet_soc_code)) | ||
self.assertEqual(response.status_code, 200) | ||
response_data = json.loads(response.get_data()) | ||
self.assertEqual(response_data['title'], job.title) | ||
|
||
def test_missing_geo(self): | ||
job = JobMasterFactory() | ||
app.db.session.add(job) | ||
response = self.app.get('v1/jobs/{}?fips=40500'.format(job.uuid)) | ||
self.assertEqual(response.status_code, 404) | ||
|
||
def test_geo_mismatch(self): | ||
job = JobMasterFactory() | ||
right_geography = GeographyFactory() | ||
wrong_geography = GeographyFactory() | ||
quarter = QuarterFactory() | ||
app.db.session.begin(subtransactions=True) | ||
app.db.session.add(job) | ||
app.db.session.add(right_geography) | ||
app.db.session.add(wrong_geography) | ||
app.db.session.add(quarter) | ||
app.db.session.commit() | ||
importance = JobImportanceFactory( | ||
geography_id=right_geography.geography_id, | ||
quarter_id=quarter.quarter_id, | ||
job_uuid=job.uuid | ||
) | ||
app.db.session.add(importance) | ||
response = self.app.get( | ||
'v1/jobs/{}?fips={}'.format( | ||
job.uuid, | ||
wrong_geography.geography_name | ||
) | ||
) | ||
self.assertEqual(response.status_code, 404) | ||
|
||
def test_geo_match(self): | ||
job = JobMasterFactory() | ||
right_geography = GeographyFactory() | ||
wrong_geography = GeographyFactory() | ||
quarter = QuarterFactory() | ||
app.db.session.begin(subtransactions=True) | ||
app.db.session.add(job) | ||
app.db.session.add(right_geography) | ||
app.db.session.add(wrong_geography) | ||
app.db.session.add(quarter) | ||
app.db.session.commit() | ||
|
||
importance = JobImportanceFactory( | ||
geography_id=right_geography.geography_id, | ||
quarter_id=quarter.quarter_id, | ||
job_uuid=job.uuid | ||
) | ||
app.db.session.add(importance) | ||
response = self.app.get( | ||
'v1/jobs/{}?fips={}'.format( | ||
job.uuid, | ||
right_geography.geography_name | ||
) | ||
) | ||
self.assertEqual(response.status_code, 200) | ||
response_data = json.loads(response.get_data()) | ||
self.assertEqual(response_data['title'], job.title) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |