def test_no_identity_okay(self): samples = Collection({TypeA: {'x': 1, 'y': 2}}) sample1 = samples.make(TypeA) sample2 = samples.make(TypeA) compare(TypeA(1, 2), actual=sample1) compare(TypeA(1, 2), actual=sample2) self.assertFalse(sample1 is sample2)
def test_foreign_key_no_session(self): Base = declarative_base() class Parent(Comparable, Base): __tablename__ = 'parent' id = Column(Integer, primary_key=True) child_id = Column(Integer, ForeignKey('child.id')) child = relationship('Child') class Child(Comparable, Base): __tablename__ = 'child' id = Column(Integer, primary_key=True) value = Column(String) samples = Collection({ Parent: { 'id': 1, 'child': Child }, Child: { 'id': 3, 'value': 'Foo' } }) parent1 = samples.make(Parent, id=1) parent2 = samples.make(Parent, id=2) session = self.make_session(Base) session.add(parent1) session.add(parent2) with ShouldRaise(IntegrityError): session.commit()
def make_all(self): Base = declarative_base() class Model(Comparable, Base): __tablename__ = 'model' id = Column('id_', Integer, primary_key=True) value = Column(String) samples = Collection({Model: {'id': 1, 'value': 'two'}}) session = self.make_session(Base) return Model, samples, session
def test_identify_and_set(self): TypeC = make_type_c() def identify_type_c(type_, attrs): self.assertTrue(TypeC is type_) return attrs['key'] samples = Collection({TypeC: {'key': 1}}) set = Set(samples, identify_type_c) sample1 = set.get(TypeC) sample2 = set.get(TypeC) self.assertTrue(type(sample1), TypeC) compare(sample1.key, expected=1) self.assertTrue(sample1 is sample2)
def test_null_primary_key(self): Model, _, session = self.make_all() samples = Collection({Model: {'value': 'two'}}) model1 = samples.make(Model) model2 = samples.make(Model) self.assertFalse(model1 is model2) session.add(model1) session.add(model2) session.commit() models = session.query(Model).all() compare(len(set(m.id for m in models)), expected=2) compare(set(m.value for m in models), expected={'two'})
def test_relationship_with_backrefs(self): Base = declarative_base() class Bar(Base): __tablename__ = 'bar' id = Column(Integer, primary_key=True) value = Column(Integer) class Foo(Base): __tablename__ = 'foo' id = Column(Integer, primary_key=True) bar_id = Column(String, ForeignKey('bar.id')) bar = relationship('Bar', backref='foos') class Baz(Base): __tablename__ = 'baz' id = Column(Integer, primary_key=True) foo_id = Column(String, ForeignKey('foo.id')) foo = relationship('Foo') collection = Collection({ Bar: { 'id': 1, 'value': 2 }, Foo: { 'id': 3, 'bar': Bar, 'bar_id': 1 }, Baz: { 'id': 4, 'foo': Foo, 'foo_id': 3 } }) samples = Set(collection) session = self.make_session(Base) session.add(samples.get(Baz, id=5, foo=samples.get(Foo, id=7))) session.commit() compare(session.query(Foo.id).all(), expected=[(7, )])
def test_foreign_key_with_session(self): Base = declarative_base() class Parent(Comparable, Base): __tablename__ = 'parent' id = Column(Integer, primary_key=True) child_id = Column(Integer, ForeignKey('child.id')) child = relationship('Child') class Child(Comparable, Base): __tablename__ = 'child' id = Column(Integer, primary_key=True) value = Column(String) collection = Collection({ Parent: { 'id': 1, 'child': Child }, Child: { 'id': 3, 'value': 'Foo' } }) samples = Set(collection) parent1 = samples.get(Parent, id=1) parent2 = samples.get(Parent, id=2) self.assertFalse(parent1 is parent2) session = self.make_session(Base) session.add(parent1) session.add(parent2) session.commit() model = session.query(Child).one() compare(Child(id=3, value='Foo'), actual=model)
def setUp(self): self.collection = Collection({dict: {}})
def test_unhashable_attributes(self): unhashable = [] collection = Collection({dict: {'y': unhashable}}) made = collection.make(dict) compare(made, expected={'y': []}) assert made['y'] is unhashable
def test_no_identity_bad(self): TypeC = make_type_c() samples = Collection({TypeC: {'key': 1}}) samples.make(TypeC) with ShouldRaise(Exception(1)): samples.make(TypeC)
def test_nested_leave_explicit_types(self): samples = Collection({TypeA: {'x': 1}, TypeB: {}}) compare(TypeA(1, TypeB), actual=samples.make(TypeA, y=TypeB))
def test_nested(self): samples = Collection({ TypeA: {'x': 1, 'y': TypeB}, TypeB: {'a': 3, 'b': 4}, }) compare(TypeA(1, TypeB(3, 4)), actual=samples.make(TypeA))
def test_override(self): samples = Collection({TypeA: {'x': 1, 'y': 2}}) compare(TypeA(1, 3), actual=samples.make(TypeA, y=3)) # check we don't mutate the sample data! compare(samples.make(TypeA), expected=TypeA(1, 2))
def test_basic(self): samples = Collection({TypeA: {'x': 1, 'y': 2}}) compare(TypeA(1, 2), actual=samples.make(TypeA))