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

[((501, 529), 'tests.helpers.disable_test_authenticated', 'disable_test_authenticated', ([], {}), '()\n', (527, 529), False, 'from tests.helpers import disable_test_authenticated\n'), ((1906, 1945), 'invenio_search.current_search_client.indices.refresh', 'current_search_client.indices.refresh', ([], {}), '()\n', (1943, 1945), False, 'from invenio_search import RecordsSearch, current_search, current_search_client\n'), ((1954, 1991), 'invenio_search.current_search_client.indices.flush', 'current_search_client.indices.flush', ([], {}), '()\n', (1989, 1991), False, 'from invenio_search import RecordsSearch, current_search, current_search_client\n'), ((2272, 2346), 'invenio_pidstore.models.PersistentIdentifier.get', 'PersistentIdentifier.get', ([], {'pid_type': '"""recid"""', 'pid_value': 'draft2_pid.pid_value'}), "(pid_type='recid', pid_value=draft2_pid.pid_value)\n", (2296, 2346), False, 'from invenio_pidstore.models import PersistentIdentifier, PIDStatus\n'), ((2360, 2414), 'sample.records.config.PublishedRecord.get_record', 'PublishedRecord.get_record', (['published2_pid.object_uuid'], {}), '(published2_pid.object_uuid)\n', (2386, 2414), False, 'from sample.records.config import DraftRecord, PublishedRecord\n'), ((2717, 2756), 'invenio_search.current_search_client.indices.refresh', 'current_search_client.indices.refresh', ([], {}), '()\n', (2754, 2756), False, 'from invenio_search import RecordsSearch, current_search, current_search_client\n'), ((2765, 2802), 'invenio_search.current_search_client.indices.flush', 'current_search_client.indices.flush', ([], {}), '()\n', (2800, 2802), False, 'from invenio_search import RecordsSearch, current_search, current_search_client\n'), ((596, 608), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (606, 608), False, 'import uuid\n'), ((629, 693), 'sample.records.config.DraftRecord.create', 'DraftRecord.create', (["{'id': '1', 'title': 'rec1'}"], {'id_': 'draft_uuid'}), "({'id': '1', 'title': 'rec1'}, id_=draft_uuid)\n", (647, 693), False, 'from sample.records.config import DraftRecord, PublishedRecord\n'), ((765, 903), 'invenio_pidstore.models.PersistentIdentifier.create', 'PersistentIdentifier.create', ([], {'pid_type': '"""drecid"""', 'pid_value': '"""1"""', 'status': 'PIDStatus.REGISTERED', 'object_type': '"""rec"""', 'object_uuid': 'draft_uuid'}), "(pid_type='drecid', pid_value='1', status=\n    PIDStatus.REGISTERED, object_type='rec', object_uuid=draft_uuid)\n", (792, 903), False, 'from invenio_pidstore.models import PersistentIdentifier, PIDStatus\n'), ((975, 987), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (985, 987), False, 'import uuid\n'), ((1012, 1085), 'sample.records.config.PublishedRecord.create', 'PublishedRecord.create', (["{'id': '3', 'title': 'rec1a'}"], {'id_': 'published_uuid'}), "({'id': '3', 'title': 'rec1a'}, id_=published_uuid)\n", (1034, 1085), False, 'from sample.records.config import DraftRecord, PublishedRecord\n'), ((1160, 1301), 'invenio_pidstore.models.PersistentIdentifier.create', 'PersistentIdentifier.create', ([], {'pid_type': '"""recid"""', 'pid_value': '"""3"""', 'status': 'PIDStatus.REGISTERED', 'object_type': '"""rec"""', 'object_uuid': 'published_uuid'}), "(pid_type='recid', pid_value='3', status=\n    PIDStatus.REGISTERED, object_type='rec', object_uuid=published_uuid)\n", (1187, 1301), False, 'from invenio_pidstore.models import PersistentIdentifier, PIDStatus\n'), ((1370, 1382), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (1380, 1382), False, 'import uuid\n'), ((1402, 1580), 'sample.records.config.DraftRecord.create', 'DraftRecord.create', (["{'id': '2', 'title': 'rec2', 'ref': {'$ref':\n    'http://localhost/drafts/records/1'}, 'ref_pub': {'$ref':\n    'http://localhost/records/3'}}"], {'id_': 'draft2_uuid'}), "({'id': '2', 'title': 'rec2', 'ref': {'$ref':\n    'http://localhost/drafts/records/1'}, 'ref_pub': {'$ref':\n    'http://localhost/records/3'}}, id_=draft2_uuid)\n", (1420, 1580), False, 'from sample.records.config import DraftRecord, PublishedRecord\n'), ((1676, 1815), 'invenio_pidstore.models.PersistentIdentifier.create', 'PersistentIdentifier.create', ([], {'pid_type': '"""drecid"""', 'pid_value': '"""2"""', 'status': 'PIDStatus.REGISTERED', 'object_type': '"""rec"""', 'object_uuid': 'draft2_uuid'}), "(pid_type='drecid', pid_value='2', status=\n    PIDStatus.REGISTERED, object_type='rec', object_uuid=draft2_uuid)\n", (1703, 1815), False, 'from invenio_pidstore.models import PersistentIdentifier, PIDStatus\n'), ((2195, 2244), 'invenio_records_draft.api.RecordContext', 'RecordContext', ([], {'record': 'rec2', 'record_pid': 'draft2_pid'}), '(record=rec2, record_pid=draft2_pid)\n', (2208, 2244), False, 'from invenio_records_draft.api import RecordContext\n'), ((1869, 1884), 'invenio_indexer.api.RecordIndexer', 'RecordIndexer', ([], {}), '()\n', (1882, 1884), False, 'from invenio_indexer.api import RecordIndexer\n'), ((2013, 2063), 'invenio_search.RecordsSearch', 'RecordsSearch', ([], {'index': '"""draft-records-record-v1.0.0"""'}), "(index='draft-records-record-v1.0.0')\n", (2026, 2063), False, 'from invenio_search import RecordsSearch, current_search, current_search_client\n'), ((2828, 2872), 'invenio_search.RecordsSearch', 'RecordsSearch', ([], {'index': '"""records-record-v1.0.0"""'}), "(index='records-record-v1.0.0')\n", (2841, 2872), False, 'from invenio_search import RecordsSearch, current_search, current_search_client\n'), ((3380, 3430), 'invenio_search.RecordsSearch', 'RecordsSearch', ([], {'index': '"""draft-records-record-v1.0.0"""'}), "(index='draft-records-record-v1.0.0')\n", (3393, 3430), False, 'from invenio_search import RecordsSearch, current_search, current_search_client\n')]