Skip to content

Commit

Permalink
build: rate limiting tests to NCBI Entrez
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrkarr committed Feb 27, 2022
1 parent 06401a2 commit aaf59fa
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion tests/ref/test_ref_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import Bio.Entrez
import ftplib
import os
import random
import shutil
import string
import tempfile
import time
import unittest

Bio.Entrez.email = '[email protected]'
email_suffix = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(8))
Bio.Entrez.email = 'biosimulators.daemon-{}@gmail.com'.format(email_suffix)


class RefUtilsTestCase(unittest.TestCase):
Expand All @@ -19,7 +23,10 @@ def tearDown(self):

def test_search_entrez_records(self):
self.assertEqual(utils.search_entrez_records('pmc', '23184105[pmid]')['IdList'], ['5813803'])
time.sleep(1)

self.assertEqual(utils.search_entrez_records('pmc', 'abc[pmid]')['Count'], '0')
time.sleep(1)

with self.assertRaisesRegex(TypeError, 'must be a non-empty string'):
utils.search_entrez_records('pmc', None)
Expand All @@ -30,6 +37,7 @@ def test_search_entrez_records(self):

def test_get_entrez_record(self):
self.assertEqual(utils.get_entrez_record('pmc', '5813803')['Id'], '5813803')
time.sleep(1)

with self.assertRaisesRegex(ValueError, 'is not a valid id'):
utils.get_entrez_record('pubmed', 'abc')
Expand All @@ -39,38 +47,54 @@ def test_get_entrez_record(self):

def test_get_pubmed_central_id(self):
self.assertEqual(utils.get_pubmed_central_id('23184105'), 'PMC5813803')
time.sleep(1)

self.assertEqual(utils.get_pubmed_central_id('1234'), None)
time.sleep(1)

self.assertEqual(utils.get_pubmed_central_id(1234), None)
time.sleep(1)

self.assertEqual(utils.get_pubmed_central_id('abc'), None)
time.sleep(1)

self.assertEqual(utils.get_pubmed_central_id(None), None)
time.sleep(1)

def test_get_reference_from_pubmed(self):
ref = utils.get_reference_from_pubmed('1234')
self.assertEqual(ref.journal, 'Drug metabolism and disposition: the biological fate of chemicals')
self.assertEqual(ref.year, '1975')
time.sleep(1)

ref = utils.get_reference_from_pubmed('23184105')
self.assertEqual(ref.year, '2012')
self.assertEqual(ref.pubmed_central_id, 'PMC5813803')
self.assertEqual(ref.doi, '10.1542/peds.2012-2758')
time.sleep(1)

ref = utils.get_reference_from_pubmed(pubmed_id=None, doi='10.1542/peds.2012-2758')
self.assertEqual(ref.year, '2012')
self.assertEqual(ref.pubmed_id, '23184105')
self.assertEqual(ref.pubmed_central_id, 'PMC5813803')
self.assertEqual(ref.doi, '10.1542/peds.2012-2758')
time.sleep(1)

ref = utils.get_reference_from_pubmed(pubmed_id=None, doi='10.1103/PhysRevLett.127.104301x')
self.assertEqual(ref, None)
time.sleep(1)

ref = utils.get_reference_from_pubmed(None, None)
self.assertEqual(ref, None)
time.sleep(1)

with self.assertRaisesRegex(ValueError, 'not a valid id'):
utils.get_reference_from_pubmed(pubmed_id='abc')
time.sleep(1)

with self.assertRaises(ValueError):
utils.get_reference_from_pubmed('000')
time.sleep(1)

def test_get_reference_from_crossref(self):
ref = utils.get_reference_from_crossref('10.1542/peds.2012-2758')
Expand Down Expand Up @@ -109,6 +133,7 @@ def test_get_reference(self):
self.assertEqual(ref.pubmed_id, '23184105')
self.assertEqual(ref.pubmed_central_id, 'PMC5813803')
self.assertEqual(ref.doi, '10.1542/peds.2012-2758')
time.sleep(1)

ref = utils.get_reference(doi='10.1542/peds.2012-2758')
self.assertIn(ref.authors, authors)
Expand All @@ -117,9 +142,11 @@ def test_get_reference(self):
self.assertEqual(ref.pubmed_id, '23184105')
self.assertEqual(ref.pubmed_central_id, 'PMC5813803')
self.assertEqual(ref.doi, '10.1542/peds.2012-2758')
time.sleep(1)

ref = utils.get_reference(pubmed_id='1234')
self.assertEqual(ref.doi, None)
time.sleep(1)

with self.assertRaises(ValueError):
utils.get_reference()
Expand Down

0 comments on commit aaf59fa

Please sign in to comment.