def linkTest(self, anntype, annvalue, anntypeuri, annexpect): rodir = self.createTestRo(testbase, "data/ro-test-1", "RO test annotation", "ro-testRoAnnotate") args = [ "ro", "link", rodir+"/"+"subdir1/subdir1-file.txt", anntype, annvalue, "-v", ] with SwitchStdout(self.outstr): status = ro.runCommand(ro_test_config.CONFIGDIR, ro_test_config.ROBASEDIR, args) outtxt = self.outstr.getvalue() assert status == 0, outtxt self.assertEqual(outtxt.count("ro link"), 1) # Read manifest and check for annotation annotations = ro_annotation._getFileAnnotations(rodir, "subdir1/subdir1-file.txt") resourceuri = ro_manifest.getComponentUri(rodir, "subdir1/subdir1-file.txt") expected_annotations = ( [ (resourceuri, anntypeuri, annexpect) ]) count = 0 for next in list(annotations): log.debug("- next %s"%(str(next[0])) ) log.debug(" - (%s, %s)"%(str(next[1]),str(next[2])) ) if next in expected_annotations: count += 1 else: self.assertTrue(False, "Not expected (%d) %s"%(count, repr(next))) self.assertEqual(count,1) self.deleteTestRo(rodir) return
def annotateTest(self, anntype, annvalue, anntypeuri, annexpect): rodir = self.createTestRo(testbase, "data/ro-test-1", "RO test annotation", "ro-testRoAnnotate") args = [ "ro", "annotate", rodir+"/"+"subdir1/subdir1-file.txt", anntype, annvalue, "-v", ] with SwitchStdout(self.outstr): status = ro.runCommand(ro_test_config.CONFIGDIR, ro_test_config.ROBASEDIR, args) outtxt = self.outstr.getvalue() assert status == 0, outtxt self.assertEqual(outtxt.count("ro annotate"), 1) # Read manifest and check for annotation annotations = ro_annotation._getFileAnnotations(rodir, "subdir1/subdir1-file.txt") resourceuri = ro_manifest.getComponentUri(rodir, "subdir1/subdir1-file.txt") expected_annotations = ( [ (resourceuri, anntypeuri, rdflib.Literal(annexpect)) ]) for i in range(1): next = annotations.next() #log.debug("Next %s"%(repr(next))) if ( next not in expected_annotations): self.assertTrue(False, "Not expected (%d) %s"%(i, repr(next))) self.assertRaises(StopIteration, annotations.next) self.deleteTestRo(rodir) return
def testAnnotateMultiple(self): rodir = self.createTestRo(testbase, "data/ro-test-1", "RO test annotation", "ro-testRoAnnotate") rofile = rodir+"/"+"subdir1/subdir1-file.txt" define_annotations = ( [ {"atypename": "type", "avalue":"atype", "atypeuri":DCTERMS.type, "aexpect":"atype" } , {"atypename": "keywords", "avalue":"asubj", "atypeuri":DCTERMS.subject, "aexpect":"asubj" } , {"atypename": "description", "avalue":"adesc", "atypeuri":DCTERMS.description, "aexpect":"adesc" } , {"atypename": "format", "avalue":"aformat", "atypeuri":DCTERMS.format, "aexpect":"aformat" } , {"atypename": "title", "avalue":"atitle", "atypeuri":DCTERMS.title, "aexpect":"atitle" } , {"atypename": "created", "avalue":"acreated", "atypeuri":DCTERMS.created, "aexpect":"acreated" } #, {"atypename": ..., "avalue":..., "atypeuri":..., "aexpect":... } #, {"atypename": ..., "avalue":..., "atypeuri":..., "aexpect":... } ]) self.annotateMultiple(rodir, rofile, define_annotations) # Read manifest and check for annotation annotations = ro_annotation._getFileAnnotations(rodir, "subdir1/subdir1-file.txt") resourceuri = ro_manifest.getComponentUri(rodir, "subdir1/subdir1-file.txt") expected_annotations = ( [ (resourceuri, a["atypeuri"], rdflib.Literal(a["aexpect"])) for a in define_annotations ]) for i in range(6): next = annotations.next() #log.debug("Next %s"%(repr(next))) if ( next not in expected_annotations): self.assertTrue(False, "Not expected (%d) %s"%(i, repr(next))) self.assertRaises(StopIteration, annotations.next) # Clean up self.deleteTestRo(rodir) return
def testLinkWithGraph(self): rodir = self.createTestRo(testbase, "data/ro-test-1", "RO test annotation", "ro-testRoAnnotate") rofile = rodir+"/"+"subdir1/subdir1-file.txt" define_annotations = ( [ {"atypename": "type", "avalue":"atype", "atypeuri":DCTERMS.type, "aexpect":"atype" } , {"atypename": "keywords", "avalue":"asubj", "atypeuri":DCTERMS.subject, "aexpect":"asubj" } , {"atypename": "description", "avalue":"adesc", "atypeuri":DCTERMS.description, "aexpect":"adesc" } , {"atypename": "format", "avalue":"aformat", "atypeuri":DCTERMS.format, "aexpect":"aformat" } , {"atypename": "title", "avalue":"atitle", "atypeuri":DCTERMS.title, "aexpect":"atitle" } , {"atypename": "created", "avalue":"acreated", "atypeuri":DCTERMS.created, "aexpect":"acreated" } ]) # Create annotation graph file and apply annotations annotation_graph = rdflib.Graph() resourceuri = ro_manifest.getComponentUri(rodir, "subdir1/subdir1-file.txt") for ann in define_annotations: annotation_graph.add( (resourceuri, ann["atypeuri"], rdflib.Literal(ann["aexpect"])) ) annotation_graph_filename = os.path.join(os.path.abspath(rodir), "annotate-subdir1-file.txt.rdf") annotation_graph.serialize(annotation_graph_filename, format='xml', base=ro_manifest.getRoUri(rodir), xml_base="") args = ["ro", "annotate", rofile, "-g", annotation_graph_filename ] with SwitchStdout(self.outstr): status = ro.runCommand(ro_test_config.CONFIGDIR, ro_test_config.ROBASEDIR, args) outtxt = self.outstr.getvalue() assert status == 0, outtxt # Read manifest and check for annotation annotations = ro_annotation._getFileAnnotations(rodir, "subdir1/subdir1-file.txt") expected_annotations = ( [ (resourceuri, a["atypeuri"], rdflib.Literal(a["aexpect"])) for a in define_annotations ]) count = 0 for next in list(annotations): log.debug("- next %s"%(str(next[0])) ) log.debug(" - (%s, %s)"%(str(next[1]),str(next[2])) ) if next in expected_annotations: count += 1 else: self.assertTrue(False, "Not expected (%d) %s"%(count, repr(next))) self.assertEqual(count,6) # Clean up self.deleteTestRo(rodir) return
def testAnnotateFileWithEscapes(self): rodir = self.createTestRo(testbase, "data/ro-test-1", "RO test annotation", "ro-testRoAnnotate") rofile1 = rodir+"/"+"filename with spaces.txt" rofile2 = rodir+"/"+"filename#with#hashes.txt" with SwitchStdout(self.outstr): for rofile in [rofile1,rofile2]: args = ["ro", "annotate", rofile, "type", "atype"] status = ro.runCommand(ro_test_config.CONFIGDIR, ro_test_config.ROBASEDIR, args) outtxt = self.outstr.getvalue() assert status == 0, outtxt # Reset output stream buffer closed self.outstr = StringIO.StringIO() # Read manifest and check for annotation for rofile in [rofile1,rofile2]: resourceuri = ro_manifest.getComponentUri(rodir, rofile) annotations = ro_annotation._getFileAnnotations(rodir, rofile) next = annotations.next() self.assertEqual(next, (resourceuri, DCTERMS.type, rdflib.Literal("atype"))) self.assertRaises(StopIteration, annotations.next) # Clean up self.deleteTestRo(rodir) return
def testRemoveGetFileAnnotations(self): rodir = self.createTestRo(testbase, "data/ro-test-1", "Test remove file annotation", "ro-testRoAnnotate") roresource = "subdir1/subdir1-file.txt" # Add anotations for file ro_annotation._addSimpleAnnotation(ro_config, rodir, roresource, "type", "Test file") ro_annotation._addSimpleAnnotation(ro_config, rodir, roresource, "description", "File in test research object") ro_annotation._addSimpleAnnotation(ro_config, rodir, roresource, "note", "Research object file created for annotation testing") ro_annotation._addSimpleAnnotation(ro_config, rodir, roresource, "title", "Test file in RO") ro_annotation._addSimpleAnnotation(ro_config, rodir, roresource, "created", "2011-12-07") ro_annotation._addSimpleAnnotation(ro_config, rodir, roresource, "rdf:type", ROTERMS.resource) # Remove annotations ro_annotation._removeSimpleAnnotation(ro_config, rodir, roresource, "description", "File in test research object") ro_annotation._removeSimpleAnnotation(ro_config, rodir, roresource, "note", None) # Retrieve the file anotations annotations = ro_annotation._getFileAnnotations(rodir, roresource) resourceuri = ro_manifest.getComponentUri(rodir, roresource) log.debug("resourceuri: %s"%(resourceuri)) expected_annotations = ( [ (resourceuri, DCTERMS.type, rdflib.Literal('Test file')) , (resourceuri, DCTERMS.title, rdflib.Literal('Test file in RO')) , (resourceuri, DCTERMS.created, rdflib.Literal('2011-12-07')) , (resourceuri, RDF.type, ROTERMS.resource) ]) for i in range(4): next = annotations.next() #log.debug("Next %s"%(repr(next))) if ( next not in expected_annotations): self.assertTrue(False, "Not expected (%d) %s"%(i, repr(next))) self.assertRaises(StopIteration, annotations.next) self.deleteTestRo(rodir) return