def test_links_can_be_saved_with_valid_urls(mockdata): good_url = 'http://www.rachel.com' li = Link(link_type='video', url=good_url) db.session.add(li) db.session.commit() saved = Link.query.filter_by(url=good_url).first() assert saved is not None
def test_incident_m2m_links(mockdata): incident = Incident.query.first() link = Link(link_type='video', url='http://www.lulz.com') incident.links.append(link) db.session.add(incident) db.session.add(link) db.session.commit() assert link in incident.links assert incident in link.incidents
def test_links_must_have_valid_urls(mockdata): bad_url = 'www.rachel.com' with raises(ValueError): Link(link_type='video', url=bad_url)
def test_advanced_csv_import__success(session, department_with_ranks, test_csv_dir): # make sure department name aligns with the csv files assert department_with_ranks.name == "Springfield Police Department" # set up existing data officer = Officer( id=49483, department_id=1, first_name="Already", last_name="InDatabase", birth_year=1951, ) session.add(officer) assignment = Assignment( id=77021, officer_id=officer.id, star_no="4567", star_date=datetime.date(2020, 1, 1), job_id=department_with_ranks.jobs[0].id, ) session.add(assignment) salary = Salary( id=33001, salary=30000, officer_id=officer.id, year=2018, is_fiscal_year=False, ) session.add(salary) incident = Incident( id=123456, report_number="Old_Report_Number", department_id=1, description="description", time=datetime.time(23, 45, 16), ) incident.officers = [officer] session.add(incident) link = Link(id=55051, title="Existing Link", url="https://www.example.org") session.add(link) officer.links = [link] # run command with the csv files in the test_csvs folder result = run_command_print_output( advanced_csv_import, [ str(department_with_ranks.name), "--officers-csv", os.path.join(test_csv_dir, "officers.csv"), "--assignments-csv", os.path.join(test_csv_dir, "assignments.csv"), "--salaries-csv", os.path.join(test_csv_dir, "salaries.csv"), "--links-csv", os.path.join(test_csv_dir, "links.csv"), "--incidents-csv", os.path.join(test_csv_dir, "incidents.csv"), ], ) # command did not fail assert result.exception is None assert result.exit_code == 0 print(list(Officer.query.all())) all_officers = { officer.unique_internal_identifier: officer for officer in Officer.query.filter_by(department_id=1).all() } # make sure all the data is imported as expected cop1 = all_officers["UID-1"] assert cop1.first_name == "Mark" assert cop1.last_name == "Smith" assert cop1.gender == "M" assert cop1.race == "WHITE" assert cop1.employment_date == datetime.date(2019, 7, 12) assert cop1.birth_year == 1984 assert cop1.middle_initial == "O" assert cop1.suffix is None salary_2018, salary_2019 = sorted(cop1.salaries, key=operator.attrgetter("year")) assert salary_2018.year == 2018 assert salary_2018.salary == 10000 assert salary_2018.is_fiscal_year is True assert salary_2018.overtime_pay is None assert salary_2019.salary == 10001 assignment_po, assignment_cap = sorted( cop1.assignments, key=operator.attrgetter("star_date")) assert assignment_po.star_no == "1234" assert assignment_po.star_date == datetime.date(2019, 7, 12) assert assignment_po.resign_date == datetime.date(2020, 1, 1) assert assignment_po.job.job_title == "Police Officer" assert assignment_po.unit_id is None assert assignment_cap.star_no == "2345" assert assignment_cap.job.job_title == "Captain" cop2 = all_officers["UID-2"] assert cop2.first_name == "Claire" assert cop2.last_name == "Fuller" assert cop2.suffix == "III" assert len(cop2.salaries) == 1 assert cop2.salaries[0].salary == 20000 assert len(cop2.assignments.all()) == 1 assert cop2.assignments[0].job.job_title == "Commander" cop3 = all_officers["UID-3"] assert cop3.first_name == "Robert" assert cop3.last_name == "Brown" assert len(cop3.assignments.all()) == 0 assert len(cop3.salaries) == 0 cop4 = all_officers["UID-4"] assert cop4.id == 49483 assert cop4.first_name == "Already" assert cop4.birth_year == 1952 assert cop4.gender == "Other" assert cop4.salaries[0].salary == 50000 assert len(cop4.assignments.all()) == 2 updated_assignment, new_assignment = sorted( cop4.assignments, key=operator.attrgetter("star_date")) assert updated_assignment.job.job_title == "Police Officer" assert updated_assignment.resign_date == datetime.date(2020, 7, 10) assert updated_assignment.star_no == "4567" assert new_assignment.job.job_title == "Captain" assert new_assignment.star_date == datetime.date(2020, 7, 10) assert new_assignment.star_no == "54321" incident = cop4.incidents[0] assert incident.report_number == "CR-1234" license_plates = { plate.state: plate.number for plate in incident.license_plates } assert license_plates["NY"] == "ABC123" assert license_plates["IL"] == "98UMC" incident2 = Incident.query.filter_by(report_number="CR-9912").one() address = incident2.address assert address.street_name == "Fake Street" assert address.cross_street1 == "Main Street" assert address.cross_street2 is None assert address.city == "Chicago" assert address.state == "IL" assert address.zip_code == "60603" assert incident2.officers == [cop1] incident3 = Incident.query.get(123456) assert incident3.report_number == "CR-39283" assert incident3.description == "Don't know where it happened" assert incident3.officers == [cop1] assert incident3.date == datetime.date(2020, 7, 26) lp = incident3.license_plates[0] assert lp.number == "XYZ11" assert lp.state is None assert incident3.address is None assert incident3.time is None link_new = cop4.links[0] assert [link_new] == list(cop1.links) assert link_new.title == "A Link" assert link_new.url == "https://www.example.com" assert {officer.id for officer in link_new.officers} == {cop1.id, cop4.id} incident_link = incident2.links[0] assert incident_link.url == "https://www.example.com/incident" assert incident_link.title == "Another Link" assert incident_link.author == "Example Times" updated_link = Link.query.get(55051) assert updated_link.title == "Updated Link" assert updated_link.officers == [] assert updated_link.incidents == [incident3]