def test_save_location_to_filepath():
    location_data = sp.load_location(None, None, None, revert_to_default=True)
    outfile = tempfile.mktemp(suffix=".json")
    try:
        sp.save_location_to_filepath(location_data, outfile)
        assert os.stat(outfile).st_size > 0
        saved_location_data = sp.load_location_from_json_str(open(outfile, 'r').read())
        assert saved_location_data.location_name == location_data.location_name
    finally:
        os.remove(outfile) if os.path.exists(outfile) else None
Beispiel #2
0
 def test_load_empty_object_test_str(self):
     """
     Make sure that an empty json object populates all lists as empty.
     Because parts of the code rely on this assumption.
     location_name is a required field so it must be present.
     """
     test_str = """{"location_name": "test_location"}"""
     location = sp.load_location_from_json_str(test_str)
     self.assertEqual(location.location_name, "test_location",
                      "location_name incorrect")
     for list_property in location.get_list_properties():
         att = getattr(location, list_property)
         self.assertTrue(att is not None and len(att) == 0)
def test_populate_parent_exception():
    """
    Test if loading parent data from a wrong file, the same location is returned
    if populate_parent_data is not a vaild json string or json type, error is returned
    """
    specific_location = ""
    state_location = ""
    country_location = "usa"
    location_data = sp.load_location(specific_location, state_location, country_location, revert_to_default=False)
    parent_data = sp.data.populate_parent_data_from_file_path(location_data, "badfile.json")
    assert parent_data == location_data
    # pass parent in list which is not supported
    with pytest.raises(RuntimeError) as err:
        test_location = sp.load_location_from_json_str('{"location_name": "X"}')
        test_location.parent = ["Y"]
        sp.populate_parent_data(test_location)
        assert "Invalid type" in str(err)
Beispiel #4
0
 def test_load_minimal_location_with_parent_filepath(self):
     test_str = self.minimal_location_with_parent_filepath_test_str()
     location = sp.load_location_from_json_str(test_str)
     self.check_minimal_location_with_parent(location)
