aboutsummaryrefslogtreecommitdiffstats
path: root/python/tests/merge_files.py
blob: c33c6f6c85cb804ff7bcd3af964a2b599d784360 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from fatcat_openapi_client import FileEntity, FileUrl
from fixtures import api

from fatcat_tools.mergers.files import FileMerger


def test_choose_primary_file(api) -> None:

    fm = FileMerger(api=api)
    fe_partial = FileEntity(
        ident="aaaasb5apzfhbbxxc7rgu2yw6m",
        sha1="b1beebb5f979121cd234c69b08e3f42af3aaaaaa",
    )
    fe_norelease = FileEntity(
        ident="bbbbsb5apzfhbbxxc7rgu2yw6m",
        sha1="b1beebb5f979121cd234c69b08e3f42af3bbbbbb",
        md5="d2c7318315bfc7d3aab0db933e95e632",
        sha256="528064c7664a96c79c80c423210f6f9f4fafe949dd59dfd1572a04b906d5e163",
        size=60719,
        mimetype="application/pdf",
    )
    fe_nourls = FileEntity(
        ident="ccccsb5apzfhbbxxc7rgu2yw6m",
        sha1="b1beebb5f979121cd234c69b08e3f42af3bbbbbb",
        md5="d2c7318315bfc7d3aab0db933e95e632",
        sha256="528064c7664a96c79c80c423210f6f9f4fafe949dd59dfd1572a04b906d5e163",
        size=60719,
        mimetype="application/pdf",
        release_ids=["dlrxjg7mxrayxfltget7fqcrjy"],
    )
    fe_complete = FileEntity(
        ident="ddddsb5apzfhbbxxc7rgu2yw6m",
        sha1="b1beebb5f979121cd234c69b08e3f42af3bbbbbb",
        md5="d2c7318315bfc7d3aab0db933e95e632",
        sha256="528064c7664a96c79c80c423210f6f9f4fafe949dd59dfd1572a04b906d5e163",
        size=60719,
        mimetype="application/pdf",
        release_ids=["dlrxjg7mxrayxfltget7fqcrjy"],
        urls=[
            FileUrl(rel="web", url="http://aughty.org/pdf/future_open.pdf"),
        ],
        extra=dict(asdf=123),
    )
    fe_pseudo_complete = FileEntity(
        ident="eeeesb5apzfhbbxxc7rgu2yw6m",
        sha1="b1beebb5f979121cd234c69b08e3f42af3bbbbbb",
        sha256="528064c7664a96c79c80c423210f6f9f4fafe949dd59dfd1572a04b906d5e163",
        size=60719,
        mimetype="application/pdf",
        release_ids=["dlrxjg7mxrayxfltget7fqcrjy"],
        urls=[
            FileUrl(rel="web", url="http://aughty.org/pdf/future_open.pdf"),
        ],
        extra=dict(asdf=123),
    )

    assert fm.choose_primary_file([fe_partial, fe_norelease]) == "bbbbsb5apzfhbbxxc7rgu2yw6m"
    assert (
        fm.choose_primary_file([fe_partial, fe_nourls, fe_norelease])
        == "ccccsb5apzfhbbxxc7rgu2yw6m"
    )
    assert (
        fm.choose_primary_file([fe_partial, fe_complete, fe_nourls, fe_norelease])
        == "ddddsb5apzfhbbxxc7rgu2yw6m"
    )
    assert (
        fm.choose_primary_file([fe_partial, fe_pseudo_complete, fe_nourls, fe_norelease])
        == "ccccsb5apzfhbbxxc7rgu2yw6m"
    )


