示例#1
0
def test_bulk_add_officers__write_static_field_no_flag(session, department,
                                                       csv_path):
    # officer with birth year set
    officer = generate_officer()
    old_birth_year = 1979
    officer.birth_year = old_birth_year
    officer.department = department
    session.add(officer)
    session.commit()
    fo_uuid = officer.unique_internal_identifier

    new_birth_year = 1983

    fieldnames = [
        "department_id",
        "first_name",
        "last_name",
        "unique_internal_identifier",
        "birth_year",
    ]
    # generate csv that assigns different birth year to that officer
    with open(csv_path, "w") as f:
        csv_writer = csv.DictWriter(f, fieldnames=fieldnames)
        csv_writer.writeheader()

        csv_writer.writerow({
            "department_id": department.id,
            "first_name": officer.first_name,
            "last_name": officer.last_name,
            "unique_internal_identifier": fo_uuid,
            "birth_year": new_birth_year,
        })

    # run command, no flag
    result = run_command_print_output(bulk_add_officers, [csv_path])

    # command fails because birth year is a static field and cannot be changed
    # without --update-static-fields set
    assert result.exit_code != 0
    assert result.exception is not None

    # officer still has original birth year
    officer = Officer.query.filter_by(unique_internal_identifier=fo_uuid).one()
    assert officer.birth_year == old_birth_year
示例#2
0
def test_bulk_add_officers__write_static_null_field(session, department,
                                                    csv_path):
    # start with an officer whose birth_year is missing
    officer = generate_officer()
    officer.birth_year = None
    officer.department = department
    session.add(officer)
    session.commit()
    fo_uuid = officer.unique_internal_identifier

    birth_year = 1983
    fieldnames = [
        "department_id",
        "first_name",
        "last_name",
        "unique_internal_identifier",
        "birth_year",
    ]
    # generate csv that provides birth_year for that officer
    with open(csv_path, "w") as f:
        csv_writer = csv.DictWriter(f, fieldnames=fieldnames)
        csv_writer.writeheader()

        csv_writer.writerow({
            "department_id": department.id,
            "first_name": officer.first_name,
            "last_name": officer.last_name,
            "unique_internal_identifier": fo_uuid,
            "birth_year": birth_year,
        })

    # run command no flags set
    result = run_command_print_output(bulk_add_officers, [csv_path])

    # command successful
    assert result.exit_code == 0
    assert result.exception is None

    # officer information is updated in the database
    officer = Officer.query.filter_by(unique_internal_identifier=fo_uuid).one()
    assert officer.birth_year == birth_year
示例#3
0
def test_bulk_add_officers__success(session, department_with_ranks, csv_path):
    # generate two officers with different names
    first_officer = generate_officer()
    first_officer.department = department_with_ranks
    print(Job.query.all())
    print(Job.query.filter_by(department=department_with_ranks).all())
    job = (Job.query.filter_by(department=department_with_ranks).filter_by(
        order=1)).first()
    fo_fn = "Uniquefirst"
    first_officer.first_name = fo_fn
    fo_ln = first_officer.last_name
    session.add(first_officer)
    session.commit()
    assignment = Assignment(baseofficer=first_officer, job_id=job.id)
    session.add(assignment)
    session.commit()
    different_officer = generate_officer()
    different_officer.department = department_with_ranks
    different_officer.job = job
    do_fn = different_officer.first_name
    do_ln = different_officer.last_name
    session.add(different_officer)
    assignment = Assignment(baseofficer=different_officer, job=job)
    session.add(assignment)
    session.commit()

    department_id = department_with_ranks.id

    # generate csv to update one existing officer and add one new

    new_officer_first_name = "Newofficer"
    new_officer_last_name = "Name"

    fieldnames = [
        "department_id",
        "first_name",
        "last_name",
        "job_title",
    ]
    with open(csv_path, "w") as f:
        csv_writer = csv.DictWriter(f, fieldnames=fieldnames)
        csv_writer.writeheader()

        csv_writer.writerow({
            "department_id": department_id,
            "first_name": first_officer.first_name,
            "last_name": first_officer.last_name,
            "job_title": RANK_CHOICES_1[2],
        })

        csv_writer.writerow({
            "department_id": department_id,
            "first_name": new_officer_first_name,
            "last_name": new_officer_last_name,
            "job_title": RANK_CHOICES_1[1],
        })

    # run command with generated csv
    result = run_command_print_output(bulk_add_officers,
                                      [csv_path, "--update-by-name"])

    # command had no errors & exceptions
    assert result.exit_code == 0
    assert result.exception is None

    # make sure that exactly three officers are assigned to the department now
    # and the first officer has two assignments stored (one original one
    # and one updated via csv)
    officer_query = Officer.query.filter_by(department_id=department_id)
    officers = officer_query.all()
    assert len(officers) == 3
    first_officer_db = officer_query.filter_by(first_name=fo_fn,
                                               last_name=fo_ln).one()
    assert {asmt.job.job_title
            for asmt in first_officer_db.assignments.all()} == {
                RANK_CHOICES_1[2],
                RANK_CHOICES_1[1],
            }
    different_officer_db = officer_query.filter_by(first_name=do_fn,
                                                   last_name=do_ln).one()
    assert [
        asmt.job.job_title for asmt in different_officer_db.assignments.all()
    ] == [RANK_CHOICES_1[1]]
    new_officer_db = officer_query.filter_by(
        first_name=new_officer_first_name,
        last_name=new_officer_last_name).one()
    assert [asmt.job.job_title for asmt in new_officer_db.assignments.all()
            ] == [RANK_CHOICES_1[1]]