コード例 #1
0
def test_csv_new_assignment(csvfile):
    # Delete all current officers and assignments
    Assignment.query.delete()
    Officer.query.delete()

    assert Officer.query.count() == 0

    df = pd.read_csv(csvfile)
    df.loc[0, 'rank'] = 'COMMANDER'
    df.to_csv(csvfile)

    n_created, n_updated = bulk_add_officers([csvfile], standalone_mode=False)
    assert n_created > 0
    assert n_updated == 0
    assert Officer.query.count() == n_created

    officer = get_officer(1, df.loc[0, 'star_no'], df.loc[0, 'first_name'],
                          df.loc[0, 'last_name'])
    assert officer
    officer_id = officer.id
    assert len(list(officer.assignments)) == 1

    # Update rank
    df.loc[0, 'rank'] = 'CAPTAIN'
    df.to_csv(csvfile)

    n_created, n_updated = bulk_add_officers([csvfile], standalone_mode=False)
    assert n_created == 0
    assert n_updated > 0
    assert Officer.query.count() == n_updated

    officer = Officer.query.filter_by(id=officer_id).one()
    assert len(list(officer.assignments)) == 2
    for assignment in officer.assignments:
        assert assignment.rank == 'COMMANDER' or assignment.rank == 'CAPTAIN'
コード例 #2
0
def test_csv_new_assignment(csvfile):
    # Delete all current officers and assignments
    Assignment.query.delete()
    Officer.query.delete()

    assert Officer.query.count() == 0

    df = pd.read_csv(csvfile)
    df.loc[0, "job_title"] = "Commander"
    df.to_csv(csvfile)

    n_created, n_updated = bulk_add_officers([csvfile], standalone_mode=False)
    assert n_created > 0
    assert n_updated == 0
    assert Officer.query.count() == n_created

    officer = get_officer(1, df.loc[0, "star_no"], df.loc[0, "first_name"],
                          df.loc[0, "last_name"])
    assert officer
    officer_id = officer.id
    assert len(list(officer.assignments)) == 1

    # Update job_title
    df.loc[0, "job_title"] = "CAPTAIN"
    df.to_csv(csvfile)

    n_created, n_updated = bulk_add_officers([csvfile], standalone_mode=False)
    assert n_created == 0
    assert n_updated == 1

    officer = Officer.query.filter_by(id=officer_id).one()
    assert len(list(officer.assignments)) == 2
    for assignment in officer.assignments:
        assert (assignment.job.job_title == "Commander"
                or assignment.job.job_title == "CAPTAIN")
コード例 #3
0
def test_csv_missing_required_field(csvfile):
    df = pd.read_csv(csvfile)
    df.drop(columns="first_name").to_csv(csvfile)

    with pytest.raises(Exception) as exc:
        bulk_add_officers([csvfile])
    assert "Missing required field" in str(exc.value)
コード例 #4
0
def test_csv_changed_static_field(csvfile):
    df = pd.read_csv(csvfile)
    df.loc[0, "birth_year"] = 666
    df.to_csv(csvfile)

    with pytest.raises(Exception) as exc:
        bulk_add_officers([csvfile])
    assert "has differing birth_year field" in str(exc.value)
コード例 #5
0
def test_csv_non_existant_dept_id(csvfile):
    df = pd.read_csv(csvfile)
    df["department_id"] = 666
    df.to_csv(csvfile)

    with pytest.raises(Exception) as exc:
        bulk_add_officers([csvfile])
    assert "Department ID 666 not found" in str(exc.value)
コード例 #6
0
def test_csv_missing_badge_and_uid(csvfile):
    df = pd.read_csv(csvfile)
    df.drop(columns=['star_no', 'unique_internal_identifier']).to_csv(csvfile)

    with pytest.raises(Exception) as exc:
        bulk_add_officers([csvfile])
    assert 'CSV file must include either badge numbers or unique identifiers for officers' in str(
        exc.value)
コード例 #7
0
def test_csv_officer_missing_badge_and_uid(csvfile):
    df = pd.read_csv(csvfile)
    df.loc[0, "star_no"] = None
    df.loc[0, "unique_internal_identifier"] = None
    df.to_csv(csvfile)

    with pytest.raises(Exception) as exc:
        bulk_add_officers([csvfile])
    assert "missing badge number and unique identifier" in str(exc.value)
コード例 #8
0
def test_csv_import_idempotence(csvfile):
    # Delete all current officers
    Officer.query.delete()

    assert Officer.query.count() == 0

    n_created, n_updated = bulk_add_officers([csvfile], standalone_mode=False)
    assert n_created > 0
    assert n_updated == 0
    officer_count = Officer.query.count()
    assert officer_count == n_created

    n_created, n_updated = bulk_add_officers([csvfile], standalone_mode=False)
    assert n_created == 0
    assert n_updated == 0
    assert Officer.query.count() == officer_count
