def testChecklistEvalExcel(self): """ Test checklist evaluation with generated Minim file from Excel source """ self.setupConfig() rodir = self.createTestRo(testbase, "testro", "RO for testMkMinim", "ro-testMkMinim") self.populateTestRo(testbase, rodir) self.annotateResource(testbase, rodir, "", "FileAnnotations.ttl") rouri = ro_manifest.getRoUri(rodir) # Create minim graph from CSV file gridname = "TestMkMinim.xls" griduri = ro_manifest.getComponentUri(rodir, gridname) gridxls = os.path.join(rodir, gridname) gridbase = "http://example.org/base/" grid = GridExcel(gridxls, baseuri=gridbase) (status, minimgr) = mkminim.mkminim(grid, baseuri=grid.resolveUri("")) self.assertEquals(status, 0) # Write Minim minimname = "TestMkMinim_minim.ttl" with open(rodir + "/" + minimname, "w") as minimfile: minimgr.serialize(minimfile, format="turtle") # Evaluate checklist minimuri = ro_manifest.getComponentUri(rodir, minimname) minimpurpose = "test1" args = ["ro", "evaluate", "checklist", "-a", "-d", rodir + "/", minimname, minimpurpose, "."] self.outstr.seek(0) with StdoutContext.SwitchStdout(self.outstr): status = ro.runCommand( os.path.join(testbase, TestConfig.ro_test_config.CONFIGDIR), os.path.join(testbase, TestConfig.ro_test_config.ROBASEDIR), args, ) outtxt = self.outstr.getvalue() assert status == 0, "Status %d, outtxt: %s" % (status, outtxt) log.debug("status %d, outtxt: %s" % (status, outtxt)) # Check response returned # filelist = [ unicode(ro_manifest.getComponentUri(rodir, f)) # for f in ["File1.txt", "File2.txt", "File3.txt"] ] filelist = ValueList( [str(ro_manifest.getComponentUri(rodir, f)) for f in ["File1.txt", "File2.txt", "File3.txt"]] ) expect = [ "Research Object file://%s/:" % (rodir), "Fully complete for test1 of resource .", "Satisfied requirements:", " At least 3 file as part values are present", " At most 3 file as part values are present", " All part resource files %s are aggregated in RO" % (filelist), " All file as part resources are accessible (live)", " Python 2.7.x present", " Files as part are partOf some indicated whole", " File exists as a part", "Research object URI: %s" % (rouri), "Minimum information URI: %s" % (minimuri), ] self.outstr.seek(0) for line in self.outstr: self.assertIn(str(line)[:-1], expect) self.deleteTestRo(rodir) return
def testGetRequirements(self): def compare_reqs(req_expect, req_found): for k in req_expect: if not k in req_found: log.debug("- not found: %s"%(k)) return False elif isinstance(req_expect[k], dict) and isinstance(req_found[k], dict): if not compare_reqs(req_expect[k], req_found[k]): return False elif req_found[k] != req_expect[k]: log.debug("- not found: %s: %s != %s "%(k,req_expect[k],req_found[k])) return False return True self.setupConfig() rodir = self.createTestRo(testbase, "test-data-1", "RO test minim", "ro-testMinim") rouri = ro_manifest.getRoUri(rodir) minimbase = ro_manifest.getComponentUri(rodir, "Minim-UserRequirements.rdf") model = ro_minim.getElementUri(minimbase, "#runnableRequirementRO") minimgraph = ro_minim.readMinimGraph(minimbase) requirements = ro_minim.getRequirements(minimgraph, model) expected_found = False r1 = ( { 'level': "MUST" , 'label': rdflib.Literal("Python 2.7") , 'softwarerule': { 'command': rdflib.Literal("python -V") , 'response': rdflib.Literal("Python 2.7") , 'derives': ro_minim.getElementUri(minimbase, "#environment-software/python-27") } , 'uri': ro_minim.getElementUri(minimbase, "#environment-software/python-27") }) r2 = ( { 'level': "MUST" , 'label': rdflib.Literal("aggregates data/UserRequirements-astro.ods") , 'datarule': { 'aggregates': ro_manifest.getComponentUri(rodir, "data/UserRequirements-astro.ods") , 'derives': ro_minim.getElementUri(minimbase, "#isPresent/data/UserRequirements-astro.ods") } , 'uri': ro_minim.getElementUri(minimbase, "#isPresent/data/UserRequirements-astro.ods") }) r3 = ( { 'level': "SHOULD" , 'label': rdflib.Literal("aggregates docs/reqs.css") , 'datarule': { 'aggregates': ro_manifest.getComponentUri(rodir, "docs/reqs.css") , 'derives': ro_minim.getElementUri(minimbase, "#isPresent/docs/reqs.css") } , 'uri': ro_minim.getElementUri(minimbase, "#isPresent/docs/reqs.css") }) r1_found = r2_found = r3_found = False for r in requirements: log.debug("requirement: %s"%(repr(r))) if compare_reqs(r1, r): r1_found = True if compare_reqs(r2, r): r2_found = True if compare_reqs(r3, r): r3_found = True self.assertTrue(r1_found, "Expected requirement(1) not found in minim") self.assertTrue(r2_found, "Expected requirement(2) not found in minim") self.assertTrue(r3_found, "Expected requirement(3) not found in minim") return
def testGetConstraint(self): self.setupConfig() rodir = self.createTestRo(testbase, "test-data-1", "RO test minim", "ro-testMinim") rouri = ro_manifest.getRoUri(rodir) minimbase = ro_manifest.getComponentUri(rodir, "Minim-UserRequirements.rdf") model = ro_minim.getElementUri(minimbase, "#runnableRequirementRO") constraint = ro_minim.getElementUri(minimbase, "#create/docs/UserRequirements-astro.csv") minimgraph = ro_minim.readMinimGraph(minimbase) c = ro_minim.getConstraint(minimgraph, rodir, "docs/UserRequirements-astro.csv", r"create.*UserRequirements-astro\.csv") self.assertEquals(c['target'], ro_manifest.getComponentUri(rodir, "docs/UserRequirements-astro.csv")) self.assertEquals(c['purpose'], rdflib.Literal("create UserRequirements-astro.csv")) self.assertEquals(c['model'], model) self.assertEquals(c['uri'], constraint) 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 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 testAddAggregatedResourcesCommand(self): """ Test function that adds aggregated resources to a research object manifest """ rodir = self.createTestRo(testbase, "data/ro-test-1", "RO test annotation", "ro-testRoAnnotate") args = [ "ro", "add", "-v", "-a", "-d", rodir, rodir ] with SwitchStdout(self.outstr): status = ro.runCommand(ro_test_config.CONFIGDIR, ro_test_config.ROBASEDIR, args) outtxt = self.outstr.getvalue() assert status == 0, outtxt log.debug("outtxt %s"%outtxt) def URIRef(path): return ro_manifest.getComponentUriAbs(rodir, path) s = ro_manifest.getComponentUri(rodir, "") g = rdflib.Graph() g.add( (s, RDF.type, RO.ResearchObject ) ) g.add( (s, ORE.aggregates, URIRef("README-ro-test-1") ) ) g.add( (s, ORE.aggregates, URIRef("filename%20with%20spaces.txt") ) ) g.add( (s, ORE.aggregates, URIRef("filename%23with%23hashes.txt") ) ) g.add( (s, ORE.aggregates, URIRef("README-ro-test-1") ) ) g.add( (s, ORE.aggregates, URIRef("subdir1/subdir1-file.txt") ) ) g.add( (s, ORE.aggregates, URIRef("subdir2/subdir2-file.txt") ) ) self.checkManifestGraph(rodir, g) self.deleteTestRo(rodir) return
def testMkMinim(self): self.setupConfig() rodir = self.createTestRo(testbase, "testro", "RO for testMkMinim", "ro-testMkMinim") rouri = ro_manifest.getRoUri(rodir) # Create minim graph from CSV file # NOTE: a base URI may be specoified when decoding the grid or when constructing the minim # graph. The Minim graph uses its own relative references, so for consistency it may # be necessary to pass the grid base URI to mkminim. The code below does this. gridname = "TestMkMinim.csv" griduri = ro_manifest.getComponentUri(rodir, gridname) gridcsv = os.path.join(rodir, gridname) gridbase = "http://example.org/base/" with open(gridcsv, "rU") as gridfile: grid = GridCSV(gridfile, baseuri=gridbase, dialect=csv.excel) (status, minimgr) = mkminim.mkminim(grid, baseuri=grid.resolveUri("")) self.assertEquals(status, 0) # Read expected graph graphname = os.path.join(rodir, "TestMkMinim.ttl") expectgr = rdflib.Graph() with open(graphname) as expectfile: expectgr.parse(file=expectfile, publicID=gridbase, format="turtle") # Check content of minim graph ###minimgr.serialize(sys.stdout, format="turtle") self.checkTargetGraph(minimgr.graph(), expectgr, msg="Not found in constructed minim graph") self.deleteTestRo(rodir) return
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 testGetRequirements(self): def compare_reqs(req_expect, req_found): for k in req_expect: if not k in req_found: log.debug("- not found: %s"%(k)) return False elif isinstance(req_expect[k], dict) and isinstance(req_found[k], dict): if not compare_reqs(req_expect[k], req_found[k]): return False elif req_found[k] != req_expect[k]: log.debug("- not found: %s: %s != %s "%(k,req_expect[k],req_found[k])) return False return True self.setupConfig() rodir = self.createTestRo(testbase, "test-data-2", "RO test minim", "ro-testMinim") rouri = ro_manifest.getRoUri(rodir) minimbase = ro_manifest.getComponentUri(rodir, "Minim-UserRequirements2.rdf") model = ro_minim.getElementUri(minimbase, "#runnableRO") minimgraph = ro_minim.readMinimGraph(minimbase) requirements = ro_minim.getRequirements(minimgraph, model) r1 = ( { 'level': "MUST" , 'label': rdflib.Literal("aggregates data/UserRequirements-astro.ods") , 'querytestrule': { 'query': rdflib.Literal("?ro a ro:ResearchObject") , 'resultmod': None , 'aggregates_t': rdflib.Literal("data/UserRequirements-astro.ods") } , 'uri': ro_minim.getElementUri(minimbase, "#isAggregated/data/UserRequirements-astro.ods") }) r2 = ( { 'level': "MUST" , 'label': rdflib.Literal("accessible data/UserRequirements-astro.ods") , 'querytestrule': { 'query': rdflib.Literal("?ro a ro:ResearchObject") , 'resultmod': None , 'islive_t': rdflib.Literal("data/UserRequirements-astro.ods") } , 'uri': ro_minim.getElementUri(minimbase, "#isAccessible/data/UserRequirements-astro.ods") }) r3 = ( { 'level': "MUST" , 'label': rdflib.Literal("labeled data/UserRequirements-astro.ods") , 'querytestrule': { 'query': rdflib.Literal("?ro a ro:ResearchObject") , 'resultmod': rdflib.Literal("ORDER BY ?ro") , 'exists': rdflib.Literal("<data/UserRequirements-astro.ods> rdfs:label ?label") } , 'uri': ro_minim.getElementUri(minimbase, "#isLabeled/data/UserRequirements-astro.ods") }) r1_found = r2_found = r3_found = False for r in requirements: log.debug("requirement: %s"%(repr(r))) if compare_reqs(r1, r): r1_found = True if compare_reqs(r2, r): r2_found = True if compare_reqs(r3, r): r3_found = True self.assertTrue(r1_found, "Expected requirement(1) not found in minim") self.assertTrue(r2_found, "Expected requirement(2) not found in minim") self.assertTrue(r3_found, "Expected requirement(3) not found in minim") return
def testGetConstraints(self): self.setupConfig() rodir = self.createTestRo(testbase, "test-data-2", "RO test minim", "ro-testMinim") rouri = ro_manifest.getRoUri(rodir) minimbase = ro_manifest.getComponentUri(rodir, "Minim-UserRequirements2.rdf") model = ro_minim.getElementUri(minimbase, "#runnableRO") constraint = ro_minim.getElementUri(minimbase, "#create/data/UserRequirements-astro.ods") # Read Minim as graph, scan constraints and look for expected value minimgraph = ro_minim.readMinimGraph(minimbase) constraints = ro_minim.getConstraints(minimgraph) expected_found = False for c in constraints: if ( c['target'] == ro_manifest.getComponentUri(rodir, "data/UserRequirements-astro.ods") and c['purpose'] == rdflib.Literal("create UserRequirements-astro.ods") and c['model'] == model and c['uri'] == constraint ) : expected_found = True break self.assertTrue(expected_found, "Expected constraint not found in minim") return
def testGetModel(self): self.setupConfig() rodir = self.createTestRo(testbase, "test-data-2", "RO test minim", "ro-testMinim") rouri = ro_manifest.getRoUri(rodir) minimbase = ro_manifest.getComponentUri(rodir, "Minim-UserRequirements2.rdf") model = ro_minim.getElementUri(minimbase, "#runnableRO") minimgraph = ro_minim.readMinimGraph(minimbase) m = ro_minim.getModel(minimgraph, model) self.assertEquals(m['label'], rdflib.Literal("Runnable RO")) self.assertEquals(m['uri'], model) return
def testMinimRead(self): """ Basic test that Minim test file can be read """ self.setupConfig() rodir = self.createTestRo(testbase, "test-data-2", "RO test minim", "ro-testMinim") rouri = ro_manifest.getRoUri(rodir) minimbase = ro_manifest.getComponentUri(rodir, "Minim-UserRequirements2.rdf") target = ro_manifest.getComponentUri(rodir, "data/UserRequirements-astro.ods") constraint = ro_minim.getElementUri(minimbase, "#create/data/UserRequirements-astro.ods") model = ro_minim.getElementUri(minimbase, "#runnableRO") g = ro_minim.readMinimGraph(minimbase) expected_minim = ( [ (target, MINIM.hasChecklist, constraint ) , (constraint, MINIM.forPurpose, rdflib.Literal('create UserRequirements-astro.ods') ) , (constraint, MINIM.toModel, model ) , (model, RDF.type, MINIM.Model ) ]) self.checkTargetGraph(g, expected_minim, msg="Not found in Minim") self.deleteTestRo(rodir) return
def testGridRead(self): """ Basic test that Minim test file can be read """ self.setupConfig() rodir = self.createTestRo(testbase, "testro", "RO for Minim creation test", "ro-testMkMinim") rouri = ro_manifest.getRoUri(rodir) gridname = "TestMkMinim.csv" griduri = ro_manifest.getComponentUri(rodir, gridname) gridcsv = os.path.join(rodir, gridname) with open(gridcsv, "rU") as gridfile: grid = GridCSV(gridfile, baseuri=griduri, dialect=csv.excel) self.assertEqual(grid[0][0], "Minim definition for MkMinim testing") self.deleteTestRo(rodir) return
def testGetModels(self): self.setupConfig() rodir = self.createTestRo(testbase, "test-data-2", "RO test minim", "ro-testMinim") rouri = ro_manifest.getRoUri(rodir) minimbase = ro_manifest.getComponentUri(rodir, "Minim-UserRequirements2.rdf") model = ro_minim.getElementUri(minimbase, "#runnableRO") minimgraph = ro_minim.readMinimGraph(minimbase) models = ro_minim.getModels(minimgraph) expected_found = False for m in models: if ( m['label'] == rdflib.Literal("Runnable RO") and m['uri'] == model ) : expected_found = True break self.assertTrue(expected_found, "Expected model not found in minim") return
def testGetComponentUri(self): self.assertEquals(ro_manifest.getComponentUri("/example/ro/dir", "a/b.txt"), rdflib.URIRef("file:///example/ro/dir/a/b.txt")) self.assertEquals(ro_manifest.getComponentUri("/example/ro/dir/", "a/b.txt"), rdflib.URIRef("file:///example/ro/dir/a/b.txt")) self.assertEquals(ro_manifest.getComponentUri("ro/dir", "a/b.txt"), rdflib.URIRef("file://%s/ro/dir/a/b.txt"%(cwd))) self.assertEquals(ro_manifest.getComponentUri("ro/dir/", "a/b.txt"), rdflib.URIRef("file://%s/ro/dir/a/b.txt"%(cwd))) self.assertEquals(ro_manifest.getComponentUri("/example/ro/dir", "a/b/d/"), rdflib.URIRef("file:///example/ro/dir/a/b/d/")) self.assertEquals(ro_manifest.getComponentUri("/example/ro/dir/", "a/b/d/"), rdflib.URIRef("file:///example/ro/dir/a/b/d/")) self.assertEquals(ro_manifest.getComponentUri("ro/dir", "a/b/d/"), rdflib.URIRef("file://%s/ro/dir/a/b/d/"%(cwd))) self.assertEquals(ro_manifest.getComponentUri("ro/dir/", "a/b/d/"), rdflib.URIRef("file://%s/ro/dir/a/b/d/"%(cwd))) return
def testManifestContent(self): """ Test content of newly created manifest """ rodir = self.createTestRo(testbase, "data/ro-test-1", "RO test annotation", "ro-testRoAnnotate") ro_graph = ro_manifest.readManifestGraph(rodir) self.checkManifestGraph(rodir, ro_graph) s = ro_manifest.getComponentUri(rodir, "") g = rdflib.Graph() g.add( (s, RDF.type, RO.ResearchObject ) ) g.add( (s, DCTERMS.creator, rdflib.Literal(ro_test_config.ROBOXUSERNAME) ) ) g.add( (s, DCTERMS.description, rdflib.Literal("RO test annotation") ) ) g.add( (s, DCTERMS.title, rdflib.Literal("RO test annotation") ) ) g.add( (s, DCTERMS.identifier, rdflib.Literal("ro-testRoAnnotate") ) ) self.checkManifestGraph(rodir, g) self.deleteTestRo(rodir) return
def testAddAggregatedResources(self): """ Test function that adds aggregated resources to a research object manifest """ rodir = self.createTestRo(testbase, "data/ro-test-1", "RO test annotation", "ro-testRoAnnotate") ro_manifest.addAggregatedResources(rodir, rodir, recurse=True) def URIRef(path): return ro_manifest.getComponentUri(rodir, path) s = ro_manifest.getComponentUri(rodir, "") g = rdflib.Graph() g.add( (s, RDF.type, RO.ResearchObject ) ) g.add( (s, ORE.aggregates, URIRef("README-ro-test-1") ) ) g.add( (s, ORE.aggregates, URIRef("subdir1/subdir1-file.txt") ) ) g.add( (s, ORE.aggregates, URIRef("subdir2/subdir2-file.txt") ) ) self.checkManifestGraph(rodir, g) self.deleteTestRo(rodir) return
def testAddGetAllAnnotations(self): rodir = self.createTestRo(testbase, "data/ro-test-1", "Test get all annotations", "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) # Retrieve the file anotations annotations = ro_annotation.getAllAnnotations(rodir) rouri = ro_manifest.getRoUri(rodir) resourceuri = ro_manifest.getComponentUri(rodir, roresource) log.debug("resourceuri: %s"%(resourceuri)) expected_annotations = ( [ (rouri, DCTERMS.description, rdflib.Literal('Test get all annotations')) , (rouri, DCTERMS.title, rdflib.Literal('Test get all annotations')) , (rouri, DCTERMS.created, rdflib.Literal('unknown')) , (rouri, DCTERMS.creator, rdflib.Literal('Test User')) , (rouri, DCTERMS.identifier, rdflib.Literal('ro-testRoAnnotate')) , (rouri, RDF.type, RO.ResearchObject) , (resourceuri, DCTERMS.type, rdflib.Literal('Test file')) , (resourceuri, DCTERMS.description, rdflib.Literal('File in test research object')) , (resourceuri, ROTERMS.note, rdflib.Literal('Research object file created for annotation testing')) , (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(12+1+6): # Annotations + aggregations next = annotations.next() #log.debug("Next %s"%(repr(next))) if ( next not in expected_annotations and next[1] != DCTERMS.created and next[1] != ORE.aggregates ): self.assertTrue(False, "Not expected (%d) %s"%(i, repr(next))) self.assertRaises(StopIteration, annotations.next) self.deleteTestRo(rodir) return
def testCreateReadRoAnnotationBody(self): """ Test function to create simple annotation body """ rodir = self.createTestRo(testbase, "data/ro-test-1", "RO test annotation", "ro-testRoAnnotate") roresource = "." attrdict = { "type": rdflib.Literal("Research Object"), # @@TODO: handle lists "keywords": ["test", "research", "object"], "description": rdflib.Literal("Test research object"), "format": rdflib.Literal("application/vnd.wf4ever.ro"), "note": rdflib.Literal("Research object created for annotation testing"), "title": rdflib.Literal("Test research object"), "created": rdflib.Literal("2011-12-07") } annotationfilebase = ro_annotation.createAnnotationBody( ro_config, rodir, roresource, attrdict) # Ann-%04d%02d%02d-%04d-%s.rdf self.assertRegexpMatches(annotationfilebase, r"Ann-\d\d\d\d\d\d\d\d-\d+-RO_test_annotation\.rdf", msg="Unexpected filename form for annotation: "+annotationfilebase) annotationfilename = ro_annotation.makeAnnotationFilename(rodir, annotationfilebase) annotationgraph = ro_annotation.readAnnotationBody(rodir, annotationfilename) attrpropdict = { "type": DCTERMS.type, # @@TODO "keywords": DCTERMS.subject, "description": DCTERMS.description, "format": DCTERMS.format, "note": ROTERMS.note, "title": DCTERMS.title, "created": DCTERMS.created } s = ro_manifest.getComponentUri(rodir, roresource) log.debug("annotation subject %s"%repr(s)) for k in attrpropdict: p = attrpropdict[k] log.debug("annotation predicate %s"%repr(p)) v = attrdict[k] a = annotationgraph.value(s, p, None) log.debug("annotation value %s"%repr(a)) #self.assertEqual(len(a), 1, "Singleton result expected") self.assertEqual(a, v) 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 getConstraint(minimgraph, rouri, target_ref, purpose_regex_string): """ Find constraint matching supplied RO, target and purpose regex Constraint is returned with: targetro_actual -> URI of resource targetres_actual -> URI of target if supplied, else subject of minium:hasConstraint """ def mkstr(u): return u and str(u) log.debug("getConstraint: rouri %s, target_ref %s"%(rouri, target_ref)) target = target_ref and ro_manifest.getComponentUri(rouri, target_ref) log.debug(" target_uri %s"%(target)) purpose = purpose_regex_string and re.compile(purpose_regex_string) templatedict = {'targetro': urllib.unquote(str(rouri))} if target: # Allow use of {+targetres} in checklist target template: templatedict['targetres'] = urllib.unquote(str(target)) for c in getConstraints(minimgraph): log.debug("- test: target %s purpose %s"%(c['target'],c['purpose'])) log.debug("- purpose %s, c['purpose'] %s"%(purpose_regex_string, c['purpose'])) if not purpose or purpose.match(c['purpose']): c['targetro_actual'] = rouri c['targetres_actual'] = target or c['target'] if not target: # No target specified in request, match any (first) constraint return c if c['target'] == target: # Match explicit target specification (subject of minim:hasConstraint) return c log.debug("- target: %s, c['target_t']: %s"%(target, repr(c['target_t']))) if c['target_t'] and c['target_t'].value == "*": # Special case: wilcard ("*") template matches any target return c if target and c['target_t']: log.debug("- expand %s"%(uritemplate.expand(c['target_t'], templatedict))) if str(target) == uritemplate.expand(c['target_t'], templatedict): # Target matches expanded template from constraint description return c return None
def testCreateReadFileAnnotationBody(self): """ Test function to create simple annotation body """ rodir = self.createTestRo(testbase, "data/ro-test-1", "RO test annotation", "ro-testRoAnnotate") roresource = "subdir1/subdir1-file.txt" attrdict = { "type": "Test file", "description": "File in test research object", "note": "File in research object created for annotation testing", "title": "Test file in RO", "created": "2011-12-07" } annotationfilebase = ro_annotation.createAnnotationBody( ro_config, rodir, roresource, attrdict) # Ann-%04d%02d%02d-%04d-%s.rdf self.assertRegexpMatches(annotationfilebase, r"Ann-\d\d\d\d\d\d\d\d-\d+-subdir1-file\.txt\.rdf", msg="Unexpected filename form for annotation: "+annotationfilebase) annotationfilename = ro_annotation.makeAnnotationFilename(rodir, annotationfilebase) annotationgraph = ro_annotation.readAnnotationBody(rodir, annotationfilename) attrpropdict = { "type": DCTERMS.type, "description": DCTERMS.description, "note": ROTERMS.note, "title": DCTERMS.title, "created": DCTERMS.created } s = ro_manifest.getComponentUri(rodir, roresource) log.debug("annotation subject %s"%repr(s)) for k in attrpropdict: p = attrpropdict[k] log.debug("annotation predicate %s"%repr(p)) v = attrdict[k] a = annotationgraph.value(s, p, None) log.debug("annotation value %s"%repr(a)) #self.assertEqual(len(a), 1, "Singleton result expected") self.assertEqual(a, v) 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
def testUri(rodir, path, uristring): self.assertEquals(ro_manifest.getComponentUri(rodir, path), rdflib.URIRef(uristring)) return
def URIRef(path): return ro_manifest.getComponentUri(rodir, path)