def test_merge_file_metadata_from(api) -> None:
    fm = FileMerger(api=api)
    fe_partial = FileEntity(
        ident="aaaasb5apzfhbbxxc7rgu2yw6m",
        sha1="b1beebb5f979121cd234c69b08e3f42af3aaaaaa",
    )
    fe_norelease = FileEntity(
        ident="bbbbsb5apzfhbbxxc7rgu2yw6m",
        sha1="b1beebb5f979121cd234c69b08e3f42af3bbbbbb",
        md5="d2c7318315bfc7d3aab0db933e95e632",
        sha256="528064c7664a96c79c80c423210f6f9f4fafe949dd59dfd1572a04b906d5e163",
        size=60719,
        mimetype="application/pdf",
    )
    fe_nourls = FileEntity(
        ident="ccccsb5apzfhbbxxc7rgu2yw6m",
        sha1="b1beebb5f979121cd234c69b08e3f42af3bbbbbb",
        md5="d2c7318315bfc7d3aab0db933e95e632",
        sha256="528064c7664a96c79c80c423210f6f9f4fafe949dd59dfd1572a04b906d5e163",
        size=60719,
        mimetype="application/pdf",
        release_ids=["dlrxjg7mxrayxfltget7fqcrjy"],
    )
    fe_complete = FileEntity(
        ident="ddddsb5apzfhbbxxc7rgu2yw6m",
        sha1="b1beebb5f979121cd234c69b08e3f42af3bbbbbb",
        md5="ddddddd315bfc7d3aab0db933e95e632",
        sha256="528064c7664a96c79c80c423210f6f9f4fafe949dd59dfd1572a04b906d5e163",
        size=60719,
        mimetype="application/pdf",
        release_ids=["dlrxjg7mxrayxfltget7fqcrjy"],
        urls=[
            FileUrl(rel="web", url="http://aughty.org/pdf/future_open.pdf"),
        ],
        extra=dict(asdf=123),
    )
    fe_pseudo_complete = FileEntity(
        ident="eeeesb5apzfhbbxxc7rgu2yw6m",
        sha1="b1beebb5f979121cd234c69b08e3f42af3bbbbbb",
        sha256="528064c7664a96c79c80c423210f6f9f4fafe949dd59dfd1572a04b906d5e163",
        size=60719,
        mimetype="application/pdf",
        release_ids=["dlrxjg7mxrayxfltget7fqcrjy"],
        urls=[
            FileUrl(rel="web", url="http://aughty.org/pdf/future_open.pdf"),
        ],
        extra=dict(asdf=123),
    )
    fe_another_release_id = FileEntity(
        ident="fffffffapzfhbbxxc7rgu2yw6m",
        release_ids=["qqqqqg7mxrayxfltget7fqcrjy"],
    )
    fe_another_url = FileEntity(
        ident="zzzzzzzapzfhbbxxc7rgu2yw6m",
        urls=[
            FileUrl(rel="repository", url="http://someuni.edu/repo/file.pdf"),
        ],
    )
    fe_more_extra = FileEntity(
        ident="fffffffapzfhbbxxc7rgu2yw6m",
        release_ids=["qqqqqg7mxrayxfltget7fqcrjy"],
        extra=dict(thang=456),
    )

    assert fm.merge_file_metadata_from(fe_nourls, fe_partial) is False
    assert fm.merge_file_metadata_from(fe_complete, fe_pseudo_complete) is False
    assert fm.merge_file_metadata_from(fe_complete, fe_complete) is False
    assert fm.merge_file_metadata_from(fe_partial, fe_norelease) is True
    assert fe_partial.md5 == fe_norelease.md5
    assert fe_partial.size == fe_norelease.size
    assert fm.merge_file_metadata_from(fe_partial, fe_complete) is True
    assert fe_partial.md5 != fe_complete.md5
    assert fe_partial.extra == fe_complete.extra
    assert set([(u.rel, u.url) for u in fe_partial.urls or []]) == set(
        [(u.rel, u.url) for u in fe_complete.urls or []]
    )
    assert fe_partial.release_ids == fe_complete.release_ids
    assert fm.merge_file_metadata_from(fe_partial, fe_another_release_id) is True
    assert fe_partial.release_ids == [
        "dlrxjg7mxrayxfltget7fqcrjy",
        "qqqqqg7mxrayxfltget7fqcrjy",
    ]
    assert fm.merge_file_metadata_from(fe_partial, fe_another_release_id) is False
    assert fm.merge_file_metadata_from(fe_partial, fe_more_extra) is True
    assert fe_partial.extra == dict(asdf=123, thang=456)
    assert fm.merge_file_metadata_from(fe_partial, fe_more_extra) is False
    assert fm.merge_file_metadata_from(fe_partial, fe_another_url) is True
    assert fe_partial.urls[-1].url == "http://someuni.edu/repo/file.pdf"
    assert fm.merge_file_metadata_from(fe_partial, fe_another_url) is False