Exemplo n.º 1
0
class CategorySearchTests(TestCase):
    """Tests for the client's search_category() and get_category() methods"""
    def setUp(self):
        self.client = OctopartClient(api_key='TEST_TOKEN')

    def test_get_category(self):
        response_body = {
            "__class__":
            "Category",
            "ancestor_names": ["Electronic Parts", "Passive Components"],
            "ancestor_uids": ["8a1e4714bb3951d9", "7542b8484461ae85"],
            "children_uids": [
                "db8a1680eaa4004d", "eb282622da0730ef", "e2992f93d3b3395e",
                "e7ca2ac0de173c0d", "f6473b4dcf9d2d80", "1db9d0b32462c67e",
                "e0b52dbfd96d3b72", "d820152ae1f903e7", "f0433d49c9e52b84"
            ],
            "name":
            "Capacitors",
            "num_parts":
            1395137,
            "parent_uid":
            "7542b8484461ae85",
            "uid":
            "f8883582c9a8234f"
        }

        with octopart_mock_response(response_body) as response:
            dict_ = self.client.get_category('f8883582c9a8234f')
            assert dict_['name'] == 'Capacitors'
            called_url = request_url_from_request_mock(response)
            assert '/categories/f8883582c9a8234f' in called_url

    def test_search_category(self):
        response_body = {
            "__class__":
            "SearchResponse",
            "results": [{
                "__class__": "SearchResult",
                "item": {
                    "__class__":
                    "Category",
                    "ancestor_names":
                    ["Electronic Parts", "Passive Components"],
                    "ancestor_uids": ["8a1e4714bb3951d9", "7542b8484461ae85"],
                    "children_uids": [
                        "db8a1680eaa4004d", "eb282622da0730ef",
                        "e2992f93d3b3395e", "e7ca2ac0de173c0d",
                        "f6473b4dcf9d2d80", "1db9d0b32462c67e",
                        "e0b52dbfd96d3b72", "d820152ae1f903e7",
                        "f0433d49c9e52b84"
                    ],
                    "name":
                    "Capacitors",
                    "num_parts":
                    1395137,
                    "parent_uid":
                    "7542b8484461ae85",
                    "uid":
                    "f8883582c9a8234f"
                }
            }]
        }

        with octopart_mock_response(response_body) as response:
            dict_ = self.client.search_category('Capacitors')
            [result] = dict_['results']
            assert result['item']['name'] == 'Capacitors'
            called_url = request_url_from_request_mock(response)
            assert '/categories/search' in called_url
            assert 'q=Capacitors' in called_url
Exemplo n.º 2
0
def get_category(uid: str) -> models.Category:
    client = OctopartClient()
    cat_dict = client.get_category(uid)
    return models.Category(cat_dict, strict=False)