aboutsummaryrefslogtreecommitdiffstats
path: root/python/tests/entity_lifecycle.py
blob: 4ac7ee682ecc7f6d86a709271593be3464fb9efa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

import json
import unittest
import tempfile
import pytest
import fatcat
import fatcat.sql
from fatcat.models import *
from fixtures import *


def test_merge_works(app):

    # two works, each with releases
    rv = app.post('/v0/work',
        data=json.dumps(dict()),
        headers={"content-type": "application/json"})
    workA_id = json.loads(rv.data.decode('utf-8'))['id']

    rv = app.post('/v0/work',
        data=json.dumps(dict()),
        headers={"content-type": "application/json"})
    workB_id = json.loads(rv.data.decode('utf-8'))['id']

    rv = app.post('/v0/release',
        data=json.dumps(dict(
            title="some release",
            work_type="journal-article",
            work=workA_id,
            doi="10.1234/A1")),
        headers={"content-type": "application/json"})
    releaseA1 = json.loads(rv.data.decode('utf-8'))['id']

    rv = app.post('/v0/release',
        data=json.dumps(dict(
            title="some release",
            work_type="journal-article",
            work=workB_id,
            doi="10.1234/B1")),
        headers={"content-type": "application/json"})
    releaseB1 = json.loads(rv.data.decode('utf-8'))['id']

    rv = app.post('/v0/release',
        data=json.dumps(dict(
            title="some release",
            work_type="journal-article",
            work=workB_id,
            doi="10.1234/A1")),
        headers={"content-type": "application/json"})
    releaseB2 = json.loads(rv.data.decode('utf-8'))['id']

    # XXX: what if workB primary was set?

    editgroup_id = 1
    rv = app.post('/v0/editgroup/{}/accept'.format(editgroup_id),
        headers={"content-type": "application/json"})
    assert rv.status_code == 200
    assert ChangelogEntry.query.count() == 1
    assert WorkIdent.query.filter(WorkIdent.is_live==True).count() == 2
    assert ReleaseIdent.query.filter(ReleaseIdent.is_live==True).count() == 3

    # merge works
    fatcat.sql.merge_works(workA_id, workB_id)
    editgroup_id = 2
    rv = app.post('/v0/editgroup/{}/accept'.format(editgroup_id),
        headers={"content-type": "application/json"})
    assert rv.status_code == 200

    # check results
    assert ChangelogEntry.query.count() == 2
    assert WorkIdent.query.filter(WorkIdent.is_live==True).count() == 2
    assert ReleaseIdent.query.filter(ReleaseIdent.is_live==True).count() == 3

    workA_json = json.loads(app.get('/v0/work/{}'.format(workA_id)).data.decode('utf-8'))
    workB_json = json.loads(app.get('/v0/work/{}'.format(workB_id)).data.decode('utf-8'))
    assert workA_json['rev'] == workB_json['rev']
    print(workA_json)
    print(workB_json)
    assert workA_json['redirect_id'] == None
    assert workB_json['redirect_id'] == workA_json['id']