Repository navigation
Expand file tree
/
Copy pathtest.py
More file actions
104 lines (83 loc) · 3.74 KB
/
Copy pathtest.py
File metadata and controls
104 lines (83 loc) · 3.74 KB
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
import unittest
from elinkapi.organization import Organization
from elinkapi.record import Record, ProductType
from elinkapi.person import Person
from elinkapi.contribution import Contribution
from datetime import date
class TestOrganizations(unittest.TestCase):
"""
Ensure pydantic functions work properly for checking ROR IDs, other requirements, etc.
"""
def test_ror_ids(self):
example_rors = ["https://ror.org/024mw5h28", "015w2mp89", "http://ror.org/05h992307"]
bad_rors = [ "nope", "0bAdone!", "https://rororg/024mw5h28" ]
# ensure good ones work
for ror in example_rors:
o = Organization(type='SPONSOR', ror_id=ror)
self.assertEqual(o.ror_id, ror, "ROR values did not match up.")
# ensure bad ones fail
for ror in bad_rors:
with self.assertRaises(ValueError, msg=f"Value {ror} should fail"):
o = Organization(type="SPONSOR", ror_id=ror)
class TestPerson(unittest.TestCase):
"""
Test various conditions are validated for Persons.
"""
def test_person_validation(self):
# try a few good values
types = [t.value for t in Person.Type]
bad_types = ["NOPE", "WRONG", "not-this-either"]
for type in types:
# make sure construction works
p = Person(type=type, last_name = "Example")
for type in bad_types:
# ensure these don't make it
with self.assertRaises(ValueError, msg=f"{type} is not a valid type"):
p = Person(type = type, last_name = "Wrong")
# ensure only contributors may have contributing types
contribution_types = [t.value for t in Contribution]
# ensure all the contributor types work properly with CONTRIBUTING
p = Person(type=Person.Type.CONTRIBUTING, last_name ="Mann")
for type in contribution_types:
p.contributor_type = type
class TestRecord(unittest.TestCase):
"""
Test basic field requirements exist.
"""
def test_record(self):
# records REQUIRE: product type and title at least
with self.assertRaises(ValueError, msg="Missing required fields."):
r = Record()
with self.assertRaises(ValueError, msg="Missing required fields."):
r = Record(title="Not enough")
with self.assertRaises(ValueError, msg="Missing required fields."):
r = Record(product_type = "TR")
# make sure at least these work
r = Record(title="Good record", product_type=ProductType.Conference)
# test all these work
for type in [t.value for t in ProductType]:
r = Record(title="Good one", product_type = type)
bad_types = ["X", "NO", "BOGUS"]
for type in bad_types:
with self.assertRaises(ValueError, msg=f"Type {type} is not a valid value."):
r = Record(title="Title is ok", product_type=type)
"""
Test some record functionality ("publication_date")
"""
def test_date_setting(self):
# construct a basic Record
r = Record(title="Example", product_type = "TR")
# try setting a str
r.publication_date = '2011-08-12'
self.assertEqual(r.publication_date, date(2011, 8, 12), "Date didn't set properly")
# try a date
r.publication_date = date(1999, 4, 14)
self.assertEqual(r.publication_date, date(1999, 4, 14), "date type not setting.")
# try some failures
bad_dates = ['9', '199', 'monday, Feb 7, 1920', "Spring"]
for value in bad_dates:
with self.assertRaises(ValueError, msg=f"Value {value} should fail"):
r.publication_date = value
# Run all the unit tests above
if __name__ == '__main__':
unittest.main()