Changeset 349
- Timestamp:
- 07/02/08 13:33:20 (5 years ago)
- Location:
- elixir/trunk
- Files:
-
- 25 modified
-
CHANGES (modified) (2 diffs)
-
elixir/__init__.py (modified) (3 diffs)
-
elixir/entity.py (modified) (6 diffs)
-
elixir/ext/list.py (modified) (11 diffs)
-
elixir/options.py (modified) (1 diff)
-
tests/test_acts_as_list.py (modified) (7 diffs)
-
tests/test_associable.py (modified) (2 diffs)
-
tests/test_autoload.py (modified) (5 diffs)
-
tests/test_class_methods.py (modified) (1 diff)
-
tests/test_custombase.py (modified) (3 diffs)
-
tests/test_dict.py (modified) (5 diffs)
-
tests/test_encryption.py (modified) (3 diffs)
-
tests/test_events.py (modified) (1 diff)
-
tests/test_fields.py (modified) (3 diffs)
-
tests/test_inherit.py (modified) (3 diffs)
-
tests/test_m2m.py (modified) (6 diffs)
-
tests/test_m2o.py (modified) (5 diffs)
-
tests/test_o2m.py (modified) (4 diffs)
-
tests/test_o2o.py (modified) (1 diff)
-
tests/test_options.py (modified) (12 diffs)
-
tests/test_order_by.py (modified) (1 diff)
-
tests/test_packages.py (modified) (1 diff)
-
tests/test_properties.py (modified) (9 diffs)
-
tests/test_sa_integration.py (modified) (2 diffs)
-
tests/test_versioning.py (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
elixir/trunk/CHANGES
r347 r349 13 13 the identity name automatically from the class itself. 14 14 - Added __setattr__ method on Metaclass so that you can add properties 15 slightly more easily after class definition (but beforesetup_all):15 slightly more easily after class definition (but *before* setup_all): 16 16 class A(Entity): 17 17 pass … … 21 21 - Added support for SQLAlchemy 0.5, and dropped support for version 0.3 and 22 22 earlier. 23 - The default session (elixir.session) uses sessionmaker() instead of 24 create_session(), which means it has now the following characterisics: 25 * autoflush=True 26 * autocommit=False (with SA 0.5 -- equivalent to transactional=True with 27 SA 0.4) 28 * autoexpire=True (with SA 0.5). 29 - removed objectstore 23 30 24 31 Bug fixes: -
elixir/trunk/elixir/__init__.py
r347 r349 50 50 'ManyToOne', 'OneToOne', 'OneToMany', 'ManyToMany', 51 51 'using_options', 'using_table_options', 'using_mapper_options', 52 'options_defaults', 'metadata', ' objectstore', 'session',52 'options_defaults', 'metadata', 'session', 53 53 'create_all', 'drop_all', 54 54 'setup_all', 'cleanup_all', … … 60 60 'metadata', 'session'] 61 61 62 63 class Objectstore(object):64 """a wrapper for a SQLAlchemy session-making object, such as65 SessionContext or ScopedSession.66 67 Uses the ``registry`` attribute present on both objects68 (versions 0.3 and 0.4) in order to return the current69 contextual session.70 """71 72 def __init__(self, ctx):73 self.context = ctx74 75 def __getattr__(self, name):76 return getattr(self.context.registry(), name)77 78 session = property(lambda s:s.context.registry())79 80 62 # default session 81 try: 82 from sqlalchemy.orm import scoped_session 83 session = scoped_session(sqlalchemy.orm.create_session) 84 except ImportError: 85 # Not on version 0.4 of sqlalchemy 86 from sqlalchemy.ext.sessioncontext import SessionContext 87 session = Objectstore(SessionContext(sqlalchemy.orm.create_session)) 88 89 # backward-compatible name 90 objectstore = session 63 session = sqlalchemy.orm.scoped_session(sqlalchemy.orm.sessionmaker()) 91 64 92 65 # default metadata … … 201 174 metadatas.clear() 202 175 203 session.cl ear()176 session.close() 204 177 205 178 sqlalchemy.orm.clear_mappers() -
elixir/trunk/elixir/entity.py
r348 r349 15 15 ForeignKeyConstraint 16 16 from sqlalchemy.orm import Query, MapperExtension, mapper, object_session, \ 17 EXT_CONTINUE, polymorphic_union 18 try: 19 from sqlalchemy.ext.sessioncontext import SessionContext 20 except ImportError: 21 # Probably on sqlalchemy version 0.5 22 pass 17 EXT_CONTINUE, polymorphic_union, ScopedSession 23 18 24 19 import elixir … … 30 25 __doc_all__ = ['Entity', 'EntityMeta'] 31 26 32 33 try:34 from sqlalchemy.orm import ScopedSession35 except ImportError:36 # Not on sqlalchemy version 0.437 ScopedSession = type(None)38 39 40 def _do_mapping(session, cls, *args, **kwargs):41 if session is None:42 return mapper(cls, *args, **kwargs)43 elif isinstance(session, ScopedSession):44 return session.mapper(cls, *args, **kwargs)45 elif isinstance(session, SessionContext):46 extension = kwargs.pop('extension', None)47 if extension is not None:48 if not isinstance(extension, list):49 extension = [extension]50 extension.append(session.mapper_extension)51 else:52 extension = session.mapper_extension53 54 class query(object):55 def __getattr__(s, key):56 return getattr(session.registry().query(cls), key)57 58 def __call__(s):59 return session.registry().query(cls)60 61 if not 'query' in cls.__dict__:62 cls.query = query()63 64 return mapper(cls, extension=extension, *args, **kwargs)65 else:66 raise Exception("Failed to map entity '%s' with its table or "67 "selectable" % cls.__name__)68 27 69 28 … … 116 75 self.metadata = getattr(self.module, '__metadata__', elixir.metadata) 117 76 self.session = getattr(self.module, '__session__', elixir.session) 118 self.objectstore = None119 77 self.collection = getattr(self.module, '__entity_collection__', 120 78 elixir.entities) … … 138 96 if self.collection is not None: 139 97 self.collection.map_entity(self.entity) 140 141 objectstore = None142 session = self.session143 if session is None or isinstance(session, ScopedSession):144 # no stinking objectstore145 pass146 elif isinstance(session, SessionContext):147 objectstore = elixir.Objectstore(session)148 elif not hasattr(session, 'registry'):149 # Both SessionContext and ScopedSession have a registry attribute,150 # but objectstores (whether Elixir's or Activemapper's) don't, so151 # if we are here, it means an Objectstore is used for the session.152 #XXX: still true for activemapper post 0.4?153 objectstore = session154 session = objectstore.context155 156 self.session = session157 self.objectstore = objectstore158 98 159 99 entity = self.entity … … 462 402 args = [self.entity.table] 463 403 464 self.entity.mapper = _do_mapping(self.session, self.entity, 465 properties=self.properties, 466 *args, **kwargs) 404 # do the mapping 405 kwargs['properties'] = self.properties 406 if self.session is None: 407 self.entity.mapper = mapper(self.entity, *args, **kwargs) 408 elif isinstance(self.session, ScopedSession): 409 self.entity.mapper = self.session.mapper(self.entity, 410 *args, **kwargs) 411 else: 412 raise Exception("Failed to map entity '%s' with its table or " 413 "selectable" % self.entity.__name__) 467 414 468 415 def after_mapper(self): … … 965 912 966 913 # session methods 967 def flush(self, *args, **kwargs):968 return object_session(self). flush([self], *args, **kwargs)914 def commit(self, *args, **kwargs): 915 return object_session(self).commit([self], *args, **kwargs) 969 916 970 917 def delete(self, *args, **kwargs): -
elixir/trunk/elixir/ext/list.py
r313 r349 6 6 Once you flag an entity with an `acts_as_list()` statement, a column will be 7 7 added to the entity called `position` which will be an integer column that is 8 managed for you by the plugin. You can pass an alternative column name to 8 managed for you by the plugin. You can pass an alternative column name to 9 9 the plugin using the `column_name` keyword argument. 10 10 … … 38 38 from elixir import * 39 39 from elixir.ext.list import acts_as_list 40 40 41 41 class ToDo(Entity): 42 42 subject = Field(String(128)) … … 51 51 name = Field(String(64)) 52 52 todos = OneToMany('ToDo', order_by='position') 53 54 55 The above example can then be used to manage ordered todo lists for people. Note56 that you must set the `order_by` property on the `Person.todo` relation in order 57 for the relation to respect the ordering. Here is an example of using this model 58 in practice:53 54 55 The above example can then be used to manage ordered todo lists for people. 56 Note that you must set the `order_by` property on the `Person.todo` relation in 57 order for the relation to respect the ordering. Here is an example of using 58 this model in practice: 59 59 60 60 .. sourcecode:: python … … 64 64 p.todos.append(ToDo(subject='Two')) 65 65 p.todos.append(ToDo(subject='One')) 66 session. flush(); session.clear()67 66 session.commit(); session.clear() 67 68 68 p = Person.query.filter_by(name='Jonathan').one() 69 69 p.todos[0].move_to_bottom() 70 70 p.todos[2].move_to_top() 71 session. flush(); session.clear()72 71 session.commit(); session.clear() 72 73 73 p = Person.query.filter_by(name='Jonathan').one() 74 74 assert p.todos[0].subject == 'One' 75 75 assert p.todos[1].subject == 'Two' 76 76 assert p.todos[2].subject == 'Three' 77 77 78 78 79 79 For more examples, refer to the unit tests for this plugin. … … 97 97 98 98 class ListEntityBuilder(object): 99 99 100 100 def __init__(self, entity, qualifier=None, column_name='position'): 101 101 self.entity = entity 102 102 self.qualifier_method = qualifier 103 103 self.column_name = column_name 104 104 105 105 def create_non_pk_cols(self): 106 106 self.position_column = Column(self.column_name, Integer) 107 107 self.entity._descriptor.add_column(self.position_column) 108 108 109 109 def after_table(self): 110 110 position_column = self.position_column 111 111 position_column_name = self.column_name 112 113 qualifier_method = self.qualifier_method 112 113 qualifier_method = self.qualifier_method 114 114 if not qualifier_method: 115 115 qualifier_method = lambda self: None 116 116 117 117 def _init_position(self): 118 118 s = select( … … 123 123 ) 124 124 a = s.alias() 125 #XXX: two func.max? 125 126 setattr(self, position_column_name, select([func.max(a.c.value)])) 126 127 _init_position = before_insert(_init_position) 127 128 128 129 def _shift_items(self): 129 130 self.table.update( … … 137 138 ).execute() 138 139 _shift_items = before_delete(_shift_items) 139 140 def move_to_bottom(self): 140 141 def move_to_bottom(self): 141 142 # move the items that were above this item up one 142 143 self.table.update( … … 149 150 } 150 151 ).execute() 151 152 152 153 # move this item to the max position 153 154 self.table.update( … … 160 161 } 161 162 ).execute() 162 163 163 164 def move_to_top(self): 164 165 # move the items that were above this item down one … … 169 170 ), 170 171 values = { 171 position_column : position_column + 1172 position_column: position_column + 1 172 173 } 173 174 ).execute() 174 175 175 176 # move this item to the first position 176 self.table.update(get_entity_where(self)).execute(**{position_column_name:1}) 177 177 self.table.update(get_entity_where(self)) \ 178 .execute(**{position_column_name: 1}) 179 178 180 def move_to(self, position): 179 181 current_position = getattr(self, position_column_name) 180 182 181 183 # determine which direction we're moving 182 184 if position < current_position: … … 194 196 ) 195 197 modifier = -1 196 198 197 199 # shift the items in between the current and new positions 198 200 self.table.update(where, values = { 199 201 position_column : position_column + modifier 200 202 }).execute() 201 203 202 204 # update this item's position to the desired position 203 self.table.update(get_entity_where(self)).execute(**{position_column_name:position}) 204 205 def move_lower(self): 206 self.move_to(getattr(self, position_column_name)+1) 207 208 def move_higher(self): 209 self.move_to(getattr(self, position_column_name)-1) 210 211 205 self.table.update(get_entity_where(self)) \ 206 .execute(**{position_column_name: position}) 207 208 def move_lower(self): 209 self.move_to(getattr(self, position_column_name) + 1) 210 211 def move_higher(self): 212 self.move_to(getattr(self, position_column_name) - 1) 213 214 212 215 # attach new methods to entity 213 216 self.entity._init_position = _init_position -
elixir/trunk/elixir/options.py
r347 r349 108 108 | | By default, entities uses the global | 109 109 | | ``elixir.session``. | 110 | | This option accepts Objectstore | 111 | | (found in Elixir and ActiveMapper), SessionContext | 112 | | (found in SQLAlchemy 0.3) or ScopedSession (found in | 113 | | SQLAlchemy 0.4) objects. It also supports ``None``, | 114 | | in which case your entity will be mapped using a | 110 | | This option takes a ``ScopedSession`` object or | 111 | | ``None``. In the later case your entity will be | 112 | | mapped using a | 115 113 | | non-contextual mapper. This option can also be set | 116 114 | | for all entities of a module by setting the | -
elixir/trunk/tests/test_acts_as_list.py
r347 r349 40 40 def test_acts_as_list(self): 41 41 # create a person 42 # you must create and flushthis _before_ you attach todo's to it42 # you must create and commit this _before_ you attach todo's to it 43 43 # because of the way that the plugin is implemented 44 44 p = Person(name='Jonathan') 45 session. flush(); session.clear()45 session.commit(); session.clear() 46 46 47 47 # add three todos, in the reverse order that we want them … … 50 50 p.todos.append(ToDo(subject='Two')) 51 51 p.todos.append(ToDo(subject='One')) 52 session. flush(); session.clear()52 session.commit(); session.clear() 53 53 54 54 # move the first item lower 55 55 p = Person.get(1) 56 56 p.todos[0].move_lower() 57 session. flush(); session.clear()57 session.commit(); session.clear() 58 58 59 59 # validate it worked … … 65 65 # move the last item to the top to put things in correct order 66 66 p.todos[2].move_to_top() 67 session. flush(); session.clear()67 session.commit(); session.clear() 68 68 69 69 # validate it worked … … 76 76 # and move_to_top 77 77 p.todos[2].move_to_top() 78 session. flush(); session.clear()78 session.commit(); session.clear() 79 79 80 80 p = Person.get(1) … … 84 84 85 85 p.todos[1].move_to_bottom() 86 session. flush(); session.clear()86 session.commit(); session.clear() 87 87 88 88 p = Person.get(1) … … 95 95 p.todos[0].move_to(3) 96 96 p.todos[2].move_to(1) 97 session. flush(); session.clear()97 session.commit(); session.clear() 98 98 99 99 # validate it worked … … 108 108 # delete the second todo list item 109 109 p.todos[1].delete() 110 session. flush(); session.clear()110 session.commit(); session.clear() 111 111 112 112 # validate that the deletion worked, and sequence numebers -
elixir/trunk/tests/test_associable.py
r347 r349 73 73 user.orders.append(neworder) 74 74 75 session. flush()75 session.commit() 76 76 session.clear() 77 77 … … 109 109 art = Article(title='Hope Soars') 110 110 111 session. flush()111 session.commit() 112 112 session.clear() -
elixir/trunk/tests/test_autoload.py
r347 r349 98 98 lisa = Person(name="Lisa", pets=[snowball]) 99 99 100 session. flush()100 session.commit() 101 101 session.clear() 102 102 … … 119 119 lisa.father = homer 120 120 121 session. flush()121 session.commit() 122 122 session.clear() 123 123 … … 141 141 simpson.persons.extend([bart, lisa]) 142 142 143 session. flush()143 session.commit() 144 144 session.clear() 145 145 … … 158 158 homer = Person(name="Homer", appreciate=[barney]) 159 159 160 session. flush()160 session.commit() 161 161 session.clear() 162 162 … … 219 219 a1 = A(id=1, name="a1") 220 220 221 session. flush()221 session.commit() 222 222 session.clear() 223 223 -
elixir/trunk/tests/test_class_methods.py
r347 r349 22 22 a1 = A(name="a1") 23 23 24 session. flush()24 session.commit() 25 25 session.clear() 26 26 -
elixir/trunk/tests/test_custombase.py
r347 r349 29 29 a1 = A(name="a1") 30 30 31 session. flush()31 session.commit() 32 32 session.clear() 33 33 … … 48 48 b1 = B(name="b1", data="-b1-") 49 49 50 session. flush()50 session.commit() 51 51 session.clear() 52 52 … … 71 71 a1.name = "a1" 72 72 73 session. flush()73 session.commit() 74 74 session.clear() 75 75 -
elixir/trunk/tests/test_dict.py
r322 r349 58 58 t2 = Table2() 59 59 t1.tbl2s.append(t2) 60 el.session. flush()60 el.session.commit() 61 61 t1.from_dict(dict(tbl2s=[])) 62 62 assert len(t1.tbl2s) == 0 … … 66 66 t2 = Table2() 67 67 t1.tbl2s.append(t2) 68 el.session. flush()68 el.session.commit() 69 69 t1.from_dict(dict(tbl2s=[{'id':t2.id, 'name':'test4'}])) 70 70 assert len(t1.tbl2s) == 1 … … 75 75 t2 = Table2() 76 76 t1.tbl2s.append(t2) 77 el.session. flush()77 el.session.commit() 78 78 try: 79 79 t1.from_dict(dict(tbl2s=[{'id':t2.id+1}])) … … 95 95 t2 = Table2(id=50, name='test4') 96 96 t1.tbl2s.append(t2) 97 el.session. flush()97 el.session.commit() 98 98 assert t1.to_dict(deep={'tbl2s':{}}) == \ 99 99 {'id':52, 'name':'test3', 'tbl2s':[{'id':50, 'name':'test4'}]} … … 102 102 t1 = Table1(id=53, name='test2') 103 103 t1.tbl3 = Table3(id=50, name='wobble') 104 el.session. flush()104 el.session.commit() 105 105 assert t1.to_dict(deep={'tbl3':{}}) == \ 106 106 {'id':53, 'name':'test2', 'tbl3':{'id':50,'name':'wobble'}} -
elixir/trunk/tests/test_encryption.py
r347 r349 51 51 jonathan.pets = [winston, nelson] 52 52 53 session. flush(); session.clear()53 session.commit(); session.clear() 54 54 55 55 p = Person.get_by(name='Jonathan LaCour') … … 63 63 p.password = 'N3wpAzzw0rd' 64 64 65 session. flush(); session.clear()65 session.commit(); session.clear() 66 66 67 67 p = Person.get_by(name='Jonathan LaCour') … … 69 69 p.name = 'Jon LaCour' 70 70 71 session. flush(); session.clear()71 session.commit(); session.clear() -
elixir/trunk/tests/test_events.py
r347 r349 72 72 def test_events(self): 73 73 d = Document(name='My Document') 74 session. flush(); session.clear()74 session.commit(); session.clear() 75 75 76 76 d = Document.query.get(1) 77 77 d.name = 'My Document Updated' 78 session. flush(); session.clear()78 session.commit(); session.clear() 79 79 80 80 d = Document.query.get(1) 81 81 d.delete() 82 session. flush(); session.clear()82 session.commit(); session.clear() 83 83 84 84 assert before_insert_called == 1 -
elixir/trunk/tests/test_fields.py
r347 r349 22 22 bart = Person(firstname="Bart", surname="Simpson") 23 23 24 session. flush()24 session.commit() 25 25 session.clear() 26 26 … … 39 39 bart = Person(firstname="Bart", surname="Simpson") 40 40 41 session. flush()41 session.commit() 42 42 session.clear() 43 43 … … 58 58 bart = Person(firstname="Bart", surname="Simpson") 59 59 60 session. flush()60 session.commit() 61 61 session.clear() 62 62 -
elixir/trunk/tests/test_inherit.py
r347 r349 46 46 E(data1='e1') 47 47 48 session. flush()48 session.commit() 49 49 session.clear() 50 50 … … 82 82 b1 = B(name="b1") # no value for other 83 83 84 session. flush()84 session.commit() 85 85 86 86 def test_delete_parent(self): … … 96 96 b1 = B(name='b1') 97 97 98 session. flush()98 session.commit() 99 99 100 100 A.table.delete().execute() -
elixir/trunk/tests/test_m2m.py
r347 r349 27 27 b1 = B(name='b1', as_=[A(name='a1')]) 28 28 29 session. flush()29 session.commit() 30 30 session.clear() 31 31 … … 51 51 b1 = B(name='b1', as_=[A(name='a1')]) 52 52 53 session. flush()53 session.commit() 54 54 session.clear() 55 55 … … 83 83 b1 = B(name='b1', as_=[A(key1=10, key2='a1')]) 84 84 85 session. flush()85 session.commit() 86 86 session.clear() 87 87 … … 108 108 rel2=[B(name='b3'), B(name='b4'), b1]) 109 109 110 session. flush()110 session.commit() 111 111 session.clear() 112 112 … … 131 131 barney.friends.append(homer) 132 132 133 session. flush()133 session.commit() 134 134 session.clear() 135 135 … … 156 156 a3 = A(name='a3') 157 157 158 session. flush()158 session.commit() 159 159 session.clear() 160 160 -
elixir/trunk/tests/test_m2o.py
r347 r349 24 24 b1 = B(name='b1', a=A(name='a1')) 25 25 26 session. flush()26 session.commit() 27 27 session.clear() 28 28 … … 45 45 b1 = B(a=A(testx=1)) 46 46 47 session. flush()47 session.commit() 48 48 session.clear() 49 49 … … 69 69 70 70 a = A() 71 session. flush()71 session.commit() 72 72 b = B(a=a) 73 session. flush()73 session.commit() 74 74 session.clear() 75 75 … … 166 166 b2 = B(name="b2", a_rel1=a1, a_rel2=a1) 167 167 168 session. flush()168 session.commit() 169 169 session.clear() 170 170 … … 190 190 rudolph = Animal(name="Rudolph", owner=santa) 191 191 192 session. flush()192 session.commit() 193 193 session.clear() 194 194 -
elixir/trunk/tests/test_o2m.py
r347 r349 26 26 b1 = B(name='b1', a=a1) 27 27 28 # does it work before a flush? (does the backref work?)28 # does it work before a commit? (does the backref work?) 29 29 assert b1 in a1.bs 30 30 31 session. flush()31 session.commit() 32 32 session.clear() 33 33 … … 55 55 lisa.father = homer 56 56 57 session. flush()57 session.commit() 58 58 session.clear() 59 59 … … 99 99 node.children.append(TreeNode(name='node3')) 100 100 101 session. flush()101 session.commit() 102 102 session.clear() 103 103 … … 119 119 rudolph = Animal(name="Rudolph", owner=santa) 120 120 121 session. flush()121 session.commit() 122 122 session.clear() 123 123 -
elixir/trunk/tests/test_o2o.py
r271 r349 21 21 b1 = B(name='b1', a=A(name='a1')) 22 22 23 session. flush()23 session.commit() 24 24 session.clear() 25 25 -
elixir/trunk/tests/test_options.py
r347 r349 4 4 5 5 from sqlalchemy import UniqueConstraint, create_engine, Column 6 from sqlalchemy.orm import create_session6 from sqlalchemy.orm import scoped_session, sessionmaker 7 7 from sqlalchemy.exceptions import SQLError, ConcurrentModificationError 8 8 from elixir import * … … 26 26 27 27 p1 = Person(name='Daniel') 28 session. flush()28 session.commit() 29 29 session.clear() 30 30 31 31 person = Person.query.first() 32 32 person.name = 'Gaetan' 33 session.flush() 33 session.commit() 34 assert person.row_version == 2 34 35 session.clear() 35 assert person.row_version == 236 36 37 37 person = Person.query.first() 38 38 person.name = 'Jonathan' 39 session.flush() 39 session.commit() 40 assert person.row_version == 3 40 41 session.clear() 41 assert person.row_version == 342 42 43 43 # check that a concurrent modification raises exception 44 44 p1 = Person.query.first() 45 s2 = create_session()45 s2 = sessionmaker()() 46 46 p2 = s2.query(Person).first() 47 47 p1.name = "Daniel" 48 48 p2.name = "Gaetan" 49 s2. flush()50 try: 51 session. flush()49 s2.commit() 50 try: 51 session.commit() 52 52 assert False 53 53 except ConcurrentModificationError: … … 113 113 cleanup_all() 114 114 115 def test_session_context(self):116 try:117 from sqlalchemy.ext.sessioncontext import SessionContext118 except ImportError:119 # we are probably on SQLAlchemy 0.5, no need to test this.120 return121 122 engine = create_engine('sqlite:///')123 124 ctx = SessionContext(lambda: create_session(bind=engine))125 126 class Person(Entity):127 using_options(session=ctx)128 firstname = Field(String(30))129 surname = Field(String(30))130 131 setup_all()132 create_all(engine)133 134 homer = Person(firstname="Homer", surname='Simpson')135 bart = Person(firstname="Bart", surname='Simpson')136 ctx.current.flush()137 138 assert Person.query.session is ctx.current139 assert Person.query.filter_by(firstname='Homer').one() is homer140 141 115 def test_manual_session(self): 142 116 engine = create_engine('sqlite:///') … … 150 124 create_all(engine) 151 125 152 session = create_session(bind=engine) 126 Session = sessionmaker(bind=engine) 127 session = Session() 153 128 154 129 homer = Person(firstname="Homer", surname='Simpson') … … 157 132 session.save(homer) 158 133 session.save(bart) 159 session. flush()134 session.commit() 160 135 161 136 bart.delete() 162 session. flush()137 session.commit() 163 138 164 139 assert session.query(Person).filter_by(firstname='Homer').one() is homer 165 140 assert session.query(Person).count() == 1 166 141 167 def test_activemapper_session(self): 168 try: 169 from sqlalchemy.orm import scoped_session, sessionmaker 170 #TODO: this test, as-is has no sense on SA 0.4 since activemapper 171 # session uses scoped_session, but we need to provide a new 172 # test for that. 173 return 174 except ImportError: 175 pass 176 177 try: 178 from sqlalchemy.ext import activemapper 179 except ImportError: 180 return 181 142 def test_scoped_session(self): 182 143 engine = create_engine('sqlite:///') 183 184 store = activemapper.Objectstore(lambda: create_session(bind=engine)) 185 186 class Person(Entity): 187 using_options(session=store) 144 Session = scoped_session(sessionmaker(bind=engine)) 145 146 class Person(Entity): 147 using_options(session=Session) 188 148 firstname = Field(String(30)) 189 149 surname = Field(String(30)) … … 194 154 homer = Person(firstname="Homer", surname='Simpson') 195 155 bart = Person(firstname="Bart", surname='Simpson') 196 197 store.flush() 198 199 assert Person.query.session is store.context.current 200 assert Person.query.filter_by(firstname='Homer').one() is homer 201 202 def test_scoped_session(self): 203 try: 204 from sqlalchemy.orm import scoped_session, sessionmaker 205 except ImportError: 206 print "Not on version 0.4 or later of sqlalchemy" 207 return 208 209 engine = create_engine('sqlite:///') 210 211 Session = scoped_session(sessionmaker(bind=engine)) 212 213 class Person(Entity): 214 using_options(session=Session) 215 firstname = Field(String(30)) 216 surname = Field(String(30)) 217 218 setup_all() 219 create_all(engine) 220 221 homer = Person(firstname="Homer", surname='Simpson') 222 bart = Person(firstname="Bart", surname='Simpson') 223 Session.flush() 156 Session.commit() 224 157 225 158 assert Person.query.session is Session() … … 227 160 228 161 def test_global_scoped_session(self): 229 try:230 from sqlalchemy.orm import scoped_session, sessionmaker231 except ImportError:232 print "Not on version 0.4 or later of sqlalchemy"233 return234 235 162 global __session__ 236 163 237 164 engine = create_engine('sqlite:///') 238 239 165 session = scoped_session(sessionmaker(bind=engine)) 240 166 __session__ = session … … 249 175 homer = Person(firstname="Homer", surname='Simpson') 250 176 bart = Person(firstname="Bart", surname='Simpson') 251 session. flush()177 session.commit() 252 178 253 179 assert Person.query.session is session() … … 276 202 bart = Person(firstname="Bart", surname='Simpson') 277 203 278 session. flush()204 session.commit() 279 205 280 206 homer2 = Person(firstname="Homer", surname='Simpson') … … 282 208 raised = False 283 209 try: 284 session. flush()210 session.commit() 285 211 except SQLError: 286 212 raised = True … … 304 230 hobbit = Book(title="The Hobbit", author=tolkien) 305 231 306 session. flush()232 session.commit() 307 233 308 234 tolkien2 = Author(name="Tolkien") 309 235 hobbit2 = Book(title="The Hobbit", author=tolkien2) 310 236 311 session. flush()237 session.commit() 312 238 313 239 hobbit3 = Book(title="The Hobbit", author=tolkien) … … 315 241 raised = False 316 242 try: 317 session. flush()243 session.commit() 318 244 except SQLError: 319 245 raised = True -
elixir/trunk/tests/test_order_by.py
r347 r349 51 51 Record(title=title, artist=artist, year=year, genres=[genre]) 52 52 53 session. flush()53 session.commit() 54 54 session.clear() 55 55 -
elixir/trunk/tests/test_packages.py
r347 r349 31 31 b1 = B(name='b1', as_=[A(name='a1')]) 32 32 33 session. flush()33 session.commit() 34 34 session.clear() 35 35 -
elixir/trunk/tests/test_properties.py
r347 r349 28 28 t2 = Tag(score1=10.0, score2=2.0) 29 29 30 session. flush()30 session.commit() 31 31 session.clear() 32 32 … … 46 46 t2 = Tag(score1=10.0, score2=2.0) 47 47 48 session. flush()48 session.commit() 49 49 session.clear() 50 50 … … 88 88 c1 = Category(name='dummy', users=[u1, u2]) 89 89 90 session. flush()90 session.commit() 91 91 session.clear() 92 92 … … 111 111 t1 = Tag(score1=10.0, score2=2.0) 112 112 113 session. flush()113 session.commit() 114 114 session.clear() 115 115 … … 125 125 126 126 A(name='foo') 127 session. flush()127 session.commit() 128 128 129 129 def test_synonym(self): … … 155 155 assert Person.email_values == ['x@y.com', 'x@z.com'] 156 156 157 session. flush()157 session.commit() 158 158 session.clear() 159 159 … … 184 184 ) 185 185 186 session. flush(); session.clear()186 session.commit(); session.clear() 187 187 188 188 p = Person.get_by(name='Alexandre da Silva') … … 193 193 194 194 u.email_address = 'new@z.com' 195 session. flush(); session.clear()195 session.commit(); session.clear() 196 196 197 197 p = Person.get_by(name='Johann Felipe Voigt') … … 208 208 a1 = A(name='a1') 209 209 210 session. flush(); session.clear()210 session.commit(); session.clear() 211 211 212 212 a = A.query.one() -
elixir/trunk/tests/test_sa_integration.py
r347 r349 41 41 42 42 session.save(b1) 43 session. flush()43 session.commit() 44 44 session.clear() 45 45 … … 71 71 # 72 72 # session.save(b1) 73 # session. flush()73 # session.commit() 74 74 # session.clear() 75 75 # -
elixir/trunk/tests/test_versioning.py
r347 r349 28 28 autoupd = Field(Integer, default=nextOne, onupdate=nextOne) 29 29 director = ManyToOne('Director', inverse='movies') 30 actors = ManyToMany('Actor', inverse='movies', tablename='movie_casting') 30 actors = ManyToMany('Actor', inverse='movies', 31 tablename='movie_casting') 31 32 using_options(tablename='movies') 32 33 acts_as_versioned(ignore=['ignoreme', 'autoupd']) … … 35 36 class Actor(Entity): 36 37 name = Field(String(60)) 37 movies = ManyToMany('Movie', inverse='actors', tablename='movie_casting') 38 movies = ManyToMany('Movie', inverse='actors', 39 tablename='movie_casting') 38 40 using_options(tablename='actors') 39 41 … … 52 54 def teardown(self): 53 55 drop_all() 54 session.cl ear()56 session.close() 55 57 56 58 def test_versioning(self): 57 59 gilliam = Director(name='Terry Gilliam') 58 monkeys = Movie(id=1, title='12 Monkeys', description='draft description', director=gilliam) 60 monkeys = Movie(id=1, title='12 Monkeys', 61 description='draft description', director=gilliam) 59 62 bruce = Actor(name='Bruce Willis', movies=[monkeys]) 60 session. flush(); session.clear()63 session.commit(); session.clear() 61 64 62 65 time.sleep(1) … … 70 73 assert movie.autoupd == 2, movie.autoupd 71 74 movie.description = 'description two' 72 session. flush(); session.clear()75 session.commit(); session.clear() 73 76 74 77 time.sleep(1) … … 78 81 movie = Movie.get_by(title='12 Monkeys') 79 82 movie.description = 'description three' 80 session. flush(); session.clear()83 session.commit(); session.clear() 81 84 82 85 # Edit the ignored field, this shouldn't change the version 83 86 monkeys = Movie.get_by(title='12 Monkeys') 84 87 monkeys.ignoreme = 1 85 session. flush(); session.clear()88 session.commit(); session.clear() 86 89 87 90 time.sleep(1) … … 107 110 assert middle_version.autoupd > oldest_version.autoupd 108 111 109 assert latest_version.version == 3, 'version=%i' % latest_version.version 112 assert latest_version.version == 3, \ 113 'version=%i' % latest_version.version 110 114 assert latest_version.description == 'description three' 111 115 assert latest_version.ignoreme == 1 … … 113 117 114 118 differences = latest_version.compare_with(oldest_version) 115 assert differences['description'] == ('description three', 'draft description') 119 assert differences['description'] == \ 120 ('description three', 'draft description') 116 121 117 122 assert len(movie.versions) == 3 … … 123 128 124 129 movie.revert_to(2) 125 session. flush(); session.clear()130 session.commit(); session.clear() 126 131 127 132 movie = Movie.get_by(title='12 Monkeys') … … 130 135 131 136 movie.description = "description 3" 132 session. flush(); session.clear();137 session.commit(); session.clear() 133 138 134 139 movie = Movie.get_by(title='12 Monkeys') 135 140 movie.description = "description 4" 136 session. flush(); session.clear();141 session.commit(); session.clear() 137 142 138 143 movie = Movie.get_by(title='12 Monkeys') … … 140 145 movie.revert_to(movie.versions[-2]) 141 146 movie.description = "description 5" 142 session. flush(); session.clear();147 session.commit(); session.clear() 143 148 144 149 movie = Movie.get_by(title='12 Monkeys') … … 146 151 assert movie.versions[-2].description == "description 3" 147 152 148 # Updates to the history table must be inside the transaction149 session.begin()150 movie = Movie(id=3, title='Foo', description='1')151 session.commit();152 153 session.begin()154 movie.description = '2'155 session.flush()156 session.rollback()157 session.clear()158 159 session.begin()160 movie = Movie.get_by(title='Foo')161 movie.description = '3'162 session.commit()163
