def test_valid(self, post_mock): """ Calling get_place_obs with valid parameters returns a valid result. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Call get_place_obs pvs = { 'placeOfBirth': 'BornInOtherStateInTheUnitedStates', 'age': 'Years5To17' } place_obs = dc.get_place_obs( 'City', '2017', 'Person', constraining_properties=pvs) self.assertListEqual(place_obs, [ { 'name': 'Marcus Hook borough', 'place': 'geoId/4247344', 'populations': { 'dc/p/pq6frs32sfvk': { 'observations': [ { 'marginOfError': 39, 'measuredProp': 'count', 'measuredValue': 67, } ], } } } ])
def test_multiple_dcids(self, post_mock): """ Calling get_property_values with multiple dcids returns valid results. """ # Set the API key dc.set_api_key('TEST-API-KEY') dcids = ['geoId/06085', 'geoId/24031'] # Get the containedInPlace Towns for Santa Clara and Montgomery County. towns = dc.get_property_values(dcids, 'containedInPlace', out=False, value_type='Town') self.assertDictEqual( towns, { 'geoId/06085': ['geoId/0643294', 'geoId/0644112'], 'geoId/24031': ['geoId/2462850'] }) # Get the name of Santa Clara and Montgomery County. names = dc.get_property_values(dcids, 'name') self.assertDictEqual( names, { 'geoId/06085': ['Santa Clara County'], 'geoId/24031': ['Montgomery County'] })
def test_bad_dcids(self, post_mock): """ Calling get_triples with dcids that do not exist returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Call get_triples where one dcid does not exist triples_1 = dc.get_triples(['geoId/06085', 'dc/MadDcid']) self.assertDictEqual( triples_1, { 'geoId/06085': [ ('geoId/06085', 'name', 'Santa Clara County'), ('geoId/0649670', 'containedInPlace', 'geoId/06085'), ('geoId/06085', 'containedInPlace', 'geoId/06'), ], 'dc/MadDcid': [] }) # Call get_triples where both dcids do not exist triples_1 = dc.get_triples(['dc/MadDcid', 'dc/MadderDcid']) self.assertDictEqual(triples_1, { 'dc/MadDcid': [], 'dc/MadderDcid': [] })
def test_multiple_dcids(self, post_mock): """ Calling get_property_labels returns valid results with multiple dcids. """ # Set the API key dc.set_api_key('TEST-API-KEY') dcids = ['State', 'County', 'City'] expected_in = ["typeOf"] expected_out = ["name", "provenance", "subClassOf", "typeOf", "url"] # Test for outgoing property labels out_props = dc.get_property_labels(dcids) self.assertDictEqual(out_props, { 'State': expected_out, 'County': expected_out, 'City': expected_out, }) # Test for incoming property labels in_props = dc.get_property_labels(dcids, out=False) self.assertDictEqual(in_props, { 'State': expected_in, 'County': expected_in, 'City': expected_in, })
def test_bad_dcids(self, urlopen): """ Calling get_observations with dcids that do not exist returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Get the input dcids_1 = ['dc/p/x6t44d8jd95rd', 'dc/MadDcid'] dcids_2 = ['dc/MadDcid', 'dc/MadderDcid'] # Call get_observations actual_1 = dc.get_observations( dcids_1, 'count', 'measuredValue', '2018-12', observation_period='P1M', measurement_method='BLSSeasonallyAdjusted') actual_2 = dc.get_observations( dcids_2, 'count', 'measuredValue', '2018-12', observation_period='P1M', measurement_method='BLSSeasonallyAdjusted') # Verify the results self.assertDictEqual(actual_1, {'dc/p/x6t44d8jd95rd': 18704962.0}) self.assertDictEqual(actual_2, {})
def test_bad_dcids(self, post_mock): """ Calling get_property_values with dcids that do not exist returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') bad_dcids_1 = ['geoId/06085', 'dc/MadDcid'] bad_dcids_2 = ['dc/MadDcid', 'dc/MadderDcid'] # Get entities containedInPlace of Santa Clara County and a dcid that does # not exist. contained_1 = dc.get_property_values(bad_dcids_1, 'containedInPlace', out=False) self.assertDictEqual(contained_1, { 'geoId/06085': ['geoId/0644112'], 'dc/MadDcid': [] }) # Get entities containedInPlace for two dcids that do not exist. contained_2 = dc.get_property_values(bad_dcids_2, 'containedInPlace') self.assertDictEqual(contained_2, { 'dc/MadDcid': [], 'dc/MadderDcid': [] })
def test_query_w_api_key(self, urlopen): """ Handles row-less response. """ # Set the API key dc.set_api_key('make_sure_I_am_replaced') dc.set_api_key(_TEST_API_KEY) # Issue a dummy SPARQL query that tells the mock to expect a key self.assertEqual(dc.query(_SPARQL_W_KEY), [])
def test_no_dcids(self, post_mock): """ Calling get_property_values with no dcids returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Get property values with an empty list of dcids. prop_vals = dc.get_property_values([], 'containedInPlace') self.assertDictEqual(prop_vals, {})
def test_no_dcids(self, post_mock): """ Calling get_triples with no dcids returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Call get_triples with no dcids triples_1 = dc.get_triples([]) self.assertDictEqual(triples_1, {})
def test_no_dcids(self, post_mock): """ Calling get_populations with no dcids returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') pops = dc.get_populations( [], 'Person', constraining_properties=self._constraints) self.assertDictEqual(pops, {})
def test_no_dcids(self, post_mock): """ Calling get_observations with no dcids returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') actual = dc.get_observations([], 'count', 'measuredValue', '2018-12', observation_period='P1M', measurement_method='BLSSeasonallyAdjusted') self.assertDictEqual(actual, {})
def test_no_dcids(self, urlopen): """ Calling get_places_in with no dcids returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Call get_places_in with no dcids. bad_dcids = dc.get_places_in(['dc/MadDcid', 'dc/MadderDcid'], 'City') self.assertDictEqual(bad_dcids, { 'dc/MadDcid': [], 'dc/MadderDcid': [] })
def test_bad_property(self, post_mock): """ Calling get_property_values with a property that does not exist returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Get propery values for a property that does not exist. prop_vals = dc.get_property_values(['geoId/06085', 'geoId/24031'], 'madProperty') self.assertDictEqual(prop_vals, {'geoId/06085': [], 'geoId/24031': []})
def test_multiple_dcids(self, post_mock): """ Calling get_populations with proper dcids returns valid results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Call get_populations populations = dc.get_populations(['geoId/06085', 'geoId/4805000'], 'Person', constraining_properties=self._constraints) self.assertDictEqual(populations, { 'geoId/06085': 'dc/p/crgfn8blpvl35', 'geoId/4805000': 'dc/p/f3q9whmjwbf36' })
def test_series_no_dcids(self, post_mock): """ Calling get_places_in with no dcids returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Get the input and expected output bad_dcids = pd.Series([]) expected = pd.Series([]) # Test get_places_in actual = dc.get_places_in(bad_dcids, 'City') assert_series_equal(actual, expected)
def test_multiple_dcids(self, urlopen): """ Calling get_places_in with proper dcids returns valid results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Call get_places_in places = dc.get_places_in(['geoId/06085', 'geoId/24031'], 'City') self.assertDictEqual( places, { 'geoId/06085': ['geoId/0649670'], 'geoId/24031': ['geoId/2467675', 'geoId/2476650'] })
def test_series_no_dcids(self, post_mock): """ Calling get_populations with no dcids returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') dcids = pd.Series([]) expected = pd.Series([]) # Call get_populations actual = dc.get_populations( dcids, 'Person', constraining_properties=self._constraints) assert_series_equal(actual, expected)
def test_no_dcids(self, post_mock): """ Calling get_property_labels with no dcids returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Test for outgoing property labels out_props = dc.get_property_labels([]) self.assertDictEqual(out_props, {}) # Test for incoming property labels in_props = dc.get_property_labels([], out=False) self.assertDictEqual(in_props, {})
def test_rows(self, urlopen): """ Sending a valid query returns the correct response. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Create the SPARQL query query_string = (''' SELECT ?name ?dcid WHERE { ?a typeOf Place . ?a name ?name . ?a dcid ("geoId/06" "geoId/21" "geoId/24") . ?a dcid ?dcid } ''') selector = lambda row: row['?name'] != 'California' # Issue the query results = dc.query(query_string) selected_results = dc.query(query_string, select=selector) # Execute the query and iterate through the results. for idx, row in enumerate(results): if idx == 0: self.assertDictEqual(row, { '?name': 'California', '?dcid': 'geoId/06' }) if idx == 1: self.assertDictEqual(row, { '?name': 'Kentucky', '?dcid': 'geoId/21' }) if idx == 2: self.assertDictEqual(row, { '?name': 'Maryland', '?dcid': 'geoId/24' }) # Verify that the select function works. for idx, row in enumerate(selected_results): if idx == 0: self.assertDictEqual(row, { '?name': 'Kentucky', '?dcid': 'geoId/21' }) if idx == 1: self.assertDictEqual(row, { '?name': 'Maryland', '?dcid': 'geoId/24' })
def test_series_multiple_dcids(self, post_mock): """ Calling get_observations with a Pandas Series and proper dcids returns a Pandas Series with valid results. """ # Set the API key dc.set_api_key('TEST-API-KEY') dcids = pd.Series( ['dc/p/x6t44d8jd95rd', 'dc/p/lr52m1yr46r44', 'dc/p/fs929fynprzs']) expected = pd.Series([18704962.0, 3075662.0, 1973955.0]) actual = dc.get_observations(dcids, 'count', 'measuredValue', '2018-12', observation_period='P1M', measurement_method='BLSSeasonallyAdjusted') assert_series_equal(actual, expected)
def test_bad_dcids(self, post_mock): """ Calling get_property_labels with dcids that do not exist returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Test for outgoing property labels out_props = dc.get_property_labels(['dc/MadDcid']) self.assertDictEqual(out_props, {'dc/MadDcid': []}) # Test for incoming property labels in_props = dc.get_property_labels(['dc/MadDcid'], out=False) self.assertDictEqual(in_props, {'dc/MadDcid': []})
def test_series_bad_property(self, post_mock): """ Calling get_property_values with a Pandas Series and a property that does not exist returns an empty result. """ # Set the API key dc.set_api_key('TEST-API-KEY') # The input and expected series dcids = pd.Series(['geoId/06085', 'geoId/24031']) expected = pd.Series([[], []]) # Call get_property_values and assert the results are correct. actual = dc.get_property_values(dcids, 'madProperty') assert_series_equal(actual, expected)
def test_multiple_dcids(self, post_mock): """ Calling get_observations with proper dcids returns valid results. """ # Set the API key dc.set_api_key('TEST-API-KEY') dcids = ['dc/p/x6t44d8jd95rd', 'dc/p/lr52m1yr46r44', 'dc/p/fs929fynprzs'] expected = { 'dc/p/lr52m1yr46r44': 3075662.0, 'dc/p/fs929fynprzs': 1973955.0, 'dc/p/x6t44d8jd95rd': 18704962.0 } actual = dc.get_observations(dcids, 'count', 'measuredValue', '2018-12', observation_period='P1M', measurement_method='BLSSeasonallyAdjusted') self.assertDictEqual(actual, expected)
def test_series_multiple_dcids(self, post_mock): """ Calling get_places_in with a Pandas Series and proper dcids returns a Pandas Series with valid results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Get the input dcids and expected output dcids = pd.Series(['geoId/06085', 'geoId/24031']) expected = pd.Series([['geoId/0649670'], ['geoId/2467675', 'geoId/2476650']]) # Call get_places_in actual = dc.get_places_in(dcids, 'City') assert_series_equal(actual, expected)
def test_series_multiple_dcids(self, post_mock): """ Calling get_populations with a Pandas Series and proper dcids returns a Pandas Series with valid results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Get the input and expected output dcids = pd.Series(['geoId/06085', 'geoId/4805000']) expected = pd.Series(['dc/p/crgfn8blpvl35', 'dc/p/f3q9whmjwbf36']) # Call get_populations actual = dc.get_populations( dcids, 'Person', constraining_properties=self._constraints) assert_series_equal(actual, expected)
def test_single_dcid(self, post_mock): """ Calling get_property_labels with a single dcid returns a valid result. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Test for outgoing property labels out_props = dc.get_property_labels(['geoId/0649670']) self.assertDictEqual( out_props, {'geoId/0649670': ["containedInPlace", "name", "geoId", "typeOf"]}) # Test with out=False in_props = dc.get_property_labels(['geoId/0649670'], out=False) self.assertDictEqual(in_props, {'geoId/0649670': []})
def test_bad_dcids(self, post_mock): """ Calling get_populations with dcids that do not exist returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Call get_populations pops_1 = dc.get_populations(['geoId/06085', 'dc/MadDcid'], 'Person', constraining_properties=self._constraints) pops_2 = dc.get_populations(['dc/MadDcid', 'dc/MadderDcid'], 'Person', constraining_properties=self._constraints) # Verify the results self.assertDictEqual(pops_1, {'geoId/06085': 'dc/p/crgfn8blpvl35'}) self.assertDictEqual(pops_2, {})
def test_dataframe(self, post_mock): """ Calling get_property_values with a Pandas DataFrame returns the correct results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # The given and expected series. dcids = pd.DataFrame({'dcids': ['geoId/06085', 'geoId/24031']}) expected = pd.Series([['geoId/0643294', 'geoId/0644112'], ['geoId/2462850']]) # Call get_property_values with the series as input actual = dc.get_property_values(dcids, 'containedInPlace', out=False, value_type='Town') assert_series_equal(actual, expected)
def test_series_bad_dcids(self, post_mock): """ Calling get_places_in with a Pandas Series and dcids that do not exist returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Get the input dcids and expected output bad_dcids_1 = pd.Series(['geoId/06085', 'dc/MadDcid']) bad_dcids_2 = pd.Series(['dc/MadDcid', 'dc/MadderDcid']) expected_1 = pd.Series([['geoId/0649670'], []]) expected_2 = pd.Series([[], []]) # Call get_places_in actual_1 = dc.get_places_in(bad_dcids_1, 'City') actual_2 = dc.get_places_in(bad_dcids_2, 'City') # Assert that the answers are correct assert_series_equal(actual_1, expected_1) assert_series_equal(actual_2, expected_2)
def test_bad_dcids(self, urlopen): """ Calling get_places_in with dcids that do not exist returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Call get_places_in with one dcid that does not exist bad_dcids_1 = dc.get_places_in(['geoId/06085', 'dc/MadDcid'], 'City') self.assertDictEqual(bad_dcids_1, { 'geoId/06085': ['geoId/0649670'], 'dc/MadDcid': [] }) # Call get_places_in when both dcids do not exist bad_dcids_2 = dc.get_places_in(['dc/MadDcid', 'dc/MadderDcid'], 'City') self.assertDictEqual(bad_dcids_2, { 'dc/MadDcid': [], 'dc/MadderDcid': [] })