import importlib.util import csv import io import pathlib import tempfile import unittest import zipfile 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_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()