from sqlalchemy.orm.collections import collection
import sqlalchemy as sa
-from sqlalchemy import testing
from sqlalchemy import Integer, String, ForeignKey, text
from sqlalchemy.testing.schema import Table, Column
from sqlalchemy import util, exc as sa_exc
from sqlalchemy.orm import create_session, mapper, relationship, \
attributes, instrumentation
from sqlalchemy.testing import fixtures
-from sqlalchemy.testing import eq_, assert_raises, assert_raises_message
+from sqlalchemy.testing import assert_raises, assert_raises_message
class Canary(sa.orm.interfaces.AttributeExtension):
def __init__(self):
canary = Canary()
instrumentation.register_class(Foo)
- attributes.register_attribute(Foo, 'attr', uselist=True, extension=canary,
+ attributes.register_attribute(Foo, 'attr', uselist=True,
+ extension=canary,
typecallable=typecallable, useobject=True)
obj = Foo()
canary = Canary()
instrumentation.register_class(Foo)
- attributes.register_attribute(Foo, 'attr', uselist=True, extension=canary,
+ attributes.register_attribute(Foo, 'attr', uselist=True,
+ extension=canary,
typecallable=typecallable, useobject=True)
obj = Foo()
if util.reduce(and_, [hasattr(direct, a) for a in
('__delitem__', 'insert', '__len__')], True):
values = [creator(), creator(), creator(), creator()]
- direct[slice(0,1)] = values
- control[slice(0,1)] = values
+ direct[slice(0, 1)] = values
+ control[slice(0, 1)] = values
assert_eq()
values = [creator(), creator()]
- direct[slice(0,-1,2)] = values
- control[slice(0,-1,2)] = values
+ direct[slice(0, -1, 2)] = values
+ control[slice(0, -1, 2)] = values
assert_eq()
values = [creator()]
- direct[slice(0,-1)] = values
- control[slice(0,-1)] = values
+ direct[slice(0, -1)] = values
+ control[slice(0, -1)] = values
assert_eq()
- values = [creator(),creator(),creator()]
+ values = [creator(), creator(), creator()]
control[:] = values
direct[:] = values
def invalid():
canary = Canary()
instrumentation.register_class(Foo)
- attributes.register_attribute(Foo, 'attr', uselist=True, extension=canary,
+ attributes.register_attribute(Foo, 'attr', uselist=True,
+ extension=canary,
typecallable=typecallable, useobject=True)
obj = Foo()
canary = Canary()
instrumentation.register_class(Foo)
- attributes.register_attribute(Foo, 'attr', uselist=True, extension=canary,
+ attributes.register_attribute(Foo, 'attr', uselist=True,
+ extension=canary,
typecallable=typecallable, useobject=True)
obj = Foo()
canary = Canary()
instrumentation.register_class(Foo)
- attributes.register_attribute(Foo, 'attr', uselist=True, extension=canary,
+ attributes.register_attribute(Foo, 'attr', uselist=True,
+ extension=canary,
typecallable=typecallable, useobject=True)
obj = Foo()
canary = Canary()
instrumentation.register_class(Foo)
- attributes.register_attribute(Foo, 'attr', uselist=True, extension=canary,
+ attributes.register_attribute(Foo, 'attr', uselist=True,
+ extension=canary,
typecallable=typecallable, useobject=True)
obj = Foo()
canary = Canary()
instrumentation.register_class(Foo)
- attributes.register_attribute(Foo, 'attr', uselist=True, extension=canary,
+ attributes.register_attribute(Foo, 'attr', uselist=True,
+ extension=canary,
typecallable=typecallable, useobject=True)
obj = Foo()
obj.attr = like_me
self.assert_(obj.attr is not direct)
self.assert_(obj.attr is not like_me)
- self.assert_(set(collections.collection_adapter(obj.attr)) == set([e2]))
+ self.assert_(
+ set(collections.collection_adapter(obj.attr)) == set([e2]))
self.assert_(e1 in canary.removed)
self.assert_(e2 in canary.added)
- # key validity on bulk assignment is a basic feature of MappedCollection
- # but is not present in basic, @converter-less dict collections.
+ # key validity on bulk assignment is a basic feature of
+ # MappedCollection but is not present in basic, @converter-less
+ # dict collections.
e3 = creator()
if isinstance(obj.attr, collections.MappedCollection):
real_dict = dict(badkey=e3)
self.assert_(e4 not in canary.data)
def test_dict(self):
- try:
- self._test_adapter(dict, self.dictable_entity,
- to_set=lambda c: set(c.values()))
- self.assert_(False)
- except sa_exc.ArgumentError, e:
- self.assert_(e.args[0] == 'Type InstrumentedDict must elect an appender method to be a collection class')
+ assert_raises_message(
+ sa_exc.ArgumentError,
+ 'Type InstrumentedDict must elect an appender '
+ 'method to be a collection class',
+ self._test_adapter, dict, self.dictable_entity,
+ to_set=lambda c: set(c.values())
+ )
- try:
- self._test_dict(dict)
- self.assert_(False)
- except sa_exc.ArgumentError, e:
- self.assert_(e.args[0] == 'Type InstrumentedDict must elect an appender method to be a collection class')
+ assert_raises_message(
+ sa_exc.ArgumentError,
+ 'Type InstrumentedDict must elect an appender method '
+ 'to be a collection class',
+ self._test_dict, dict
+ )
def test_dict_subclass(self):
class MyDict(dict):
canary = Canary()
instrumentation.register_class(Foo)
- attributes.register_attribute(Foo, 'attr', uselist=True, extension=canary,
+ attributes.register_attribute(Foo, 'attr', uselist=True,
+ extension=canary,
typecallable=typecallable, useobject=True)
obj = Foo()
pass
canary = Canary()
instrumentation.register_class(Foo)
- attributes.register_attribute(Foo, 'attr', uselist=True, extension=canary,
+ attributes.register_attribute(Foo, 'attr', uselist=True,
+ extension=canary,
typecallable=Custom, useobject=True)
obj = Foo()
canary = Canary()
creator = self.entity_maker
instrumentation.register_class(Foo)
- attributes.register_attribute(Foo, 'attr', uselist=True, extension=canary, useobject=True)
+ attributes.register_attribute(Foo, 'attr', uselist=True,
+ extension=canary, useobject=True)
obj = Foo()
col1 = obj.attr
@classmethod
def define_tables(cls, metadata):
Table('parents', metadata,
- Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
+ Column('id', Integer, primary_key=True,
+ test_needs_autoincrement=True),
Column('label', String(128)))
Table('children', metadata,
- Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
+ Column('id', Integer, primary_key=True,
+ test_needs_autoincrement=True),
Column('parent_id', Integer, ForeignKey('parents.id'),
nullable=False),
Column('a', String(128)),
self.assert_(set(p.children.keys()) == set(['foo', 'bar']))
self.assert_(p.children['foo'].id != cid)
- self.assert_(len(list(collections.collection_adapter(p.children))) == 2)
+ self.assert_(
+ len(list(collections.collection_adapter(p.children))) == 2)
session.flush()
session.expunge_all()
p = session.query(Parent).get(pid)
- self.assert_(len(list(collections.collection_adapter(p.children))) == 2)
+ self.assert_(
+ len(list(collections.collection_adapter(p.children))) == 2)
collections.collection_adapter(p.children).remove_with_event(
p.children['foo'])
- self.assert_(len(list(collections.collection_adapter(p.children))) == 1)
+ self.assert_(
+ len(list(collections.collection_adapter(p.children))) == 1)
session.flush()
session.expunge_all()
p = session.query(Parent).get(pid)
- self.assert_(len(list(collections.collection_adapter(p.children))) == 1)
+ self.assert_(
+ len(list(collections.collection_adapter(p.children))) == 1)
del p.children['bar']
- self.assert_(len(list(collections.collection_adapter(p.children))) == 0)
+ self.assert_(
+ len(list(collections.collection_adapter(p.children))) == 0)
session.flush()
session.expunge_all()
p = session.query(Parent).get(pid)
- self.assert_(len(list(collections.collection_adapter(p.children))) == 0)
+ self.assert_(
+ len(list(collections.collection_adapter(p.children))) == 0)
def _test_composite_mapped(self, collection_class):
p = session.query(Parent).get(pid)
- self.assert_(set(p.children.keys()) == set([('foo', '1'), ('foo', '2')]))
+ self.assert_(
+ set(p.children.keys()) == set([('foo', '1'), ('foo', '2')]))
cid = p.children[('foo', '1')].id
collections.collection_adapter(p.children).append_with_event(
p = session.query(Parent).get(pid)
- self.assert_(set(p.children.keys()) == set([('foo', '1'), ('foo', '2')]))
+ self.assert_(
+ set(p.children.keys()) == set([('foo', '1'), ('foo', '2')]))
self.assert_(p.children[('foo', '1')].id != cid)
- self.assert_(len(list(collections.collection_adapter(p.children))) == 2)
+ self.assert_(
+ len(list(collections.collection_adapter(p.children))) == 2)
def test_mapped_collection(self):
collection_class = collections.mapped_collection(lambda c: c.a)
self._test_scalar_mapped(collection_class)
def test_declarative_column_mapped(self):
- """test that uncompiled attribute usage works with column_mapped_collection"""
+ """test that uncompiled attribute usage works with
+ column_mapped_collection"""
from sqlalchemy.ext.declarative import declarative_base
Bar = self.classes.Bar
bar = self.tables["x.bar"]
mapper(Foo, self.tables.foo, properties={
- "foo_id":self.tables.foo.c.id
+ "foo_id": self.tables.foo.c.id
})
mapper(Bar, bar, inherits=Foo, properties={
- "bar_id":bar.c.id,
+ "bar_id": bar.c.id,
})
bar_spec = Bar(foo_id=1, bar_id=2, bat_id=3)
@classmethod
def define_tables(cls, metadata):
Table('sometable', metadata,
- Column('col1',Integer, primary_key=True, test_needs_autoincrement=True),
+ Column('col1', Integer, primary_key=True,
+ test_needs_autoincrement=True),
Column('data', String(30)))
Table('someothertable', metadata,
- Column('col1', Integer, primary_key=True, test_needs_autoincrement=True),
+ Column('col1', Integer, primary_key=True,
+ test_needs_autoincrement=True),
Column('scol1', Integer,
ForeignKey('sometable.col1')),
Column('data', String(20)))
def test_basic(self):
- someothertable, sometable = self.tables.someothertable, self.tables.sometable
+ someothertable, sometable = self.tables.someothertable, \
+ self.tables.sometable
class MyList(list):
pass
pass
mapper(Foo, sometable, properties={
- 'bars':relationship(Bar, collection_class=MyList)
+ 'bars': relationship(Bar, collection_class=MyList)
})
mapper(Bar, someothertable)
f = Foo()
def test_lazyload(self):
"""test that a 'set' can be used as a collection and can lazyload."""
- someothertable, sometable = self.tables.someothertable, self.tables.sometable
+ someothertable, sometable = self.tables.someothertable, \
+ self.tables.sometable
class Foo(object):
pass
class Bar(object):
pass
mapper(Foo, sometable, properties={
- 'bars':relationship(Bar, collection_class=set)
+ 'bars': relationship(Bar, collection_class=set)
})
mapper(Bar, someothertable)
f = Foo()
def test_dict(self):
"""test that a 'dict' can be used as a collection and can lazyload."""
- someothertable, sometable = self.tables.someothertable, self.tables.sometable
+ someothertable, sometable = self.tables.someothertable, \
+ self.tables.sometable
class Foo(object):
del self[id(item)]
mapper(Foo, sometable, properties={
- 'bars':relationship(Bar, collection_class=AppenderDict)
+ 'bars': relationship(Bar, collection_class=AppenderDict)
})
mapper(Bar, someothertable)
f = Foo()
f.bars.clear()
def test_dict_wrapper(self):
- """test that the supplied 'dict' wrapper can be used as a collection and can lazyload."""
+ """test that the supplied 'dict' wrapper can be used as a
+ collection and can lazyload."""
- someothertable, sometable = self.tables.someothertable, self.tables.sometable
+ someothertable, sometable = self.tables.someothertable, \
+ self.tables.sometable
class Foo(object):
self._test_list(ListLike)
def _test_list(self, listcls):
- someothertable, sometable = self.tables.someothertable, self.tables.sometable
+ someothertable, sometable = self.tables.someothertable, \
+ self.tables.sometable
class Parent(object):
pass
pass
mapper(Parent, sometable, properties={
- 'children':relationship(Child, collection_class=listcls)
+ 'children': relationship(Child, collection_class=listcls)
})
mapper(Child, someothertable)
assert control == list(p.children)
def test_custom(self):
- someothertable, sometable = self.tables.someothertable, self.tables.sometable
+ someothertable, sometable = self.tables.someothertable, \
+ self.tables.sometable
class Parent(object):
pass
return iter(self.data)
mapper(Parent, sometable, properties={
- 'children':relationship(Child, collection_class=MyCollection)
+ 'children': relationship(Child, collection_class=MyCollection)
})
mapper(Child, someothertable)
assert not hasattr(Touchy, 'no_touch')
assert 'no_touch' in dir(Touchy)
- instrumented = collections._instrument_class(Touchy)
- assert True
+ collections._instrument_class(Touchy)