Esempio n. 1
0
def test_record_empty(app, db):
    """Test record creation."""
    # Empty record creation works, and injects a schema.
    record = Draft.create({})
    db.session.commit()
    assert record.schema

    # JSONSchema validation works.
    pytest.raises(ValidationError, Draft.create, {'metadata': {'title': 1}})
def test_draft_create_empty(app, db, location):
    """Test draft creation."""
    # Empty draft creation works, and injects a schema.
    draft = Draft.create({})
    db.session.commit()
    assert draft.schema

    # JSONSchema validation works.
    pytest.raises(ValidationError, Draft.create, {'metadata': {'title': 1}})
def test_draft_create_parent(app, db, location):
    """Test draft creation of the parent record."""
    draft = Draft.create({})
    db.session.commit()
    assert draft.schema.endswith('record-v1.0.0.json')
    assert draft.pid
    assert draft.parent.schema.endswith('parent-v1.0.0.json')
    assert draft.parent.pid

    assert draft.model.parent_id == draft.model.parent.id
    assert draft.pid.object_uuid != draft.parent.pid.object_uuid
def test_draft_parent_state_hard_delete(app, db, location):
    """Test force deletion of a draft."""
    # Initial state: Only draft exists (i.e. no other record versions)
    draft = Draft.create({})
    db.session.commit()
    # happens on:
    # - service.delete_draft for an *unpublished* record
    draft.delete(force=True)
    db.session.commit()
    # Make sure no parent and no parent state is left-behind
    assert ParentState.query.count() == 0
    assert ParentRecordMetadata.query.count() == 0
    assert DraftMetadata.query.count() == 0
    assert RecordMetadata.query.count() == 0
def test_get_records_by_parent(app, db, location):
    """Test get by parent."""
    # Create two published records
    record_v1 = Record.publish(Draft.create({}))
    db.session.commit()
    draft = Draft.new_version(record_v1)
    draft.commit()
    db.session.commit()
    record_v2 = Record.publish(draft)
    db.session.commit()

    # Get all two versions.
    parent = record_v2.parent
    records = Record.get_records_by_parent(parent)

    # Check that we reuse the parent we passed in.
    assert id(parent) == id(records[0].parent) == id(records[1].parent)
def test_draft_create_new_version(app, db, location):
    """Test draft creation of the parent record."""
    # A published record.
    record = Record.publish(Draft.create({}))
    db.session.commit()
    # Create a draft for a new version (happens in service.new_version())
    draft = Draft.new_version(record)
    draft.commit()
    db.session.commit()

    record = Record.get_record(record.id)
    draft = Draft.get_record(draft.id)

    assert record.id != draft.id  # different uuids
    assert record.parent.id == draft.parent.id  # same parent
    assert draft.versions.is_latest_draft is True
    assert draft.versions.is_latest is False
    assert record.versions.is_latest_draft is False
    assert record.versions.is_latest is True
def test_draft_parent_state_soft_delete(app, db, location):
    """Test soft deletion of a draft."""
    # Simulate a record being edited.
    draft = Draft.create({})
    record = Record.create({}, parent=draft.parent)
    db.session.commit()
    # happens on:
    # - service.publish()
    # - service.delete_draft() for a *published* record
    draft.delete(force=False)
    db.session.commit()

    assert ParentState.query.count() == 1
    assert ParentRecordMetadata.query.count() == 1
    assert RecordMetadata.query.count() == 1

    record = Record.get_record(record.id)
    assert record.versions.next_draft_id is None
    assert record.versions.latest_id == record.id
def test_draft_parent_state_hard_delete_with_parent(app, db, location):
    """Test force deletion of a draft."""
    # Initial state: A previous reccord version exists, in addition to draft
    draft = Draft.create({})
    record = Record.create({}, parent=draft.parent)
    db.session.commit()
    # happens on:
    # - service.delete_draft for an *unpublished* record
    draft.delete(force=True)
    db.session.commit()
    # Make sure parent/parent state is still there
    assert ParentState.query.count() == 1
    assert ParentRecordMetadata.query.count() == 1
    assert RecordMetadata.query.count() == 1
    assert DraftMetadata.query.count() == 0

    record = Record.get_record(record.id)
    assert record.versions.next_draft_id is None
    assert record.versions.latest_id == record.id
def test_draft_create_parent_state(app, db, location):
    """Test draft creation of the parent record."""
    draft = Draft.create({})
    db.session.commit()

    # Assert that associated objects were created
    assert ParentState.query.count() == 1
    assert DraftMetadata.query.count() == 1
    assert ParentRecordMetadata.query.count() == 1
    assert RecordMetadata.query.count() == 0

    def assert_state(d):
        # An initial draft is not published, so latest_id/index is None
        assert d.model.index == 1
        assert d.versions.index == 1
        assert d.versions.latest_id is None
        assert d.versions.latest_index is None
        assert d.versions.next_draft_id == d.id

    assert_state(draft)
    assert_state(Draft.get_record(draft.id))
def test_record_create_parent_state(app, db, location):
    """Test draft creation of the parent record."""
    draft = Draft.create({})
    draft.commit()
    db.session.commit()
    record = Record.publish(draft)
    record.commit()
    db.session.commit()

    def assert_state(r):
        # An initial draft is not published, so latest_id/index is None
        assert r.versions.latest_id == r.id
        assert r.versions.latest_index == 1
        assert r.versions.next_draft_id is None
        assert r.versions.index == 1
        assert r.versions.is_latest is True
        assert r.versions.is_latest_draft is True
        assert r.model.index == 1
        assert r.model.parent_id == draft.model.parent_id

    assert_state(record)
    assert_state(Record.get_record(record.id))
def example_record(db, input_data):
    """Example record."""
    record = Draft.create(input_data, expires_at=datetime(2020, 9, 7, 0, 0))
    db.session.commit()
    return record
Esempio n. 12
0
def test_record_via_field(app, db):
    """Record creation via field."""
    record = Draft.create({}, metadata={'title': 'test'})
    assert record.metadata == {'title': 'test'}
Esempio n. 13
0
def example_record(app, db):
    """Example record."""
    record = Draft.create({}, metadata={'title': 'Test'})
    db.session.commit()
    return record
Esempio n. 14
0
def example_draft(db, input_data):
    """Example draft."""
    draft = Draft.create(input_data, expires_at=datetime(2020, 9, 7, 0, 0))
    draft.commit()
    db.session.commit()
    return draft