コード例 #9
0
def test_csv_import_update(csvfile):
    n_existing = Officer.query.count()

    assert n_existing > 0

    n_created, n_updated = bulk_add_officers([csvfile], standalone_mode=False)

    assert n_created == 0
    assert n_updated == 0
    assert Officer.query.count() == n_existing
コード例 #10
0
def test_csv_import_new(csvfile):
    # Delete all current officers and assignments
    Assignment.query.delete()
    Officer.query.delete()

    assert Officer.query.count() == 0

    n_created, n_updated = bulk_add_officers([csvfile], standalone_mode=False)

    assert n_created > 0
    assert Officer.query.count() == n_created
    assert n_updated == 0
コード例 #11
0
def test_csv_new_salary(csvfile):
    # Delete all current officers and salaries
    Salary.query.delete()
    Officer.query.delete()

    assert Officer.query.count() == 0

    df = pd.read_csv(csvfile)
    df.loc[0, "salary"] = "123456.78"
    df.to_csv(csvfile)

    n_created, n_updated = bulk_add_officers([csvfile], standalone_mode=False)
    assert n_created > 0
    assert n_updated == 0
    officer_count = Officer.query.count()
    assert officer_count == n_created

    officer = get_officer(1, df.loc[0, "star_no"], df.loc[0, "first_name"],
                          df.loc[0, "last_name"])
    assert officer
    officer_id = officer.id
    assert len(list(officer.salaries)) == 1

    # Update salary
    df.loc[0, "salary"] = "150000"
    df.to_csv(csvfile)

    assert Officer.query.count() > 0
    n_created, n_updated = bulk_add_officers([csvfile], standalone_mode=False)
    assert n_created == 0
    assert n_updated == 1
    assert Officer.query.count() == officer_count

    officer = Officer.query.filter_by(id=officer_id).one()
    assert len(list(officer.salaries)) == 2
    for salary in officer.salaries:
        assert float(salary.salary) == 123456.78 or float(
            salary.salary) == 150000.00
コード例 #12
0
def test_csv_new_name(csvfile):
    df = pd.read_csv(csvfile)
    officer_uid = df.loc[0, "unique_internal_identifier"]
    assert officer_uid

    df.loc[0, "first_name"] = "FOO"
    df.to_csv(csvfile)

    n_created, n_updated = bulk_add_officers([csvfile], standalone_mode=False)
    assert n_created == 0
    assert n_updated == 1

    officer = Officer.query.filter_by(
        unique_internal_identifier=officer_uid).one()

    assert officer.first_name == "FOO"
コード例 #13
0
def test_csv_new_officer(csvfile):
    df = pd.read_csv(csvfile)

    n_rows = len(df.index)
    assert n_rows > 0

    n_officers = Officer.query.count()
    assert n_officers > 0

    new_uid = str(uuid.uuid4())
    new_officer = {  # Must match fields in csvfile
        "department_id": 1,
        "unique_internal_identifier": new_uid,
        "first_name": "FOO",
        "last_name": "BAR",
        "middle_initial": None,
        "suffix": None,
        "gender": "F",
        "race": "BLACK",
        "employment_date": None,
        "birth_year": None,
        "star_no": 666,
        "job_title": "CAPTAIN",
        "unit": None,
        "star_date": None,
        "resign_date": None,
        "salary": 1.23,
        "salary_year": 2019,
        "salary_is_fiscal_year": True,
        "overtime_pay": 4.56,
    }
    df = df.append([new_officer])
    df.to_csv(csvfile)

    n_created, n_updated = bulk_add_officers([csvfile], standalone_mode=False)
    assert n_created == 1
    assert n_updated == 0

    officer = Officer.query.filter_by(unique_internal_identifier=new_uid).one()

    assert officer.first_name == "FOO"
    assert Officer.query.count() == n_officers + 1
コード例 #14
0
def test_csv_new_officer(csvfile):
    df = pd.read_csv(csvfile)

    n_rows = len(df.index)
    assert n_rows > 0

    n_officers = Officer.query.count()
    assert n_officers > 0

    new_uid = str(uuid.uuid4())
    new_officer = {  # Must match fields in csvfile
        'department_id': 1,
        'unique_internal_identifier': new_uid,
        'first_name': 'FOO',
        'last_name': 'BAR',
        'middle_initial': None,
        'suffix': None,
        'gender': 'F',
        'race': 'BLACK',
        'employment_date': None,
        'birth_year': None,
        'star_no': 666,
        'rank': 'CAPTAIN',
        'unit': None,
        'star_date': None,
        'resign_date': None
    }
    df = df.append([new_officer])
    df.to_csv(csvfile)

    n_created, n_updated = bulk_add_officers([csvfile], standalone_mode=False)
    assert n_created == 1
    assert n_updated == n_rows

    officer = Officer.query.filter_by(unique_internal_identifier=new_uid).one()

    assert officer.first_name == 'FOO'
    assert Officer.query.count() == n_officers + 1