diff options
Diffstat (limited to 'test/test_digikey.py')
-rw-r--r-- | test/test_digikey.py | 70 |
1 files changed, 55 insertions, 15 deletions
diff --git a/test/test_digikey.py b/test/test_digikey.py index 015b7ca..733dcf7 100644 --- a/test/test_digikey.py +++ b/test/test_digikey.py @@ -1,20 +1,24 @@ -import configparser -import ee.digikey as dk import io -import os.path -import pytest -import sys from itertools import groupby +from pathlib import Path -basedir = os.path.dirname(os.path.abspath(__file__)) +import pytest +import requests + +import ee.digikey as dk + +basedir = Path(__file__).parent +static_copies = basedir / "digikey" / "static-copies" # type: Path digikey = dk.Digikey() -client = dk.DigikeyClient(digikey, on_download=print) +client = dk.DigikeyClient(digikey, cache_dir=basedir / "digikey" / "http_cache", on_download=print) @pytest.mark.digikey def test_digikey_1(tmpdir): - res = client.search("TCR2LF18LM(CTTR-ND") + content = cache_file("https://www.digikey.com/products/en?lang=en&site=us&pageSize=10", "TCR2LF18LM(CTTR-ND") + + res = client.parse_string(content) assert res.response_type == dk.SearchResponseTypes.SINGLE p = res.products[0] assert p.part_number == "TCR2LF18LM(CTTR-ND" @@ -36,23 +40,59 @@ def test_digikey_1(tmpdir): @pytest.mark.digikey -def test_digikey_2(tmpdir): - res = client.search("TCR2LF", page_size=500) +def test_digikey_2(): + content = cache_file("https://www.digikey.com/products/en?lang=en&site=us&keywords=TCR2LF&pageSize=500", "TCR2LF") + + res = client.parse_string(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) == 28 - print("path={}".format(tmpdir)) - repo = dk.DigikeyRepository(digikey, tmpdir) - 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)) - res = client.search(dpn) + + content = cache_file("https://www.digikey.com/products/en?lang=en&site=us&pageSize=500", dpn) + + res = client.parse_string(content) assert res.response_type == dk.SearchResponseTypes.SINGLE p = res.products[0] - repo.save(p) + assert p.part_number == dpn + assert p.mpn == mpn + + +@pytest.mark.digikey +def test_digikey_3(): + content = cache_file("https://www.digikey.com/products/en?lang=en&site=us&pageSize=10", "S1MTR") + + res = client.parse_string(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) == 1 + p = res.products[0] + assert p.part_number == "1655-1506-1-ND" + assert p.mpn == "S1MTR" + assert p.url == "/product-detail/en/smc-diode-solutions/S1MTR/1655-1506-1-ND/6022951" + + +def cache_file(url, keyword): + path = static_copies / "search-{}.html".format(keyword) + + if not path.is_file(): + path.parent.mkdir(parents=True, exist_ok=True) + res = requests.get(url, params=dict(keywords=keyword)) + + assert res.status_code == 200 + + with open(path, "w") as f: + f.write(res.text) + assert path.stat().st_size > 0 + + with open(path, "r") as f: + content = f.read() + + return content |