def test_dataset_is_public_and_has_data(self):
        ''' We can accurately tell if a dataset is public and has data.
        '''
        # create a department
        department = Department.create(name="LM Police Department",
                                       short_name="LMPD",
                                       load_defaults=True)

        # none of the datasets have data, so they should all return false
        assert department.dataset_is_public_and_has_data("complaints") == False
        assert department.dataset_is_public_and_has_data("uof") == False
        assert department.dataset_is_public_and_has_data("ois") == False
        assert department.dataset_is_public_and_has_data("assaults") == False
        # the total count should be zero
        assert department.displayable_dataset_count() == 0

        # create incidents and verify that the datasets are now displayable
        UseOfForceIncidentLMPD.create(department_id=department.id,
                                      opaque_id="23456bcdef")
        assert department.dataset_is_public_and_has_data("uof") == True
        assert department.displayable_dataset_count() == 1

        # now make them all not public, and they should be false again
        department.is_public_use_of_force_incidents = False
        assert department.dataset_is_public_and_has_data("uof") == False
        assert department.displayable_dataset_count() == 0
    def test_non_public_depts_display_for_users_with_access(self, testapp):
        ''' Users can see links to datasets they're allowed to access on the front page
        '''
        impd = Department.create(name="I Police Department",
                                 short_name="IMPD",
                                 is_public=True)
        UseOfForceIncidentIMPD.create(department_id=impd.id,
                                      opaque_id="12345abcde")
        bpd = Department.create(name="B Police Department",
                                short_name="BPD",
                                is_public=False)
        UseOfForceIncidentBPD.create(department_id=bpd.id,
                                     opaque_id="12345abcde")
        lmpd = Department.create(name="LM Police Department",
                                 short_name="LMPD",
                                 is_public=False)
        UseOfForceIncidentLMPD.create(department_id=lmpd.id,
                                      opaque_id="12345abcde")

        # A non logged-in user can only see the public department
        response = testapp.get("/", status=200)
        soup = BeautifulSoup(response.text)
        assert soup.find("a", href="/department/IMPD/useofforce") is not None
        assert soup.find("a", href="/department/BPD/useofforce") is None
        assert soup.find("a", href="/department/LMPD/useofforce") is None

        # A user associated with a particular department can see that department's
        # available datasets when logged in
        create_and_log_in_user(testapp=testapp,
                               department=bpd,
                               username="******")
        response = testapp.get("/", status=200)
        soup = BeautifulSoup(response.text)
        assert soup.find("a", href="/department/IMPD/useofforce") is not None
        assert soup.find("a", href="/department/BPD/useofforce") is not None
        assert soup.find("a", href="/department/LMPD/useofforce") is None

        # A user with admin access can see all departments' available datasets
        create_and_log_in_user(testapp=testapp,
                               department=impd,
                               rolename='admin',
                               username="******")
        response = testapp.get("/", status=200)
        soup = BeautifulSoup(response.text)
        assert soup.find("a", href="/department/IMPD/useofforce") is not None
        assert soup.find("a", href="/department/BPD/useofforce") is not None
        assert soup.find("a", href="/department/LMPD/useofforce") is not None

        # Log out and only the public department should be visible
        testapp.get(url_for('public.logout')).follow()
        response = testapp.get("/", status=200)
        soup = BeautifulSoup(response.text)
        assert soup.find("a", href="/department/IMPD/useofforce") is not None
        assert soup.find("a", href="/department/BPD/useofforce") is None
        assert soup.find("a", href="/department/LMPD/useofforce") is None
    def test_csv_response(self, testapp):
        # create a department and an LMPD uof incident
        department = Department.create(name="LM Police Department",
                                       short_name="LMPD",
                                       load_defaults=False)

        uof_check = dict(
            department_id=department.id,
            opaque_id="Check Opaque ID",
            occured_date=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
            bureau="Check Bureau",
            division="Check Division",
            unit="Check Unit",
            platoon="Check Platoon",
            disposition="Check Disposition",
            use_of_force_reason="Check UOF Reason",
            officer_force_type="Check Officer Force Type",
            service_type="Check Service Type",
            arrest_made=False,
            arrest_charges="Check Arrest Charges",
            resident_injured=True,
            resident_hospitalized=False,
            resident_condition="Check Resident Condition",
            officer_injured=False,
            officer_hospitalized=False,
            officer_condition="Check Officer Condition",
            resident_identifier="Check Resident Identifier",
            resident_race="Check Resident Race",
            resident_sex="Check Resident Sex",
            resident_age="Check Resident Age",
            officer_race="Check Officer Race",
            officer_sex="Check Officer Sex",
            officer_age="Check Officer Age",
            officer_years_of_service="Check Officer Years Of Service",
            officer_identifier="Check Officer Identifier")

        UseOfForceIncidentLMPD.create(**uof_check)

        response = testapp.get("/department/{}/uof.csv".format(department.id))

        incidents = list(csv.DictReader(io.StringIO(response.text)))

        # build a variable to csv header lookup from the csv schema
        csv_schema = UseOfForceIncidentLMPD.get_csv_schema()
        schema_lookup = dict(
            zip([col[1] for col in csv_schema],
                [col[0] for col in csv_schema]))

        assert len(incidents) == 1
        for check_key in uof_check.keys():
            if check_key == 'department_id':
                continue
            assert str(
                uof_check[check_key]) == incidents[0][schema_lookup[check_key]]
    def test_multiple_depts_display(self, testapp):
        impd = Department.create(name="I Police Department", short_name="IMPD", is_public=True)
        UseOfForceIncidentIMPD.create(department_id=impd.id, opaque_id="12345abcde")
        bpd = Department.create(name="B Police Department", short_name="BPD", is_public=True)
        UseOfForceIncidentBPD.create(department_id=bpd.id, opaque_id="12345abcde")
        lmpd = Department.create(name="LM Police Department", short_name="LMPD", is_public=False)
        UseOfForceIncidentLMPD.create(department_id=lmpd.id, opaque_id="12345abcde")

        response = testapp.get("/", status=200)
        soup = BeautifulSoup(response.text)
        assert soup.find("a", href="/department/IMPD/useofforce") is not None
        assert soup.find("a", href="/department/BPD/useofforce") is not None
        assert soup.find("a", href="/department/LMPD/useofforce") is None
    def test_non_public_depts_display_for_users_with_access(self, testapp):
        """ Users can see links to datasets they're allowed to access on the front page
        """
        impd = Department.create(name="I Police Department", short_name="IMPD", is_public=True)
        UseOfForceIncidentIMPD.create(department_id=impd.id, opaque_id="12345abcde")
        bpd = Department.create(name="B Police Department", short_name="BPD", is_public=False)
        UseOfForceIncidentBPD.create(department_id=bpd.id, opaque_id="12345abcde")
        lmpd = Department.create(name="LM Police Department", short_name="LMPD", is_public=False)
        UseOfForceIncidentLMPD.create(department_id=lmpd.id, opaque_id="12345abcde")

        # A non logged-in user can only see the public department
        response = testapp.get("/", status=200)
        soup = BeautifulSoup(response.text)
        assert soup.find("a", href="/department/IMPD/useofforce") is not None
        assert soup.find("a", href="/department/BPD/useofforce") is None
        assert soup.find("a", href="/department/LMPD/useofforce") is None

        # A user associated with a particular department can see that department's
        # available datasets when logged in
        create_and_log_in_user(testapp=testapp, department=bpd, username="******")
        response = testapp.get("/", status=200)
        soup = BeautifulSoup(response.text)
        assert soup.find("a", href="/department/IMPD/useofforce") is not None
        assert soup.find("a", href="/department/BPD/useofforce") is not None
        assert soup.find("a", href="/department/LMPD/useofforce") is None

        # A user with admin access can see all departments' available datasets
        create_and_log_in_user(testapp=testapp, department=impd, rolename="admin", username="******")
        response = testapp.get("/", status=200)
        soup = BeautifulSoup(response.text)
        assert soup.find("a", href="/department/IMPD/useofforce") is not None
        assert soup.find("a", href="/department/BPD/useofforce") is not None
        assert soup.find("a", href="/department/LMPD/useofforce") is not None

        # Log out and only the public department should be visible
        testapp.get(url_for("public.logout")).follow()
        response = testapp.get("/", status=200)
        soup = BeautifulSoup(response.text)
        assert soup.find("a", href="/department/IMPD/useofforce") is not None
        assert soup.find("a", href="/department/BPD/useofforce") is None
        assert soup.find("a", href="/department/LMPD/useofforce") is None
    def test_csv_response(self, testapp):
        # create a department and an LMPD uof incident
        department = Department.create(name="LM Police Department", short_name="LMPD", load_defaults=False)

        uof_check = dict(department_id=department.id, opaque_id="Check Opaque ID", occured_date=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), bureau="Check Bureau", division="Check Division", unit="Check Unit", platoon="Check Platoon", disposition="Check Disposition", use_of_force_reason="Check UOF Reason", officer_force_type="Check Officer Force Type", service_type="Check Service Type", arrest_made=False, arrest_charges="Check Arrest Charges", resident_injured=True, resident_hospitalized=False, resident_condition="Check Resident Condition", officer_injured=False, officer_hospitalized=False, officer_condition="Check Officer Condition", resident_identifier="Check Resident Identifier", resident_race="Check Resident Race", resident_sex="Check Resident Sex", resident_age="Check Resident Age", officer_race="Check Officer Race", officer_sex="Check Officer Sex", officer_age="Check Officer Age", officer_years_of_service="Check Officer Years Of Service", officer_identifier="Check Officer Identifier")

        UseOfForceIncidentLMPD.create(**uof_check)

        response = testapp.get("/department/{}/uof.csv".format(department.id))

        incidents = list(csv.DictReader(io.StringIO(response.text)))

        # build a variable to csv header lookup from the csv schema
        csv_schema = UseOfForceIncidentLMPD.get_csv_schema()
        schema_lookup = dict(zip([col[1] for col in csv_schema], [col[0] for col in csv_schema]))

        assert len(incidents) == 1
        for check_key in uof_check.keys():
            if check_key == 'department_id':
                continue
            assert str(uof_check[check_key]) == incidents[0][schema_lookup[check_key]]
    def test_multiple_depts_display(self, testapp):
        impd = Department.create(name="I Police Department",
                                 short_name="IMPD",
                                 is_public=True)
        UseOfForceIncidentIMPD.create(department_id=impd.id,
                                      opaque_id="12345abcde")
        bpd = Department.create(name="B Police Department",
                                short_name="BPD",
                                is_public=True)
        UseOfForceIncidentBPD.create(department_id=bpd.id,
                                     opaque_id="12345abcde")
        lmpd = Department.create(name="LM Police Department",
                                 short_name="LMPD",
                                 is_public=False)
        UseOfForceIncidentLMPD.create(department_id=lmpd.id,
                                      opaque_id="12345abcde")

        response = testapp.get("/", status=200)
        soup = BeautifulSoup(response.text)
        assert soup.find("a", href="/department/IMPD/useofforce") is not None
        assert soup.find("a", href="/department/BPD/useofforce") is not None
        assert soup.find("a", href="/department/LMPD/useofforce") is None
    def test_dataset_is_public_and_has_data(self):
        ''' We can accurately tell if a dataset is public and has data.
        '''
        # create a department
        department = Department.create(name="LM Police Department", short_name="LMPD", load_defaults=True)

        # none of the datasets have data, so they should all return false
        assert department.dataset_is_public_and_has_data("complaints") == False
        assert department.dataset_is_public_and_has_data("uof") == False
        assert department.dataset_is_public_and_has_data("ois") == False
        assert department.dataset_is_public_and_has_data("assaults") == False
        # the total count should be zero
        assert department.displayable_dataset_count() == 0

        # create incidents and verify that the datasets are now displayable
        UseOfForceIncidentLMPD.create(department_id=department.id, opaque_id="23456bcdef")
        assert department.dataset_is_public_and_has_data("uof") == True
        assert department.displayable_dataset_count() == 1

        # now make them all not public, and they should be false again
        department.is_public_use_of_force_incidents = False
        assert department.dataset_is_public_and_has_data("uof") == False
        assert department.displayable_dataset_count() == 0