def testReentrance(self): class Test(self.declarative_base): __tablename__ = 'Test' id = Column(Integer, primary_key=True) field = Column(String) SADatum = self.SADatum = newSADatum(self.metadata) datum1 = SADatum('Test', 'id') SADatum.cleanup() SADatum = self.SADatum = newSADatum(self.metadata) datum1 = SADatum('Test', 'id')
def testWithSchemaOnly1(self): SADatum = self.SADatum = newSADatum(self.metadata) try: SADatum('Test', 'id', field='test') self.fail("No expection raised") except ValueError, e: self.assertEqual("Test is not defined in the metadata", e.args[0])
def testPrimaryKey4(self): table = Table('Test', self.metadata, Column('id', Integer, primary_key=True) ) SADatum = newSADatum(self.metadata) datum = SADatum('Test') self.assertEqual(('id', ), datum._tableau_id_fields)
def testAuxPropertyWithSchemaOnly(self): table = Table('Test', self.metadata, Column('id', Integer, primary_key=True), Column('field', String)) SADatum = self.SADatum = newSADatum(self.metadata) datum = SADatum(table, 'id', field='test') datum._aux = 1 self.assertEqual(1, datum._aux)
def testWithSchemaOnly3(self): table = Table('Test', self.metadata, Column('id', Integer, primary_key=True), Column('field', String)) SADatum = self.SADatum = newSADatum(self.metadata) datum = SADatum(table, 'id', field='test') self.assertEqual('Test', datum._tableau_schema) self.assertEqual(table, datum._tableau_table)
def testReentrance(self): class Test(self.declarative_base): __tablename__ = 'Test' id = Column(Integer, primary_key=True) field = Column(String) SADatum = self.SADatum = newSADatum(self.metadata) datum1 = SADatum( 'Test', 'id' ) SADatum.cleanup() SADatum = self.SADatum = newSADatum(self.metadata) datum1 = SADatum( 'Test', 'id' )
def testAuxPropertyWithDeclarative(self): class Test(self.declarative_base): __tablename__ = 'Test' id = Column(Integer, primary_key=True) field = Column(String) SADatum = self.SADatum = newSADatum(self.metadata) datum = SADatum('Test', 'id', field='test') datum._aux = 1 self.assertEqual(1, datum._aux)
def testPrimaryKey1(self): table = Table('Test', self.metadata, Column('id', Integer, primary_key=True)) SADatum = self.SADatum = newSADatum(self.metadata) try: SADatum('Test', 'oops') self.fail("No expection raised") except ValueError, e: self.assertEqual( "id_fields does not match to the table definition ([oops] != [id])", e.args[0])
def testWithSchemaOnly1(self): SADatum = newSADatum(self.metadata) try: SADatum( 'Test', 'id', field='test' ) self.fail("No expection raised") except ValueError, e: self.assertEqual("Test is not defined in the metadata", e.args[0])
def testWithDeclarative2(self): class Test(self.declarative_base): __tablename__ = 'Test' id = Column(Integer, primary_key=True) field = Column(String) SADatum = self.SADatum = newSADatum(self.metadata) datum = SADatum('Test', 'id', field='test') self.assertEqual('Test', datum._tableau_schema) self.assertEqual(Test.__table__, datum._tableau_table) self.assertEqual('test', datum.field)
def testWithDeclarative1(self): class Test(self.declarative_base): __tablename__ = 'Oops' id = Column(Integer, primary_key=True) SADatum = self.SADatum = newSADatum(self.metadata) try: SADatum('Test', 'id', field='test') self.fail("No expection raised") except ValueError, e: self.assertEqual("Test is not defined in the metadata", e.args[0])
def testIfDeclarativeIsAddableToSession(self): class Foo(self.declarative_base): __tablename__ = 'Foo' id = Column(Integer, primary_key=True) field = Column(String) bars = relationship('Bar') def some_model_specific_method(self): return self.field class Bar(self.declarative_base): __tablename__ = 'Bar' id = Column(Integer, primary_key=True) type = Column(String) foo_id = Column(Integer, ForeignKey('Foo.id')) __mapper_args__ = { 'polymorphic_on': type } class Foobar(Bar): __tablename__ = 'Foobar' __mapper_args__ = {'polymorphic_identity': 'foobar'} id = Column(Integer, ForeignKey('Bar.id'), primary_key=True) SADatum = newSADatum(self.metadata, self.declarative_base) datum = SADatum( 'Foo', auto('id'), field='test', bars=one_to_many([ SADatum( 'Bar', auto('id') ), SADatum( 'Foobar', auto('id') ) ], 'foo_id' ) ) self.assertEqual('Foo', datum._tableau_schema) self.assertEqual(Foo.__table__, datum._tableau_table) self.assertEqual('test', datum.field) self.assertIsInstance(datum, Foo) self.assertEqual('test', datum.some_model_specific_method()) self.assertIsInstance(datum.bars[0], Bar) self.assertIsInstance(datum.bars[1], Bar) engine = create_engine('sqlite+pysqlite:///', echo=True) session = sessionmaker(engine)() self.metadata.create_all(engine) session.add(datum) session.flush() self.assertEqual(1, datum.id) self.assertEqual(1, datum.bars[0].id) self.assertEqual(2, datum.bars[1].id)
def testManyToOne3(self): metadata = MetaData() Table('Schema', metadata, Column('id', Integer, primary_key=True), Column('parent_id', Integer)) SADatum = self.SADatum = newSADatum(metadata) a = SADatum('Schema', 'id', id=1, parent=many_to_one(Datum('Foo', 'id', id=1), 'parent_id')) suite = DataSuite() DataWalker(suite)(a) self.assertEqual(1, a._tableau_fields['parent'].render())
def testAuxPropertyWithDeclarative(self): class Test(self.declarative_base): __tablename__ = 'Test' id = Column(Integer, primary_key=True) field = Column(String) SADatum = self.SADatum = newSADatum(self.metadata) datum = SADatum( 'Test', 'id', field='test' ) datum._aux = 1 self.assertEqual(1, datum._aux)
def testAuxPropertyWithSchemaOnly(self): table = Table('Test', self.metadata, Column('id', Integer, primary_key=True), Column('field', String) ) SADatum = self.SADatum = newSADatum(self.metadata) datum = SADatum( table, 'id', field='test' ) datum._aux = 1 self.assertEqual(1, datum._aux)
def testDefaultValue(self): table = Table('Test', self.metadata, Column('id', Integer, primary_key=True), Column('field', String, default='foo')) SADatum = self.SADatum = newSADatum(self.metadata) datum1 = SADatum('Test', 'id') datum2 = SADatum('Test', 'id', field='test') self.assertEqual('Test', datum1._tableau_schema) self.assertEqual(table, datum1._tableau_table) self.assertEqual('foo', datum1.field) self.assertEqual('Test', datum2._tableau_schema) self.assertEqual(table, datum2._tableau_table) self.assertEqual('test', datum2.field)
def testDefaultValueFunction(self): table = Table('Test', self.metadata, Column('id', Integer, primary_key=True), Column('field', String, default=lambda : 'foo', onupdate=lambda : "update") ) SADatum = newSADatum(self.metadata) datum1 = SADatum( 'Test', 'id' ) self.assertEqual('Test', datum1._tableau_schema) self.assertEqual(table, datum1._tableau_table) self.assertEqual('foo', datum1.field)
def testWithSchemaOnly3(self): table = Table('Test', self.metadata, Column('id', Integer, primary_key=True), Column('field', String) ) SADatum = newSADatum(self.metadata) datum = SADatum( table, 'id', field='test' ) self.assertEqual('Test', datum._tableau_schema) self.assertEqual(table, datum._tableau_table)
def testPrimaryKey3(self): table = Table('Test', self.metadata, Column('id', Integer, primary_key=True) ) SADatum = newSADatum(self.metadata) try: SADatum( 'Test', ('id1', 'id2'), ) self.fail("No expection raised") except ValueError, e: self.assertEqual("id_fields does not match to the table definition ([id1,id2] != [id])", e.args[0])
def testWithDeclarative2(self): class Test(self.declarative_base): __tablename__ = 'Test' id = Column(Integer, primary_key=True) field = Column(String) SADatum = self.SADatum = newSADatum(self.metadata) datum = SADatum( 'Test', 'id', field='test' ) self.assertEqual('Test', datum._tableau_schema) self.assertEqual(Test.__table__, datum._tableau_table) self.assertEqual('test', datum.field)
def testWithDeclarative1(self): class Test(self.declarative_base): __tablename__ = 'Oops' id = Column(Integer, primary_key=True) SADatum = newSADatum(self.metadata, self.declarative_base) try: SADatum( 'Test', 'id', field='test' ) self.fail("No expection raised") except ValueError, e: self.assertEqual("Test is not defined in the metadata", e.args[0])
def testIfDeclarativeIsWalkable(self): class Test(self.declarative_base): __tablename__ = 'Test' id = Column(Integer, primary_key=True) field = Column(String) SADatum = self.SADatum = newSADatum(self.metadata) datum = SADatum('Test', auto('id'), field='test') self.assertEqual('Test', datum._tableau_schema) self.assertEqual(Test.__table__, datum._tableau_table) self.assertEqual('test', datum.field) suite = DataSuite() DataWalker(suite)(datum) self.assertEqual(1, datum.id)
def testWithDeclarative2(self): table = Table('Test', self.metadata, Column('id', Integer, primary_key=True)) class Test(self.declarative_base): __tablename__ = 'Oops' id = Column(Integer, primary_key=True) SADatum = newSADatum(self.metadata, self.declarative_base) try: SADatum( 'Test', 'id', field='test' ) self.fail("No expection raised") except ValueError, e: self.assertEqual("declarative class for Test is not in the class registry", e.args[0])
def testIfDeclarativeIsWalkable(self): class Test(self.declarative_base): __tablename__ = 'Test' id = Column(Integer, primary_key=True) field = Column(String) SADatum = self.SADatum = newSADatum(self.metadata) datum = SADatum( 'Test', auto('id'), field='test' ) self.assertEqual('Test', datum._tableau_schema) self.assertEqual(Test.__table__, datum._tableau_table) self.assertEqual('test', datum.field) suite = DataSuite() DataWalker(suite)(datum) self.assertEqual(1, datum.id)
def testManyToOne3(self): metadata = MetaData() Table('Schema', metadata, Column('id', Integer, primary_key=True), Column('parent_id', Integer) ) SADatum = newSADatum(metadata) a = SADatum( 'Schema', 'id', id=1, parent=many_to_one( Datum('Foo', 'id', id=1), 'parent_id' ) ) suite = DataSuite() DataWalker(suite)(a) self.assertEqual(1, a._tableau_fields['parent'].render())
def testIfDeclarativeIsAddableToSession(self): class Foo(self.declarative_base): __tablename__ = 'Foo' id = Column(Integer, primary_key=True) field = Column(String) bars = relationship('Bar') def some_model_specific_method(self): return self.field class Bar(self.declarative_base): __tablename__ = 'Bar' id = Column(Integer, primary_key=True) type = Column(String) foo_id = Column(Integer, ForeignKey('Foo.id')) __mapper_args__ = {'polymorphic_on': type} class Foobar(Bar): __tablename__ = 'Foobar' __mapper_args__ = {'polymorphic_identity': 'foobar'} id = Column(Integer, ForeignKey('Bar.id'), primary_key=True) SADatum = self.SADatum = newSADatum(self.metadata) datum = SADatum( 'Foo', auto('id'), field='test', bars=one_to_many( [SADatum('Bar', auto('id')), SADatum('Foobar', auto('id'))], 'foo_id')) self.assertEqual('Foo', datum._tableau_schema) self.assertEqual(Foo.__table__, datum._tableau_table) self.assertEqual('test', datum.field) self.assertEqual('test', datum.some_model_specific_method()) engine = create_engine('sqlite+pysqlite:///', echo=True) session = sessionmaker(engine)() self.metadata.create_all(engine) session.add(datum) session.flush() self.assertEqual(1, datum.id) self.assertEqual(1, datum.bars[0].id) self.assertEqual(2, datum.bars[1].id)
def testDefaultValue(self): table = Table('Test', self.metadata, Column('id', Integer, primary_key=True), Column('field', String, default='foo') ) SADatum = newSADatum(self.metadata) datum1 = SADatum( 'Test', 'id' ) datum2 = SADatum( 'Test', 'id', field='test' ) self.assertEqual('Test', datum1._tableau_schema) self.assertEqual(table, datum1._tableau_table) self.assertEqual('foo', datum1.field) self.assertEqual('Test', datum2._tableau_schema) self.assertEqual(table, datum2._tableau_table) self.assertEqual('test', datum2.field)
def testPrimaryKey4(self): table = Table('Test', self.metadata, Column('id', Integer, primary_key=True)) SADatum = self.SADatum = newSADatum(self.metadata) datum = SADatum('Test') self.assertEqual(('id', ), datum._tableau_id_fields)