def test_Group_update(self): """ Here we test if we can update an existing group :return: None """ con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') group = Group(con=con, name="KNORA-PY UPDATE", description="Test project for knora-py", project="http://rdfh.ch/projects/0001", status=True, selfjoin=False).create() self.iri = group.id group.name = "KNORA-PY UPDATE - modified" group.description = "gaga gaga gaga gaga gaga gaga gaga" group.selfjoin = True group.status = False ngroup = group.update() self.assertEqual(group.name, 'KNORA-PY UPDATE - modified') self.assertEqual(group.description, 'gaga gaga gaga gaga gaga gaga gaga') self.assertEqual(group.project, 'http://rdfh.ch/projects/0001') self.assertFalse(group.status) self.assertTrue(group.selfjoin)
def test_loginout(self): con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') self.assertIsNotNone(con.token) con.logout() self.assertIsNone(con.token) self.assertRaisesRegex(BaseError, 'KNORA-ERROR: status code=400*', con.login, 'invalid', 'invalid')
def test_ListNode_getAllLists(self): """ gett all root nodes, that is all lists :return: None """ con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') lists = ListNode.getAllLists(con, 'http://rdfh.ch/projects/0001') tmp = list(map(lambda a: a.id, lists)) self.assertIn('http://rdfh.ch/lists/0001/otherTreeList', tmp) self.assertIn('http://rdfh.ch/lists/0001/treeList', tmp)
def test_getAllGroups(self): """ Test if we can retrieve all groups :return: None """ con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') groups = Group.getAllGroups(con) self.assertIsNotNone(groups) for group in groups: group.print()
def test_ListNode_read(self): """ Read an existing node :return: None """ con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') node = ListNode(con=con, id='http://rdfh.ch/lists/0001/otherTreeList').read() self.assertEqual(node.id, 'http://rdfh.ch/lists/0001/otherTreeList') self.assertEqual(node.project, 'http://rdfh.ch/projects/0001') self.assertEqual(node.label['en'], 'Tree list root') self.assertTrue(node.isRootNode)
def test_ListNode_create(self): con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') node = ListNode(con=con, project=self.project, label=LangString({Languages.DE: "root node 1"}), comment=LangString({Languages.DE: "first root node"}), name="test_node_1").create() self.assertIsNotNone(node.id) self.assertEqual(node.project, self.project) self.assertEqual(node.label['de'], "root node 1") self.assertEqual(node.comment['de'], "first root node") self.assertEqual(node.name, "test_node_1") self.assertTrue(node.isRootNode)
def test_Group_read(self): """ Test if we can read an existing group and retrieve information about it :return: None """ con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') group = Group(con=con, id='http://rdfh.ch/groups/0001/thing-searcher').read() self.assertEqual(group.name, 'Thing searcher') self.assertEqual(group.description, 'A group for thing searchers.') self.assertEqual(group.project, 'http://rdfh.ch/projects/0001') self.assertTrue(group.status) self.assertTrue(group.selfjoin)
def test_ListNode_read2(self): """ read another existing node :return: """ con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') node = ListNode(con=con, id='http://rdfh.ch/lists/0001/otherTreeList03').read() self.assertEqual(node.id, 'http://rdfh.ch/lists/0001/otherTreeList03') self.assertEqual(node.label['en'], 'Other Tree list node 03') self.assertFalse(node.isRootNode) self.assertEqual(node.rootNodeIri, 'http://rdfh.ch/lists/0001/otherTreeList') self.assertIsNone(node.children)
def test_ResourceClass_update(self): # # Connect to Knora # con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') # # Create a test ontology # onto = Ontology( con=con, project=self.project, name='resclass-test-onto-2', label='resclass test ontology 2', ).create() last_modification_date = onto.lastModificationDate self.assertIsNotNone(onto.id) # # create test resource class # last_modification_date, resclass = ResourceClass( con=con, context=onto.context, name=self.name, ontology_id=onto.id, label=self.label, comment=self.comment).create(last_modification_date) onto.lastModificationDate = last_modification_date self.assertIsNotNone(resclass.id) # # Modify the resource class # resclass.addLabel('en', "This is english gaga") resclass.rmLabel('de') resclass.addComment('it', "Commentario italiano") last_modification_date, resclass = resclass.update( last_modification_date) onto.lastModificationDate = last_modification_date self.assertEqual(resclass.label['en'], "This is english gaga") self.assertEqual(resclass.comment['it'], "Commentario italiano") # # Now delete the resource class to clean up # last_modification_date = resclass.delete(last_modification_date) onto.lastModificationDate = last_modification_date
def test_ListNode_getAllNodes(self): """ Get all node of a list :return: None """ con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') root = ListNode( con=con, id='http://rdfh.ch/lists/0001/otherTreeList').getAllNodes() self.assertTrue(root.isRootNode) self.assertEqual(root.project, 'http://rdfh.ch/projects/0001') self.assertEqual(root.label['en'], 'Tree list root') self.assertIsNotNone(root.children) self.assertEqual(root.children[0].id, 'http://rdfh.ch/lists/0001/otherTreeList01') self.assertEqual(root.children[0].name, 'Other Tree list node 01') self.assertEqual(root.children[0].label['en'], 'Other Tree list node 01') self.assertEqual(root.children[1].id, 'http://rdfh.ch/lists/0001/otherTreeList02') self.assertEqual(root.children[1].name, 'Other Tree list node 02') self.assertEqual(root.children[1].label['en'], 'Other Tree list node 02') self.assertEqual(root.children[2].id, 'http://rdfh.ch/lists/0001/otherTreeList03') self.assertEqual(root.children[2].name, 'Other Tree list node 03') self.assertEqual(root.children[2].label['en'], 'Other Tree list node 03') self.assertIsNotNone(root.children[2].children) self.assertEqual(root.children[2].children[0].id, 'http://rdfh.ch/lists/0001/otherTreeList10') self.assertEqual(root.children[2].children[0].name, 'Other Tree list node 10') self.assertEqual(root.children[2].children[0].label['en'], 'Other Tree list node 10') self.assertEqual(root.children[2].children[1].id, 'http://rdfh.ch/lists/0001/otherTreeList11') self.assertEqual(root.children[2].children[1].name, 'Other Tree list node 11') self.assertEqual(root.children[2].children[1].label['en'], 'Other Tree list node 11')
def setUp(self) -> None: # # Connect to Knora # self.con = Connection('http://0.0.0.0:3333') self.con.login('*****@*****.**', 'test') # # Create a test ontology # self.onto = Ontology( con=self.con, project=self.project, name=self.onto_name, label=self.onto_label, ).create() self.assertIsNotNone(self.onto.id) self.last_modification_date = self.onto.lastModificationDate
def test_post(self): resinfo = """{ "@type" : "anything:Thing", "knora-api:attachedToProject" : { "@id" : "http://rdfh.ch/projects/0001" }, "anything:hasBoolean" : { "@type" : "knora-api:BooleanValue", "knora-api:booleanValueAsBoolean" : true }, "rdfs:label" : "knora-py thing", "knora-api:hasPermissions" : "CR knora-admin:Creator|V http://rdfh.ch/groups/0001/thing-searcher", "@context" : { "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "knora-api" : "http://api.knora.org/ontology/knora-api/v2#", "rdfs" : "http://www.w3.org/2000/01/rdf-schema#", "xsd" : "http://www.w3.org/2001/XMLSchema#", "anything" : "http://0.0.0.0:3333/ontology/0001/anything/v2#" } } """ con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') res = con.post('/v2/resources', resinfo) self.assertIsNotNone(res['@id']) id = res['@id'] self.assertEqual(res['@type'], 'anything:Thing') self.assertEqual(res['rdfs:label'], 'knora-py thing') eraseinfo = """{ "@id" : "%s", "@type" : "anything:Thing", "@context" : { "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "knora-api" : "http://api.knora.org/ontology/knora-api/v2#", "rdfs" : "http://www.w3.org/2000/01/rdf-schema#", "xsd" : "http://www.w3.org/2001/XMLSchema#", "anything" : "http://0.0.0.0:3333/ontology/0001/anything/v2#" } } """ % id res = con.post('/v2/resources/erase', eraseinfo) self.assertIsNotNone(res['knora-api:result']) self.assertEqual(res['knora-api:result'], 'Resource erased')
def test_Group(self): """ Test the creation of the group instance (without interaction with the triple store...) :return: None """ con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') group = Group(con=con, name="KNORA-PY-TEST", description="Test project for knora-py", project="http://rdfh.ch/projects/0001", status=True, selfjoin=False) self.assertEqual(group.name, 'KNORA-PY-TEST') self.assertEqual(group.description, 'Test project for knora-py') self.assertEqual(group.project, 'http://rdfh.ch/projects/0001') self.assertTrue(group.status) self.assertFalse(group.selfjoin)
def test_ListNode_update(self): """ Update the data of a node... :return: """ con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') node = ListNode(con=con, project=self.project, label=LangString({Languages.DE: "root node 3"}), comment=LangString({Languages.DE: "Third root node"}), name="test_node_3").create() node.addLabel('fr', 'Une racine d\' une liste') node.rmLabel('de') node.addComment('fr', 'un commentaire en français') node.rmComment('de') node.name = 'GAGAGA' node.update() self.assertEqual(node.label['fr'], 'Une racine d\' une liste') self.assertEqual(node.comment['fr'], 'un commentaire en français') self.assertEqual(node.name, 'GAGAGA')
def test_ResourceClass_create(self): # # Connect to Knora # con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') # # Create a test ontology # onto = Ontology( con=con, project=self.project, name='resclass-test-onto-1', label='resclass test ontology 1', ).create() last_modification_date = onto.lastModificationDate self.assertIsNotNone(onto.id) # # Create new resource class # last_modification_date, resclass = ResourceClass( con=con, context=onto.context, name=self.name, ontology_id=onto.id, label=self.label, comment=self.comment).create(last_modification_date) onto.lastModificationDate = last_modification_date self.assertIsNotNone(resclass.id) self.assertEqual(resclass.name, self.name) self.assertEqual(resclass.label['de'], self.label['de']) self.assertEqual(resclass.comment['de'], self.comment['de']) # # Delete the new resource class # last_modification_date = resclass.delete(last_modification_date) onto.lastModificationDate = last_modification_date
def test_Group_create(self): """ Test if we can create a new group in the triple store :return: None """ global __iri con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') group = Group(con=con, name="KNORA-PY CREATE", description="Test project for knora-py", project="http://rdfh.ch/projects/0001", status=True, selfjoin=False).create() self.iri = group.id self.assertEqual(group.name, 'KNORA-PY CREATE') self.assertEqual(group.description, 'Test project for knora-py') self.assertEqual(group.project, 'http://rdfh.ch/projects/0001') self.assertTrue(group.status) self.assertFalse(group.selfjoin)
def test_Group_delete(self): """ Here we test if we can mark an existing group as deleted (it will not be deleted completely from the triplestore, but marked!!) :return: None """ con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') group = Group(con=con, name="KNORA-PY DELETE", description="Test project for knora-py", project="http://rdfh.ch/projects/0001", status=True, selfjoin=False).create() self.iri = group.id ngroup = group.delete() self.assertEqual(ngroup.name, 'KNORA-PY DELETE') self.assertEqual(ngroup.description, 'Test project for knora-py') self.assertEqual(ngroup.project, 'http://rdfh.ch/projects/0001') self.assertFalse(ngroup.status) self.assertFalse(ngroup.selfjoin)
def test_ListNode_hierarchy(self): """ Create a node and a sub-node :return: None """ con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') node = ListNode(con=con, project=self.project, label=LangString({Languages.DE: "root node 2"}), comment=LangString({Languages.DE: "second root node"}), name="test_node_2").create() node2 = ListNode( con=con, project=self.project, label=LangString({Languages.DE: 'Eine Knoten der Liste'}), comment=LangString({Languages.DE: "So ein Kommentar"}), name="NODE2", parent=node).create() self.assertEqual(node2.label['de'], 'Eine Knoten der Liste') self.assertEqual(node2.comment['de'], "So ein Kommentar") self.assertEqual(node2.name, "NODE2") self.assertFalse(node2.isRootNode)
def test_getAllUsers(self): con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') all_users = User.getAllUsers(con) for u in all_users: self.assertIsNotNone(u.id)
def xml_upload(input_file: str, server: str, user: str, password: str, imgdir: str, sipi: str, verbose: bool) -> bool: current_dir = os.path.dirname(os.path.realpath(__file__)) xmlschema_doc = etree.parse(os.path.join(current_dir, 'knora-data-schema.xsd')) xmlschema = etree.XMLSchema(xmlschema_doc) doc = etree.parse(input_file) xmlschema.assertValid(doc) del xmlschema del doc del xmlschema_doc print("The input data file is syntactically correct and passed validation!") # # Connect to the DaSCH Service Platform API # con = Connection(server) con.login(user, password) proj_context = ProjectContext(con=con) # # read the XML file containing the data, including project shortcode # context: etree.iterparse = etree.iterparse(input_file, events=("start", "end")) resources: List[KnoraResource] = [] permissions: Dict[str, XmlPermission] = {} shortcode: Union[str, None] = None default_ontology = None while True: event, node = next(context) if event == 'start': if node.tag == 'knora': default_ontology = node.attrib['default-ontology'] shortcode = node.attrib['shortcode'] proj_context.shortcode = shortcode elif event == 'start' and node.tag == 'resource': resources.append(KnoraResource(context, node, default_ontology)) elif event == 'start' and node.tag == 'permissions': permission = XmlPermission(context, node, proj_context) permissions[permission.id] = permission elif event == 'end': if node.tag == 'knora': break # # sort the resources so that resources which do not link to others come first # resources = do_sortorder(resources) sipi = Sipi(sipi, con.get_token()) factory = ResourceInstanceFactory(con, shortcode) permissions_lookup: Dict[str, Permissions] = {} for key, perm in permissions.items(): permissions_lookup[key] = perm.get_permission_instance() resclassnames = factory.get_resclass_names() resclasses: Dict[str, type] = {} for resclassname in resclassnames: resclasses[resclassname] = factory.get_resclass(resclassname) resiri_lookup: StrDict = {} for resource in resources: if resource.image: img = sipi.upload_image(os.path.join(imgdir, resource.image)) stillimage = img['uploadedFiles'][0]['internalFilename'] else: stillimage = None instance = resclasses[resource.restype](con=con, label=resource.label, permissions=permissions_lookup.get(resource.permissions), stillimage=stillimage, values=resource.get_propvals(resiri_lookup, permissions_lookup)).create() resiri_lookup[resource.id] = instance.iri print("Created:", instance.iri)
def test_get(self): con = Connection('http://0.0.0.0:3333') res = con.get("/ontology/0001/anything/simple/v2") con.logout() self.assertIsNotNone(res['@graph']) self.assertRaises(BaseError, con.get, "/gagaga")
import os sys.path.append(os.path.dirname(os.path.realpath(__file__))) from dsplib.models.connection import Connection from dsplib.models.resource import ResourceInstanceFactory from dsplib.models.value import BooleanValue, ColorValue, DateValue, DecimalValue, IntValue, IntervalValue, TextValue, \ UriValue, KnoraStandoffXml, make_value from dsplib.models.permission import PermissionValue, Permissions, PermissionsIterator from dsplib.models.sipi import Sipi from pprint import pprint # # Connect to server and make a login # con = Connection('http://0.0.0.0:3333') con.login('*****@*****.**', 'test') # # Make class factory for project 'anything. The factory creates classes that implement the CRUD methods # for the given resource classes, that is to create, read, update and delete instances (=resources) of the given classesd # factory = ResourceInstanceFactory(con, 'anything') resclassnames = factory.get_resclass_names() # # Get an python class of a BlueThing resource class # BlueThing = factory.get_resclass('anything:BlueThing') print("====================================================")
def setUp(self) -> None: self.con = Connection('http://0.0.0.0:3333') self.con.login('*****@*****.**', 'test')
def test_Connection(self): con = Connection('http://0.0.0.0:3333') self.assertIsInstance(con, Connection)
def __init__(self, *args, **kw): super(OpenConnectionDialog, self).__init__(*args, **kw, title="Open connection...", style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER) topsizer = wx.BoxSizer(wx.VERTICAL) panel1 = wx.Panel(self) l0 = wx.StaticText(panel1, label="Server: ") server = wx.TextCtrl(panel1, name="server", value="http://0.0.0.0:3333", size=wx.Size(200, -1)) l1 = wx.StaticText(panel1, label="Username: "******"username", value="*****@*****.**", size=wx.Size(200, -1)) l2 = wx.StaticText(panel1, label="Password: "******"password", value="test", size=wx.Size(200, -1), style=wx.TE_PASSWORD) gsizer = wx.GridSizer(cols=2) gsizer.Add(l0, flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL, border=3) gsizer.Add(server, wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL, border=3) gsizer.Add(l1, flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL, border=3) gsizer.Add(username, wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL, border=3) gsizer.Add(l2, flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL, border=3) gsizer.Add(password, flag=wx.ALIGN_CENTER_VERTICAL | wx.EXPAND | wx.ALL, border=3) gsizer.SetSizeHints(panel1) panel1.SetSizer(gsizer) panel1.SetAutoLayout(1) gsizer.Fit(panel1) topsizer.Add(panel1, flag=wx.EXPAND | wx.ALL, border=5) bsizer = self.CreateStdDialogButtonSizer(wx.OK | wx.CANCEL) topsizer.Add(bsizer, flag=wx.EXPAND | wx.ALL, border=5) self.SetSizerAndFit(topsizer) self.ShowModal() if self.GetReturnCode() == wx.ID_OK: server_str = server.GetLineText(0) username_str = username.GetLineText(0) password_str = password.GetLineText(0) self.con = Connection(server_str) self.con.login(username_str, password_str) else: print("CANCEL PRESSED")