本文整理汇总了Python中invenio_records.Record类的典型用法代码示例。如果您正苦于以下问题:Python Record类的具体用法?Python Record怎么用?Python Record使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Record类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_validate_with_format
def test_validate_with_format(app, db):
"""Test that validation can accept custom format rules."""
with app.app_context():
checker = FormatChecker()
checker.checks("foo")(lambda el: el.startswith("foo"))
data = {"bar": "foo", "$schema": {"properties": {"bar": {"format": "foo"}}}}
# test record creation with valid data
assert data == Record.create(data)
record = Record.create(data, format_checker=checker)
# test direct call to validate with valid data
assert record.validate(format_checker=checker) is None
# test commit with valid data
record.commit(format_checker=checker)
record["bar"] = "bar"
# test direct call to validate with invalid data
with pytest.raises(ValidationError) as excinfo:
record.validate(format_checker=checker)
assert "'bar' is not a 'foo'" in str(excinfo.value)
# test commit with invalid data
with pytest.raises(ValidationError) as excinfo:
record.commit(format_checker=checker)
assert "'bar' is not a 'foo'" in str(excinfo.value)
data["bar"] = "bar"
# test record creation with invalid data
with pytest.raises(ValidationError) as excinfo:
record = Record.create(data, format_checker=checker)
assert "'bar' is not a 'foo'" in str(excinfo.value)
示例2: test_valid_delete
def test_valid_delete(app):
"""Test VALID record delete request (DELETE .../records/<record_id>)."""
with app.app_context():
# create the record using the internal API
pid, record = create_record(test_data)
with app.test_client() as client:
headers = [('Accept', 'application/json')]
res = client.delete(url_for('invenio_records_rest.recid_item',
pid_value=pid.pid_value),
headers=headers)
assert res.status_code == 204
# check database state
with pytest.raises(NoResultFound):
Record.get_record(record.id)
assert pid.is_deleted()
# check that DELETE with non JSON accepted format will work
# as it returns nothing
pid, record = create_record(test_data)
headers = [('Accept', 'video/mp4')]
res = client.delete(url_for('invenio_records_rest.recid_item',
pid_value=pid.pid_value),
headers=headers)
assert res.status_code == 204
示例3: test_validate_partial
def test_validate_partial(app, db):
"""Test partial validation."""
schema = {"properties": {"a": {"type": "string"}, "b": {"type": "string"}}, "required": ["b"]}
data = {"a": "hello", "$schema": schema}
with app.app_context():
# Test validation on create()
# normal validation should fail because 'b' is required
with pytest.raises(ValidationError) as exc_info:
Record.create(data)
assert "'b' is a required property" == exc_info.value.message
# validate with a less restrictive validator
record = Record.create(data, validator=PartialDraft4Validator)
# set wrong data types should fails in any case
data_incorrect = copy.deepcopy(data)
data_incorrect["a"] = 1
with pytest.raises(ValidationError) as exc_info:
Record.create(data_incorrect, validator=PartialDraft4Validator)
assert "1 is not of type 'string'" == exc_info.value.message
# Test validation on commit()
# validation not passing with normal validator
with pytest.raises(ValidationError) as exc_info:
record.commit()
assert "'b' is a required property" == exc_info.value.message
# validation passing with less restrictive validator
assert data == record.commit(validator=PartialDraft4Validator)
# set wrong data types should fails in any case
record["a"] = 1
with pytest.raises(ValidationError) as exc_info:
record.commit(validator=PartialDraft4Validator)
示例4: test_record_update_mutable
def test_record_update_mutable(app, db):
"""Test updating mutables in a record."""
recid = uuid.UUID("262d2748-ba41-456f-a844-4d043a419a6f")
# Create a new record with two mutables, a list and a dict
rec = Record.create({"title": "Title", "list": ["foo"], "dict": {"moo": "boo"}}, id_=recid)
# Make sure mutables are there before and after commit
assert rec == {"title": "Title", "list": ["foo"], "dict": {"moo": "boo"}}
db.session.commit()
db.session.expunge_all()
rec = Record.get_record(recid)
assert rec == {"title": "Title", "list": ["foo"], "dict": {"moo": "boo"}}
# Set the mutables under key
rec["list"] = ["bar"]
rec["dict"] = {"eggs": "bacon"}
rec.commit()
# Make sure it commits to DB
assert rec == {"title": "Title", "list": ["bar"], "dict": {"eggs": "bacon"}}
db.session.commit()
db.session.expunge_all()
rec = Record.get_record(recid)
assert rec == {"title": "Title", "list": ["bar"], "dict": {"eggs": "bacon"}}
# Update the mutables under key
rec["list"].append("spam")
rec["dict"]["ham"] = "chicken"
rec.commit()
# Make sure it commits to DB
assert rec == {"title": "Title", "list": ["bar", "spam"], "dict": {"eggs": "bacon", "ham": "chicken"}}
db.session.commit()
db.session.expunge_all()
rec = Record.get_record(recid)
assert rec == {"title": "Title", "list": ["bar", "spam"], "dict": {"eggs": "bacon", "ham": "chicken"}}
示例5: test_valid_put
def test_valid_put(app):
"""Test VALID record patch request (PATCH .../records/<record_id>)."""
with app.app_context():
InvenioRecordsREST(app)
# create the record using the internal API
internal_record = Record.create(test_data)
with app.test_client() as client:
headers = [('Content-Type', 'application/json'),
('Accept', 'application/json')]
res = client.put(url_for(blueprint.name + '.' +
RecordResource.view_name,
record_id=internal_record.model.id),
data=json.dumps(test_data_patched),
headers=headers)
assert res.status_code == 200
# check that the returned record matches the given data
response_data = json.loads(res.get_data(as_text=True))
assert response_data['data'] == test_data_patched
# check that an internal record returned id and that it contains
# the same data
assert 'id' in response_data.keys()
internal_record = Record.get_record(response_data['id'])
assert internal_record == response_data['data']
# check that the returned self link returns the same data
subtest_self_link(response_data, res.headers, client)
示例6: record_upsert
def record_upsert(json):
"""Insert or update a record."""
control_number = json.get('control_number', json.get('recid'))
if control_number:
control_number = int(control_number)
pid_type = InspireRecordIdProvider.schema_to_pid_type(json['$schema'])
try:
pid = PersistentIdentifier.get(pid_type, control_number)
record = Record.get_record(pid.object_uuid)
record.update(json)
record.commit()
except PIDDoesNotExistError:
record = Record.create(json, id_=None)
# Create persistent identifier.
inspire_recid_minter(str(record.id), json)
if json.get('deleted'):
new_recid = get_recid_from_ref(json.get('new_record'))
if new_recid:
merged_record = get_db_record(pid_type, new_recid)
merge_pidstores_of_two_merged_records(merged_record.id, record.id)
else:
soft_delete_pidstore_for_record(record.id)
return record
示例7: test_file_permissions
def test_file_permissions(app, db, test_object, # fixtures
user, access_right, expected):
"""Test file permissions."""
# Create test users
admin = User(email='[email protected]', password='123456')
owner = User(email='[email protected]', password='123456')
auth = User(email='[email protected]', password='123456')
db.session.add_all([admin, owner, auth])
db.session.add(
ActionUsers.allow(ActionNeed('admin-access'), user=admin)
)
# Create test record
rec_uuid = uuid.uuid4()
PersistentIdentifier.create(
'recid',
'1',
object_type='rec',
object_uuid=rec_uuid,
status=PIDStatus.REGISTERED
)
Record.create({
'recid': 1,
'owners': [2],
'access_right': access_right,
'_files': [
{
'key': test_object.key,
'bucket': str(test_object.bucket_id),
'checksum': 'invalid'
},
]
}, id_=rec_uuid)
db.session.add(
RecordsBuckets(record_id=rec_uuid, bucket_id=test_object.bucket_id)
)
file_url = url_for(
'invenio_records_ui.recid_files',
pid_value='1',
filename=test_object.key
)
db.session.commit()
with app.test_client() as client:
if user:
# Login as user
with client.session_transaction() as sess:
sess['user_id'] = User.query.filter_by(
email='{}@zenodo.org'.format(user)).one().id
sess['_fresh'] = True
res = client.get(file_url)
assert res.status_code == expected
示例8: files
def files():
"""Load files."""
data_path = os.path.join(os.path.dirname(__file__), 'data')
# Create location
loc = Location(name='local', uri=data_path, default=True)
db.session.commit()
# Bucket
bucket = Bucket.create(loc)
# Example files from the data folder
example_files = (
'markdown.md',
'csvfile.csv',
'zipfile.zip',
'jsonfile.json',
'xmlfile.xml',
'notebook.ipynb',
'jpgfile.jpg',
'pngfile.png',
)
# Create single file records
for f in example_files:
with open(os.path.join(data_path, f), 'rb') as fp:
create_object(bucket, f, fp)
# Create a multi-file record
rec_uuid = uuid4()
provider = RecordIdProvider.create(object_type='rec', object_uuid=rec_uuid)
data = {
'pid_value': provider.pid.pid_value,
'files': []
}
# Template to create different files
template_file = {
'uri': '/files/{0}/{1}',
'key': '',
'bucket': str(bucket.id),
'local': True
}
for filename in example_files:
file_data = template_file.copy()
file_data['uri'] = file_data['uri'].format(str(bucket.id), filename)
file_data['key'] = filename
data['files'].append(file_data)
Record.create(data, id_=rec_uuid)
db.session.commit()
示例9: test_change_record_community
def test_change_record_community(app, test_records):
"""Test updating the community field fails."""
with app.app_context():
record = Record.get_record(test_records[0].record_id)
del record['community']
with pytest.raises(AlteredRecordError):
record.commit()
with app.app_context():
record = Record.get_record(test_records[0].record_id)
record['community'] = str(uuid.uuid4())
with pytest.raises(AlteredRecordError):
record.commit()
示例10: test_db
def test_db():
"""Test database backend."""
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get(
'SQLALCHEMY_DATABASE_URI', 'sqlite:///test.db'
)
FlaskCLI(app)
InvenioDB(app)
InvenioRecords(app)
with app.app_context():
create_database(db.engine.url)
db.create_all()
assert len(db.metadata.tables) == 3
data = {'title': 'Test'}
from invenio_records.models import RecordMetadata as RM
# Create a record
with app.app_context():
assert RM.query.count() == 0
record_uuid = Record.create(data).id
db.session.commit()
assert RM.query.count() == 1
db.session.commit()
# Retrieve created record
with app.app_context():
record = Record.get_record(record_uuid)
assert record.dumps() == data
with pytest.raises(NoResultFound):
Record.get_record(uuid.uuid4())
record['field'] = True
record = record.patch([
{'op': 'add', 'path': '/hello', 'value': ['world']}
])
assert record['hello'] == ['world']
record.commit()
db.session.commit()
with app.app_context():
record2 = Record.get_record(record_uuid)
assert record2.model.version_id == 2
assert record2['field']
assert record2['hello'] == ['world']
db.session.commit()
# Cannot commit record without model (i.e. Record.create_record)
with app.app_context():
record3 = Record({'title': 'Not possible'})
with pytest.raises(RecordNotCommitableError):
record3.commit()
with app.app_context():
db.drop_all()
drop_database(db.engine.url)
示例11: migrate_chunk
def migrate_chunk(chunk, broken_output=None, dry_run=False):
from invenio_indexer.api import RecordIndexer
from ..pidstore.minters import inspire_recid_minter
indexer = RecordIndexer()
index_queue = []
for raw_record in chunk:
record = marc_create_record(raw_record, keep_singletons=False)
json_record = create_record(record)
if '$schema' in json_record:
json_record['$schema'] = url_for(
'invenio_jsonschemas.get_schema',
schema_path="records/{0}".format(json_record['$schema'])
)
rec_uuid = str(Record.create(json_record, id_=None).id)
# Create persistent identifier.
pid = inspire_recid_minter(rec_uuid, json_record)
index_queue.append(pid.object_uuid)
db.session.commit()
# Request record indexing
for i in index_queue:
indexer.index_by_id(i)
# Send task to migrate files.
return rec_uuid
示例12: test_reindex
def test_reindex(app, script_info):
"""Test reindex."""
# load records
with app.test_request_context():
runner = CliRunner()
rec_uuid = uuid.uuid4()
data = {'title': 'Test0'}
record = Record.create(data, id_=rec_uuid)
db.session.commit()
# Initialize queue
res = runner.invoke(cli.queue, ['init', 'purge'],
obj=script_info)
assert 0 == res.exit_code
res = runner.invoke(cli.reindex, ['--yes-i-know'], obj=script_info)
assert 0 == res.exit_code
res = runner.invoke(cli.run, [], obj=script_info)
assert 0 == res.exit_code
sleep(5)
indexer = RecordIndexer()
index, doc_type = indexer.record_to_index(record)
res = current_search_client.get(index=index, doc_type=doc_type,
id=rec_uuid)
assert res['found']
# Destroy queue
res = runner.invoke(cli.queue, ['delete'],
obj=script_info)
assert 0 == res.exit_code
示例13: test_record_dump
def test_record_dump(app, db):
"""Test record dump method."""
with app.app_context():
record = Record.create({"foo": {"bar": "Bazz"}})
record_dump = record.dumps()
record_dump["foo"]["bar"] = "Spam"
assert record_dump["foo"]["bar"] != record["foo"]["bar"]
示例14: test_change_record_schema_fails
def test_change_record_schema_fails(app, test_records):
"""Test updating the $schema field fails."""
with app.app_context():
record = Record.get_record(test_records[0].record_id)
del record['$schema']
with pytest.raises(AlteredRecordError):
record.commit()
示例15: load_records
def load_records(app, filename, schema, tries=5):
"""Try to index records."""
indexer = RecordIndexer()
records = []
with app.app_context():
with mock.patch('invenio_records.api.Record.validate',
return_value=None):
data_filename = pkg_resources.resource_filename(
'invenio_records', filename)
records_data = load(data_filename)
with db.session.begin_nested():
for item in records_data:
record_id = uuid.uuid4()
item_dict = dict(marc21.do(item))
item_dict['$schema'] = schema
recid_minter(record_id, item_dict)
oaiid_minter(record_id, item_dict)
record = Record.create(item_dict, id_=record_id)
indexer.index(record)
records.append(record.id)
db.session.commit()
# Wait for indexer to finish
for i in range(tries):
response = current_search_client.search()
if response['hits']['total'] >= len(records):
break
current_search.flush_and_refresh('_all')
return records