• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CenterForOpenScience / SHARE / 4874879116

03 May 2023 05:45PM UTC coverage: 91.948% (+7.6%) from 84.377%
4874879116

Pull #799

github

GitHub
Merge d68b96e9c into 84c969956
Pull Request #799: Release/23.0.0

2703 of 2703 new or added lines in 59 files covered. (100.0%)

23488 of 25545 relevant lines covered (91.95%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.06
/tests/validation/test_changeset.py
1
import re
1✔
2
import pytest
1✔
3

4
from django.core.exceptions import ValidationError
1✔
5

6
from share.models.validators import JSONLDValidator
1✔
7

8

9
class TestJSONLDValidator:
1✔
10
    CASES = [{
1✔
11
        'out': "'@graph' is a required property at /",
1✔
12
        'in': {},
1✔
13
    }, {
1✔
14
        'out': "Additional properties are not allowed ('foo' was unexpected) at /",
1✔
15
        'in': {'foo': 'bar', '@graph': []}
1✔
16
    }, {
1✔
17
        'out': "{} is not of type 'array' at /@graph",
1✔
18
        'in': {
1✔
19
            '@graph': {}
1✔
20
        }
21
    }, {
1✔
22
        'out': "1 is not of type 'array' at /@graph",
1✔
23
        'in': {
1✔
24
            '@graph': 1
1✔
25
        }
26
    }, {
1✔
27
        'out': "1.0 is not of type 'array' at /@graph",
1✔
28
        'in': {
1✔
29
            '@graph': 1.0
1✔
30
        }
31
    }, {
1✔
32
        'out': "None is not of type 'array' at /@graph",
1✔
33
        'in': {
1✔
34
            '@graph': None
1✔
35
        }
36
    }, {
1✔
37
        'out': "'foo' is not of type 'array' at /@graph",
1✔
38
        'in': {
1✔
39
            '@graph': 'foo'
1✔
40
        }
41
    }, {
1✔
42
        'out': "@graph may not be empty",
1✔
43
        'in': {
1✔
44
            '@graph': []
1✔
45
        }
46
    }, {
1✔
47
        'out': "'@id' is a required property at /@graph/0",
1✔
48
        'in': {
1✔
49
            '@graph': [{'@type': ''}]
1✔
50
        }
51
    }, {
1✔
52
        'out': "1 is not of type 'object' at /@graph/0",
1✔
53
        'in': {
1✔
54
            '@graph': [1]
1✔
55
        }
56
    }, {
1✔
57
        'out': "None is not of type 'object' at /@graph/1",
1✔
58
        'in': {
1✔
59
            '@graph': [{'@id': '', '@type': ''}, None]
1✔
60
        }
61
    }, {
1✔
62
        'out': "'@type' is a required property at /@graph/0",
1✔
63
        'in': {
1✔
64
            '@graph': [{'@id': ''}]
1✔
65
        }
66
    }, {
1✔
67
        'out': "'Dinosaurs' is not a valid type",
1✔
68
        'in': {
1✔
69
            '@graph': [{'@id': '', '@type': 'Dinosaurs'}]
1✔
70
        }
71
    }, {
1✔
72
        'out': re.compile(r"'Tag' is not one of \[('\w+', )+'\w+'\] at /@graph/0"),
1✔
73
        'in': {
1✔
74
            '@graph': [{
1✔
75
                '@id': '_:123',
1✔
76
                '@type': 'throughtags',
1✔
77
                'tag': {'@id': '_:789', '@type': 'Tag'},
1✔
78
                'creative_work': {'@id': '_:456', '@type': 'Tag'},
1✔
79
            }]
80
        }
81
    }, {
1✔
82
        'out': 'Unresolved references [{"@id": "_:456", "@type": "preprint"}, {"@id": "_:789", "@type": "tag"}]',
1✔
83
        'in': {
1✔
84
            '@graph': [{
1✔
85
                '@id': '_:123',
1✔
86
                '@type': 'throughtags',
1✔
87
                'tag': {'@id': '_:789', '@type': 'Tag'},
1✔
88
                'creative_work': {'@id': '_:456', '@type': 'Preprint'},
1✔
89
            }]
90
        }
91
    }, {
1✔
92
        'out': "'creative_work' is a required property at /@graph/0",
1✔
93
        'in': {
1✔
94
            '@graph': [{
1✔
95
                '@id': '_:123',
1✔
96
                '@type': 'throughtags',
1✔
97
                'tag': {'@id': '_:789', '@type': 'Tag'},
1✔
98
            }]
99
        }
100
    }, {
1✔
101
        'out': "Additional properties are not allowed ('shouldnt' was unexpected) at /@graph/0",
1✔
102
        'in': {
1✔
103
            '@graph': [{
1✔
104
                '@id': '_:123',
1✔
105
                '@type': 'throughtags',
1✔
106
                'shouldnt': 'behere',
1✔
107
                'tag': {'@id': 'id', '@type': 'tag'},
1✔
108
                'creative_work': {'@id': 'id', '@type': 'creativework'},
1✔
109
            }]
110
        }
111
    }, {
1✔
112
        'out': re.compile(r"^Additional properties are not allowed \('(shouldnt|pls)', '(shouldnt|pls)' were unexpected\) at /@graph/0$"),
1✔
113
        'in': {
1✔
114
            '@graph': [{
1✔
115
                '@id': '_:123',
1✔
116
                '@type': 'throughtags',
1✔
117
                'pls': 'toleeb',
1✔
118
                'shouldnt': 'behere',
1✔
119
                'tag': {'@id': 'id', '@type': 'tag'},
1✔
120
                'creative_work': {'@id': 'id', '@type': 'creativework'},
1✔
121
            }]
122
        }
123
    }, {
1✔
124
        'out': re.compile("{.+} is not valid under any of the given schemas at /@graph/0/tag$"),
1✔
125
        'in': {
1✔
126
            '@graph': [{
1✔
127
                '@id': '_:123',
1✔
128
                '@type': 'throughtags',
1✔
129
                'creative_work': {'@id': '_:123', '@type': 'foo'},
1✔
130
                'tag': {'@id': '_:123', '@type': 'foo', 'il': 'legal'}
1✔
131
            }]
132
        }
133
    }, {
1✔
134
        'out': "'extra should be a dict' is not of type 'object' at /@graph/0/extra",
1✔
135
        'in': {
1✔
136
            '@graph': [{
1✔
137
                '@id': '_:123',
1✔
138
                '@type': 'Tag',
1✔
139
                'name': 'A Tag',
1✔
140
                'extra': 'extra should be a dict'
1✔
141
            }]
142
        }
143
    }, {
1✔
144
        'out': None,
1✔
145
        'in': {
1✔
146
            '@graph': [{
1✔
147
                '@id': '_:123',
1✔
148
                '@type': 'Tag',
1✔
149
                'name': 'A Tag',
1✔
150
                'extra': {
1✔
151
                    'with some': 'extra data'
1✔
152
                }
153
            }]
154
        }
155
    }, {
1✔
156
        'out': "1 is not of type 'string' at /@graph/0/name",
1✔
157
        'in': {
1✔
158
            '@graph': [{
1✔
159
                '@id': '_:123',
1✔
160
                '@type': 'Tag',
1✔
161
                'name': 1
1✔
162
            }]
163
        }
164
    }, {
1✔
165
        'out': None,
1✔
166
        'in': {
1✔
167
            '@graph': [{
1✔
168
                '@id': '_:123',
1✔
169
                '@type': 'CreativeWork',
1✔
170
                'title': 'Some title',
1✔
171
                'description': 'description',
1✔
172
                'tags': [{
1✔
173
                    '@id': '_:456',
1✔
174
                    '@type': 'throughtags'
1✔
175
                }]
176
            }, {
1✔
177
                '@id': '_:456',
1✔
178
                '@type': 'throughtags',
1✔
179
                'tag': {'@id': '_:789', '@type': 'tag'},
1✔
180
                'creative_work': {'@id': '_:123', '@type': 'creativework'},
1✔
181
            }, {
1✔
182
                '@id': '_:789',
1✔
183
                '@type': 'tag',
1✔
184
                'name': 'New Tag',
1✔
185
            }]
186
        }
187
    }, {
1✔
188
        'out': "'throughtugs' is not one of ['THROUGHTAGS', 'ThroughTags', 'throughtags'] at /@graph/0/tags/0/@type",
1✔
189
        'in': {
1✔
190
            '@graph': [{
1✔
191
                '@id': '_:123',
1✔
192
                '@type': 'CreativeWork',
1✔
193
                'title': 'Some title',
1✔
194
                'description': 'description',
1✔
195
                'tags': [{
1✔
196
                    '@id': '_:456',
1✔
197
                    '@type': 'throughtugs'
1✔
198
                }]
199
            }, {
1✔
200
                '@id': '_:456',
1✔
201
                '@type': 'throughtags',
1✔
202
                'tag': {'@id': '_:789', '@type': 'tag'},
1✔
203
                'creative_work': {'@id': '_:123', '@type': 'creativework'},
1✔
204
            }, {
1✔
205
                '@id': '_:789',
1✔
206
                '@type': 'tag',
1✔
207
                'name': 'New Tag',
1✔
208
            }]
209
        }
210
    }, {
1✔
211
        'out': "'giraffe' is not a 'uri' at /@graph/0/uri",
1✔
212
        'in': {
1✔
213
            '@graph': [{
1✔
214
                '@id': '_:123',
1✔
215
                '@type': 'WorkIdentifier',
1✔
216
                'uri': 'giraffe',
1✔
217
                'creative_work': {'@id': '_:234', '@type': 'creativework'}
1✔
218
            }, {
1✔
219
                '@id': '_:234',
1✔
220
                '@type': 'creativework',
1✔
221
                'title': 'Giraffes are tall'
1✔
222
            }]
223
        }
224
    }, {
1✔
225
        'out': "'creative_work' is a required property at /@graph/0",
1✔
226
        'in': {
1✔
227
            '@graph': [{
1✔
228
                '@id': '_:123',
1✔
229
                '@type': 'workidentifier',
1✔
230
                'uri': 'https://share.osf.io/foo',
1✔
231
            }]
232
        }
233
    }, {
1✔
234
        'out': None,
1✔
235
        'in': {
1✔
236
            '@graph': [{
1✔
237
                '@id': '_:123',
1✔
238
                '@type': 'WorkIdentifier',
1✔
239
                'uri': 'https://share.osf.io/foo',
1✔
240
                'creative_work': {'@id': '_:234', '@type': 'creativework'}
1✔
241
            }, {
1✔
242
                '@id': '_:234',
1✔
243
                '@type': 'creativework',
1✔
244
                'title': 'Giraffes are tall'
1✔
245
            }]
246
        }
247
    }]
248

249
    @pytest.mark.parametrize('data, message', [(case['in'], case['out']) for case in CASES])
1✔
250
    def test_validator(self, data, message):
1✔
251
        try:
1✔
252
            JSONLDValidator()(data)
1✔
253
        except ValidationError as e:
1✔
254
            assert message is not None, 'Raised "{}"'.format(e.args[0])
1✔
255
            if isinstance(message, str):
1✔
256
                assert message == e.args[0]
1✔
257
            else:
×
258
                assert message.match(e.args[0]) is not None
1✔
259
        else:
×
260
            assert message is None, 'No exception was raised. Expecting {}'.format(message)
1✔
261

262
    # @pytest.mark.parametrize('data, message', [(case['in'], case['out']) for case in CASES])
263
    # def test_benchmark_validator(self, benchmark, data, message):
264
    #     benchmark(self.test_validator, data, message)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc