def test_person_summary(): s = Summarizer() people = [ Person( id=ocd_uuid("person"), name="Jorna Corno", gender="F", image="https://example.com/image1", party=[ Party(name="Democratic"), Party(name="Democratic", end_date="1990") ], roles=[], ), Person( id=ocd_uuid("person"), name="Linda Under", gender="F", image="https://example.com/image2", party=[Party(name="Democratic"), Party(name="Progressive")], extras={"religion": "Zoroastrian"}, contact_details=[ ContactDetail(fax="123-435-9999", note="Capitol Office") ], other_identifiers=[ OtherIdentifier(scheme="fake", identifier="abc") ], ids=PersonIdBlock(twitter="fake"), roles=[], ), Person( id=ocd_uuid("person"), name="Phil Gort", gender="M", image="https://example.com/image3", party=[Party(name="Republican")], contact_details=[ ContactDetail(voice="123-435-9999", note="Capitol Office") ], other_identifiers=[ OtherIdentifier(scheme="fake", identifier="123") ], roles=[], ), ] for p in people: s.summarize(p) assert s.parties == {"Republican": 1, "Democratic": 2, "Progressive": 1} assert s.contact_counts == { "Capitol Office voice": 1, "Capitol Office fax": 1 } assert s.id_counts == {"fake": 2, "twitter": 1} assert s.optional_fields == {"gender": 3, "image": 3} assert s.extra_counts == {"religion": 1}
def test_party_on_person(): p = Person( id=VALID_PERSON_ID, name="Tony Tigre", party=[Party(name="Democratic")], roles=[], ) with pytest.raises(ValidationError): # no such party p.party = [Party(name="Vampire")]
def test_compute_merge_party(): class Model(BaseModel): party: list[Party] = [] # no change a = Model(party=[Party(name="Democratic")]) b = Model(party=[Party(name="Democratic")]) assert compute_merge(a, b) == [] # set end date on prior party c = Model(party=[Party(name="Republican")]) today = datetime.date.today().strftime("%Y-%m-%d") assert compute_merge(a, c) == [ Replace( "party", [ Party(name="Democratic"), ], [ Party(name="Democratic", end_date=today), Party(name="Republican"), ], ) ] # extra data isn't affected d = Model(party=[ Party(name="Independent", end_date=today), Party(name="Republican"), ]) assert compute_merge(d, c) == []
def test_person_commas(): with pytest.raises(ValidationError): Person( id="ocd-person/11111111-2222-3333-4444-555555555555", name="Jones, Joan", party=[Party(name="Democratic")], roles=[], ) good_comma = Person( id="ocd-person/11111111-2222-3333-4444-555555555555", name="Joan Jones, Jr.", party=[Party(name="Democratic")], roles=[], ) assert good_comma.name
def test_retire_person(): person = Person( id="ocd-person/11110000-2222-3333-4444-555555555555", name="Test Person", party=[ Party(name="Democratic"), ], roles=[ Role(type="lower", end_date="2000-01-01", district="1", jurisdiction=JID), # leave old end date alone Role(type="upper", start_date="2018-01-01", district="2", jurisdiction=JID), # add end date Role(type="governor", end_date="2030-01-01", jurisdiction=JID), # move up future end date ], ) person, num = retire_person(person, "2018-10-01") assert num == 2 assert person.roles[0].end_date == "2000-01-01" assert person.roles[1].end_date == "2018-10-01" assert person.roles[2].end_date == "2018-10-01" # idempotent person, num = retire_person(person, "2018-11-01") assert num == 0
def test_validate_roles_retired(roles, expected): person = Person( id=EXAMPLE_OCD_PERSON_ID, name="Example Person", roles=roles, party=[Party(name="Republican")], ) assert validate_roles(person, "roles", retired=True) == expected
def person(): PERSON_ID = "ocd-person/abcdefab-0000-1111-2222-1234567890ab" return Person( id=PERSON_ID, name="Jane Smith", party=[Party(name="Democratic")], roles=[], image="https://example.com/image", extras={"something": "special"}, )
def test_party_required_on_legislator(): p = Person( id=VALID_PERSON_ID, name="Tony Tigre", party=[Party(name="Democratic")], roles=[ Role(type=RoleType.UPPER, district=1, jurisdiction=VALID_JURISDICTION_ID) ], ) with pytest.raises(ValidationError): # no party! p.party = []
def test_person_party(person): created, updated = load_person(person) p = DjangoPerson.objects.get(pk=person.id) assert p.memberships.count() == 1 assert p.memberships.get().organization.name == "Democratic" assert p.primary_party == "Democratic" person.party.append(Party(name="Republican", end_date="2018-10-06")) created, updated = load_person(person) assert updated is True assert p.primary_party == "Democratic" p = DjangoPerson.objects.get(pk=person.id) p.memberships.count() == 2 p.memberships.exclude(end_date="").count() == 1
def test_multiple_parties(): p = Person( id=VALID_PERSON_ID, name="Tony Tigre", party=[Party(name="Democratic")], roles=[], ) with pytest.raises(ValidationError): # can't have two active major parties p.party = [Party(name="Democratic"), Party(name="Republican")] # can be in multiple parties as long as one is non-major p.party = [Party(name="Democratic"), Party(name="Green")] # or if one is obsolete p.party = [Party(name="Democratic", end_date="2010"), Party(name="Republican")]
def test_person_basics(): with pytest.raises(ValidationError): Person(name="missing fields") good = Person( id="ocd-person/11111111-2222-3333-4444-555555555555", name="Joan Jones", party=[Party(name="Democratic")], roles=[], ) assert good.name with pytest.raises(ValidationError): good.death_date = "X" with pytest.raises(ValidationError): good.birth_date = "X" with pytest.raises(ValidationError): good.birth_date = "X" with pytest.raises(ValidationError): good.id = "123" with pytest.raises(ValidationError): good.image = "/fragment"
def test_party_cls(): party = Party(name="Democratic") assert party.name with pytest.raises(ValidationError): party.end_date = "x"