Пример #1
0
    def testSomething(self):
        registerStubs()

        e = OCQLEngine()
        rq = e.compile("[ c in ICourse | c ]")

        self.assert_(isinstance(rq, RunnableQuery))
Пример #2
0
    def testSomething(self):
        registerStubs()

        e = OCQLEngine()
        rq = e.compile("[ c in ICourse | c ]")

        self.assert_(isinstance(rq, RunnableQuery))
Пример #3
0
    def setUp(self):
        provideAdapter(QueryParser)
        provideAdapter(QueryNullParser)
        provideAdapter(QueryOptimizer)
        provideAdapter(Rewriter)
        provideAdapter(AlgebraOptimizer)
        provideAdapter(AlgebraCompiler)
        provideAdapter(TestMetadata)

        self.engine = OCQLEngine()
Пример #4
0
    def setUp(self):
        provideAdapter(QueryParser)
        provideAdapter(QueryNullParser)
        provideAdapter(QueryOptimizer)
        provideAdapter(Rewriter)
        provideAdapter(AlgebraOptimizer)
        provideAdapter(AlgebraCompiler)
        provideAdapter(Metadata)
        registerAdapters()
        setupInterfaces(self)
        setupCatalog(self)

        self.engine = OCQLEngine()
Пример #5
0
def console():
    utils.setupAdapters(None)
    provideAdapter(TestMetadata)

    engine = OCQLEngine()

    while True:
        inp = raw_input("ocql>")

        if inp == "bye" or inp == "quit":
            break

        try:
            print engine.compile(inp).execute()
        except Exception, e:
            print "Exception occurred: %s" % str(e)
Пример #6
0
def console():
    utils.setupAdapters(None)
    provideAdapter(TestMetadata)

    engine = OCQLEngine()

    while True:
        inp = raw_input("ocql>")

        if inp == "bye" or inp == "quit":
            break

        try:
            print engine.compile(inp).execute()
        except Exception, e:
            print "Exception occurred: %s" % str(e)
Пример #7
0
class testMetadata(unittest.TestCase):
    def setUp(self):
        setupAdapters(self)
        setupInterfaces(self)
        setupCatalog(self)

        self.engine = OCQLEngine()

    def test_metadata_reanalyze(self):
        #check to see how ReanalyzeRequired works
        metadata = IDB(None)

        symbols = SymbolContainer()
        #
        # Filtering --one result using optimization
        #
        # set [ c in IStudent , c.country="USA" | c.name]
        #
        query = "[c in IStudent , c.country=USA | c.name]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'IStudent')),
                Eq(metadata, symbols, Identifier(metadata, symbols,
                                                 'c.country'),
                   Identifier(metadata, symbols, '"USA"'))
            ], Identifier(metadata, symbols, 'c.name')))

        try:
            run = self.engine.compile(qo)
            self.assert_(
                'metadata.getFromIndex("IStudent", "country", "==", "USA"))' in
                run.code)

            self.delete_index('student_country')

            #no reanalyze here, raises exception
            result = run.execute(noretry=True)

            self.fail("ReanalyzeRequired expected")
        except ReanalyzeRequired:
            pass

        #reanalyze here, no exception, returns result
        result = run.execute()

        #code changes
        self.assert_('metadata.getAll("IStudent"))' in run.code)

    def delete_index(self, todel):
        metadata = IDB(None)
        catalogs = getUtilitiesFor(ICatalog)
        intids = getUtility(IIntIds)
        for name, catalog in catalogs:
            for iname, index in catalog.items():
                if iname == todel:
                    del catalog[iname]
