Ejemplo n.º 1
0
    def test_inspection(self):
        
        from dblayer.backend.postgresql import inspector
        
        dsn = ''
        
        db = inspector.DatabaseInspector()
        #database_class = db.inspect(test_constants.TEST_DSN, 'InspectedDatabase')
        database_class = db.inspect(dsn, 'InspectedDatabase')
        self.assert_(issubclass(database_class, dblayer.model.database.Database))
        
        source = database_class.pretty_format_class()
        
        inspected_model_path = os.path.join(MODEL_DIR, 'inspected_model.py')
        with open(inspected_model_path, 'wt') as inspected_model_file:
            inspected_model_file.write(source)
            
        from dblayer.test import inspected_model
        
        inspected_database = inspected_model.InspectedDatabase
        self.assert_(inspected_database, dblayer.model.database.Database)
        
        for table in inspected_database._table_list:
            self.assertIsInstance(table, dblayer.model.table.Table)

        model.generate(
            module_path='inspected_abstraction.py', 
            database_model_class=inspected_database,
            abstraction_class_name='InspectedDatabase')

        from dblayer.test import inspected_abstraction
        
        db = inspected_abstraction.InspectedDatabase()
        with db.session(dsn):
            
            with db.transaction():
                self.load_data(db)
            with db.transaction():
                self.verify_data(db)
            with db.transaction():
                self.modify_data(db)

            for table in inspected_database._table_list:
                with db.transaction():
                    assert isinstance(table, dblayer.model.table.Table)
                    fn_get = getattr(db, 'get_%s' % table._table_name)
                    fn_find = getattr(db, 'find_%s' % table._table_name)
                    fn_update = getattr(db, 'update_%s' % table._table_name, None)
                    record1 = fn_find()
                    if record1 is None:
                        continue
                    fn_update(record1)
                    record2 = fn_get(id=record1.id)
                    if hasattr(record2, 'last_modified'):
                        record2.last_modified = record1.last_modified
                    self.assertEqual(record1, record2)
Ejemplo n.º 2
0
    def test_class_formatting(self):
        
        code = model.TestDatabaseModel.pretty_format_class()
        namespace = {}
        exec(code, namespace, namespace)
        database_model_class = namespace.get('TestDatabaseModel')
        self.assert_(issubclass(database_model_class, model.database.Database))
        
        with open('abstraction.py', 'rt') as module_file:
            old_source = module_file.read()
            
        model.generate(database_model_class=database_model_class)

        with open('abstraction.py', 'rt') as module_file:
            new_source = module_file.read()
            
        # Ignore the module docstring, since that contains a timestamp
        RX_TIMESTAMP = re.compile('\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}')
        old_source = RX_TIMESTAMP.subn('<TIMESTAMP>', old_source)[1]
        new_source = RX_TIMESTAMP.subn('<TIMESTAMP>', new_source)[1]
        
        self.assertEqual(old_source, new_source)
Ejemplo n.º 3
0
# -*- coding: utf8 -*-

import os, re, time, datetime, unittest

import dblayer
import dblayer.model.table
import dblayer.model.database
from dblayer.test import model

MODEL_DIR = os.path.dirname(model.__file__)

model.generate()
import abstraction

from dblayer import constants
from dblayer.backend.base import clauses
from dblayer.backend.postgresql import format
from dblayer.graph import gml

from dblayer.test import constants as test_constants

class TestGraph(unittest.TestCase):
    
    def testGML(self):
        model_instance = model.TestDatabaseModel('TestDatabase')
        exporter = gml.GMLExporter(model_instance)
        exporter.export('model.gml')

class TestAbstraction(unittest.TestCase):
    
    def setUp(self):