|
| 1 | +"""Tests for the structured job postings widget + field.""" |
| 2 | + |
| 3 | +from django.http import QueryDict |
| 4 | +from django.test import TestCase, override_settings |
| 5 | +from model_bakery import baker |
| 6 | + |
| 7 | +from apps.sponsors.models import RequiredTextAsset |
| 8 | +from apps.sponsors.structured_job_postings import ( |
| 9 | + StructuredJobPostingsField, |
| 10 | + StructuredJobPostingsWidget, |
| 11 | + parse_structured_job_postings, |
| 12 | + serialize_structured_job_postings, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class ParseStructuredJobPostingsTests(TestCase): |
| 17 | + def test_empty_returns_empty_list(self): |
| 18 | + self.assertEqual(parse_structured_job_postings(""), []) |
| 19 | + self.assertEqual(parse_structured_job_postings(None), []) |
| 20 | + |
| 21 | + def test_two_field_no_location(self): |
| 22 | + result = parse_structured_job_postings("Engineer | https://example.com/1") |
| 23 | + self.assertEqual( |
| 24 | + result, |
| 25 | + [{"title": "Engineer", "location": "", "url": "https://example.com/1"}], |
| 26 | + ) |
| 27 | + |
| 28 | + def test_three_field_with_location(self): |
| 29 | + result = parse_structured_job_postings("Engineer | Remote | https://example.com/1") |
| 30 | + self.assertEqual( |
| 31 | + result, |
| 32 | + [{"title": "Engineer", "location": "Remote", "url": "https://example.com/1"}], |
| 33 | + ) |
| 34 | + |
| 35 | + def test_multiple_rows(self): |
| 36 | + text = "A | https://a.example.com\nB | NYC | https://b.example.com" |
| 37 | + self.assertEqual( |
| 38 | + parse_structured_job_postings(text), |
| 39 | + [ |
| 40 | + {"title": "A", "location": "", "url": "https://a.example.com"}, |
| 41 | + {"title": "B", "location": "NYC", "url": "https://b.example.com"}, |
| 42 | + ], |
| 43 | + ) |
| 44 | + |
| 45 | + def test_legacy_markdown_preserved_as_title_only_row(self): |
| 46 | + text = "Please highlight these roles below\n- see careers page" |
| 47 | + result = parse_structured_job_postings(text) |
| 48 | + self.assertEqual(len(result), 2) |
| 49 | + self.assertEqual(result[0]["title"], "Please highlight these roles below") |
| 50 | + self.assertEqual(result[0]["url"], "") |
| 51 | + |
| 52 | + def test_crlf_and_bom_handled(self): |
| 53 | + text = "\ufeffA | https://a.example.com\r\nB | https://b.example.com\r\n" |
| 54 | + result = parse_structured_job_postings(text) |
| 55 | + self.assertEqual([r["title"] for r in result], ["A", "B"]) |
| 56 | + |
| 57 | + |
| 58 | +class SerializeStructuredJobPostingsTests(TestCase): |
| 59 | + def test_drops_empty_rows(self): |
| 60 | + rows = [ |
| 61 | + {"title": "", "location": "", "url": ""}, |
| 62 | + {"title": "Engineer", "location": "", "url": "https://example.com"}, |
| 63 | + ] |
| 64 | + self.assertEqual( |
| 65 | + serialize_structured_job_postings(rows), |
| 66 | + "Engineer | https://example.com", |
| 67 | + ) |
| 68 | + |
| 69 | + def test_omits_location_when_blank(self): |
| 70 | + rows = [{"title": "A", "location": "", "url": "https://a.example.com"}] |
| 71 | + self.assertEqual( |
| 72 | + serialize_structured_job_postings(rows), |
| 73 | + "A | https://a.example.com", |
| 74 | + ) |
| 75 | + |
| 76 | + def test_roundtrip(self): |
| 77 | + text = "Engineer | Remote | https://example.com/1\nAdvocate | Pittsburgh | https://example.com/2" |
| 78 | + self.assertEqual( |
| 79 | + serialize_structured_job_postings(parse_structured_job_postings(text)), |
| 80 | + text, |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +class StructuredJobPostingsWidgetTests(TestCase): |
| 85 | + def test_format_value_pads_blank_rows(self): |
| 86 | + widget = StructuredJobPostingsWidget() |
| 87 | + rows = widget.format_value("") |
| 88 | + self.assertGreaterEqual(len(rows), 15) |
| 89 | + self.assertTrue(all(r["title"] == "" for r in rows)) |
| 90 | + |
| 91 | + def test_format_value_preserves_filled_rows_then_pads(self): |
| 92 | + widget = StructuredJobPostingsWidget() |
| 93 | + rows = widget.format_value("Engineer | Remote | https://example.com/1") |
| 94 | + self.assertEqual(rows[0], {"title": "Engineer", "location": "Remote", "url": "https://example.com/1"}) |
| 95 | + self.assertGreaterEqual(len(rows), 15) |
| 96 | + |
| 97 | + def test_value_from_datadict_composes_rows(self): |
| 98 | + widget = StructuredJobPostingsWidget() |
| 99 | + data = QueryDict(mutable=True) |
| 100 | + data.setlist("jobs__title", ["Engineer", "Advocate", ""]) |
| 101 | + data.setlist("jobs__location", ["Remote", "Pittsburgh, PA", ""]) |
| 102 | + data.setlist("jobs__url", ["https://example.com/1", "https://example.com/2", ""]) |
| 103 | + value = widget.value_from_datadict(data, {}, "jobs") |
| 104 | + self.assertEqual( |
| 105 | + value, |
| 106 | + "Engineer | Remote | https://example.com/1\nAdvocate | Pittsburgh, PA | https://example.com/2", |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +@override_settings(STRUCTURED_JOB_POSTINGS_INTERNAL_NAMES=("job_listings",)) |
| 111 | +class RequiredTextAssetAsFormFieldTests(TestCase): |
| 112 | + def test_job_listings_internal_name_uses_structured_field(self): |
| 113 | + asset = baker.prepare( |
| 114 | + RequiredTextAsset, |
| 115 | + internal_name="job_listings_for_us_pycon_org_2026", |
| 116 | + label="Job listings", |
| 117 | + help_text="", |
| 118 | + max_length=None, |
| 119 | + ) |
| 120 | + field = asset.as_form_field() |
| 121 | + self.assertIsInstance(field, StructuredJobPostingsField) |
| 122 | + |
| 123 | + def test_unrelated_internal_name_uses_default_textarea(self): |
| 124 | + asset = baker.prepare( |
| 125 | + RequiredTextAsset, |
| 126 | + internal_name="general_text", |
| 127 | + label="Some text", |
| 128 | + help_text="", |
| 129 | + max_length=None, |
| 130 | + ) |
| 131 | + field = asset.as_form_field() |
| 132 | + self.assertNotIsInstance(field, StructuredJobPostingsField) |
| 133 | + |
| 134 | + @override_settings(STRUCTURED_JOB_POSTINGS_INTERNAL_NAMES=()) |
| 135 | + def test_unconfigured_setting_disables_structured(self): |
| 136 | + asset = baker.prepare( |
| 137 | + RequiredTextAsset, |
| 138 | + internal_name="job_listings_for_us_pycon_org_2026", |
| 139 | + label="Job listings", |
| 140 | + help_text="", |
| 141 | + max_length=None, |
| 142 | + ) |
| 143 | + field = asset.as_form_field() |
| 144 | + self.assertNotIsInstance(field, StructuredJobPostingsField) |
0 commit comments