Пример #8
0
class testOLD(unittest.TestCase):
    def setUp(self):
        provideAdapter(QueryParser)
        provideAdapter(QueryNullParser)
        provideAdapter(QueryOptimizer)
        provideAdapter(Rewriter)
        provideAdapter(AlgebraOptimizer)
        provideAdapter(AlgebraCompiler)
        provideAdapter(TestMetadata)
        registerAdapters()

        self.engine = OCQLEngine()

    def doone(self, query, qo, expected):
        print "==============="
        print "query:", query

        algebra_ = qo.rewrite(algebra)

        print "algebra:", algebra_

        code = algebra_.compile()
        compile(code, '<string>', 'eval')
        q = RunnableQuery(engine, algebra_, code)

        print "code:", code
        print "---------------"
        print "got:     ", q.execute()
        print "expected:", expected

    def doit(self, query, qo, expected):
        run = self.engine.compile(qo)
        result = run.execute()

        self.assertEqual(expected, result)

    def test_old(self):
        metadata = IDB(None)
        symbols = SymbolContainer()

        #
        # Simple empty query
        #
        # set [ ]
        #
        query = "set [ ]"
        qo = Head(
            Query(metadata, symbols, set, [],
                  Identifier(metadata, symbols, '')))

        self.doit(query, qo, set([]))

        symbols = SymbolContainer()
        #
        # Simple SELECT ALL
        #
        # set [ c in ICourse | c ]
        #
        query = "[ c in ICourse | c ]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'ICourse')),
            ], Identifier(metadata, symbols, 'c')))

        #caution, these here are object references
        self.doit(query, qo, set([C1, C2, C3]))

        symbols = SymbolContainer()
        #
        # Selecting a property
        #
        # set [ c in ICourse | c.code ]
        #
        query = "[ c in ICourse | c.code ]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'ICourse')),
            ], Identifier(metadata, symbols, 'c.code')))

        self.doit(query, qo, set(["C1", "C2", "C3"]))

        symbols = SymbolContainer()
        #
        # Filtering -- empty result
        #
        # set [ c in ICourse , c.credits>3 | c.code ]
        #
        query = "[ c in ICourse, c.credits>3 | c.code ]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'ICourse')),
                Gt(metadata, symbols, Identifier(metadata, symbols,
                                                 'c.credits'),
                   Constant(metadata, symbols, '3')),
            ], Identifier(metadata, symbols, 'c.code')))

        #from pub.dbgpclient import brk; brk('172.16.144.39')

        self.doit(query, qo, set([]))

        symbols = SymbolContainer()
        #
        # Filtering -- full result
        #
        # set [ c in ICourse , c.credits<=3 | c.code ]
        #
        query = "[ c in ICourse, c.credits<=3 | c.code ]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'ICourse')),
                Le(metadata, symbols, Identifier(metadata, symbols,
                                                 'c.credits'),
                   Constant(metadata, symbols, '3')),
            ], Identifier(metadata, symbols, 'c.code')))

        self.doit(query, qo, set(["C1", "C2", "C3"]))

        symbols = SymbolContainer()
        #
        # Filtering -- one result
        #
        # set [ c in ICourse , c.credits=3 | c.code ]
        #
        query = "[ c in ICourse, c.credits=3 | c.code ]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'ICourse')),
                Eq(metadata, symbols, Identifier(metadata, symbols,
                                                 'c.credits'),
                   Constant(metadata, symbols, '3')),
            ], Identifier(metadata, symbols, 'c.code')))

        self.doit(query, qo, set(["C2", "C3"]))

        symbols = SymbolContainer()
        #
        # Two filters -- full results
        #
        # set [ c in ICourse , c.credits<5, c.credits >=1  | c.code ]
        #
        query = "[ c in ICourse, c.credits<3, c.credits>=1 | c.code ]"
        qo = Head(
            Query(metadata, symbols, set,
                  [
                      In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                         Identifier(metadata, symbols, 'ICourse')),
                      Lt(metadata, symbols,
                         Identifier(metadata, symbols, 'c.credits'),
                         Constant(metadata, symbols, '5')),
                      Ge(metadata, symbols,
                         Identifier(metadata, symbols, 'c.credits'),
                         Constant(metadata, symbols, '1')),
                  ], Identifier(metadata, symbols, 'c.code')))

        self.doit(query, qo, set(["C1", "C2", "C3"]))

        symbols = SymbolContainer()
        #
        # Two filters -- one result
        #
        # set [ c in ICourse , c.credits<=2, 2<=c.credits  | c.code ]
        #
        query = "[ c in ICourse, c.credits<=2, 2<=c.credits | c.code ]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'ICourse')),
                Le(metadata, symbols, Identifier(metadata, symbols,
                                                 'c.credits'),
                   Constant(metadata, symbols, '2')),
                Le(metadata, symbols, Constant(metadata, symbols, '2'),
                   Identifier(metadata, symbols, 'c.credits')),
            ], Identifier(metadata, symbols, 'c.code')))

        self.doit(query, qo, set(["C1"]))

        symbols = SymbolContainer()
        #
        # Two filters -- one result
        #
        # set [ c in ICourse , c.credits>=2, 2>=c.credits  | c.code ]
        #
        query = "[ c in ICourse, c.credits>=2, 2>=c.credits | c.code ]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'ICourse')),
                Ge(metadata, symbols, Identifier(metadata, symbols,
                                                 'c.credits'),
                   Constant(metadata, symbols, '2')),
                Ge(metadata, symbols, Constant(metadata, symbols, '2'),
                   Identifier(metadata, symbols, 'c.credits')),
            ], Identifier(metadata, symbols, 'c.code')))

        self.doit(query, qo, set(["C1"]))

        symbols = SymbolContainer()
        #
        # Two filters -- no result
        #
        # set [ c in ICourse , c.credits=3, c.credits!=3  | c.code ]
        #
        query = "[ c in ICourse, c.credits=3, c.credits!=3 | c.code ]"
        qo = Head(
            Query(metadata, symbols, set,
                  [
                      In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                         Identifier(metadata, symbols, 'ICourse')),
                      Eq(metadata, symbols,
                         Identifier(metadata, symbols, 'c.credits'),
                         Constant(metadata, symbols, '3')),
                      Ne(metadata, symbols,
                         Identifier(metadata, symbols, 'c.credits'),
                         Constant(metadata, symbols, '3')),
                  ], Identifier(metadata, symbols, 'c.code')))

        self.doit(query, qo, set([]))

        symbols = SymbolContainer()
        #
        # join -- Departments running curses
        #
        # set [ c in ICourse d, in IDepartments ,
        # some c.runBy = d  | d.name ]
        #
        query = "[ c in ICourse, d in IDepartments, d = some c.runBy | d.name  ]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'ICourse')),
                In(metadata, symbols, Identifier(metadata, symbols, 'd'),
                   Identifier(metadata, symbols, 'IDepartments')),
                Eq(
                    metadata, symbols, Identifier(metadata, symbols, 'd'),
                    Quanted(
                        metadata, symbols, Some(metadata, symbols, ''),
                        Property(metadata, symbols,
                                 Identifier(metadata, symbols, 'c'),
                                 Identifier(metadata, symbols, 'runBy')))),
            ], Identifier(metadata, symbols, 'd.name')))

        self.doit(query, qo, set(['Other department', 'Computing Science']))

        symbols = SymbolContainer()
        #
        # join -- Departments running some 3 credits curses
        #
        # set [ d in ICourse, c in ICourse, c.credits=3, some c.runBy = d | d.name ]
        #
        query = "[ c in ICourse, d in IDepartments, c.credits=3, d = some c.runBy | d.name  ]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'ICourse')),
                In(metadata, symbols, Identifier(metadata, symbols, 'd'),
                   Identifier(metadata, symbols, 'IDepartments')),
                Eq(metadata, symbols, Identifier(metadata, symbols,
                                                 'c.credits'),
                   Constant(metadata, symbols, '3')),
                Eq(
                    metadata, symbols, Identifier(metadata, symbols, 'd'),
                    Quanted(
                        metadata, symbols, Some(metadata, symbols, ''),
                        Property(metadata, symbols,
                                 Identifier(metadata, symbols, 'c'),
                                 Identifier(metadata, symbols, 'runBy')))),
            ], Identifier(metadata, symbols, 'd.name')))

        self.doit(query, qo, set(['Computing Science']))

        symbols = SymbolContainer()
        # join -- Departments running some not 3 credits curses
        #
        # [ d in IDepartments, c in ICourse, some c.runBy = d, some c.credits != 3| d.name ]
        #
        query = """[ d in IDepartments,
        c in ICourse,
        some c.runBy = d, c.credits != 3| d.name ]"""
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'd'),
                   Identifier(metadata, symbols, 'IDepartments')),
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'ICourse')),
                Eq(
                    metadata, symbols, Identifier(metadata, symbols, 'd'),
                    Quanted(
                        metadata, symbols, Some(metadata, symbols, ''),
                        Property(metadata, symbols,
                                 Identifier(metadata, symbols, 'c'),
                                 Identifier(metadata, symbols, 'runBy')))),
                Ne(metadata, symbols, Constant(metadata, symbols, '3'),
                   Identifier(metadata, symbols, 'c.credits')),
            ], Identifier(metadata, symbols, 'd.name')))

        self.doit(query, qo, set(['Other department', 'Computing Science']))

        symbols = SymbolContainer()
        #
        #
        # join -- Departments running just 2 credits curses
        #
        # set [ d in IDepartments, every set [ c in ICourse, some c.runBy = d | c.credits ] = 3  | d.name ]
        #
        query = """set [ d in IDepartments,
            every
            set [ c in ICourse, some c.runBy = d | c.credits ] = 2
            | d.name ]"""
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'd'),
                   Identifier(metadata, symbols, 'IDepartments')),
                Eq(
                    metadata, symbols,
                    Quanted(
                        metadata, symbols, Every(metadata, symbols, ''),
                        Query(metadata, symbols, set, [
                            In(metadata, symbols,
                               Identifier(metadata, symbols, 'c'),
                               Identifier(metadata, symbols, 'ICourse')),
                            Eq(
                                metadata, symbols,
                                Identifier(metadata, symbols, 'd'),
                                Quanted(
                                    metadata, symbols,
                                    Some(metadata, symbols, ''),
                                    Property(
                                        metadata, symbols,
                                        Identifier(metadata, symbols, 'c'),
                                        Identifier(metadata, symbols,
                                                   'runBy')))),
                        ], Identifier(metadata, symbols, 'c.credits'))),
                    Constant(metadata, symbols, '2')),
            ], Identifier(metadata, symbols, 'd.name')))

        self.doit(query, qo, set(['Other department']))
