python - How can I use a different schema for a SqlAlchemy model class in a Pyramid application during testing -
for pyramid application uses multiple databases. declaring sqlalchemy classes this:
class a(mybase): __table_args__ = {'schema': 'schema_a'} class b(mybase): __table_args__ = {'schema': 'schema_b'} i want test type of database used in production (mysql). however, having big problem changing schema, because __table_args__ processes (at module load time).
i have been trying 'pytest-dbfixtures' makes possible start empty mysql database. reason approach not compatible standard pyramid ` approach creates sqlalchemy session mill @ module load time:
dbsession = scoped_session(sessionmaker(extension=zopetransactionextension())) so resorting like:
class a(mybase): __table_args__ = {'schema': function_generating_schema_name()} where function_generating_schema_name ugly checks see if code invoked testrunner or not. there should more elegant way!
i solved launching instance of mysql dedicated testing. instead of using differently named testing database on same mysql server, have mysql server on different port. running multiple servers built mysql (see docs here , here), here's how set things on debian 7:
add configuration new server/etc/mysql/my.cnf. initially, set new file in /etc/mysql/conf.d/mynewserver, mysqld_multi did not pick there (apparently 1 can have totally separate config file , utilize mysqld_multi --defaults-file=/path/to/file, digress). mysql's multi server feature identifies each server positive integer can pick, chose 13: [mysqld13] pid-file = /var/run/mysqld/mysqld_13.pid socket = /var/run/mysqld/mysqld_13.sock port = 3307 datadir = /var/lib/mysql_13 tmpdir = /tmp/mysql_13 log_error = /var/log/mysql/error_13.log create /var/lib/mysql_13 , /tmp/mysql_13 dirs, chown mysql:mysql
initialize datadir: mysql_install_db --user=mysql --datadir=/var/lib/mysql_13
start new server: mysqld_multi start 13
try connect: mysql -u root -p 3307 -h 127.0.0.1. nb, have mysql bound 0.0.0.0 , access using tcp/ip, you're on standard unix socket configuration, mysql needs --socket=/var/run/mysqld/mysqld_13.sock instead of port number.
python sqlalchemy pyramid py.test
No comments:
Post a Comment