forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_validators.py
52 lines (43 loc) · 1.72 KB
/
test_validators.py
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
import pytest
from osf.exceptions import ValidationValueError
from osf.models import validators
from osf_tests.factories import SubjectFactory
# Ported from tests/framework/test_mongo.py
def test_string_required_passes_with_string():
assert validators.string_required('Hi!') is True
def test_string_required_fails_when_empty():
with pytest.raises(ValidationValueError):
validators.string_required(None)
with pytest.raises(ValidationValueError):
validators.string_required('')
@pytest.mark.django_db
def test_validate_expand_subject_hierarchy():
fruit = SubjectFactory()
apple = SubjectFactory(parent=fruit)
grapes = SubjectFactory(parent=fruit)
raisins = SubjectFactory(parent=grapes)
# test send in two flattened hierarchies that share a base
subject_list = [fruit._id, apple._id, grapes._id]
expanded = validators.expand_subject_hierarchy(subject_list)
assert len(expanded) == 3
assert fruit in expanded
assert apple in expanded
assert grapes in expanded
# test send in third level of a 3-tier hierarchy
subject_list = [raisins._id]
expanded = validators.expand_subject_hierarchy(subject_list)
assert len(expanded) == 3
assert raisins in expanded
assert grapes in expanded
assert fruit in expanded
# test send in first and third levels
subject_list = [raisins._id, fruit._id]
expanded = validators.expand_subject_hierarchy(subject_list)
assert len(expanded) == 3
assert raisins in expanded
assert grapes in expanded
assert fruit in expanded
# test invalid hierarchy
subject_list = [fruit._id, '12345_bad_id']
with pytest.raises(ValidationValueError):
validators.expand_subject_hierarchy(subject_list)