import configparser import ee.digikey as dk import io import os.path import pytest import sys from itertools import groupby basedir = os.path.dirname(os.path.abspath(__file__)) digikey = dk.Digikey() client = dk.DigikeyClient(digikey, on_download=print) @pytest.mark.digikey def test_digikey_1(tmpdir): res = client.search("TCR2LF18LM(CTTR-ND") 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 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(tmpdir): res = client.search("TCR2LF", page_size=500) 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) assert res.response_type == dk.SearchResponseTypes.SINGLE p = res.products[0] repo.save(p)