Beispiel #1
0
def testBasic(DEBUG = False):
    from glob import glob
    from sre import sub
    for testFile in glob('data/examples/*.rq'):#glob('data/*/*.rq'):
        store = plugin.get(STORE,Store)()
        bootStrapStore(store)
        store.commit()

        prefix = testFile.split('.rq')[-1]
        manifestPath = '/'.join(testFile.split('/')[:-1]+['manifest.n3'])
        manifestPath2 = '/'.join(testFile.split('/')[:-1]+['manifest.ttl'])
        queryFileName = testFile.split('/')[-1]
        store = plugin.get(STORE,Store)()
        store.open(configString,create=False)
        assert len(store) == 0
        manifestG=ConjunctiveGraph(store)
        if not os.path.exists(manifestPath):
            assert os.path.exists(manifestPath2)
            manifestPath = manifestPath2
        manifestG.default_context.parse(open(manifestPath),publicID=TEST_BASE,format='n3')
        manifestData = \
           manifestG.query(
                                  PARSED_MANIFEST_QUERY,
                                  initBindings={'?query' : TEST_BASE[queryFileName]},
                                  initNs=manifestNS,
                                  DEBUG = False)
        store.rollback()
        store.close()
        for source,testCaseName,testCaseComment,expectedRT in manifestData:

            if expectedRT:
                expectedRT = '/'.join(testFile.split('/')[:-1]+[expectedRT.replace(TEST_BASE,'')])
            if source:
                source = '/'.join(testFile.split('/')[:-1]+[source.replace(TEST_BASE,'')])

            testCaseName = testCaseComment and testCaseComment or testCaseName
            print "## Source: %s ##"%source
            print "## Test: %s ##"%testCaseName
            print "## Result: %s ##"%expectedRT

            #Expected results
            if expectedRT:
                store = plugin.get(STORE,Store)()
                store.open(configString,create=False)
                resultG=ConjunctiveGraph(store).default_context
#                if DEBUG:
#                    print "###"*10
#                    print "parsing: ", open(expectedRT).read()
#                    print "###"*10
                assert len(store) == 0
                print "## Parsing (%s) ##"%(expectedRT)
                if not trialAndErrorRTParse(resultG,expectedRT,DEBUG):
                    if DEBUG:
                        print "Unexpected result format (for %s), skipping"%(expectedRT)
                    store.rollback()
                    store.close()
                    continue
                if DEBUG:
                    print "## Done .. ##"

                rtVars = [rtVar for rtVar in resultG.objects(None,RESULT_NS.resultVariable)]
                bindings = []
                resultSetNode = resultG.value(predicate=RESULT_NS.value,object=RESULT_NS.ResultSet)
                for solutionNode in resultG.objects(resultSetNode,RESULT_NS.solution):
                    bindingDict = dict([(key,None) for key in rtVars])
                    for bindingNode in resultG.objects(solutionNode,RESULT_NS.binding):
                        value = resultG.value(subject=bindingNode,predicate=RESULT_NS.value)
                        name  = resultG.value(subject=bindingNode,predicate=RESULT_NS.variable)
                        bindingDict[name] = value
                    bindings.append(tuple([bindingDict[vName] for vName in rtVars]))
                if DEBUG:
                    print "Expected bindings: ", bindings
                    print open(expectedRT).read()
                store.rollback()
                store.close()

            if testFile.startswith('data/NegativeSyntax'):
                try:
                    query = open(testFile).read()
                    p = Parse(query,DEBUG)
                except:
                    continue
                else:
                    raise Exception("Test %s should have failed!"%testFile)
            if testFile in tests2Skip:
                print "Skipping test (%s)"%testCaseName
                continue
            query = open(testFile).read()
            print "### %s (%s) ###"%(testCaseName,testFile)
            print query
            p = Parse(query,DEBUG_PARSE)
            if DEBUG:
                print p
            if EVALUATE and source:
                if DEBUG:
                    print "### Source Graph: ###"
                    print open(source).read()
                store = plugin.get(STORE,Store)()
                store.open(configString,create=False)
                g=ConjunctiveGraph(store)
                try:
                    g.parse(open(source),format='n3')
                except:
                    print "Unexpected data format (for %s), skipping"%(source)
                    store.rollback()
                    store.close()
                    continue
                #print store
                rt = g.query(p,DEBUG = DEBUG)
                if expectedRT:
                    if rt != bindings and Set([Set(i) for i in rt]) != Set([Set(i) for i in bindings]):#unorderedComparison(rt,bindings):
                        print "### Expected Result (%s) ###"%expectedRT
                        pprint(bindings)
                        print "### Actual Results ###"
                        pprint(rt)
                        raise Exception("### TEST FAILED!: %s ###"%testCaseName)
                    else:
                        print "### TEST PASSED!: %s ###"%testCaseName
                store.rollback()