class PartMatchTests(TestCase): """Tests for the client's match() method""" def setUp(self): self.client = OctopartClient(api_key='TEST_TOKEN') @patch('requests.get') def test_too_many_match_queries(self, mock_get): queries = [{'q': 'fake-mpn'}] * 21 with pytest.raises(ValueError): self.client.match(queries) # the exception should prevent any queries from being made assert not mock_get.called @patch('requests.get') def test_malformed_match_query(self, mock_get): with pytest.raises(OctopartError): self.client.match([{'q': ["not", "a", "string"]}]) # the exception should prevent any queries from being made assert not mock_get.called def test_exact_match_true(self): response_body = { "__class__": "PartsMatchResponse", "results": [{ "__class__": "PartsMatchResult", "items": [] }] } with octopart_mock_response(response_body) as response: dict_ = self.client.match([{ 'q': 'ATTINY2313-20SU-zZz' }], exact_only=True) [result] = dict_['results'] assert len(result['items']) == 0 called_url = request_url_from_request_mock(response) assert '/parts/match' in called_url assert 'exact_only=true' in request_url_from_request_mock(response) def test_hide_directive(self): response_body = { "results": [{ "__class__": "PartsMatchResult", "items": [{ "__class__": "Part", "brand": { "__class__": "Brand", "homepage_url": "http://www.microchip.com", "name": "Microchip", "uid": "ddaa1ca0f0300a6d" }, "manufacturer": { "__class__": "Manufacturer", "homepage_url": "http://www.microchip.com", "name": "Microchip", "uid": "d78f1285fe150448" }, "mpn": "ATTINY2313-20SU", "octopart_url": "https://octopart.com/attiny2313-20su-microchip-77762028", # noqa "offers": [{ "__class__": "PartOffer", "eligible_region": "", "factory_lead_days": 94, "factory_order_multiple": None, "in_stock_quantity": 14517, "is_authorized": True, "is_realtime": False, "last_updated": "2018-04-30T12:26:55Z", "moq": 1, "multipack_quantity": None, "octopart_rfq_url": None, "on_order_eta": None, "on_order_quantity": None, "order_multiple": None, "packaging": "Tube", "prices": { "USD": [[1, "1.79000"], [25, "1.64320"], [100, "1.48400"]] } }], "product_url": "https://octopart.com/click/track?ak=a8cfd5a0&sig=09ff747&sid=459&ppid=77762028&vpid=408719850&ct=offers", # noqa "seller": { "__class__": "Seller", "display_flag": "US", "has_ecommerce": True, "homepage_url": "http://www.digikey.com", "id": "459", "name": "Digi-Key", "uid": "2c3be9310496fffc" }, "sku": "ATTINY2313-20SU-ND" }] }] } with octopart_mock_response(response_body) as response: dict_ = self.client.match([{ 'q': 'ATTINY2313-20SU' }], hide=['offers']) [result] = dict_['results'] [item] = result['items'] assert item['mpn'] == "ATTINY2313-20SU" called_url = request_url_from_request_mock(response) assert '/parts/match' in called_url assert 'hide%5B%5D=offers' in called_url def test_show_directive(self): with octopart_mock_response() as response: self.client.match([{'q': 'FAKE_MPN'}], show=['offers']) called_url = request_url_from_request_mock(response) assert '/parts/match' in called_url assert 'show%5B%5D=offers' in called_url def test_include_directive(self): with octopart_mock_response() as response: self.client.match([{'q': 'FAKE_MPN'}], includes=['cad_models']) called_url = request_url_from_request_mock(response) assert '/parts/match' in called_url assert 'include%5B%5D=cad_models' in called_url def test_no_directives(self): with octopart_mock_response() as response: self.client.match([{'q': 'FAKE_MPN'}]) called_url = request_url_from_request_mock(response) assert '/parts/match' in called_url assert 'hide%5B%5D=' not in called_url assert 'show%5B%5D=' not in called_url assert 'include%5B%5D=' not in called_url assert 'exact_only=' not in called_url def test_complete_example(self): with octopart_mock_response() as response: self.client.match([ { 'mpn': 'MPN1', 'brand': 'Brand 1' }, { 'mpn_or_sku': 'MPN-or#SKU2' }, ], exact_only=True, show=['brand.name'], includes=['imagesets']) called_url = request_url_from_request_mock(response) # %22brand%22%3A+%22Brand+1%22 is "brand": "Brand 1" assert '%22brand%22%3A+%22Brand+1%22' in called_url # %22mpn%22%3A+%22MPN1%22 is "mpn": "MPN1" assert '%22mpn%22%3A+%22MPN1%22' in called_url # %22mpn_or_sku%22%3A+%22MPN-or%23SKU2%22%7D%5D is # "22mpn_or_sku": "MPN-or#SKU2" assert '%22mpn_or_sku%22%3A+%22MPN-or%23SKU2%22%' in called_url assert 'exact_only=true' in called_url assert 'show%5B%5D=brand.name' in called_url assert 'include%5B%5D=imagesets' in called_url # %5B%5D is []
def test_bad_api_token(self): client = OctopartClient(api_key='BAD_TOKEN') with pytest.raises(OctopartError): client.match([{'q': 'RUM001L02T2CL'}])