Пример #1
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"])
        print response
        expected = 361
        assert_equals(response["metrics"]['dryad:package_views']['values']["raw"], expected)
Пример #2
0
def get_item_from_tiid(tiid, format=None, include_history=False, callback_name=None):
    try:
        item = item_module.get_item(tiid, myrefsets, myredis)
    except (LookupError, AttributeError):
        abort_custom(404, "item does not exist")

    if not item:
        abort_custom(404, "item does not exist")

    if item_module.is_currently_updating(tiid, myredis):
        response_code = 210 # not complete yet
        item["currently_updating"] = True
    else:
        response_code = 200
        item["currently_updating"] = False

    api_key = request.args.get("key", None)
    clean_item = item_module.clean_for_export(item, api_key, os.getenv("API_ADMIN_KEY"))
    clean_item["HTTP_status_code"] = response_code  # hack for clients who can't read real response codes

    resp_string = json.dumps(clean_item, sort_keys=True, indent=4)
    if callback_name is not None:
        resp_string = callback_name + '(' + resp_string + ')'

    resp = make_response(resp_string, response_code)

    return resp
Пример #3
0
    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"])
        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])
Пример #4
0
def get_item_from_tiid(tiid,
                       format=None,
                       include_history=False,
                       callback_name=None):
    try:
        item = item_module.get_item(tiid, myrefsets, myredis)
    except (LookupError, AttributeError):
        abort_custom(404, "item does not exist")

    if not item:
        abort_custom(404, "item does not exist")

    if item_module.is_currently_updating(tiid, myredis):
        response_code = 210  # not complete yet
        item["currently_updating"] = True
    else:
        response_code = 200
        item["currently_updating"] = False

    api_key = request.args.get("key", None)
    clean_item = item_module.clean_for_export(item, api_key,
                                              os.getenv("API_ADMIN_KEY"))
    clean_item[
        "HTTP_status_code"] = response_code  # hack for clients who can't read real response codes

    resp_string = json.dumps(clean_item, sort_keys=True, indent=4)
    if callback_name is not None:
        resp_string = callback_name + '(' + resp_string + ')'

    resp = make_response(resp_string, response_code)

    return resp
Пример #5
0
    def test_adds_genre(self):
        self.TEST_OBJECT = item_module.create_objects_from_item_doc(self.ITEM_DATA)        
        self.db.session.add(self.TEST_OBJECT)
        self.db.session.commit()

        item = item_module.get_item("test", self.myrefsets, self.d)
        assert_equals(item["biblio"]['genre'], "article")
Пример #6
0
    def test_adds_genre(self):
        self.TEST_OBJECT = item_module.create_objects_from_item_doc(self.ITEM_DATA)        
        self.db.session.add(self.TEST_OBJECT)
        self.db.session.commit()

        item = item_module.get_item("test", self.myrefsets, self.r)
        assert_equals(item["biblio"]['genre'], "article")
Пример #7
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)
Пример #8
0
    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])
Пример #9
0
 def test_clean_for_export_given_wrong_secret_key(self):
     self.d.save(self.ITEM_DATA)
     item = item_module.get_item("test", self.myrefsets, self.d)
     item["metrics"]["scopus:citations"] = {"values":{"raw": 22}}
     item["metrics"]["citeulike:bookmarks"] = {"values":{"raw": 33}}
     response = item_module.clean_for_export(item, "WRONG", "SECRET")
     print response["metrics"].keys()
     expected = ['bar:views', 'wikipedia:mentions']
     assert_equals(response["metrics"].keys(), expected)
Пример #10
0
    def test_clean_for_export_given_correct_secret_key(self):
        self.save_test_item()

        item = item_module.get_item("test", self.myrefsets, self.r)
        item["metrics"]["scopus:citations"] = {"values":{"raw": 22}}
        item["metrics"]["citeulike:bookmarks"] = {"values":{"raw": 33}}
        response = item_module.clean_for_export(item, "SECRET", "SECRET")
        print response["metrics"].keys()
        expected = ['altmetric_com:tweets', 'wikipedia:mentions', 'scopus:citations', 'citeulike:bookmarks']
        assert_equals(sorted(response["metrics"].keys()), sorted(expected))
Пример #11
0
    def test_clean_for_export_no_key(self):
        self.save_test_item()

        item = item_module.get_item("test", self.myrefsets, self.r)
        item["metrics"]["scopus:citations"] = {"values":{"raw": 22}}
        item["metrics"]["citeulike:bookmarks"] = {"values":{"raw": 33}}
        response = item_module.clean_for_export(item)
        print response["metrics"].keys()
        expected = ['altmetric_com:tweets', 'wikipedia:mentions']
        assert_items_equal(response["metrics"].keys(), expected)
Пример #12
0
    def test_clean_for_export_given_wrong_secret_key(self):
        self.save_test_item()

        item = item_module.get_item("test", self.myrefsets, self.d)
        item["metrics"]["scopus:citations"] = {"values":{"raw": 22}}
        item["metrics"]["citeulike:bookmarks"] = {"values":{"raw": 33}}
        response = item_module.clean_for_export(item, "WRONG", "SECRET")
        print response["metrics"].keys()
        expected = ['topsy:tweets', 'wikipedia:mentions']
        assert_equals(response["metrics"].keys(), expected)
Пример #13
0
def get_item_from_tiid(tiid, format=None, include_history=False):

    try:
        item = item_module.get_item(tiid, myrefsets, mydao, include_history)
    except (LookupError, AttributeError):
        abort(404)

    if not item:
        abort(404)

    if item_module.is_currently_updating(tiid, myredis):
        response_code = 210 # not complete yet
        item["currently_updating"] = True
    else:
        response_code = 200
        item["currently_updating"] = False

    api_key = request.args.get("key", None)
    clean_item = item_module.clean_for_export(item, api_key, os.getenv("API_KEY"))
    resp = make_response(json.dumps(clean_item, sort_keys=True, indent=4),
                         response_code)
    resp.mimetype = "application/json"

    return resp
Пример #14
0
 def test_adds_genre(self):
     # put the item in the db
     self.d.save(self.ITEM_DATA)
     item = item_module.get_item("test", self.myrefsets, self.d)
     assert_equals(item["biblio"]['genre'], "article")