89 lines
3.8 KiB
Python
89 lines
3.8 KiB
Python
import importlib.util
|
|
import csv
|
|
import io
|
|
import pathlib
|
|
import tempfile
|
|
import unittest
|
|
import zipfile
|
|
from unittest import mock
|
|
|
|
|
|
ROOT = pathlib.Path(__file__).resolve().parents[2]
|
|
SPEC = importlib.util.spec_from_file_location(
|
|
"nextpath_refresh", ROOT / "scripts" / "nextpath-opportunity-refresh.py"
|
|
)
|
|
MODULE = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(MODULE)
|
|
|
|
|
|
class NextPathOpportunityRefreshTests(unittest.TestCase):
|
|
def test_award_level_labels_are_human_readable(self):
|
|
self.assertEqual(MODULE.AWARD_LEVELS["5"], "bachelor degree")
|
|
self.assertEqual(MODULE.AWARD_LEVELS["21"], "certificate 12 weeks to under one year")
|
|
|
|
def write_zip(self, path, member, fieldnames, rows):
|
|
output = io.StringIO()
|
|
writer = csv.DictWriter(output, fieldnames=fieldnames)
|
|
writer.writeheader()
|
|
writer.writerows(rows)
|
|
with zipfile.ZipFile(path, "w") as archive:
|
|
archive.writestr(member, output.getvalue())
|
|
|
|
def test_ipeds_history_uses_exact_cip_and_arizona_primary_major(self):
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
cache = pathlib.Path(temporary)
|
|
for year in MODULE.IPEDS_YEARS:
|
|
self.write_zip(
|
|
cache / f"HD{year}.zip",
|
|
f"HD{year}.csv",
|
|
["UNITID", "STABBR"],
|
|
[{"UNITID": "1", "STABBR": "AZ"}, {"UNITID": "2", "STABBR": "CA"}],
|
|
)
|
|
self.write_zip(
|
|
cache / f"C{year}_A.zip",
|
|
f"C{year}_A.csv",
|
|
["UNITID", "CIPCODE", "MAJORNUM", "CTOTALT"],
|
|
[
|
|
{"UNITID": "1", "CIPCODE": "52.0203", "MAJORNUM": "1", "CTOTALT": "103"},
|
|
{"UNITID": "1", "CIPCODE": "52.0201", "MAJORNUM": "1", "CTOTALT": "999"},
|
|
{"UNITID": "1", "CIPCODE": "52.0203", "MAJORNUM": "2", "CTOTALT": "40"},
|
|
{"UNITID": "2", "CIPCODE": "52.0203", "MAJORNUM": "1", "CTOTALT": "88"},
|
|
],
|
|
)
|
|
self.assertEqual(
|
|
MODULE.ipeds_completion_history({"520203"}, cache),
|
|
{str(year): 103 for year in MODULE.IPEDS_YEARS},
|
|
)
|
|
|
|
def test_ipeds_histories_scan_each_annual_export_once_for_all_careers(self):
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
cache = pathlib.Path(temporary)
|
|
for year in MODULE.IPEDS_YEARS:
|
|
self.write_zip(cache / f"HD{year}.zip", f"HD{year}.csv", ["UNITID", "STABBR"], [
|
|
{"UNITID": "1", "STABBR": "AZ"},
|
|
])
|
|
self.write_zip(cache / f"C{year}_A.zip", f"C{year}_A.csv", ["UNITID", "CIPCODE", "MAJORNUM", "CTOTALT"], [
|
|
{"UNITID": "1", "CIPCODE": "11.0101", "MAJORNUM": "1", "CTOTALT": "10"},
|
|
{"UNITID": "1", "CIPCODE": "15.0101", "MAJORNUM": "1", "CTOTALT": "20"},
|
|
])
|
|
mapping = {"mappings": {
|
|
"15-1252.00": {"cip6": ["110101"]},
|
|
"17-2112.00": {"cip6": ["150101"]},
|
|
}}
|
|
with mock.patch.object(MODULE, "zip_rows", wraps=MODULE.zip_rows) as rows:
|
|
histories = MODULE.ipeds_completion_histories(mapping, cache)
|
|
self.assertEqual(histories["15-1252.00"]["2024"], 10)
|
|
self.assertEqual(histories["17-2112.00"]["2024"], 20)
|
|
self.assertEqual(rows.call_count, len(MODULE.IPEDS_YEARS) * 2)
|
|
|
|
def test_extracts_arizona_projection(self):
|
|
body = {"rows": [{
|
|
"Area": "Arizona", "AvgAnnualOpenings": "1,234", "BaseYear": "2024", "ProjYear": "2034",
|
|
"PercentChange": "14.5",
|
|
}]}
|
|
self.assertEqual(MODULE.projected_openings(body), (1234, "2024-2034", 14.5))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|