[sqlalchemy] Testing sqlalchemy applications
I have a question related to sqlalchemy and testing, not sure if this is the best place to ask so let me know if I am asking here in error. I am trying to learn to write and run tests using py.test. Currently I am working on a spreadsheet scrapper that gathers data from a directory tree of spreadsheets and puts them into an sqlite database to be manipulated from there. In any function that has a session.query object however I get a "global name 'session' not defined" error when I run the tests. Here is an example of the code: def get_list_of_filesprocessed(): '''Look in filesprocessed table and return list of all the values in the filename field'' return [r.filename for r in session.query(FilesProcessed).all()] if __name__ == '__main__': engine = create_engine('sqlite:///scrapper.db', echo=True) Base = declarative_base() Session = sessionmaker(bind=engine) session = Session() How can I make the session available to the tests when they are run? I have tried creating a decorator in the test file to initialize the session to no avail. Any hints are appreciated. thomas -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Getting the entity name, ideally the ORM class in the shard selector?
from that expr, assuming it's a Column, you should be able to say attribute.expression.table, where "attribute" is that InstrumentedAttribute object. If it can be any kind of expression, then you'd need to look into some of the SQL utilities to find tables within the expression. On Apr 22, 2013, at 8:01 PM, Rob Fowler wrote: > If the query does not have a table in it we don't get the tables. > For example, the following simple query gets a list of connection_strings > column from the Connection table: > > aa = session.query(Connection.connection_string) > aa.column_descriptions > > [{'aliased': False, > 'expr': , > 'name': 'connection_string', > 'type': String(length=100)}] > > Exploring into the expr I am not sure how to get the table from that. > > On Tuesday, 23 April 2013 02:32:15 UTC+10, Michael Bayer wrote: > > > use column_descriptions: > http://docs.sqlalchemy.org/en/rel_0_8/orm/query.html#sqlalchemy.orm.query.Query.column_descriptions > > > > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@googlegroups.com. > Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Getting the entity name, ideally the ORM class in the shard selector?
If the query does not have a table in it we don't get the tables. For example, the following simple query gets a list of connection_strings column from the Connection table: aa = session.query(Connection.connection_string) aa.column_descriptions [{'aliased': False, 'expr': , 'name': 'connection_string', 'type': String(length=100)}] Exploring into the expr I am not sure how to get the table from that. On Tuesday, 23 April 2013 02:32:15 UTC+10, Michael Bayer wrote: > > > > use column_descriptions: > http://docs.sqlalchemy.org/en/rel_0_8/orm/query.html#sqlalchemy.orm.query.Query.column_descriptions > > > > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
[sqlalchemy] is this select correct ?
This seems to work. I'm just hoping to run this past the wisdom of other users... SqlAlchemy + PostgreSQL We have a table called `example` which contains versioned "fields" of a record, attributed to an owner. Goal: assemble a record of the most recent field versions for a given owner class Example(): id = sa.Column(sa.Integer, primary_key=True) owner_id = sa.Column(sa.Integer, sa.ForeignKey("owner.id"), nullable=False, ) field_id = sa.Column(sa.Integer, sa.ForeignKey("field.id"), nullable=False, ) version = sa.Column(sa.Integer, nullable=False, default=0) text = sa.Column(sa.Text, default='', nullable=True ) result = dbSession.writer.query( model.core.Example )\ .filter(\ model.core.Example.owner_id == 100 )\ .distinct( model.core.Example.field_id )\ .order_by( model.core.Example.field_id.desc() , model.core.Example.version.desc() )\ .all() SQL SELECT DISTINCT ON ( field_id ) id , field_id , owner_id , version , text FROM example WHERE owner_id = 100 ORDER BY field_id , version DESC; -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] TypeDecorator to SQL expression ..?
On Apr 22, 2013, at 6:08 AM, Julien Cigar wrote: > Hello, > > With the following custom type: > > class JSONEncodedDict(types.TypeDecorator): >""" Represents an immutable structure as a JSON-encoded string. """ > >impl = types.TEXT > >def process_bind_param(self, value, dialect): >return json.dumps(value) if value is not None else None > >def process_result_value(self, value, dialect): >return json.loads(value) if value is not None else None > > is there a method to override to automatically transform instances of this > class when they're used in a SQL expression (for example an > myquery.order_by(col), where col is an instance of JSONEncodedDict)? process_bind_param() will take effect anywhere you put a literal value into a query, including order_by(). An expression like myquery.order_by(col), assuming "col" is a Column object or similar, is already representative of a column name in SQL so there's nothing to be transformed there.If you want special SQL expressions to render within the query based on the usage of a type, there's a new method called "bind_expression()" which is documented here: http://docs.sqlalchemy.org/en/rel_0_8/core/types.html#types-sql-value-processing -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Getting the entity name, ideally the ORM class in the shard selector?
On Apr 21, 2013, at 10:04 PM, Rob Fowler wrote: > We are using the sharding module included in our application. > > Currently we are selecting shards based on field. This works really well, as > the field that gets hashed to select the shard needs to be migrated to the > other tables that exist only in that shard. > > Now we would like to have some static entities in all databases we would like > to use the table name to indicated that the table is available in all shards. > Ideally we would define a parent class 'unsharded' and derive from that class > for all the unsharded entities. > > I can see the current entities are in _entities, but as that begins with an > underscore I don't think I am meant to be using it. > > What should I use to get the entities from the query? > use column_descriptions: http://docs.sqlalchemy.org/en/rel_0_8/orm/query.html#sqlalchemy.orm.query.Query.column_descriptions > > ps. Loving sqlalchemy and just started setting up Alembic for our project > (flask, sqlalchemy, postgres). > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@googlegroups.com. > Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
[sqlalchemy] TypeDecorator to SQL expression ..?
Hello, With the following custom type: class JSONEncodedDict(types.TypeDecorator): """ Represents an immutable structure as a JSON-encoded string. """ impl = types.TEXT def process_bind_param(self, value, dialect): return json.dumps(value) if value is not None else None def process_result_value(self, value, dialect): return json.loads(value) if value is not None else None is there a method to override to automatically transform instances of this class when they're used in a SQL expression (for example an myquery.order_by(col), where col is an instance of JSONEncodedDict)? Thank you! Julien -- No trees were killed in the creation of this message. However, many electrons were terribly inconvenienced. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.