예제 #1
0
class TestMapper(unittest.TestCase):
    """Test Mapper class"""
    def setUp(self):
        """ auto init"""
        self.mapper = Mapper()
        self.mapfile = 'testmap.yaml'
        udb = UnittestDB()
        metadata = udb.load_from_file('starting_db.yaml')
        self.mapper.load_mapfile(self.mapfile)
        self.mapper.validate_map(metadata)
        self.assertEqual(12, len(self.mapper.dict))
        
    def test_get_key(self):
        """test get_key"""
        self.assertTrue('dataset' == self.mapper.get_key('Files.Path'))
        self.assertFalse('Dataset' == self.mapper.get_key('Files.Path'))
        self.assertFalse('dataset' == self.mapper.get_key('files.path'))

    def test_get_column(self):
        """test get_column""" 
        self.assertTrue( \
          'ProcessedDataset.PhysicsGroup' == self.mapper.get_column('phygrp'))
        self.assertFalse( \
          'Block.Name' ==  self.mapper.get_column('Block'))
        self.assertFalse( \
          'FILES.Path' == self.mapper.get_column('dataset'))
        self.assertFalse( \
          'files.block' == self.mapper.get_column('file.block'))
예제 #2
0
 def setUp(self):
     """ auto init"""
     self.mapper = Mapper()
     self.mapfile = 'testmap.yaml'
     udb = UnittestDB()
     metadata = udb.load_from_file('starting_db.yaml')
     self.mapper.load_mapfile(self.mapfile)
     self.mapper.validate_map(metadata)
     self.assertEqual(12, len(self.mapper.dict))
예제 #3
0
 def set_mapper(self, mapfile='map.yaml'):
     """set mapper"""
     self.mapper = Mapper()
     self.mapper.load_mapfile(mapfile)
예제 #4
0
class App():
    """Application demo"""
    def __init__(self, verbose = 0):
        """initialize"""
        class MySchema(object):
            """class encapsulate tables structure"""
            def __init__(self, tables=None):
                """initialize """
                self.tables = tables

            def set_tables(self, tables):
                """set tables"""
                self.tables = tables
        self.schema = MySchema()
        self.manager = None
        self.db_name = None
        self.querybuilder = None
        self.url = None
        self.mapper = None

    def set_manager(self, url, alias):
        """set manager"""
        self.manager = DBManager()
        self.url = url
        self.db_name = alias
        
    def get_db_connection(self):
        """get db connection"""
        return self.manager.connect(self.url)    

    def close_db_connection(self):
        """close db connection"""
        return self.manager.close(self.db_name)
    
    def set_mapper(self, mapfile='map.yaml'):
        """set mapper"""
        self.mapper = Mapper()
        self.mapper.load_mapfile(mapfile)

    def set_querybuilder(self, schema_file=None):
        """set querybuilder"""
        metadata = MetaData()
        tables = None
        if schema_file:
            metadata = load_from_file(schema_file)
            tables = metadata.tables
            self.schema = metadata
        else: 
            tables = self.manager.load_tables(self.db_name)
            self.schema.set_tables(tables)
        self.querybuilder = Schema(tables)
           
    def parse_input(self, in_puts):
        """parse input"""
        return test_query_parser(self.mapper, in_puts)
    
    def generate_sqlalchemy_query(self, query):
        """generate sqlalcemy query"""
#    print type(process_dataset.c.Name)
#    print type(process_dataset.columns['Name'])
        return generate_query(self.schema, query)
    
    def build_query(self, query):
        """build query"""
#    print "query._raw_columns is ", select_test._raw_columns
#    print "query.inner_columns is ", [col for col in select_test.inner_columns]
#    print "query.froms is ", select_test.froms
#    print dir(select_test)
        return  self.querybuilder.build_query(query)

    def execute_query(self, query):
        """execute query"""
        try:
            result =  self.manager.execute(query)
            return result
        except Error:
            print Error
            return None