def setUp(self): self.config = None #placeholder self.TEST_PROVIDER_CONFIG = [("wikipedia", {})] self.d = None # do the same thing for the redis db, set up the test redis database. We're using DB Number 8 self.r = tiredis.from_url("redis://localhost:6379", db=REDIS_UNITTEST_DATABASE_NUMBER) self.r.flushdb() provider_queues = {} providers = ProviderFactory.get_providers(self.TEST_PROVIDER_CONFIG) for provider in providers: provider_queues[provider.provider_name] = backend.PythonQueue( provider.provider_name + "_queue") self.b = backend.Backend(backend.RedisQueue("alias-unittest", self.r), provider_queues, [backend.PythonQueue("couch_queue")], self.r) self.fake_item = { "_id": "1", "type": "item", "num_providers_still_updating": 1, "aliases": { "pmid": ["111"] }, "biblio": {}, "metrics": {}, "last_modified": datetime.datetime(2013, 1, 1) } self.fake_aliases_dict = {"pmid": ["222"]} self.tiid = "abcd" self.db = setup_postgres_for_unittests(db, app)
def test_add_to_couch_queue_if_nonzero_given_metrics(self): test_couch_queue = backend.PythonQueue("test_couch_queue") provider_worker = backend.ProviderWorker( mocks.ProviderMock("myfakeprovider"), None, None, None, {"a": test_couch_queue}, None, self.r) metrics_method_response = { 'dryad:package_views': (361, 'http://dx.doi.org/10.5061/dryad.7898'), 'dryad:total_downloads': (176, 'http://dx.doi.org/10.5061/dryad.7898'), 'dryad:most_downloaded_file': (65, 'http://dx.doi.org/10.5061/dryad.7898') } response = provider_worker.add_to_couch_queue_if_nonzero( "aaatiid", #start fake tiid with "a" so in first couch queue metrics_method_response, "metrics", "dummy") # test that it put it on the queue in_queue = test_couch_queue.pop() expected = { 'method_name': 'metrics', 'tiid': 'aaatiid', 'provider_name': 'myfakeprovider', 'analytics_credentials': 'dummy', 'new_content': metrics_method_response } print in_queue assert_equals(in_queue, expected) # check nothing in redis since it had a value response = self.r.get_num_providers_currently_updating("aaatiid") assert_equals(response, 0)
def test_run_metrics_in_queue(self): test_couch_queue = backend.PythonQueue("test_couch_queue") test_couch_queue_dict = {self.fake_item["_id"][0]: test_couch_queue} provider_worker = backend.ProviderWorker( mocks.ProviderMock("myfakeprovider"), None, None, None, test_couch_queue_dict, None, self.r) metrics_method_response = { 'dryad:package_views': (361, 'http://dx.doi.org/10.5061/dryad.7898'), 'dryad:total_downloads': (176, 'http://dx.doi.org/10.5061/dryad.7898'), 'dryad:most_downloaded_file': (65, 'http://dx.doi.org/10.5061/dryad.7898') } response = provider_worker.add_to_couch_queue_if_nonzero( self.fake_item["_id"], metrics_method_response, "metrics", "dummy") # save basic item beforehand item_obj = item_module.create_objects_from_item_doc(self.fake_item) self.db.session.add(item_obj) self.db.session.commit() # run couch_worker = backend.CouchWorker(test_couch_queue, self.r, self.d) couch_worker.run() # check couch_queue has value after response = item_module.get_item(self.fake_item["_id"], {}, self.r) print response expected = 361 assert_equals( response["metrics"]['dryad:package_views']['values']["raw"], expected)
def test_run_aliases_in_queue(self): test_couch_queue = backend.PythonQueue("test_couch_queue") test_couch_queue_dict = {self.fake_item["_id"][0]: test_couch_queue} provider_worker = backend.ProviderWorker( mocks.ProviderMock("myfakeprovider"), None, None, None, test_couch_queue_dict, None, self.r) response = provider_worker.add_to_couch_queue_if_nonzero( self.fake_item["_id"], {"doi": ["10.5061/dryad.3td2f"]}, "aliases", "dummy") # save basic item beforehand item_obj = item_module.create_objects_from_item_doc(self.fake_item) self.db.session.add(item_obj) self.db.session.commit() # run couch_worker = backend.CouchWorker(test_couch_queue, self.r, self.d) response = couch_worker.run() expected = None assert_equals(response, expected) # check couch_queue has value after response = item_module.get_item(self.fake_item["_id"], {}, self.r) print response expected = {'pmid': ['111'], 'doi': ['10.5061/dryad.3td2f']} assert_equals(response["aliases"], expected) # check has updated last_modified time now = datetime.datetime.utcnow().isoformat() assert_equals(response["last_modified"][0:10], now[0:10])
def test_add_to_couch_queue_if_nonzero(self): test_couch_queue = backend.PythonQueue("test_couch_queue") provider_worker = backend.ProviderWorker( mocks.ProviderMock("myfakeprovider"), None, None, None, {"a": test_couch_queue}, None, self.r) response = provider_worker.add_to_couch_queue_if_nonzero( "aaatiid", #start fake tiid with "a" so in first couch queue {"doi": ["10.5061/dryad.3td2f"]}, "aliases", "dummy") # test that it put it on the queue in_queue = test_couch_queue.pop() expected = ('aaatiid', {'doi': ['10.5061/dryad.3td2f']}, 'aliases') assert_equals(in_queue, expected)
def test_add_to_couch_queue_if_nonzero_given_empty_metrics_response(self): test_couch_queue = backend.PythonQueue("test_couch_queue") provider_worker = backend.ProviderWorker( mocks.ProviderMock("myfakeprovider"), None, None, None, {"a": test_couch_queue}, None, self.r) metrics_method_response = {} response = provider_worker.add_to_couch_queue_if_nonzero( "aaatiid", #start fake tiid with "a" so in first couch queue metrics_method_response, "metrics", "dummy") # test that it did not put it on the queue in_queue = test_couch_queue.pop() expected = None assert_equals(in_queue, expected) # check decremented in redis since the payload was null response = num_left = self.r.get_num_providers_left("aaatiid") assert_equals(response, -1)
def test_run_nothing_in_queue(self): test_couch_queue = backend.PythonQueue("test_couch_queue") couch_worker = backend.CouchWorker(test_couch_queue, self.r, self.d) response = couch_worker.run() expected = None assert_equals(response, expected)