def test_get_relationships_many_to_many(self): """ Tests getting the fields for a many-to-many """ Base = declarative_base() association_table = Table( 'association', Base.metadata, Column('left_id', Integer, ForeignKey('left.id')), Column('right_id', Integer, ForeignKey('right.id')) ) class Parent(Base): __tablename__ = 'left' id = Column(Integer, primary_key=True) children = relationship("Child", secondary=association_table, backref='parents') class Child(Base): __tablename__ = 'right' id = Column(Integer, primary_key=True) resp = _get_relationships(Parent) self.assertEqual(len(resp), 1) rel = resp[0] self.assertIsInstance(rel, ListRelationship) self.assertEqual(rel.name, 'children') self.assertEqual(rel._relation, 'Child') resp = _get_relationships(Child) self.assertEqual(len(resp), 1) rel = resp[0] self.assertIsInstance(rel, ListRelationship) self.assertEqual(rel.name, 'parents') self.assertEqual(rel._relation, 'Parent')
def test_get_relationships_for_model_one_to_many(self): """ Tests getting the fields for a one_to_many """ Base = declarative_base() class Parent(Base): __tablename__ = 'parent' id = Column(Integer, primary_key=True) children = relationship("Child", backref="parent") class Child(Base): __tablename__ = 'child' id = Column(Integer, primary_key=True) parent_id = Column(Integer, ForeignKey('parent.id')) resp = _get_relationships(Parent) self.assertEqual(len(resp), 1) rel = resp[0] self.assertIsInstance(rel, ListRelationship) self.assertEqual(rel.name, 'children') self.assertEqual(rel._relation, 'Child') resp = _get_relationships(Child) self.assertEqual(len(resp), 1) rel = resp[0] self.assertIsInstance(rel, Relationship) self.assertIsNotInstance(rel, ListRelationship) self.assertEqual(rel.name, 'parent') self.assertEqual(rel._relation, 'Parent')
def test_get_relationships_one_to_one(self): """ Tests getting the fields for a one-to-one """ Base = declarative_base() class Parent(Base): __tablename__ = 'parent' id = Column(Integer, primary_key=True) child_id = Column(Integer, ForeignKey('child.id')) child = relationship("Child", backref=backref("parent", uselist=False)) class Child(Base): __tablename__ = 'child' id = Column(Integer, primary_key=True) resp = _get_relationships(Parent) self.assertEqual(len(resp), 1) rel = resp[0] self.assertIsInstance(rel, Relationship) self.assertIsNotInstance(rel, ListRelationship) self.assertEqual(rel.name, 'child') self.assertEqual(rel._relation, 'Child') resp = _get_relationships(Child) self.assertEqual(len(resp), 1) rel = resp[0] self.assertIsInstance(rel, Relationship) self.assertIsNotInstance(rel, ListRelationship) self.assertEqual(rel.name, 'parent') self.assertEqual(rel._relation, 'Parent')
def test_get_relationships_many_to_many(self): """ Tests getting the fields for a many-to-many """ Base = declarative_base() association_table = Table( 'association', Base.metadata, Column('left_id', Integer, ForeignKey('left.id')), Column('right_id', Integer, ForeignKey('right.id'))) class Parent(Base): __tablename__ = 'left' id = Column(Integer, primary_key=True) children = relationship("Child", secondary=association_table, backref='parents') class Child(Base): __tablename__ = 'right' id = Column(Integer, primary_key=True) resp = _get_relationships(Parent) self.assertEqual(len(resp), 1) rel = resp[0] self.assertIsInstance(rel, ListRelationship) self.assertEqual(rel.name, 'children') self.assertEqual(rel._relation, 'Child') resp = _get_relationships(Child) self.assertEqual(len(resp), 1) rel = resp[0] self.assertIsInstance(rel, ListRelationship) self.assertEqual(rel.name, 'parents') self.assertEqual(rel._relation, 'Parent')
def test_get_relationships_for_model(self): """ Tests a simple get_field_for_model """ Base = declarative_base() class MyModel(Base): __tablename__ = 'blah' id = Column(Integer, primary_key=True) value = Column(String) resp = _get_relationships(MyModel) self.assertTupleEqual(tuple(), resp)