def article_chunks(self, count, **kwargs): ''' :param count: chunk size if requested, default is 20 ''' entrez = OpenEmoryEntrezClient() self.date_opts = self._date_opts(self.min_date, self.max_date, self.auto_date) qs = entrez.get_emory_articles(**self.date_opts) return Paginator(qs, count)
def setUp(self): self.entrez = OpenEmoryEntrezClient()
class EntrezTest(TestCase): def setUp(self): self.entrez = OpenEmoryEntrezClient() @patch('openemory.harvest.entrez.sleep') @patch('openemory.harvest.entrez.xmlmap') def test_get_emory_articles(self, mock_xmlmap, mock_sleep): '''Verify that test_emory_articles makes an appropriate request to E-Utils and interprets the result appropriately.''' # set up mocks def mock_load(url, xmlclass): '''mock-like method wrapping load_xmlobject_from_file without actually making a network query, but still calling the requested xmlclass constructor. ''' # figure out what fixture to return fixture = (mock_load.return_fixtures[mock_load.call_count] if mock_load.call_count < len(mock_load.return_fixtures) else mock_load.return_fixtures[-1]) mock_load.call_count += 1 mock_load.urls.append(url) test_response_path = fixture_path(fixture) test_response_obj = xmlmap.load_xmlobject_from_file(test_response_path, xmlclass=xmlclass) return test_response_obj mock_load.call_count = 0 mock_load.urls = [] mock_xmlmap.load_xmlobject_from_file = mock_load # configure the return values mock_load.return_fixtures = [ 'esearch-response-withhist.xml', 'efetch-retrieval-from-hist.xml', ] # make the call article_qs = self.entrez.get_emory_articles() self.assertEqual(mock_xmlmap.load_xmlobject_from_file.call_count, 1) # check the first query url self.assertTrue('esearch.fcgi' in mock_load.urls[0]) # these should always be in there per E-Utils policy (see entrez.py) self.assertTrue('tool=' in mock_load.urls[0]) self.assertTrue('email=' in mock_load.urls[0]) # these are what we're currently querying for. note that these may # change as our implementation develops. if they do (causing these # assertions to fail) then we probably need to update our fixture. self.assertTrue('db=pmc' in mock_load.urls[0]) self.assertTrue('term=emory' in mock_load.urls[0]) self.assertTrue('field=affl' in mock_load.urls[0]) self.assertTrue('usehistory=y' in mock_load.urls[0]) # fetch one articles = article_qs[:20] # grab a slice to limit the query articles[0] self.assertEqual(mock_xmlmap.load_xmlobject_from_file.call_count, 2) # check that we slept between calls (reqd by eutils policies) self.assertEqual(mock_sleep.call_count, 1) sleep_args, sleep_kwargs = mock_sleep.call_args self.assertTrue(sleep_args[0] >= 0.3) # check the second query url self.assertTrue('efetch.fcgi' in mock_load.urls[1]) # always required self.assertTrue('tool=' in mock_load.urls[1]) self.assertTrue('email=' in mock_load.urls[1]) # what we're currently querying for self.assertTrue('db=pmc' in mock_load.urls[1]) self.assertTrue('usehistory=y' in mock_load.urls[1]) self.assertTrue('query_key=' in mock_load.urls[1]) self.assertTrue('WebEnv=' in mock_load.urls[1]) self.assertTrue('retmode=xml' in mock_load.urls[1]) self.assertTrue('retstart=0' in mock_load.urls[1]) self.assertTrue('retmax=20' in mock_load.urls[1])
class EntrezTest(TestCase): def setUp(self): self.entrez = OpenEmoryEntrezClient() @patch('openemory.harvest.entrez.sleep') @patch('openemory.harvest.entrez.xmlmap') def test_get_emory_articles(self, mock_xmlmap, mock_sleep): '''Verify that test_emory_articles makes an appropriate request to E-Utils and interprets the result appropriately.''' # set up mocks def mock_load(url, xmlclass): '''mock-like method wrapping load_xmlobject_from_file without actually making a network query, but still calling the requested xmlclass constructor. ''' # figure out what fixture to return fixture = (mock_load.return_fixtures[mock_load.call_count] if mock_load.call_count < len(mock_load.return_fixtures) else mock_load.return_fixtures[-1]) mock_load.call_count += 1 mock_load.urls.append(url) test_response_path = fixture_path(fixture) test_response_obj = xmlmap.load_xmlobject_from_file( test_response_path, xmlclass=xmlclass) return test_response_obj mock_load.call_count = 0 mock_load.urls = [] mock_xmlmap.load_xmlobject_from_file = mock_load # configure the return values mock_load.return_fixtures = [ 'esearch-response-withhist.xml', 'efetch-retrieval-from-hist.xml', ] # make the call article_qs = self.entrez.get_emory_articles() self.assertEqual(mock_xmlmap.load_xmlobject_from_file.call_count, 1) # check the first query url self.assertTrue('esearch.fcgi' in mock_load.urls[0]) # these should always be in there per E-Utils policy (see entrez.py) self.assertTrue('tool=' in mock_load.urls[0]) self.assertTrue('email=' in mock_load.urls[0]) # these are what we're currently querying for. note that these may # change as our implementation develops. if they do (causing these # assertions to fail) then we probably need to update our fixture. self.assertTrue('db=pmc' in mock_load.urls[0]) self.assertTrue('term=emory' in mock_load.urls[0]) self.assertTrue('field=affl' in mock_load.urls[0]) self.assertTrue('usehistory=y' in mock_load.urls[0]) # fetch one articles = article_qs[:20] # grab a slice to limit the query articles[0] self.assertEqual(mock_xmlmap.load_xmlobject_from_file.call_count, 2) # check that we slept between calls (reqd by eutils policies) self.assertEqual(mock_sleep.call_count, 1) sleep_args, sleep_kwargs = mock_sleep.call_args self.assertTrue(sleep_args[0] >= 0.3) # check the second query url self.assertTrue('efetch.fcgi' in mock_load.urls[1]) # always required self.assertTrue('tool=' in mock_load.urls[1]) self.assertTrue('email=' in mock_load.urls[1]) # what we're currently querying for self.assertTrue('db=pmc' in mock_load.urls[1]) self.assertTrue('usehistory=y' in mock_load.urls[1]) self.assertTrue('query_key=' in mock_load.urls[1]) self.assertTrue('WebEnv=' in mock_load.urls[1]) self.assertTrue('retmode=xml' in mock_load.urls[1]) self.assertTrue('retstart=0' in mock_load.urls[1]) self.assertTrue('retmax=20' in mock_load.urls[1])