import io from itertools import groupby from pathlib import Path import pytest import ee.digikey as dk from ee.money import Money, get_default_context basedir = Path(__file__).parent static_copies = basedir / "digikey" / "static-copies" # type: Path digikey = dk.Digikey() parser = dk.DigikeyParser(digikey) client = dk.DigikeyClient("https://www.digikey.com/products/en", cache_dir=basedir / "digikey" / "static-copies", on_download=print) force_refresh = False # Set to True to always fetch the updated html files @pytest.mark.digikey def test_digikey_1(tmpdir): content = client.search("TCR2LF18LM(CTTR-ND") res = parser.parse_string(client.baseurl, content) assert res.response_type == dk.SearchResponseTypes.SINGLE p = res.products[0] assert p.part_number == "TCR2LF18LM(CTTR-ND" assert p.mpn == "TCR2LF18,LM(CT" assert len(p.attributes) > 5 assert len(p.price_breaks) == 6 assert p.price_breaks[0].quantity == 3000 assert p.price_breaks[0].per_piece_price == get_default_context().parse("USD 0.07") repo = dk.DigikeyRepository(digikey, str(tmpdir)) x = io.StringIO() p.to_ini(repo._make_configparser()).write(x) print(x.getvalue()) repo.save(p) repo = dk.DigikeyRepository(digikey, str(tmpdir)) repo.load_all() ps = repo.find_by_mpn(p.mpn) assert ps y = io.StringIO() p.to_ini(repo._make_configparser()).write(y) assert x.getvalue() == y.getvalue() @pytest.mark.digikey def test_digikey_2(): content = client.product_search("TCR2LF", page_size=500) res = parser.parse_string(client.baseurl, content) assert res.response_type == dk.SearchResponseTypes.MANY [print("dpn={}, mpn={}".format(p.part_number, p.mpn)) for p in res.products] assert len(res.products) > 10 for mpn, parts in groupby(sorted(res.products, key=lambda p: p.mpn), lambda p: p.mpn): parts = list(parts) print("mpn={}, parts={}".format(mpn, [p.part_number for p in parts])) dpn = parts[0].part_number print("Downloading {} as {}".format(mpn, dpn)) content = client.product_search(dpn) res = parser.parse_string(client.baseurl, content) assert res.response_type == dk.SearchResponseTypes.SINGLE p = res.products[0] assert p.part_number == dpn assert p.mpn == mpn @pytest.mark.digikey def test_digikey_3(): content = client.product_search("RS1MTR") res = parser.parse_string(client.baseurl, content) assert res.response_type == dk.SearchResponseTypes.MANY [print("dpn={}, mpn={}".format(p.part_number, p.mpn)) for p in res.products] assert len(res.products) > 0 p = next((p for p in res.products if p.part_number == "1655-1501-1-ND"), None) assert p.mpn == "RS1MTR" assert p.url == "https://www.digikey.com/product-detail/en/smc-diode-solutions/RS1MTR/1655-1501-1-ND/6022946" @pytest.mark.digikey def test_store(): store = dk.DigikeyStore.from_store_code("us") assert store.products_url == "https://www.digikey.com/products/en" assert store.url == "http://purl.org/ee/supplier/digikey?store=us" assert store.store == "us" store = dk.DigikeyStore.from_url(store.url) assert store.products_url == "https://www.digikey.com/products/en" assert store.url == "http://purl.org/ee/supplier/digikey?store=us" assert store.store == "us" store = dk.DigikeyStore.from_store_code("no") assert store.products_url == "https://www.digikey.no/products/en" assert store.url == "http://purl.org/ee/supplier/digikey?store=no" assert store.store == "no" store = dk.DigikeyStore.from_url(store.url) assert store.products_url == "https://www.digikey.no/products/en" assert store.url == "http://purl.org/ee/supplier/digikey?store=no" assert store.store == "no"