-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathep3apid_test.py
More file actions
executable file
·115 lines (104 loc) · 4.71 KB
/
ep3apid_test.py
File metadata and controls
executable file
·115 lines (104 loc) · 4.71 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
105
106
107
108
109
110
111
#!/usr/bin/env python3
'''
ep3api_test.py tests the ep3api.py python library used in acacia object.
'''
import sys
from cltests import TestSet, T, IsSuccessful
from ep3apid import Ep3API, User
api = Ep3API('http://localhost:8484', 'lemurprints')
def test_eprint_lists():
t = T()
keys, err = api.keys()
t.Expected(None, err, f"Did not expect an error for keys, {err}")
t.Expected(True, keys != None, "Should have a list of keys")
for key in keys:
obj, err = api.eprint(key)
if err:
t.Expected(False, True, f"api.eprint({key}), did not expect err {err}")
if obj == None:
t.Expected(True, False, f"api.eprint({key}) should have returned an object")
return t.Results()
def test_user():
t = T()
usernames, err = api.usernames()
t.Expected(None, err, f"Did not expect an error for usernames, {err}")
t.Expected(True, usernames != None, "Should have some usernames")
t.Expected(True, isinstance(usernames, list), "Expected a list of usernames")
for i, name in enumerate(usernames):
m, err = api.user(name)
if err:
t.Expected(True, False, f'Did not expect error ({i}, {name}) {err}')
else:
u = User(m)
print(f'DEBUG u: {u.to_string()}')
t.Expected(True, u.uname != '', f'Expected u.uname to be populated ({i}, {name})')
t.Expected(True, u.userid != 0, f'Expected non-zero u.userid ({i}, {name})')
t.Expected(True, u.name != None, f'Expected u.name to be populated ({i}, {name})')
t.Expected(True, u.display_name != '', f'Expected u.display_name to be populated ({i}, {name})')
#t.Expected(True, u.role != '', f'Expected u.role, got empty string ({i}, {name})')
#required = []
#t.Expected(True, u.has_role(required), f'Expected u.has_role({required}) to return True')
return t.Results()
def test_caltechauthors():
t = T()
old_repo_id = api.repo_id
repo_id = 'caltechauthors'
#print(f'Switching repositories from {old_repo_id} to {repo_id}')
t.Expected(True, api.use(repo_id), f'Expected API repo change to {repo_id}')
usernames, err = api.usernames()
for i, name in enumerate(usernames):
try:
m, err = api.user(name)
except Exception as e:
print(f'{i}, "{name}", {err}, expection:', e)
u = User(m)
required = ['admin', 'editor', 'minuser', 'user' ]
if u.role:
t.Expected(True, u.role != '', f'Expected u.role, got empty string ({i}, {name}, {repo_id})')
t.Expected(True, u.has_role(required), f'Expected u.has_role({required}) to return True, u.role "{u.role}" ({i}, {name}, {repo_id})')
if err:
t.Expected(False, True, f'Expected no error in api.user({name}), {err} ({i}, {name}, {repo_id}')
t.Expected(True, api.use(old_repo_id), f'Expected API repo changed back to {old_repo_id}')
return t.Results()
def test_demo_setup():
old_repo_id = api.repo_id
repo_id = 'caltechauthors'
t = T()
t.Expected(True, api.use(repo_id), f'Expected to switch to {repo_id}')
usernames, err = api.usernames()
t.Expected(None, err, f"Did not expect an error for usernames, {err}")
if isinstance(usernames, list):
t.Expected(True, len(usernames) > 0, 'Expected non-zero usernames')
userids, err = api.lookup_userid('rsdoiel')
t.Expected(True, isinstance(userids, list), 'Expected a user id list')
if isinstance(userids, list):
t.Expected(userids[0], 5487, f'Expected user id 5487, got {userids[0]}')
dois, err = api.doi()
t.Expected(None, err, f"Did not expect an error for doi, {err}")
if isinstance(dois, list):
#for doi in dois:
# print(f'DEBUG doi {doi}')
t.Expected(True, len(dois) > 0, 'Expected non-zero dois')
ids, err = api.doi(dois[0])
t.Expected(None, err, "Did not expect an error for ids")
t.Expected(True, len(ids) == 1, f'Expected a single id for DOI, {len(ids)}')
eprint, err = api.eprint(ids[0])
t.Expected(True, not err, "Did not expect an eprint record, {err}")
t.Expected(True, isinstance(eprint, dict), "Expected a dict for eprint")
t.Expected(True, api.use(old_repo_id), f'Expected to switch back to {old_repo_id}')
return t.Results()
if __name__ == '__main__':
ts = TestSet("ep3api_test")
ts.add(test_eprint_lists)
ts.add(test_user)
repositories, err = api.repositories()
repo_id = 'caltechauthors'
if repo_id in repositories:
ts.add(test_demo_setup)
else:
print(f'Skipping test_demo_setup')
if repo_id in repositories:
ts.add(test_caltechauthors)
else:
print(f'Skipping test_{repo_id}')
IsSuccessful(ts.run())