예제 #1
0
파일: yelp.py 프로젝트: hartliddell/api
    def retrieve_search_summary(self, index=0):
        """Retrieve the ID of a specific place."""
        if not self.search_results:
            return None
        if not self.search_results.get('businesses'):
            return None

        search_summary = PlaceSearchSummary()
        business = self.search_results.get('businesses')[index]

        search_summary.id = business.get('id', '')
        search_summary.name = business.get('name', '')
        search_summary.address = ' '.join(
            business.get('location', {}).get('display_address', ''))

        return search_summary
예제 #2
0
    def retrieve_search_summary(self, index=0):
        """
        Retrieve the search information (ID, name and address) of a specific place.

        :param int index: position of the place to look for in the results.
        :return: the summary information of a specific place.
        :rtype: PlaceSearchSummary
        """
        if not self.search_results:
            return None
        if not self.search_results.get('results'):
            return None
        search_summary = PlaceSearchSummary()
        business = self.search_results.get('results')[index]

        search_summary.id = business.get('place_id', '')
        search_summary.name = business.get('name', '')
        search_summary.address = business.get('vicinity', '')

        return search_summary
예제 #3
0
    def test_retrieve_search_summary_02(self, google_collector):
        """Ensure the first result of the search is returned as a `PlaceSearchSummary`."""
        gmaps = google_collector
        gmaps.search_results = GOOGLE_MAPS_SEARCH_RESPONSE
        actual = gmaps.retrieve_search_summary()
        expected = PlaceSearchSummary(
            id='ChIJyWEHuEmuEmsRm9hTkapTCrk',
            name='Rhythmboat Cruises',
            address='Pyrmont Bay Wharf Darling Dr, Sydney',
        )

        assert actual == expected
예제 #4
0
    def test_retrieve_search_summary_02(self):
        """Ensure the first result of the search is returned as a `PlaceSearchSummary`."""
        yelp = YelpCollector()
        yelp.search_results = YELP_SEARCH_RESPONSE
        actual = yelp.retrieve_search_summary()
        expected = PlaceSearchSummary(
            id='four-barrel-coffee-san-francisco',
            name='Four Barrel Coffee',
            address='',
        )

        assert actual == expected
예제 #5
0
    def test_lookup_place_02(self, mocker):
        """Ensure lookup returns details for a given name/address pair."""
        fake_place_id = self.fake.pystr()
        fake_name = self.fake.pystr()
        fake_address = self.fake.address()
        c = CollectorClient(self.fake.pystr())
        c.search_places = mocker.Mock()
        c.retrieve_search_summary = mocker.Mock(return_value=PlaceSearchSummary(id=fake_place_id))
        c.get_place_details = mocker.Mock()

        c.lookup_place(name=fake_name, address=fake_address)

        c.search_places.assert_called_with(address=fake_address, terms=fake_name, limit=1)
        c.get_place_details.assert_called_with(fake_place_id)
예제 #6
0
def integration_test_yelp_retrieve_place_details():
    client_api_key = os.environ['RYR_COLLECTOR_YELP_API_KEY']

    client = CollectorClient('yelp', api_key=client_api_key)
    client.authenticate()
    epoch_search_summary = PlaceSearchSummary(
        'krzzyozIVGC7pX1lfVO40w',
        'Epoch Coffee',
        '221 West North Loop Boulevard, Austin',
    )
    place_details = client.retrieve_place_details(
        epoch_search_summary.id,
        epoch_search_summary.name,
        epoch_search_summary.address,
    )

    print(json.dumps(place_details))
예제 #7
0
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