Beispiel #5
0
    def test_load_minimal_location(self):
        test_str = self.minimal_test_str()
        location = sp.load_location_from_json_str(test_str)

        self.assertEqual(location.location_name, "test_location",
                         "location_name incorrect")

        self.assertEqual(len(location.data_provenance_notices), 2,
                         "Array length incorrect")

        self.assertEqual(location.data_provenance_notices[0], "notice1",
                         "Array entry incorrect")

        self.assertEqual(location.data_provenance_notices[1], "notice2",
                         "Array entry incorrect")

        self.assertEqual(len(location.reference_links), 2,
                         "Array length incorrect")

        self.assertEqual(location.reference_links[0], "reference1",
                         "Array entry incorrect")

        self.assertEqual(location.reference_links[1], "reference2",
                         "Array entry incorrect")

        self.assertEqual(len(location.citations), 2,
                         "Array length incorrect")

        self.assertEqual(location.citations[0], "citation1",
                         "Array entry incorrect")
        self.assertEqual(location.citations[1], "citation2",
                         "Array entry incorrect")

        # Not checking parent field.

        self.assertEqual(len(location.population_age_distribution_16), 16,
                         "Array length incorrect")

        # Just checking the first couple entries
        self.assertEqual(len(location.population_age_distribution_16[0]), 3,
                         "Array length incorrect")
        self.assertEqual(location.population_age_distribution_16[0][0], 0,
                         "Array entry incorrect")
        self.assertEqual(location.population_age_distribution_16[0][1], 4,
                         "Array entry incorrect")
        self.assertEqual(location.population_age_distribution_16[0][2], 0.0529582569989547,
                         "Array entry incorrect")

        self.assertEqual(len(location.population_age_distribution_16[1]), 3,
                         "Array length incorrect")
        self.assertEqual(location.population_age_distribution_16[1][0], 5,
                         "Array entry incorrect")
        self.assertEqual(location.population_age_distribution_16[1][1], 9,
                         "Array entry incorrect")
        self.assertEqual(location.population_age_distribution_16[1][2], 0.0558095490543863,
                         "Array entry incorrect")

        self.assertEqual(len(location.employment_rates_by_age), 2,
                         "Array length incorrect")

        self.assertEqual(len(location.employment_rates_by_age[0]), 2,
                         "Array length incorrect")
        self.assertEqual(location.employment_rates_by_age[0][0], 19,
                         "Array entry incorrect")
        self.assertEqual(location.employment_rates_by_age[0][1], 0.300,
                         "Array entry incorrect")

        self.assertEqual(len(location.employment_rates_by_age[1]), 2,
                         "Array length incorrect")
        self.assertEqual(location.employment_rates_by_age[1][0], 20,
                         "Array entry incorrect")
        self.assertEqual(location.employment_rates_by_age[1][1], 0.693,
                         "Array entry incorrect")

        self.assertEqual(len(location.enrollment_rates_by_age), 2,
                         "Array length incorrect")
        self.assertEqual(len(location.enrollment_rates_by_age[0]), 2,
                         "Array length incorrect")
        self.assertEqual(location.enrollment_rates_by_age[0][0], 2,
                         "Array entry incorrect")
        self.assertEqual(location.enrollment_rates_by_age[0][1], 0,
                         "Array entry incorrect")

        self.assertEqual(len(location.enrollment_rates_by_age[1]), 2,
                         "Array length incorrect")
        self.assertEqual(location.enrollment_rates_by_age[1][0], 3,
                         "Array entry incorrect")
        self.assertEqual(location.enrollment_rates_by_age[1][1], 0.529,
                         "Array entry incorrect")

        self.assertEqual(len(location.household_head_age_brackets), 2,
                         "Array length incorrect")
        self.assertEqual(len(location.household_head_age_brackets[0]), 2,
                         "Array length incorrect")
        self.assertEqual(location.household_head_age_brackets[0][0], 18,
                         "Array entry incorrect")
        self.assertEqual(location.household_head_age_brackets[0][1], 19,
                         "Array entry incorrect")

        self.assertEqual(len(location.household_head_age_brackets[1]), 2,
                         "Array length incorrect")
        self.assertEqual(location.household_head_age_brackets[1][0], 20,
                         "Array entry incorrect")
        self.assertEqual(location.household_head_age_brackets[1][1], 24,
                         "Array entry incorrect")

        self.assertEqual(len(location.household_head_age_distribution_by_family_size), 2,
                         "Array length incorrect")

        self.assertEqual(len(location.household_head_age_distribution_by_family_size[0]), 3,
                         "Array length incorrect")
        self.assertEqual(location.household_head_age_distribution_by_family_size[0][0], 2,
                         "Array entry incorrect")
        self.assertEqual(location.household_head_age_distribution_by_family_size[0][1], 163,
                         "Array entry incorrect")
        self.assertEqual(location.household_head_age_distribution_by_family_size[0][2], 999,
                         "Array entry incorrect")

        self.assertEqual(len(location.household_head_age_distribution_by_family_size[1]), 3,
                         "Array length incorrect")
        self.assertEqual(location.household_head_age_distribution_by_family_size[1][0], 3,
                         "Array entry incorrect")
        self.assertEqual(location.household_head_age_distribution_by_family_size[1][1], 115,
                         "Array entry incorrect")
        self.assertEqual(location.household_head_age_distribution_by_family_size[1][2], 757,
                         "Array entry incorrect")

        self.assertEqual(len(location.household_size_distribution), 2,
                         "Array length incorrect")

        self.assertEqual(len(location.household_size_distribution[0]), 2,
                         "Array length incorrect")
        self.assertEqual(location.household_size_distribution[0][0], 1,
                         "Array entry incorrect")
        self.assertEqual(location.household_size_distribution[0][1], 0.2781590909877753,
                         "Array entry incorrect")

        self.assertEqual(len(location.household_size_distribution[1]), 2,
                         "Array length incorrect")
        self.assertEqual(location.household_size_distribution[1][0], 2,
                         "Array entry incorrect")
        self.assertEqual(location.household_size_distribution[1][1], 0.3443313103056699,
                         "Array entry incorrect")

        self.assertEqual(len(location.ltcf_resident_to_staff_ratio_distribution), 2,
                         "Array length incorrect")

        self.assertEqual(len(location.ltcf_resident_to_staff_ratio_distribution[0]), 3,
                         "Array length incorrect")
        self.assertEqual(location.ltcf_resident_to_staff_ratio_distribution[0][0], 1,
                         "Array entry incorrect")
        self.assertEqual(location.ltcf_resident_to_staff_ratio_distribution[0][1], 1,
                         "Array entry incorrect")
        self.assertEqual(location.ltcf_resident_to_staff_ratio_distribution[0][2], 0.0,
                         "Array entry incorrect")

        self.assertEqual(len(location.ltcf_resident_to_staff_ratio_distribution[1]), 3,
                         "Array length incorrect")
        self.assertEqual(location.ltcf_resident_to_staff_ratio_distribution[1][0], 2,
                         "Array entry incorrect")
        self.assertEqual(location.ltcf_resident_to_staff_ratio_distribution[1][1], 2,
                         "Array entry incorrect")
        self.assertEqual(location.ltcf_resident_to_staff_ratio_distribution[1][2], 5.0,
                         "Array entry incorrect")

        self.assertEqual(len(location.ltcf_num_residents_distribution), 2,
                         "Array length incorrect")

        self.assertEqual(len(location.ltcf_num_residents_distribution[0]), 3,
                         "Array length incorrect")
        self.assertEqual(location.ltcf_num_residents_distribution[0][0], 0,
                         "Array entry incorrect")
        self.assertEqual(location.ltcf_num_residents_distribution[0][1], 19,
                         "Array entry incorrect")
        self.assertEqual(location.ltcf_num_residents_distribution[0][2], 0.0,
                         "Array entry incorrect")

        self.assertEqual(len(location.ltcf_num_residents_distribution[1]), 3,
                         "Array length incorrect")
        self.assertEqual(location.ltcf_num_residents_distribution[1][0], 20,
                         "Array entry incorrect")
        self.assertEqual(location.ltcf_num_residents_distribution[1][1], 39,
                         "Array entry incorrect")
        self.assertEqual(location.ltcf_num_residents_distribution[1][2], 0.08955223880597014,
                         "Array entry incorrect")

        self.assertEqual(len(location.ltcf_num_staff_distribution), 2,
                         "Array length incorrect")

        self.assertEqual(len(location.ltcf_num_staff_distribution[0]), 3,
                         "Array length incorrect")
        self.assertEqual(location.ltcf_num_staff_distribution[0][0], 0,
                         "Array entry incorrect")
        self.assertEqual(location.ltcf_num_staff_distribution[0][1], 19,
                         "Array entry incorrect")
        self.assertEqual(location.ltcf_num_staff_distribution[0][2], 0.014925373134328358,
                         "Array entry incorrect")

        self.assertEqual(len(location.ltcf_num_staff_distribution[1]), 3,
                         "Array length incorrect")
        self.assertEqual(location.ltcf_num_staff_distribution[1][0], 20,
                         "Array entry incorrect")
        self.assertEqual(location.ltcf_num_staff_distribution[1][1], 39,
                         "Array entry incorrect")
        self.assertEqual(location.ltcf_num_staff_distribution[1][2], 0.07462686567164178,
                         "Array entry incorrect")

        self.assertEqual(len(location.school_size_brackets), 2,
                         "Array length incorrect")

        self.assertEqual(len(location.school_size_brackets[0]), 2,
                         "Array length incorrect")
        self.assertEqual(location.school_size_brackets[0][0], 20,
                         "Array entry incorrect")
        self.assertEqual(location.school_size_brackets[0][1], 50,
                         "Array entry incorrect")

        self.assertEqual(len(location.school_size_brackets[1]), 2,
                         "Array length incorrect")
        self.assertEqual(location.school_size_brackets[1][0], 51,
                         "Array entry incorrect")
        self.assertEqual(location.school_size_brackets[1][1], 100,
                         "Array entry incorrect")

        self.assertEqual(len(location.school_size_distribution), 2,
                         "Array length incorrect")

        self.assertEqual(location.school_size_distribution[0], 0.027522935779816515,
                         "Array entry incorrect")
        self.assertEqual(location.school_size_distribution[1], 0.009174311926605505,
                         "Array entry incorrect")

        self.assertEqual(len(location.school_size_distribution_by_type), 2,
                         "Array length incorrect")

        self.assertEqual(location.school_size_distribution_by_type[0].school_type, "pk-es",
                         "school_type incorrect")
        self.assertEqual(len(location.school_size_distribution_by_type[0].size_distribution), 2,
                         "Array length incorrect")
        self.assertEqual(location.school_size_distribution_by_type[0].size_distribution[0], 0.25,
                         "Array entry incorrect")
        self.assertEqual(location.school_size_distribution_by_type[0].size_distribution[1], 0.5,
                         "Array entry incorrect")

        self.assertEqual(location.school_size_distribution_by_type[1].school_type, "ms",
                         "school_type incorrect")
        self.assertEqual(len(location.school_size_distribution_by_type[1].size_distribution), 2,
                         "Array length incorrect")
        self.assertEqual(location.school_size_distribution_by_type[1].size_distribution[0], 0.35,
                         "Array entry incorrect")
        self.assertEqual(location.school_size_distribution_by_type[1].size_distribution[1], 0.65,
                         "Array entry incorrect")

        self.assertEqual(len(location.school_types_by_age), 2,
                         "Array length incorrect")

        self.assertEqual(location.school_types_by_age[0].school_type, "pk-es",
                         "School type value incorrect")
        self.assertEqual(len(location.school_types_by_age[0].age_range), 2,
                         "Array length incorrect")
        self.assertEqual(location.school_types_by_age[0].age_range[0], 3,
                         "Array entry incorrect")
        self.assertEqual(location.school_types_by_age[0].age_range[1], 10,
                         "Array entry incorrect")

        self.assertEqual(location.school_types_by_age[1].school_type, "ms",
                         "School type value incorrect")
        self.assertEqual(len(location.school_types_by_age[1].age_range), 2,
                         "Array length incorrect")
        self.assertEqual(location.school_types_by_age[1].age_range[0], 11,
                         "Array entry incorrect")
        self.assertEqual(location.school_types_by_age[1].age_range[1], 13,
                         "Array entry incorrect")

        self.assertEqual(len(location.workplace_size_counts_by_num_personnel), 2,
                         "Array length incorrect")

        self.assertEqual(len(location.workplace_size_counts_by_num_personnel[0]), 3,
                         "Array length incorrect")
        self.assertEqual(location.workplace_size_counts_by_num_personnel[0][0], 1,
                         "Array entry incorrect")
        self.assertEqual(location.workplace_size_counts_by_num_personnel[0][1], 4,
                         "Array entry incorrect")
        self.assertEqual(location.workplace_size_counts_by_num_personnel[0][2], 2947,
                         "Array entry incorrect")

        self.assertEqual(len(location.workplace_size_counts_by_num_personnel[1]), 3,
                         "Array length incorrect")
        self.assertEqual(location.workplace_size_counts_by_num_personnel[1][0], 5,
                         "Array entry incorrect")
        self.assertEqual(location.workplace_size_counts_by_num_personnel[1][1], 9,
                         "Array entry incorrect")
        self.assertEqual(location.workplace_size_counts_by_num_personnel[1][2], 992,
                         "Array entry incorrect")