Ejemplo n.º 1
0
def search_brand(query: str,
                 start: t.Optional[int] = None,
                 limit: t.Optional[int] = None,
                 sortby: t.Optional[t.List[t.Tuple[str, str]]] = None,
                 ) -> t.List[models.Brand]:
    client = OctopartClient()
    res = client.search_brand(
        query=query, start=start, limit=limit, sortby=sortby)
    return [models.Brand(bd.get('item', {})) for bd in res.get('results', [])]
Ejemplo n.º 2
0
class BrandSearchTests(TestCase):
    """Tests for the client's search_brand() and get_brand() methods"""
    def setUp(self):
        self.client = OctopartClient(api_key='TEST_TOKEN')

    def test_get_brand(self):
        response_body = {
            "__class__": "Brand",
            "homepage_url": "http://www.newark.com",
            "name": "Newark",
            "uid": "98785972bc7c4fbf"
        }

        with octopart_mock_response(response_body) as response:
            brand = self.client.get_brand('98785972bc7c4fbf')
            assert brand['uid'] == '98785972bc7c4fbf'
            called_url = request_url_from_request_mock(response)
            assert '/brands/98785972bc7c4fbf' in called_url

    def test_search_brand(self):
        response_body = {
            "__class__":
            "SearchResponse",
            "results": [{
                "__class__": "SearchResult",
                "item": {
                    "__class__": "Brand",
                    "homepage_url": "http://www.newark.com",
                    "name": "Newark",
                    "uid": "98785972bc7c4fbf"
                }
            }]
        }

        with octopart_mock_response(response_body) as response:
            response_data = self.client.search_brand('Newark')
            [result] = response_data['results']
            assert result['item']['name'] == 'Newark'
            called_url = request_url_from_request_mock(response)
            assert '/brands/search' in called_url
            assert 'q=Newark' in called_url