import uuid

from invenio_indexer.api import RecordIndexer
from invenio_pidstore.models import PersistentIdentifier, PIDStatus
from invenio_records_draft.api import RecordContext
from invenio_records_draft.proxies import current_drafts
from invenio_search import RecordsSearch, current_search, current_search_client
from sample.records.config import DraftRecord, PublishedRecord
from tests.helpers import disable_test_authenticated


def test_publish(app, db, schemas, mappings, prepare_es):
    with disable_test_authenticated():
        with db.session.begin_nested():
            draft_uuid = uuid.uuid4()

            rec1 = DraftRecord.create({
                'id': '1',
                'title': 'rec1'
            }, id_=draft_uuid)
            draft1_pid = PersistentIdentifier.create(
                pid_type='drecid', pid_value='1', status=PIDStatus.REGISTERED,
                object_type='rec', object_uuid=draft_uuid
            )

            published_uuid = uuid.uuid4()
            published = PublishedRecord.create({
                'id': '3',
                'title': 'rec1a'
            }, id_=published_uuid)
            published_pid = PersistentIdentifier.create(
                pid_type='recid', pid_value='3', status=PIDStatus.REGISTERED,
                object_type='rec', object_uuid=published_uuid
            )

            draft2_uuid = uuid.uuid4()
            rec2 = DraftRecord.create({
                'id': '2',
                'title': 'rec2',
                'ref': {'$ref': 'http://localhost/drafts/records/1'},
                'ref_pub': {'$ref': 'http://localhost/records/3'}
            }, id_=draft2_uuid)
            draft2_pid = PersistentIdentifier.create(
                pid_type='drecid', pid_value='2', status=PIDStatus.REGISTERED,
                object_type='rec', object_uuid=draft2_uuid
            )
            RecordIndexer().index(rec2)

        current_search_client.indices.refresh()
        current_search_client.indices.flush()

        es_draft2 = RecordsSearch(index='draft-records-record-v1.0.0').\
            get_record(draft2_pid.object_uuid).execute()
        assert len(es_draft2.hits) == 1

        current_drafts.publish(RecordContext(record=rec2, record_pid=draft2_pid))

        published2_pid = PersistentIdentifier.get(pid_type='recid', pid_value=draft2_pid.pid_value)
        pr = PublishedRecord.get_record(published2_pid.object_uuid)
        assert pr.dumps() == {
            '$schema': 'https://localhost/schemas/records/record-v1.0.0.json',
            'id': '2',
            'ref': {'$ref': 'http://localhost/records/1'},
            'ref_pub': {'$ref': 'http://localhost/records/3'},
            'title': 'rec2'
        }

        current_search_client.indices.refresh()
        current_search_client.indices.flush()

        es_published2 = RecordsSearch(index='records-record-v1.0.0').\
            get_record(published2_pid.object_uuid).execute()
        assert len(es_published2.hits) == 1
        es_published2 = es_published2.hits[0].to_dict()
        es_published2.pop('_created')
        es_published2.pop('_updated')
        assert es_published2 == {
            '$schema': 'https://localhost/schemas/records/record-v1.0.0.json',
            'id': '2',
            'ref': {'published': '1'},
            'ref_pub': {'published': '3'},
            'title': 'rec2'}

        es_draft2 = RecordsSearch(index='draft-records-record-v1.0.0').\
            get_record(draft2_pid.object_uuid).execute()
        assert len(es_draft2.hits) == 0

[((13, 9, 13, 37), 'tests.helpers.disable_test_authenticated', 'disable_test_authenticated', ({}, {}), '()', False, 'from tests.helpers import disable_test_authenticated\n'), ((49, 8, 49, 47), 'invenio_search.current_search_client.indices.refresh', 'current_search_client.indices.refresh', ({}, {}), '()', False, 'from invenio_search import RecordsSearch, current_search, current_search_client\n'), ((50, 8, 50, 45), 'invenio_search.current_search_client.indices.flush', 'current_search_client.indices.flush', ({}, {}), '()', False, 'from invenio_search import RecordsSearch, current_search, current_search_client\n'), ((58, 25, 58, 99), 'invenio_pidstore.models.PersistentIdentifier.get', 'PersistentIdentifier.get', (), '', False, 'from invenio_pidstore.models import PersistentIdentifier, PIDStatus\n'), ((59, 13, 59, 67), 'sample.records.config.PublishedRecord.get_record', 'PublishedRecord.get_record', ({(59, 40, 59, 66): 'published2_pid.object_uuid'}, {}), '(published2_pid.object_uuid)', False, 'from sample.records.config import DraftRecord, PublishedRecord\n'), ((68, 8, 68, 47), 'invenio_search.current_search_client.indices.refresh', 'current_search_client.indices.refresh', ({}, {}), '()', False, 'from invenio_search import RecordsSearch, current_search, current_search_client\n'), ((69, 8, 69, 45), 'invenio_search.current_search_client.indices.flush', 'current_search_client.indices.flush', ({}, {}), '()', False, 'from invenio_search import RecordsSearch, current_search, current_search_client\n'), ((15, 25, 15, 37), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((17, 19, 20, 30), 'sample.records.config.DraftRecord.create', 'DraftRecord.create', (), '', False, 'from sample.records.config import DraftRecord, PublishedRecord\n'), ((21, 25, 24, 13), 'invenio_pidstore.models.PersistentIdentifier.create', 'PersistentIdentifier.create', (), '', False, 'from invenio_pidstore.models import PersistentIdentifier, PIDStatus\n'), ((26, 29, 26, 41), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((27, 24, 30, 34), 'sample.records.config.PublishedRecord.create', 'PublishedRecord.create', (), '', False, 'from sample.records.config import DraftRecord, PublishedRecord\n'), ((31, 28, 34, 13), 'invenio_pidstore.models.PersistentIdentifier.create', 'PersistentIdentifier.create', (), '', False, 'from invenio_pidstore.models import PersistentIdentifier, PIDStatus\n'), ((36, 26, 36, 38), 'uuid.uuid4', 'uuid.uuid4', ({}, {}), '()', False, 'import uuid\n'), ((37, 19, 42, 31), 'sample.records.config.DraftRecord.create', 'DraftRecord.create', (), '', False, 'from sample.records.config import DraftRecord, PublishedRecord\n'), ((43, 25, 46, 13), 'invenio_pidstore.models.PersistentIdentifier.create', 'PersistentIdentifier.create', (), '', False, 'from invenio_pidstore.models import PersistentIdentifier, PIDStatus\n'), ((56, 31, 56, 80), 'invenio_records_draft.api.RecordContext', 'RecordContext', (), '', False, 'from invenio_records_draft.api import RecordContext\n'), ((47, 12, 47, 27), 'invenio_indexer.api.RecordIndexer', 'RecordIndexer', ({}, {}), '()', False, 'from invenio_indexer.api import RecordIndexer\n'), ((52, 20, 52, 70), 'invenio_search.RecordsSearch', 'RecordsSearch', (), '', False, 'from invenio_search import RecordsSearch, current_search, current_search_client\n'), ((71, 24, 71, 68), 'invenio_search.RecordsSearch', 'RecordsSearch', (), '', False, 'from invenio_search import RecordsSearch, current_search, current_search_client\n'), ((84, 20, 84, 70), 'invenio_search.RecordsSearch', 'RecordsSearch', (), '', False, 'from invenio_search import RecordsSearch, current_search, current_search_client\n')]