Пример #9
0
    def setUp(self):
        setupAdapters(self)
        setupInterfaces(self)
        setupCatalog(self)

        self.engine = OCQLEngine()
Пример #10
0
class testZope(unittest.TestCase):
    def setUp(self):
        setupAdapters(self)
        setupInterfaces(self)
        setupCatalog(self)

        self.engine = OCQLEngine()

    #just copy following methods from test_old
    def doone(self, query, qo, expected):
        print "==============="
        print "query:", query

        algebra_ = qo.rewrite(algebra)

        print "algebra:", algebra_

        code = algebra_.compile()
        compile(code, '<string>', 'eval')
        q = RunnableQuery(engine, algebra_, code)

        print "code:", code
        print "---------------"
        print "got:     ", q.execute()
        print "expected:", expected

    def doit(self, query, qo, expected):
        run = self.engine.compile(qo)
        result = run.execute()

        self.assertEqual(expected, result)

    def test_gsoc(self):
        metadata = IDB(None)
        symbols = SymbolContainer()

        #
        # Simple empty query
        #
        # set [ ]
        #
        query = "set [ ]"
        qo = Head(
            Query(metadata, symbols, set, [],
                  Identifier(metadata, symbols, '')))

        self.doit(query, qo, set([]))

        symbols = SymbolContainer()
        #
        # Simple empty query
        #
        # list [ ]
        #
        query = "list [ ]"
        qo = Head(
            Query(metadata, symbols, list, [],
                  Identifier(metadata, symbols, '')))

        self.doit(query, qo, [])

        symbols = SymbolContainer()
        #
        # Simple SELECT ALL
        #
        # set [ c in IStudent | c ]
        #
        query = "set [c in IStudent | c]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'IStudent'))
            ], Identifier(metadata, symbols, 'c')))

        self.doit(query, qo, set(metadata.getAll('IStudent')))

        symbols = SymbolContainer()
        #
        # Selecting a property
        #
        # set [ c in IStudent | c.name ]
        #
        query = "set [c in IStudent | c.name]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'IStudent'))
            ], Identifier(metadata, symbols, 'c.name')))
        self.doit(query, qo, set(["Charith", "Jane", "Ann", "Stewart"]))

        symbols = SymbolContainer()
        #
        # Filtering --one result
        #
        # set [ c in IProject; c.description="test" | c.name]
        #
        query = "set [c in IProject; c.description==test | c.name]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'IProject')),
                Eq(metadata, symbols,
                   Identifier(metadata, symbols, 'c.description'),
                   Identifier(metadata, symbols, '"test"'))
            ], Identifier(metadata, symbols, 'c.name')))

        self.doit(query, qo, set(["Save the world"]))

        symbols = SymbolContainer()
        #
        # Filtering --one result using optimization
        #
        # set [ c in IStudent; c.country="USA" | c.name]
        #
        query = "set [c in IStudent; c.country==USA | c.name]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'IStudent')),
                Eq(metadata, symbols, Identifier(metadata, symbols,
                                                 'c.country'),
                   Identifier(metadata, symbols, '"USA"'))
            ], Identifier(metadata, symbols, 'c.name')))

        self.doit(
            query, qo,
            set([
                metadata.getFromIndex('IStudent', 'country', '==',
                                      'USA')[0].name
            ]))

        symbols = SymbolContainer()
        #
        # Filtering --one result using optimization
        #
        # set [ c in IStudent; c.country!="USA" | c.name]
        #
        query = "[c in IStudent; c.country != USA | c.name]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'IStudent')),
                Ne(metadata, symbols, Identifier(metadata, symbols,
                                                 'c.country'),
                   Identifier(metadata, symbols, '"USA"'))
            ], Identifier(metadata, symbols, 'c.name')))

        self.doit(
            query, qo,
            set([
                i.name for i in metadata.getFromIndex('IStudent', 'country',
                                                      '!=', 'USA')
            ]))

        symbols = SymbolContainer()
        #
        # Filtering --one result using optimization
        #
        # set [ c in IStudent; c.country <= "Sri Lanka" | c.name]
        #
        query = "set [c in IStudent; c.country <= 'Sri Lanka' | c.name]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'IStudent')),
                Le(metadata, symbols, Identifier(metadata, symbols,
                                                 'c.country'),
                   Identifier(metadata, symbols, '"Sri Lanka"'))
            ], Identifier(metadata, symbols, 'c.name')))

        self.doit(
            query, qo,
            set([
                i.name for i in metadata.getFromIndex('IStudent', 'country',
                                                      '<=', 'Sri Lanka')
            ]))

        symbols = SymbolContainer()
        #
        # Filtering --one result using optimization
        #
        # set [ c in IStudent; c.country >= "Sri Lanka" | c.name]
        #
        query = "set [c in IStudent; c.country >= 'Sri Lanka' | c.name]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'IStudent')),
                Ge(metadata, symbols, Identifier(metadata, symbols,
                                                 'c.country'),
                   Identifier(metadata, symbols, '"Sri Lanka"'))
            ], Identifier(metadata, symbols, 'c.name')))

        self.doit(
            query, qo,
            set([
                i.name for i in metadata.getFromIndex('IStudent', 'country',
                                                      '>=', 'Sri Lanka')
            ]))

        symbols = SymbolContainer()
        #
        # Filtering --one result using optimization
        #
        # set [ c in IStudent; c.country < "Sri Lanka" | c.name]
        #
        query = "set [c in IStudent; c.country < 'Sri Lanka' | c.name]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'IStudent')),
                Lt(metadata, symbols, Identifier(metadata, symbols,
                                                 'c.country'),
                   Identifier(metadata, symbols, '"Sri Lanka"'))
            ], Identifier(metadata, symbols, 'c.name')))

        self.doit(
            query, qo,
            set([
                i.name for i in metadata.getFromIndex('IStudent', 'country',
                                                      '<', 'Sri Lanka')
            ]))

        symbols = SymbolContainer()
        #
        # Filtering --one result using optimization
        #
        # set [ c in IStudent; c.country > "Sri Lanka" | c.name]
        #
        query = "set [c in IStudent; c.country > 'Sri Lanka' | c.name]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'IStudent')),
                Gt(metadata, symbols, Identifier(metadata, symbols,
                                                 'c.country'),
                   Identifier(metadata, symbols, '"Sri Lanka"'))
            ], Identifier(metadata, symbols, 'c.name')))

        self.doit(
            query, qo,
            set([
                i.name for i in metadata.getFromIndex('IStudent', 'country',
                                                      '>', 'Sri Lanka')
            ]))

        symbols = SymbolContainer()
        #
        #
        # join -- Mentor who is mentoring Hungary student
        #
        # set [ m in IMentor; every set [ s in IStudent; some s.mentor == m | s.country ] == Hungary  | m.name ]
        #
        query = """set [ m in IMentor;
            every
            set [ s in IStudent; some s.mentor == m; s.country == Hungary | s.name] == Stewart
            | m.name ]"""
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'm'),
                   Identifier(metadata, symbols, 'IMentor')),
                Eq(
                    metadata, symbols,
                    Quanted(
                        metadata, symbols, Every(metadata, symbols, ''),
                        Query(metadata, symbols, set, [
                            In(metadata, symbols,
                               Identifier(metadata, symbols, 's'),
                               Identifier(metadata, symbols, 'IStudent')),
                            Eq(metadata, symbols,
                               Identifier(metadata, symbols, 's.country'),
                               Identifier(metadata, symbols, '"Hungary"')),
                            Eq(
                                metadata, symbols,
                                Identifier(metadata, symbols, 'm'),
                                Quanted(
                                    metadata, symbols,
                                    Some(metadata, symbols, ''),
                                    Property(
                                        metadata, symbols,
                                        Identifier(metadata, symbols, 's'),
                                        Identifier(metadata, symbols,
                                                   'mentor'))))
                        ], Identifier(metadata, symbols, 's.name'))),
                    Constant(metadata, symbols, 'Stewart')),
            ], Identifier(metadata, symbols, 'm.name')))

        self.doit(query, qo, set(['John Doe']))
