def test_efetch_IPGs_error(hits): with requests_mock.Mocker() as mock, pytest.raises(requests.HTTPError): mock.post( "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?", status_code=400, ) context.efetch_IPGs([hit.subject for hit in hits])
def test_efetch_IPGs(hits): with requests_mock.Mocker() as mock: mock.post("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?") context.efetch_IPGs([hit.subject for hit in hits]) assert mock.request_history[0].url == ( "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?" "db=protein&rettype=ipg&retmode=text&retmax=10000")
def test_efetch_IPGs_IOError(hits, monkeypatch): def mock_ioerror(*args, **kwargs): raise IOError from Bio import Entrez monkeypatch.setattr(Entrez, "efetch", mock_ioerror) with pytest.raises(IOError): context.efetch_IPGs([hit.subject for hit in hits])
def test_efetch_IPGs_output(tmp_path, monkeypatch): def mock_output(*args, **kwargs): text = "testing".encode("utf-8") return io.BytesIO(text) from Bio import Entrez monkeypatch.setattr(Entrez, "efetch", mock_output) test_out = tmp_path / "out.tsv" context.efetch_IPGs(["placeholder"], output_file=test_out) assert test_out.read_text() == "testing"
def test_efetch_IPGs_output(hits, tmp_path): with requests_mock.Mocker() as mock: mock.post("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?", text="test") test_out = tmp_path / "out.tsv" with test_out.open("w") as handle: context.efetch_IPGs([hit.subject for hit in hits], output_handle=handle) assert test_out.read_text() == "test"
def test_efetch_IPGs_chunks(monkeypatch): def mock_chunks( _, rettype=None, retmode=None, retmax=None, id=None, ): text = "\n".join(id).encode("utf-8") return io.BytesIO(text) from Bio import Entrez monkeypatch.setattr(Entrez, "efetch", mock_chunks) for num in [500, 10001, 1]: ids = ["id"] * num table = context.efetch_IPGs(ids) assert len(table) == num
def test_efetch_IPGs_no_ids(): with pytest.raises(ValueError): context.efetch_IPGs([])