def combine_collector_results(collector_results): """Combine the results provided by several collectors.""" c = BusinessInfo() for collector_result in collector_results: c = c.merge(collector_result) return c
def test_combine_collector_results_00(self): """Ensure results are combined correctly.""" b0 = BusinessInfo(name='name1') b1 = BusinessInfo(address='address2') b2 = BusinessInfo(name='name1', address='address2') task = tasks.combine_collector_results.s([b0, b1]).apply() assert task.successful() assert task.result == b2
def test_collect_place_details_00(self, mocker): """ Ensure data are collected from multiple collectors then combined. Test with "Epoch Coffee Shop - Northloop, Austin, TX." """ kwargs = { 'place_id': 'ChIJG-gJw2vKRIYROWi2uwOp8QE', 'name': 'Epoch Coffee - North Loop', 'address': '221 West North Loop Boulevard, Austin', } bi_google = BusinessInfo( name='Epoch Coffee - North Loop', address='221 W N Loop Blvd, Austin, TX 78751, USA', latitude=30.31865599999999, longitude=-97.72445499999999, type='', phone='(512) 454-3762', email='', contact_name='', website='http://www.epochcoffee.com/', parking_info='', extra_info='', weight=0) bi_yelp = BusinessInfo( name='Epoch Coffee', address='221 W North Loop Blvd Austin, TX 78751', latitude=30.3186, longitude=-97.72457, type='Coffee & Tea, Cafes', phone='+15124543762', email='', contact_name='', website='', parking_info='', extra_info='', weight=10) bi_combined = BusinessInfo( name='Epoch Coffee - North Loop', address='221 W N Loop Blvd, Austin, TX 78751, USA', latitude=30.31865599999999, longitude=-97.72445499999999, type='Coffee & Tea, Cafes', phone='(512) 454-3762', email='', contact_name='', website='http://www.epochcoffee.com/', parking_info='', extra_info='', weight=0) mocker.patch('api.celery.tasks.collect_place_details_from_google', return_value=bi_google) mocker.patch('api.celery.tasks.collect_place_details_from_yelp', return_value=bi_yelp) # task = tasks.collect_place_details(**kwargs) task = chord([tasks.collect_place_details_from_yelp.s(kwargs['name'], kwargs['address'])])( tasks.combine_collector_results.s()).apply()
def test_retrieve_place_details_01(self, mocker): """Ensure retrieve_place_details returns a dictionary.""" yelp = YelpCollector() response = requests.Response() response.status_code = 200 response.json = Mock(return_value=YELP_DETAILS_RESPONSE) mocker.patch.object(requests, 'get', return_value=response) yelp.get_place_details(self.fake.pystr()) actual = yelp.to_business_info() expected = BusinessInfo( name='Gary Danko', address='800 N Point St San Francisco, CA 94109', latitude=37.80587, longitude=-122.42058, type='American (New)', phone='+14152520800', email='', contact_name='', website='', parking_info='', extra_info='', weight=0) assert actual == expected
def to_business_info(self): """Convert the raw data to a BusinessInfo object.""" # Ensure we have data to convert. if not self.result: return None # Define convenience variables. r = self.result location = r.get('location', {}) coordinates = r.get('coordinates', {}) # Populate the business information. b = BusinessInfo(weight=self.weight) b.name = r.get('name', '') b.address = ' '.join(location.get('display_address', '')) b.phone = r.get('phone', '') b.latitude = coordinates.get('latitude', 0.0) b.longitude = coordinates.get('longitude', 0.0) b.type = ', '.join([d['title'] for d in r['categories']]) return b
def to_business_info(self): """Convert the raw data to a BusinessInfo object.""" # Ensure we have data to convert. if not self.result: return None if not self.result.get('result'): return None # Define convenience variables. r = self.result.get('result') location = r.get('geometry', {}).get('location', {}) # Populate the business information. b = BusinessInfo(weight=self.weight) b.name = r.get('name', '') b.address = r.get('formatted_address', '') b.phone = r.get('formatted_phone_number', '') b.website = r.get('website', '') b.latitude = location.get('lat', 0.0) b.longitude = location.get('lng', 0.0) return b
def test_place_details_01(self, mocker, google_collector): """Ensure retrieve_place_details returns a dictionary.""" gmaps = google_collector mocker.patch.object(googlemaps.Client, 'place', return_value=GOOGLE_MAPS_DETAILS_RESPONSE) _ = gmaps.get_place_details(self.fake.pystr()) actual = gmaps.to_business_info() expected = BusinessInfo( name='Google', address='5, 48 Pirrama Rd, Pyrmont NSW 2009, Australia', latitude=-33.866651, longitude=151.195827, type='', phone='(02) 9374 4000', email='', contact_name='', website='https://www.google.com.au/about/careers/locations/sydney/', parking_info='', extra_info='', weight=0) assert actual == expected
class TestBusinessInfo: """Implement tests for BusinessInfo.""" # Merge scenario list. # The first tuple contains: # 0. first BusinessInfo object # 1. second BusinessInfo object # 2. expected result # The second tuple is a description of the scenario. merge_scenarios = [ (( BusinessInfo(name='name1'), BusinessInfo(address='address2'), BusinessInfo(name='name1', address='address2'), ), 'Ensure different properties are merged for same weight objects.'), (( BusinessInfo(name='name1'), BusinessInfo(name='name2'), BusinessInfo(name='name1'), ), 'Ensure no property is overwritten for same weight objects.'), (( BusinessInfo(name='name1', weight=1), BusinessInfo(name='name2', weight=5), BusinessInfo(name='name1'), ), 'Ensure objects with the less weight overwrites properties.'), (( BusinessInfo(name='name1', weight=3), BusinessInfo(name='name2', weight=2), BusinessInfo(name='name2'), ), 'Ensure objects with the less weight overwrites properties.'), (( BusinessInfo(name='name1', weight=3), BusinessInfo(address='address2', weight=2), BusinessInfo(name='name1', address='address2'), ), 'Ensure objects with the less weight overwrites properties.'), (( BusinessInfo(name='name1', weight=3), PlaceSearchSummary(), BusinessInfo(name='name1', weight=3), ), 'Ensure objects of different types don\'t merge'), ] def scenario_inputs(scenarios): """Parse the scenarios and feed the data to the test function.""" return [test_input[0] for test_input in scenarios] def scenario_ids(scenarios): """Parse the scenarios and feed the IDs to the test function.""" return [test_input[1] for test_input in scenarios] def test_geolocation_00(self): """Ensure geolocation is computed properly for default objects.""" b1 = BusinessInfo() assert b1.geolocation() == '0.0,0.0' def test_geolocation_01(self): """Ensure geolocation is computed properly.""" b1 = BusinessInfo(latitude=1.0, longitude=2.0) assert b1.geolocation() == '1.0,2.0' @pytest.mark.parametrize("test_input", scenario_inputs(merge_scenarios), ids=scenario_ids(merge_scenarios)) def test_merge(self, test_input): """Ensure objects are merge correctly.""" actual = test_input[0].merge(test_input[1]) expected = test_input[2] assert actual == expected def test_to_json_00(self): """Ensure the object serializes to JSON correctly.""" b = BusinessInfo(name='name1', address='address2') actual = b.to_json(indent=None) expected = '{"__instance_type__": ["api.apps.api.collectors.base", "BusinessInfo"], "attributes": {"address": "address2", "contact_name": "", "email": "", "extra_info": "", "latitude": 0.0, "longitude": 0.0, "name": "name1", "parking_info": "", "phone": "", "type": "", "website": "", "weight": 0}}' assert actual == expected def test_from_json_00(self): """Ensure the object get deserialized from JSON correctly.""" json_object = '{"__instance_type__": ["api.apps.api.collectors.base", "BusinessInfo"], "attributes": {"address": "address2", "contact_name": "", "email": "", "extra_info": "", "latitude": 0.0, "longitude": 0.0, "name": "name1", "parking_info": "", "phone": "", "type": "", "website": "", "weight": 0}}' actual = BusinessInfo.from_json(json_object) expected = BusinessInfo(name='name1', address='address2') assert actual == expected
def test_from_json_00(self): """Ensure the object get deserialized from JSON correctly.""" json_object = '{"__instance_type__": ["api.apps.api.collectors.base", "BusinessInfo"], "attributes": {"address": "address2", "contact_name": "", "email": "", "extra_info": "", "latitude": 0.0, "longitude": 0.0, "name": "name1", "parking_info": "", "phone": "", "type": "", "website": "", "weight": 0}}' actual = BusinessInfo.from_json(json_object) expected = BusinessInfo(name='name1', address='address2') assert actual == expected
def test_geolocation_01(self): """Ensure geolocation is computed properly.""" b1 = BusinessInfo(latitude=1.0, longitude=2.0) assert b1.geolocation() == '1.0,2.0'
def test_geolocation_00(self): """Ensure geolocation is computed properly for default objects.""" b1 = BusinessInfo() assert b1.geolocation() == '0.0,0.0'