def test_build_params(): """Test we build the right set of parameters.""" config = core.Config(molecule='nucleotide', verbose=False) dl_id = 'TEST' expected_params = { 'tool': 'ncbi-acc-download', 'retmode': 'text', 'rettype': 'gbwithparts', 'id': 'TEST', 'db': 'nucleotide' } params = core.build_params(dl_id, config) assert params == expected_params expected_params = { 'tool': 'ncbi-acc-download', 'retmode': 'text', 'rettype': 'fasta', 'id': 'TEST', 'db': 'protein' } config.molecule = 'protein' params = core.build_params(dl_id, config) assert params == expected_params
def test_generate_url(): """Test URL generation.""" config = core.Config() expected = "{}?{}".format(ENTREZ_URL, "retmode=text&id=FAKE&db=nucleotide&rettype=gbwithparts") assert expected == core.generate_url("FAKE", config) config.format = 'gff3' expected = "{}?{}".format(SVIEWER_URL, "retmode=text&id=FAKE&db=nucleotide&report=gff3") assert expected == core.generate_url("FAKE", config)
def test_validate_and_write_extended_validation(req): """Test extended validation before writing.""" handle = StringIO() req.get('http://fake/', text=u'>foo\nMAGIC') r = requests.get('http://fake/') config = core.Config(extended_validation='loads', molecule='protein') core._validate_and_write(r, handle, 'FAKE', config) assert handle.getvalue() == u'>foo\nMAGIC'
def test_validate_and_write_error_pattern_raises(req): """Test scanning the download file for error patterns.""" handle = StringIO() req.get('http://fake/', text=u'ID list is empty') r = requests.get('http://fake/') config = core.Config() with pytest.raises(core.BadPatternError): core._validate_and_write(r, handle, 'FAKE', config)
def test_download_to_file(req, tmpdir): """Test downloading things from NCBI.""" req.get(ENTREZ_URL, text='This works.') outdir = tmpdir.mkdir('outdir') filename = outdir.join('foo') expected = outdir.join('foo.gbk') config = core.Config(molecule='nucleotide', verbose=False) core.download_to_file('FOO', config, filename=filename) assert expected.check()
def test_validate_and_write_emit(req): """Test writing prints dots in verbose mode.""" handle = StringIO() req.get('http://fake/', text=u'This is a sequence file, honest.') r = requests.get('http://fake/') output = StringIO() config = core.Config() config.emit = output.write core._validate_and_write(r, handle, 'FAKE', config) assert output.getvalue() == u'.\n' assert handle.getvalue() == u'This is a sequence file, honest.'
def test_download_to_file_append(req, tmpdir): """Test appending multiple downloads into a single file.""" req.get(ENTREZ_URL, text='This works.\n') outdir = tmpdir.mkdir('outdir') filename = outdir.join('foo.txt') expected = outdir.join('foo.txt') config = core.Config(molecule='nucleotide', verbose=False, out='foo.txt') core.download_to_file('FOO', config, filename=str(filename), append=False) core.download_to_file('BAR', config, filename=str(filename), append=True) core.download_to_file('BAZ', config, filename=str(filename), append=True) assert expected.check() assert len(expected.readlines()) == 3
def test_validate_and_write_error_pattern_raises(req): """Test scanning the download file for error patterns.""" handle = StringIO() req.get('http://fake/', text=u'ID list is empty') r = requests.get('http://fake/') config = core.Config() with pytest.raises(BadPatternError): core._validate_and_write(r, handle, 'FAKE', config) req.get('http://fake/', text=u'Error: CEFetchPApplication::proxy_stream(): Failed to retrieve sequence: NC_405534') r = requests.get('http://fake/') with pytest.raises(BadPatternError): core._validate_and_write(r, handle, 'FAKE', config)
def test_generate_url_with_api_key(): """Test URL generation for API key""" config = core.Config(api_key='97ae64f04b7e33672d5591a575b5f0c4c908') expected = "{}?{}".format( ENTREZ_URL, "retmode=text&id=FAKE&db=nucleotide&api_key=97ae64f04b7e33672d5591a575b5f0c4c908&rettype=gbwithparts" ) assert expected == core.generate_url("FAKE", config) config.format = 'gff3' expected = "{}?{}".format( SVIEWER_URL, "retmode=text&id=FAKE&db=nucleotide&api_key=97ae64f04b7e33672d5591a575b5f0c4c908&report=gff3" ) assert expected == core.generate_url("FAKE", config)
def test_download_to_file_retry(req, tmpdir): """Test downloading things from NCBI, retrying after a 429 status.""" req.get(ENTREZ_URL, response_list=[ { "text": u'Whoa, slow down', "status_code": 429, "headers": { "Retry-After": "0" } }, { "text": 'This works.' }, ]) outdir = tmpdir.mkdir('outdir') filename = outdir.join('foo') expected = outdir.join('foo.gbk') config = core.Config(molecule='nucleotide', verbose=False) core.download_to_file('FOO', config, filename=filename) assert expected.check()