def test_subjects_gnd(self): """Test contributor rules.""" from invenio.modules.records.api import Record r = Record.create({'subjects': [ {'term': 'Smith, John', 'identifier': 'gnd:118604740', 'scheme': 'gnd'}, ]}, 'json') # Test that "gnd:" is not added in MARC print(r.produce('json_for_marc')) assert {'6501_a': 'Smith, John', '6501_0': '(gnd)118604740'} \ in r.produce('json_for_marc') r = Record.create( '<record>' '<datafield tag="650" ind1="1" ind2=" ">' '<subfield code="a">Smith, John</subfield>' '<subfield code="0">(gnd)118604740</subfield>' '</datafield>' '</record>', master_format='marc' ) # Test that "gnd:" is added back in JSON assert r['subjects'] == [{ 'identifier': 'gnd:118604740', 'scheme': 'gnd', 'term': 'Smith, John'}]
def test_types(self): """Test upload_type rules.""" from invenio.modules.records.api import Record for t in cfg['UPLOAD_TYPES']: if t['subtypes']: for st in t['subtypes']: r = Record.create( '<record><datafield tag="980" ind1=" " ind2=" ">' '<subfield code="b">{1}</subfield>' '<subfield code="a">{0}</subfield>' '</datafield></record>'.format(t['type'], st['type']), master_format='marc' ) assert r['upload_type'] == {"type": t['type'], "subtype": st['type']} assert len(r.get('collections', [])) == 0 else: r = Record.create( '<record><datafield tag="980" ind1=" " ind2=" ">' '<subfield code="a">{0}</subfield>' '</datafield></record>'.format(t['type']), master_format='marc' ) assert r['upload_type'] == {"type": t['type']} assert len(r.get('collections', [])) == 0
def test_jsonalchemy_toint_usage(self): """Test the usage of ``to_int`` function in real life example. The ``test_toint`` model contains a field which contains an integer subfield. Whenever the record is obtained from ``MARCXML``, the string in mentioned subfield has to be converted to an integer. However, JSONAlchemy fills every absent subfield with a ``None`` value. If the record is not provided with the integer subfield and the built-in ``int`` function is used, the code will crash. The ``to_int`` function used inside definition of ``test_toint`` field prevents it. Here the unprovided subfield is ``999__a``. """ xml = '<collection><record><datafield tag="999" ind1="" ind2= "">' \ '<subfield code="b">Value</subfield></datafield></record>' \ '</collection>' from invenio.modules.records.api import Record simple_record = Record.create(xml, master_format='marc', model="test_toint", namespace='testsuite') self.assertEqual(len(simple_record.__dict__['_dict']['__meta_metadata__']['__errors__']), 0) # Check if it works when the value is provided. xml = '<collection><record><datafield tag="999" ind1="" ind2= "">' \ '<subfield code="a">9999</subfield>' \ '<subfield code="b">Value</subfield></datafield></record>' \ '</collection>' simple_record = Record.create(xml, master_format='marc', model="test_toint", namespace='testsuite') self.assertEqual(simple_record['with_integers'][0]['some_int'], 9999)
def test_gnd(self): """Test contributor rules.""" from invenio.modules.records.api import Record r = Record.create({'contributors': [ {'name': 'Smith, John', 'gnd': 'gnd:118604740', 'type': 'DataCurator'}, ]}, 'json') print r.produce('json_for_marc') # Test that "gnd:" is not added in MARC assert {'700__0': ['(gnd)118604740', None], '700__4': 'cur', '700__a': 'Smith, John'} \ in r.produce('json_for_marc') r = Record.create( '<record>' '<datafield tag="700" ind1=" " ind2=" ">' '<subfield code="4">cur</subfield>' '<subfield code="a">Smith, John</subfield>' '<subfield code="0">(gnd)118604740</subfield>' '</datafield>' '</record>', master_format='marc' ) # Test that "gnd:" is added back in JSON print r['contributors'] assert r['contributors'] == [{ 'gnd': 'gnd:118604740', 'name': 'Smith, John', 'orcid': '', 'type': 'DataCurator' }]
def update(recid): """View for INSPIRE author update form.""" # Store referrer in session for later redirection to original page session["author_update_referrer"] = request.referrer data = {} if recid: try: url = os.path.join(cfg["AUTHORS_UPDATE_BASE_URL"], "record", str(recid), "export", "xm") xml = requests.get(url) data = Record.create(xml.text.encode("utf-8"), 'marc', model='author').produce("json_for_form") convert_for_form(data) except requests.exceptions.RequestException: pass data["recid"] = recid else: return redirect(url_for("inspire_authors.new")) form = AuthorUpdateForm(data=data) ctx = { "action": url_for('.submitupdate'), "name": "authorUpdateForm", "id": "authorUpdateForm", } return render_template('authors/forms/update_form.html', form=form, **ctx)
def test_jsonalchemy_tooldvalue(self): """Test behaviour of ``set_default_value``. In this example, the value provided to the reader in ``d`` subfield is in wrong format. However, the behaviour of ``JSONAlchemy`` in such case is to skip the value. Given the below value of the subfield, the module crashes in ``set_default_value``. The error has been caught. What is the reason behind the mentioned behaviour needs further investigation. """ from invenio.modules.records.api import Record # Check if it works when the value is provided. xml = '''<collection><record><datafield tag="100" ind1=" " ind2=" "> <subfield code="a">Guy, Bobby</subfield> <subfield code="d">I like trains</subfield> <subfield code="g">ACTIVE</subfield> <subfield code="q">Bobby Guy</subfield> </datafield></record></collection>''' simple_record = Record.create(xml, master_format='marc', model="test_oldvalue", namespace='testsuite') self.assertEqual(simple_record['dates']['birth'], None)
def test_lossless_marc_import_export(self): from invenio.modules.records.api import Record r = Record.create(test_marc, master_format="marc").dumps() for k in test_record.keys(): self.assertEqual(test_record[k], r[k])
def test_json_for_ld(self): from invenio.modules.records.api import Record r = Record.create({'title': 'Test'}, 'json') import copy r = Record(json=copy.copy(test_record), master_format='marc') r.produce('json_for_ld')
def _create_marcxml_record(obj, eng): from invenio.modules.records.api import Record obj.log.info("Creating marcxml record") x = Record.create(obj.data, 'json', model='author') obj.extra_data["marcxml"] = x.legacy_export_as_marc() obj.log.info("Produced MarcXML: \n {}".format( obj.extra_data["marcxml"]) )
def marshal_deposition(cls, deposition): """ Generate a JSON representation for REST API of a Deposition """ # Get draft if deposition.has_sip() and '_edit' in deposition.drafts: draft = deposition.get_draft('_edit') metadata_fields = cls.marshal_metadata_edit_fields elif deposition.has_sip(): # FIXME: Not based on latest available data in record. sip = deposition.get_latest_sip(sealed=True) draft = record_to_draft( Record.create(sip.package, master_format='marc'), post_process=process_draft ) metadata_fields = cls.marshal_metadata_edit_fields else: draft = deposition.get_or_create_draft('_metadata') metadata_fields = cls.marshal_metadata_fields # Fix known differences in marshalling current_app.logger.debug(draft.values) draft.values = filter_empty_elements(draft.values) current_app.logger.debug(draft.values) # Set disabled values to None in output for field, flags in draft.flags.items(): if 'disabled' in flags and field in draft.values: current_app.logger.debug(field) del draft.values[field] # Marshal deposition obj = marshal(deposition, cls.marshal_deposition_fields) # Marshal the metadata attribute obj['metadata'] = marshal(unicodifier(draft.values), metadata_fields) # Add record and DOI information from latest SIP for sip in deposition.sips: if sip.is_sealed(): recjson = sip.metadata if recjson.get('recid'): obj['record_id'] = fields.Integer().format( recjson.get('recid') ) obj['record_url'] = fields.String().format(url_for( 'record.metadata', recid=recjson.get('recid'), _external=True )) if (recjson.get('doi') and recjson.get('doi').startswith( cfg['CFG_DATACITE_DOI_PREFIX'] + "/")): obj['doi'] = fields.String().format(recjson.get('doi')) obj['doi_url'] = fields.String().format( "http://dx.doi.org/%s" % obj['doi'] ) break return obj
def test_pre1900_embargo_date(self): from invenio.modules.records.api import Record r = Record.create( '<record><datafield tag="942" ind1="" ind2="">' '<subfield code="a">0900-12-31</subfield>' '</datafield></record>', master_format='marc' ) self.assertEqual(date(900, 12, 31), r['embargo_date']) self.assertEqual('0900-12-31', r.dumps()['embargo_date']) assert '0900-12-31' in r.legacy_export_as_marc()
def test_json_for_ld(self): from invenio.modules.records.api import Record r = Record.create({"title": "Test"}, "json") import copy r = Record(json=copy.copy(test_record), master_format="marc") ld = r.produce("json_for_ld") print(ld)
def test_json_for_form(self): from invenio.modules.records.api import Record r = Record.create({'title': 'Test'}, 'json') assert r.produce('json_for_form')['title'] == 'Test' assert {'245__a': 'Test'} in r.produce('json_for_marc') import copy r = Record(json=copy.copy(test_record), master_format='marc') form_json = r.produce('json_for_form') for k, v in test_form_json.items(): self.assertEqual(form_json[k], test_form_json[k])
def test_pre1900_publication_date(self): from invenio.modules.records.api import Record r = Record.create( '<record><datafield tag="260" ind1="" ind2="">' '<subfield code="c">0900-12-31</subfield>' "</datafield></record>", master_format="marc", ) self.assertEqual(date(900, 12, 31), r["publication_date"]) self.assertEqual("0900-12-31", r.dumps()["publication_date"]) assert "0900-12-31" in r.legacy_export_as_marc()
def test_jsonalchemy_toint_usage(self): """Test the usage of ``to_int`` function in real life example. The ``test_toint`` model contains a field which contains an integer subfield. Whenever the record is obtained from ``MARCXML``, the string in mentioned subfield has to be converted to an integer. However, JSONAlchemy fills every absent subfield with a ``None`` value. If the record is not provided with the integer subfield and the built-in ``int`` function is used, the code will crash. The ``to_int`` function used inside definition of ``test_toint`` field prevents it. Here the unprovided subfield is ``999__a``. """ xml = '<collection><record><datafield tag="999" ind1="" ind2= "">' \ '<subfield code="b">Value</subfield></datafield></record>' \ '</collection>' from invenio.modules.records.api import Record simple_record = Record.create(xml, master_format='marc', model="test_toint", namespace='testsuite') self.assertEqual( len(simple_record.__dict__['_dict']['__meta_metadata__'] ['__errors__']), 0) # Check if it works when the value is provided. xml = '<collection><record><datafield tag="999" ind1="" ind2= "">' \ '<subfield code="a">9999</subfield>' \ '<subfield code="b">Value</subfield></datafield></record>' \ '</collection>' simple_record = Record.create(xml, master_format='marc', model="test_toint", namespace='testsuite') self.assertEqual(simple_record['with_integers'][0]['some_int'], 9999)
def test_json_for_form(self): from invenio.modules.records.api import Record r = Record.create({"title": "Test"}, "json") assert r.produce("json_for_form")["title"] == "Test" assert {"245__a": "Test"} in r.produce("json_for_marc") import copy r = Record(json=copy.copy(test_record), master_format="marc") form_json = r.produce("json_for_form") for k, v in test_form_json.items(): self.assertEqual(form_json[k], test_form_json[k])
def get_xml_and_jsonify(rep_no): """ Retreives XML data from CDS and returns jsonified temp record :param rep_no: The report number to be retreived :type rep_no: String :returns: dict Workflow - Download an XML file from CDS using a link like: http://cds.cern.ch/search?p=reportnumber%3A"CERN-THESIS-2013-297"&of=xm JSONify the xml and return it. """ xml = get("""http://cds.cern.ch/search?p=reportnumber%%3A"%s"&of=xm""" % rep_no).content if xml[83] == '1' and xml[84] == ' ': return Record.create(xml, 'marc', model='data_analysis_cds_extract') return None
def _get_arxiv_id_from_inspire(doi): """ """ url_values = urllib.urlencode({'p': 'doi', 'doi': doi, 'of': 'xm'}) url = 'https://inspirehep.net/search?' + url_values try: collectionxml = urllib.urlopen(url).read() except IOError: raise else: try: recordxml = list(split_blob(collectionxml, 'marc'))[0] except IndexError: return None inspire_record = Record.create(recordxml, master_format='marc', namespace='recordext') return _get_arxiv_id_from_record(inspire_record)
def test_formjson_for_contributors(self): """Test contributor rules.""" from invenio.modules.records.api import Record r = Record.create( '<record>' '<datafield tag="700" ind1=" " ind2=" ">' '<subfield code="u">Test</subfield>' '<subfield code="4">cph</subfield>' '<subfield code="a">Nielsen, Lars</subfield>' '</datafield>' '<datafield tag="700" ind1=" " ind2=" ">' '<subfield code="u">Hansen</subfield>' '<subfield code="4">edt</subfield>' '<subfield code="a">Viggo</subfield>' '</datafield>' '<datafield tag="700" ind1=" " ind2=" ">' '<subfield code="4">edt</subfield>' '<subfield code="a">Hansen</subfield>' '</datafield>' '</record>', master_format='marc' ) assert len(r['contributors']) == 3 assert len(r.produce('json_for_form')['contributors']) == 3
def setUp(self): from invenio.modules.records.api import Record self.record_good = Bibtex(Record.create(test_record, 'json')) self.record_bad = Bibtex(Record.create(test_bad_record, 'json')) self.record_empty = Bibtex({})
def test_json_for_form(self): from invenio.modules.records.api import Record r = Record.create({'title': 'Test'}, 'json') assert r.produce('json_for_form') == {'title': 'Test'} assert r.produce('json_for_marc') == [{'245__a': 'Test'}]
def test_lossless_marc_import_export(self): from invenio.modules.records.api import Record r = Record.create(test_marc, master_format='marc').dumps() for k in test_record.keys(): self.assertEqual(test_record[k], r[k])