/
test_resource.py
84 lines (69 loc) · 3.83 KB
/
test_resource.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
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
"""end to end tests for resource class"""
import unittest
from knora.dsplib.models.connection import Connection
from knora.dsplib.models.permission import PermissionValue, Permissions
from knora.dsplib.models.resource import ResourceInstanceFactory
from knora.dsplib.models.sipi import Sipi
from knora.dsplib.models.value import KnoraStandoffXml, make_value
class TestResource(unittest.TestCase):
def setUp(self) -> None:
"""
is executed before all tests; sets up a connection and logs in as user root
"""
self.con = Connection('http://0.0.0.0:3333')
self.con.login('root@example.com', 'test')
def test_Resource_create(self) -> None:
# make class factory for project anything. The factory creates classes that implement the CRUD methods
# for the given resource classes
factory = ResourceInstanceFactory(self.con, 'anything')
# get a blue_thing resource class
blue_thing = factory.get_resclass_type('anything:BlueThing')
a_blue_thing = blue_thing(con=self.con,
label='BlueThing',
values={
'anything:hasBoolean': True,
'anything:hasColor': ['#ff0033', '#0077FF'],
'anything:hasDate': make_value(value='1833', comment='not verified!'),
'anything:hasDecimal': 3.14159,
'anything:hasInteger': "42",
'anything:hasTimeStamp': "2004-04-12T13:20:00Z",
'anything:hasInterval': [make_value(value="0.0:3.0", comment="first interval"),
make_value(value="3.5:3.7", comment="second interval")],
'anything:hasRichtext': KnoraStandoffXml("This is <em>bold</em> text."),
'anything:hasText': "Dies ist ein einfacher Text",
'anything:hasUri': 'http://test.com:65500/res'
}).create()
new_blue_thing = a_blue_thing.read()
self.assertEqual(new_blue_thing.label, "BlueThing")
self.assertEqual(new_blue_thing.value("anything:hasColor"), ['#ff0033', '#0077FF'])
self.assertEqual(new_blue_thing.value("anything:hasInteger"), 42)
self.assertEqual(new_blue_thing.value("anything:hasBoolean"), True)
self.assertEqual(new_blue_thing.value("anything:hasDecimal"), 3.14159)
self.assertEqual(new_blue_thing.value("anything:hasText"), "Dies ist ein einfacher Text")
thing_picture = factory.get_resclass_type('anything:ThingPicture')
sipi = Sipi('http://0.0.0.0:1024', self.con.get_token())
img = sipi.upload_bitstream('testdata/bitstreams/test.tif')
file_ref = img['uploadedFiles'][0]['internalFilename']
res_perm = Permissions({PermissionValue.M: ["knora-admin:UnknownUser", "knora-admin:KnownUser"],
PermissionValue.CR: ["knora-admin:Creator", "knora-admin:ProjectAdmin"]})
resource_bitstream = {
'value': 'testdata/bitstreams/test.tif',
'internal_file_name': file_ref,
'permissions': res_perm
}
thing_picture(
con=self.con,
label='ThingPicture',
bitstream=resource_bitstream,
permissions=res_perm,
values={
'anything:hasPictureTitle': make_value(value="A Thing Picture named Lena", permissions=res_perm)
}
).create()
def tearDown(self) -> None:
"""
is executed after all tests are run through; performs a log out
"""
self.con.logout()
if __name__ == '__main__':
unittest.main()