Пример #11
0
class testZope(unittest.TestCase):
    def setUp(self):
        provideAdapter(QueryParser)
        provideAdapter(QueryNullParser)
        provideAdapter(QueryOptimizer)
        provideAdapter(Rewriter)
        provideAdapter(AlgebraOptimizer)
        provideAdapter(AlgebraCompiler)
        provideAdapter(Metadata)
        registerAdapters()
        setupInterfaces(self)
        setupCatalog(self)

        self.engine = OCQLEngine()

    #just copy following methods from test_old
    def doone(self, query, qo, expected):
        print "==============="
        print "query:", query

        algebra_ = qo.rewrite(algebra)

        print "algebra:", algebra_

        code = algebra_.compile()
        compile(code, '<string>', 'eval')
        q = RunnableQuery(engine, algebra_, code)

        print "code:", code
        print "---------------"
        print "got:     ", q.execute()
        print "expected:", expected

    def doit(self, query, qo, expected):
        run = self.engine.compile(qo)
        result = run.execute()

        self.assertEqual(expected, result)

    def test_gsoc(self):
        metadata = IDB(None)
        symbols = SymbolContainer()

        #
        # Simple empty query
        #
        # set [ ]
        #
        query = "set [ ]"
        qo = Head(
            Query(metadata, symbols, set, [],
                  Identifier(metadata, symbols, '')))

        self.doit(query, qo, set([]))

        symbols = SymbolContainer()
        #
        # Simple SELECT ALL
        #
        # set [ c in IStudent | c ]
        #
        query = "[c in IStudent | c]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'IStudent'))
            ], Identifier(metadata, symbols, 'c')))

        self.doit(query, qo, set(metadata.getAll('IStudent')))

        symbols = SymbolContainer()
        #
        # Selecting a property
        #
        # set [ c in IStudent | c.name ]
        #
        query = "[c in IStudent | c.name]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'IStudent'))
            ], Identifier(metadata, symbols, 'c.name')))
        self.doit(query, qo, set(["Charith", "Jane", "Ann"]))

        symbols = SymbolContainer()
        #
        # Filtering --one result
        #
        # set [ c in IProject , c.description="test" | c.name]
        #
        query = "[c in IProject , c.description=test | c.name]"
        qo = Head(
            Query(metadata, symbols, set, [
                In(metadata, symbols, Identifier(metadata, symbols, 'c'),
                   Identifier(metadata, symbols, 'IProject')),
                Eq(metadata, symbols,
                   Identifier(metadata, symbols, 'c.description'),
                   Identifier(metadata, symbols, '"test"'))
            ], Identifier(metadata, symbols, 'c.name')))

        self.doit(query, qo, set(["Save the world"]))
