def test_doesnt_exist(self): if PY2: exc = ImportError else: exc = ModuleNotFoundError with ShouldRaise(exc): scan('test.package.nothere')
def test_doesnt_exist(self): if PY2: text = 'No module named package' else: text = "No module named 'test.package'" with ShouldRaise(ImportError(text)): scan('test.package.nothere')
def test_doesnt_exist(self): if PY2: text = 'No module named package' exc = ImportError else: text = "No module named 'test.package'" exc = ModuleNotFoundError with ShouldRaise(exc(text)): scan('test.package.nothere')
def test_ignore_imports_from_other_modules(self): self.dir.write('package0/__init__.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer class Model1(declarative_base()): __tablename__ = 'table1' id = Column('id', Integer, primary_key=True) """) self.dir.write('package1/__init__.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer class Model2(declarative_base()): __tablename__ = 'table2' id = Column('id', Integer, primary_key=True) from package0 import Model1 """) self.dir.write('package1/subpack/__init__.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer class Model3(declarative_base()): __tablename__ = 'table3' id = Column('id', Integer, primary_key=True) """) s = scan('package1') self.failUnless(isinstance(s, Source)) compare(['table2','table3'], sorted(s.metadata.tables.keys()))
def test_single_table_inheritance(self): # create module self.dir.write('somemodule.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer, String, MetaData # the base class BaseThing(declarative_base()): __tablename__ = 'table' id = Column('id', Integer, primary_key=True) # type 1 class Type1Thing(BaseThing): foo = Column('foo', Integer) # type 2 class Type2Thing(BaseThing): bar = Column('bar', Integer) """) s = scan('somemodule') self.failUnless(isinstance(s,Source)) compare(['table'], sorted(s.metadata.tables.keys())) # for fun: compare(['id', 'foo', 'bar'], s.metadata.tables['table'].c.keys())
def test_ignore_imports_from_other_modules(self): self.dir.write( 'package0/__init__.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer class Model1(declarative_base()): __tablename__ = 'table1' id = Column('id', Integer, primary_key=True) """) self.dir.write( 'package1/__init__.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer class Model2(declarative_base()): __tablename__ = 'table2' id = Column('id', Integer, primary_key=True) from package0 import Model1 """) self.dir.write( 'package1/subpack/__init__.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer class Model3(declarative_base()): __tablename__ = 'table3' id = Column('id', Integer, primary_key=True) """) s = scan('package1') self.failUnless(isinstance(s, Source)) compare(['table2', 'table3'], sorted(s.metadata.tables.keys()))
def test_single_table_inheritance(self): # create module self.dir.write( 'somemodule.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer, String, MetaData # the base class BaseThing(declarative_base()): __tablename__ = 'table' id = Column('id', Integer, primary_key=True) # type 1 class Type1Thing(BaseThing): foo = Column('foo', Integer) # type 2 class Type2Thing(BaseThing): bar = Column('bar', Integer) """) s = scan('somemodule') self.failUnless(isinstance(s, Source)) compare(['table'], sorted(s.metadata.tables.keys())) # for fun: compare(['id', 'foo', 'bar'], s.metadata.tables['table'].c.keys())
def test_module(self): # create module self.dir.write('somemodule.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer class User(declarative_base()): __tablename__ = 'user' id = Column('id', Integer, primary_key=True) """) s = scan('somemodule') self.failUnless(isinstance(s, Source)) compare(['user'], s.metadata.tables.keys())
def test_module(self): # create module self.dir.write( 'somemodule.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer class User(declarative_base()): __tablename__ = 'user' id = Column('id', Integer, primary_key=True) """) s = scan('somemodule') self.failUnless(isinstance(s, Source)) compare(['user'], s.metadata.tables.keys())
def test_type_of_things_to_scan_for(self): # create module self.dir.write('somemodule.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer, String, MetaData # a non-mapped old-style class class Bad1: pass # a non-mapped new-style class class Bad2(object): pass # table metadata = MetaData() table1 = Table('table1', metadata, Column('id', Integer, primary_key=True), ) # table that won't be found as not explicitly passed in metadata = MetaData() table2 = Table('table2', metadata, Column('id', Integer, primary_key=True), ) # declarative class Model3(declarative_base()): __tablename__ = 'table3' id = Column('id', Integer, primary_key=True) """) from somemodule import table1 s = scan('somemodule',tables=[table1]) self.failUnless(isinstance(s, Source)) compare(['table1', 'table3'], sorted(s.metadata.tables.keys()))
def test_type_of_things_to_scan_for(self): # create module self.dir.write( 'somemodule.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer, String, MetaData # a non-mapped old-style class class Bad1: pass # a non-mapped new-style class class Bad2(object): pass # table metadata = MetaData() table1 = Table('table1', metadata, Column('id', Integer, primary_key=True), ) # table that won't be found as not explicitly passed in metadata = MetaData() table2 = Table('table2', metadata, Column('id', Integer, primary_key=True), ) # declarative class Model3(declarative_base()): __tablename__ = 'table3' id = Column('id', Integer, primary_key=True) """) from somemodule import table1 s = scan('somemodule', tables=[table1]) self.failUnless(isinstance(s, Source)) compare(['table1', 'table3'], sorted(s.metadata.tables.keys()))
def test_package(self): # create package package_dir = self.dir.makedir('somepackage') self.dir.write('somepackage/__init__.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer class Table1(declarative_base()): __tablename__ = 'table1' id = Column('id', Integer, primary_key=True) """) self.dir.write('somepackage/table2.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer class Table2(declarative_base()): __tablename__ = 'table2' id = Column('id', Integer, primary_key=True) """) s = scan('somepackage') self.failUnless(isinstance(s, Source)) compare(['table1','table2'], sorted(s.metadata.tables.keys()))
def test_package(self): # create package package_dir = self.dir.makedir('somepackage') self.dir.write( 'somepackage/__init__.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer class Table1(declarative_base()): __tablename__ = 'table1' id = Column('id', Integer, primary_key=True) """) self.dir.write( 'somepackage/table2.py', b""" from mortar_rdb import declarative_base from sqlalchemy import Table, Column, Integer class Table2(declarative_base()): __tablename__ = 'table2' id = Column('id', Integer, primary_key=True) """) s = scan('somepackage') self.failUnless(isinstance(s, Source)) compare(['table1', 'table2'], sorted(s.metadata.tables.keys()))