本文整理汇总了Python中invenio_records.Record.commit方法的典型用法代码示例。如果您正苦于以下问题:Python Record.commit方法的具体用法?Python Record.commit怎么用?Python Record.commit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类invenio_records.Record
的用法示例。
在下文中一共展示了Record.commit方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_db
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import commit [as 别名]
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)
示例2: test_db
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import commit [as 别名]
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():
db.create_all()
assert 'records_metadata' in db.metadata.tables
assert 'records_metadata_version' in db.metadata.tables
assert 'transaction' in db.metadata.tables
schema = {
'type': 'object',
'properties': {
'title': {'type': 'string'},
'field': {'type': 'boolean'},
'hello': {'type': 'array'},
},
'required': ['title'],
}
data = {'title': 'Test', '$schema': schema}
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(MissingModelError):
record3.commit()
with app.app_context():
db.drop_all()
示例3: test_db
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import commit [as 别名]
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():
db.drop_all()
db.create_all()
assert 'records_metadata' in db.metadata.tables
assert 'records_metadata_version' in db.metadata.tables
assert 'transaction' in db.metadata.tables
schema = {
'type': 'object',
'properties': {
'title': {'type': 'string'},
'field': {'type': 'boolean'},
'hello': {'type': 'array'},
},
'required': ['title'],
}
data = {'title': 'Test', '$schema': schema}
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(MissingModelError):
record3.commit()
# Check invalid schema values
with app.app_context():
data = {
'$schema': 'http://json-schema.org/geo#',
'latitude': 42,
'longitude': 42,
}
record_with_schema = Record.create(data).commit()
db.session.commit()
record_with_schema['latitude'] = 'invalid'
with pytest.raises(ValidationError):
record_with_schema.commit()
# Allow types overriding on schema validation
with app.app_context():
data = {
'title': 'Test',
'hello': tuple(['foo', 'bar']),
'$schema': schema
}
app.config['RECORDS_VALIDATION_TYPES'] = {}
with pytest.raises(ValidationError):
Record.create(data).commit()
app.config['RECORDS_VALIDATION_TYPES'] = {'array': (list, tuple)}
record_uuid = Record.create(data).commit()
db.session.commit()
with app.app_context():
db.drop_all()