Пример #12
0
    def setUp(self):
        setupAdapters(self)
        setupInterfaces(self)
        setupCatalog(self)

        self.engine = OCQLEngine()
Пример #13
0
class testZope(unittest.TestCase):
    def setUp(self):
        setupAdapters(self)
        setupInterfaces(self)
        setupCatalog(self)

        self.engine = OCQLEngine()

    #just copy following methods from test_old
    def doone(self, query, qo, expected):
        print "==============="
        print "query:",query

        algebra_=qo.rewrite(algebra)

        print "algebra:",algebra_

        code=algebra_.compile();
        compile(code,'<string>','eval')
        q = RunnableQuery(engine,algebra_,code)

        print "code:",code
        print "---------------"
        print "got:     ", q.execute()
        print "expected:", expected

    def doit(self, query, qo, expected):
        run = self.engine.compile(qo)
        result = run.execute()

        self.assertEqual(expected, result)


    def test_gsoc(self):
        metadata = IDB(None)
        symbols = SymbolContainer()

        #
        # Simple empty query
        #
        # set [ ]
        #
        query = "set [ ]"
        qo=Head(Query(metadata, symbols,
                 set,
                 [] ,
                 Identifier(metadata, symbols,
                            '') ))

        self.doit(query, qo, set([]))


        symbols = SymbolContainer()
        #
        # Simple empty query
        #
        # list [ ]
        #
        query = "list [ ]"
        qo=Head(Query(metadata, symbols,
                 list,
                 [] ,
                 Identifier(metadata, symbols,
                            '') ))

        self.doit(query, qo, [])


        symbols = SymbolContainer()
        #
        # Simple SELECT ALL
        #
        # set [ c in IStudent | c ]
        #
        query = "set [c in IStudent | c]"
        qo = Head(Query(
                metadata, symbols,
                set,
                [
                    In(
                       metadata, symbols,
                       Identifier(metadata,symbols,'c'),
                       Identifier(metadata,symbols,'IStudent'))
                ], Identifier(metadata,symbols,'c')))

        self.doit(query, qo, set(metadata.getAll('IStudent')))


        symbols = SymbolContainer()
        #
        # Selecting a property
        #
        # set [ c in IStudent | c.name ]
        #
        query = "set [c in IStudent | c.name]"
        qo = Head(Query(
                   metadata, symbols,
                   set,
                   [
                        In(
                           metadata, symbols,
                           Identifier(metadata, symbols,'c'),
                           Identifier(metadata, symbols, 'IStudent'))
                    ],Identifier(metadata, symbols, 'c.name')))
        self.doit(query, qo, set(["Charith", "Jane", "Ann", "Stewart"]))


        symbols = SymbolContainer()
        #
        # Filtering --one result
        #
        # set [ c in IProject; c.description="test" | c.name]
        #
        query = "set [c in IProject; c.description==test | c.name]"
        qo = Head(Query(
                   metadata, symbols,
                   set,
                   [
                        In(
                           metadata, symbols,
                           Identifier(metadata,symbols,'c'),
                           Identifier(metadata,symbols, 'IProject')),
                        Eq(
                           metadata,symbols,
                           Identifier(metadata, symbols, 'c.description'),
                           Identifier(metadata, symbols, '"test"'))
                   ], Identifier(metadata, symbols, 'c.name')))

        self.doit(query, qo, set(["Save the world"]))


        symbols = SymbolContainer()
        #
        # Filtering --one result using optimization
        #
        # set [ c in IStudent; c.country="USA" | c.name]
        #
        query = "set [c in IStudent; c.country==USA | c.name]"
        qo = Head(Query(
                   metadata, symbols,
                   set,
                   [
                        In(
                           metadata, symbols,
                           Identifier(metadata,symbols,'c'),
                           Identifier(metadata,symbols, 'IStudent')),
                        Eq(
                           metadata,symbols,
                           Identifier(metadata, symbols, 'c.country'),
                           Identifier(metadata, symbols, '"USA"'))
                   ], Identifier(metadata, symbols, 'c.name')))

        self.doit(query, qo, set([metadata.getFromIndex('IStudent', 'country','==', 'USA')[0].name]))


        symbols = SymbolContainer()
        #
        # Filtering --one result using optimization
        #
        # set [ c in IStudent; c.country!="USA" | c.name]
        #
        query = "[c in IStudent; c.country != USA | c.name]"
        qo = Head(Query(
                   metadata, symbols,
                   set,
                   [
                        In(
                           metadata, symbols,
                           Identifier(metadata,symbols,'c'),
                           Identifier(metadata,symbols, 'IStudent')),
                        Ne(
                           metadata,symbols,
                           Identifier(metadata, symbols, 'c.country'),
                           Identifier(metadata, symbols, '"USA"'))
                   ], Identifier(metadata, symbols, 'c.name')))

        self.doit(query, qo, set([i.name for i in metadata.getFromIndex('IStudent', 'country','!=', 'USA')]))


        symbols = SymbolContainer()
        #
        # Filtering --one result using optimization
        #
        # set [ c in IStudent; c.country <= "Sri Lanka" | c.name]
        #
        query = "set [c in IStudent; c.country <= 'Sri Lanka' | c.name]"
        qo = Head(Query(
                   metadata, symbols,
                   set,
                   [
                        In(
                           metadata, symbols,
                           Identifier(metadata,symbols,'c'),
                           Identifier(metadata,symbols, 'IStudent')),
                        Le(
                           metadata,symbols,
                           Identifier(metadata, symbols, 'c.country'),
                           Identifier(metadata, symbols, '"Sri Lanka"'))
                   ], Identifier(metadata, symbols, 'c.name')))

        self.doit(query, qo, set([i.name for i in metadata.getFromIndex('IStudent', 'country','<=', 'Sri Lanka')]))


        symbols = SymbolContainer()
        #
        # Filtering --one result using optimization
        #
        # set [ c in IStudent; c.country >= "Sri Lanka" | c.name]
        #
        query = "set [c in IStudent; c.country >= 'Sri Lanka' | c.name]"
        qo = Head(Query(
                   metadata, symbols,
                   set,
                   [
                        In(
                           metadata, symbols,
                           Identifier(metadata,symbols,'c'),
                           Identifier(metadata,symbols, 'IStudent')),
                        Ge(
                           metadata,symbols,
                           Identifier(metadata, symbols, 'c.country'),
                           Identifier(metadata, symbols, '"Sri Lanka"'))
                   ], Identifier(metadata, symbols, 'c.name')))

        self.doit(query, qo, set([i.name for i in metadata.getFromIndex('IStudent', 'country','>=', 'Sri Lanka')]))


        symbols = SymbolContainer()
        #
        # Filtering --one result using optimization
        #
        # set [ c in IStudent; c.country < "Sri Lanka" | c.name]
        #
        query = "set [c in IStudent; c.country < 'Sri Lanka' | c.name]"
        qo = Head(Query(
                   metadata, symbols,
                   set,
                   [
                        In(
                           metadata, symbols,
                           Identifier(metadata,symbols,'c'),
                           Identifier(metadata,symbols, 'IStudent')),
                        Lt(
                           metadata,symbols,
                           Identifier(metadata, symbols, 'c.country'),
                           Identifier(metadata, symbols, '"Sri Lanka"'))
                   ], Identifier(metadata, symbols, 'c.name')))

        self.doit(query, qo, set([i.name for i in metadata.getFromIndex('IStudent', 'country','<', 'Sri Lanka')]))


        symbols = SymbolContainer()
        #
        # Filtering --one result using optimization
        #
        # set [ c in IStudent; c.country > "Sri Lanka" | c.name]
        #
        query = "set [c in IStudent; c.country > 'Sri Lanka' | c.name]"
        qo = Head(Query(
                   metadata, symbols,
                   set,
                   [
                        In(
                           metadata, symbols,
                           Identifier(metadata,symbols,'c'),
                           Identifier(metadata,symbols, 'IStudent')),
                        Gt(
                           metadata,symbols,
                           Identifier(metadata, symbols, 'c.country'),
                           Identifier(metadata, symbols, '"Sri Lanka"'))
                   ], Identifier(metadata, symbols, 'c.name')))

        self.doit(query, qo, set([i.name for i in metadata.getFromIndex('IStudent', 'country','>', 'Sri Lanka')]))


        symbols = SymbolContainer()
        #
        #
        # join -- Mentor who is mentoring Hungary student
        #
        # set [ m in IMentor; every set [ s in IStudent; some s.mentor == m | s.country ] == Hungary  | m.name ]
        #
        query = """set [ m in IMentor;
            every
            set [ s in IStudent; some s.mentor == m; s.country == Hungary | s.name] == Stewart
            | m.name ]"""
        qo=Head(Query(
            metadata, symbols,
            set,
            [
                In(
                    metadata, symbols,
                    Identifier(metadata, symbols,'m'),
                    Identifier(metadata, symbols,'IMentor')),
                Eq(
                    metadata, symbols,
                    Quanted(
                        metadata, symbols,
                        Every(metadata, symbols, ''),
                        Query(
                            metadata, symbols,
                            set,
                            [
                                In(
                                    metadata, symbols,
                                    Identifier(metadata, symbols,'s'),
                                    Identifier(metadata, symbols,'IStudent')),
                                Eq(
                                    metadata,symbols,
                                    Identifier(metadata, symbols, 's.country'),
                                    Identifier(metadata, symbols, '"Hungary"')),
                                Eq(
                                    metadata, symbols,
                                    Identifier(metadata, symbols,'m'),
                                    Quanted(
                                        metadata, symbols,
                                        Some(metadata, symbols, ''),
                                        Property(metadata, symbols,
                                            Identifier(metadata, symbols, 's'),
                                            Identifier(metadata, symbols, 'mentor'))
                                        ))
                            ], Identifier(metadata, symbols, 's.name'))
                    ),Constant(metadata, symbols,'Stewart')),
            ] ,Identifier(metadata, symbols,'m.name')))

        self.doit(query, qo, set(['John Doe']))
