def to_fatality(d): """ Turn a normalized tokenized description into a Fatality object. This conversion validates the dict against the model, ensuring that each attribute matches the specification. The tokens that are not part of the model are ignored. :param dict d: a dict representing the tokenized version of the description. :return: either a `model.Report` object representing the dict, or the list of errors preventing its creation. :rtype: tuple(model.Report, list()) """ fatality = None err = [] # Prepare the dict to populate the report. dd = {k.lower(): v for k, v in d.items()} try: fatality = model.Fatality(**dd) except ValidationError as e: err = [ f"{error['loc'][0]}: \"{dd.get(error['loc'][0])}\" {error['msg']} ({error['type']})" for error in e.errors() ] return fatality, err
def test_fatality_model_01(self): """Ensure a full model works.""" m = model.Fatality( age=40, dob=datetime.datetime.now().date(), ethnicity=model.Ethnicity.undefined, first="first", gender=model.Gender.undefined, generation="generation", last="last", middle="middle", ) assert m is not None for _, v in m.dict().items(): assert v
def test_report_model_01(self): """Ensure a full model works.""" m = model.Report( case='19-123456', date=datetime.datetime.now().date(), crash=1, latitude=30.222337, link=fake.uri(), location=fake.pystr(), longitude=-97.678343, notes=fake.paragraph(), time=datetime.datetime.now().time(), fatalities=[model.Fatality()], ) assert m is not None for _, v in m.dict().items(): assert v
def load_scrapd2(entry): """ Load a ScrAPD 2 entry and returns a ScrAPD3 one. """ r = model.Report(case=entry['Case'], date=date_utils.parse_date(entry.get('Date'))) if entry.get('Fatal crashes this year'): r.crash = int(entry.get('Fatal crashes this year')) if entry.get('Link'): r.link = entry.get('Link') if entry.get('Latitude'): r.latitude = entry.get('Latitude') if entry.get('Location'): r.location = entry.get('Location') if entry.get('Longitude'): r.longitude = entry.get('Longitude') if entry.get('Notes'): r.notes = entry.get('Notes') if entry.get('Time'): r.time = date_utils.parse_time(entry.get('Time')) f = model.Fatality() if entry.get('Age'): f.age = int(entry.get('Age')) if entry.get('DOB'): f.dob = date_utils.parse_date(entry.get('DOB')) if entry.get('Ethnicity'): try: f.ethnicity = model.Ethnicity(entry.get('Ethnicity').capitalize()) except ValueError: f.ethnicity = model.Ethnicity.undefined if entry.get('First Name'): f.first = entry.get('First Name') if entry.get('Gender'): try: f.gender = model.Gender(entry.get('Gender').capitalize()) except ValueError: f.gender = model.Gender.undefined if entry.get('Last Name'): f.last = entry.get('Last Name') r.fatalities = [f] r.compute_fatalities_age() return r
"both pronounced on scene. The van driver remained on scene and is cooperating with the ongoing investigation. " "The family of Cedric Benson respectfully requests privacy during this difficult time and asks that media refrain " "from contacting them.", "Time": "10:20 PM" }] SCRAPD3_ENTRY = model.Report( case='19-2291933', crash=50, date=datetime.date(2019, 8, 17), fatalities=[ model.Fatality( age=36, dob=datetime.date(1982, 12, 28), ethnicity=model.Ethnicity.black, first='Cedric', gender=model.Gender.male, last='Benson', ), ], link='http://austintexas.gov/news/traffic-fatality-50-3', location='4500 FM 2222/Mount Bonnell Road', notes= 'The preliminary investigation yielded testimony from witnesses who reported seeing the ' 'BMW motorcycle driven by Cedric Benson traveling at a high rate of speed westbound in the left ' 'lane of FM 2222. A white, 2014 Dodge van was stopped at the T-intersection of Mount Bonnell Road ' 'and FM 2222. After checking for oncoming traffic, the van attempted to turn left on to FM 2222 ' 'when it was struck by the oncoming motorcycle. The driver of the van was evaluated by EMS ' 'on scene and refused transport. The passenger of the van and a bystander at the scene attempted ' 'to render aid to Mr. Benson and his passenger Aamna Najam. Cedric Benson and Aamna Najam were both ' 'pronounced on scene. The van driver remained on scene and is cooperating with the ongoing '
Location: 1400 E. Highway 71 eastbound Deceased: Corbin Sabillon-Garcia, White male, DOB 02/09/80 The preliminary investigation shows that a 2003 Ford F150 was traveling northbound on the US Highway 183 northbound ramp to E. Highway 71, eastbound. The truck went across the E. Highway 71 and US Highway 183 ramp, rolled and came to a stop north of the roadway. """, 'expected': model.Report( case='18-3640187', crash=73, date=datetime.date(2018, 12, 30), fatalities=[ model.Fatality( age=38, dob=datetime.date(1980, 2, 9), ethnicity=model.Ethnicity.white, first='Corbin', gender=model.Gender.male, last='Sabillon-Garcia', middle='', ), ], location='1400 E. Highway 71 eastbound', time=datetime.time(2, 24), ), 'errors': None, }, { 'id': 'no-notes', 'page':
page_scenarios = [ { 'id': 'single fatality', 'page': 'traffic-fatality-2-3', 'expected': model.Report( case='19-0161105', crash=2, date=datetime.date(2019, 1, 16), fatalities=[ model.Fatality( age=58, dob=datetime.date(1960, 2, 15), ethnicity=model.Ethnicity.white, first='Ann', gender=model.Gender.female, last='Bottenfield-Seago', ), ], location='West William Cannon Drive and Ridge Oak Road', notes= 'The preliminary investigation shows that the grey, 2003 Volkwagen ' 'Jetta being driven by Ann Bottenfield-Seago failed to yield at a ' 'stop sign while attempting to turn westbound on to West William ' 'Cannon Drive from Ridge Oak Road. The Jetta collided with a black, ' '2017 Chevrolet truck that was eastbound in the inside lane of West ' 'William Cannon Drive. Bottenfield-Seago was pronounced deceased at ' 'the scene. The passenger in the Jetta and the driver of the truck ' 'were both transported to a local hospital with non-life threatening ' 'injuries. No charges are expected to be filed.',
dob_search_scenarios = [ { 'input_': 'Rosbel “Rudy” Tamez, Hispanic male (D.O.B. 10-10-54)', 'expected': 5, 'id': '(D.O.B.)', }, ] deceased_scenarios = [ { 'input_': 'Rosbel “Rudy” Tamez, Hispanic male (D.O.B. 10-10-54)', 'expected': [ model.Fatality( dob=datetime.date(1954, 10, 10), ethnicity=model.Ethnicity.hispanic, first='Rosbel', gender=model.Gender.male, last='Tamez', middle='“Rudy”', ) ], 'id': 'comma-delimited', }, { 'input_': 'Eva Marie Gonzales, W/F, DOB: 01-22-1961 (passenger)', 'expected': [ model.Fatality( dob=datetime.date(1961, 1, 22), ethnicity=model.Ethnicity.white, first='Eva', gender=model.Gender.female, last='Gonzales',
def test_age_must_be_positive(self): """Ensure the age field is always positive.""" with pytest.raises(ValueError): model.Fatality(age=-1)
def test_fatality_model_00(self): """Ensure an empty model works.""" m = model.Fatality() assert m is not None for _, v in m.dict().items(): assert isinstance(v, Enum) or not v
import pytest from scrapd.core import model from scrapd.core.constant import Fields # Set faker object. fake = Faker() compute_age_scenarios = [ { 'input_': model.Report( case='19-123456', date=datetime.date(2019, 1, 16), fatalities=[ model.Fatality(dob=datetime.date(1960, 2, 15)), ], ), 'expected': [58], 'id': 'single-regular', }, { 'input_': model.Report( case='19-123456', date=datetime.date(2019, 1, 16), fatalities=[ model.Fatality(age=100), ], ),