Пример #14
0
    def setUp(self):
        setupAdapters(self)
        provideAdapter(TestMetadata)

        self.engine = OCQLEngine()
Пример #15
0
class testMetadata(unittest.TestCase):
    def setUp(self):
        setupAdapters(self)
        setupInterfaces(self)
        setupCatalog(self)

        self.engine = OCQLEngine()

    def test_metadata_reanalyze(self):
        #check to see how ReanalyzeRequired works
        metadata = IDB(None)

        symbols = SymbolContainer()
        #
        # Filtering --one result using optimization
        #
        # set [ c in IStudent , c.country="USA" | c.name]
        #
        query = "[c in IStudent , c.country=USA | c.name]"
        qo = Head(Query(
                   metadata, symbols,
                   set,
                   [
                        In(
                           metadata, symbols,
                           Identifier(metadata,symbols,'c'),
                           Identifier(metadata,symbols, 'IStudent')),
                        Eq(
                           metadata,symbols,
                           Identifier(metadata, symbols, 'c.country'),
                           Identifier(metadata, symbols, '"USA"'))
                   ], Identifier(metadata, symbols, 'c.name')))

        try:
            run = self.engine.compile(qo)
            self.assert_('metadata.getFromIndex("IStudent", "country", "==", "USA"))' in run.code)

            self.delete_index('student_country')

            #no reanalyze here, raises exception
            result = run.execute(noretry=True)

            self.fail("ReanalyzeRequired expected")
        except ReanalyzeRequired:
            pass

        #reanalyze here, no exception, returns result
        result = run.execute()

        #code changes
        self.assert_('metadata.getAll("IStudent"))' in run.code)



    def delete_index(self, todel):
        metadata = IDB(None)
        catalogs = getUtilitiesFor(ICatalog)
        intids = getUtility(IIntIds)
        for name, catalog in catalogs:
            for iname, index in catalog.items():
                if iname == todel:
                    del catalog[iname]