Re: [Catalyst] Using test database with TWAM?
On Thu, Apr 30, 2009 at 05:29:12PM +0100, Iain wrote: > If your wondering why I went to all this trouble. We have several > developers so running multiple tests at the same time caused all kinds > of strange problems and nobody wanted to maintain a DB per developer! If you're using ::Fixtures and ->deploy maintaining a DB per developer is -easy-. For shadowcat clients we usually set them up with a db, per developer, per major branch. Works like a dream. -- Matt S Trout Catalyst and DBIx::Class consultancy with a clue Technical Director and a commit bit: http://shadowcat.co.uk/catalyst/ Shadowcat Systems Limited mst (@) shadowcat.co.ukhttp://shadowcat.co.uk/blog/matt-s-trout/ ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Using test database with TWAM?
On Thu, Apr 30, 2009 at 11:42:39AM -0400, Jesse Sheidlower wrote: > > I assume this is more of a general Perl testing question than > a Catalyst question, but > > I finally sat down to write real tests for one of my apps, > with the hopes that I'll do this regularly for everything. I > was using the Tutorial chapter on testing, and the docs for > Test::WWW::Mechanize::Catalyst, for ideas. > > I quickly hit a wall because I couldn't figure out how to run > this with a test database. Since I need to test modification > and deletion stuff, but have real data, I can't just run this > against my actual app. So I figured I would dump the sql from > my production app and have my test program load this into a > database called "test-db" or whatever, having the same format > and schema as the real db, and then just drop the test db at > the end of the test. You want DBIx::Class::Fixtures for the dump/load crap, and DBIC's ->deploy method for the "dump/create the tables" stuff. Hop onto the dbix-class list if you get stuck with that part. > Unfortunately I don't know how to do this from a test script, > and I don't know how to tell TWAM to run a particular app but > with a different DBIC schema (in this case, the identical one > but with a different name, pointing to the test db) Then you want to read the ConfigLoader docs and search for LOCAL_SUFFIX for how to define a testing config. -- Matt S Trout Catalyst and DBIx::Class consultancy with a clue Technical Director and a commit bit: http://shadowcat.co.uk/catalyst/ Shadowcat Systems Limited mst (@) shadowcat.co.ukhttp://shadowcat.co.uk/blog/matt-s-trout/ ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Using test database with TWAM?
On Fri, May 1, 2009 at 4:10 AM, Jesse Sheidlower wrote: > > Thanks--this is all very useful. I do not, in fact, have any > reason for a different schema; a different DSN is all I need. > > I do keep the connection details in a config file. > > I guess my question then becomes the more prosaic one of > detail, esp. as I haven't really done much with tests before: > How do I populate this database from within my test script? > How do I get my test script to use a different config file for > the test database? And Catalyst-wise, how do I run TWAM > against the version of my app that's using this other > config-file setup, either from the test server, or externally? > > Thanks. I do know there's the bit in the Testing tutorial > about using a test database, but the larger-scale things > weren't really addressed. (I'll add them to the Tutorial, if I > get good answers!) > I actually serialize out my test data into CSV files, and then use this: http://our.coldhardcode.com/jshirley/2009/01/initializing-a-database-with-d.html You can Moose this up now... it's a rather old recipe that I've been taking around with me for some time. -J ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Using test database with TWAM?
On Thu, Apr 30, 2009 at 03:10:13PM -0400, Jesse Sheidlower wrote: > > Thanks--this is all very useful. I do not, in fact, have any > reason for a different schema; a different DSN is all I need. > > I do keep the connection details in a config file. > > I guess my question then becomes the more prosaic one of > detail, esp. as I haven't really done much with tests before: > How do I populate this database from within my test script? I have a module that knows how to create a database and then import a database dump that's kept in svn. We use Postgresql so the module simply does a createdb and a pg_restore. The database is dropped on DESTROY after the test is run. The module sets environment variables pointing to the database it created that are then used by the test script (or really Catalyst) when the tests run. This module is used at the top of any test script that needs a database for the tests. Like Peter, I have a config file that has different sections for different "modes" of operation. Mostly used for dev, testing, qa, staging, and production configurations. I have a module that manages the configuration. That is, it's a module that you pass an application "home" directory and a "mode" and it returns a hash of the config. It's separate from Catalyst because it's used by more than just Catalyst. Now, normally the configuration for the database is defined for production, staging, etc. but for "make_test" (which is the default) the dsn is empty: make_test: database: dsn:dbi:Pg: user: password: It's possible to override the host, port, etc. if the database server is on another machine. The module that creates the test database sees this and populates the PG* environment variables, creates the database and imports the data, then the test script runs and likewise the application then uses the same PG* environment variables to access the test database. > How do I get my test script to use a different config file for > the test database? Well, mostly it's not the test script that needs the configuration data, but the application. In my case all that's needed is an environment variable that says what "mode" to run in. I have a plugin for Catalyst that loads the configuration file. It's a thin wrapper around the module that loads config data. It uses an environment variable to select the "mode", but the plugin defaults to "development", which is the more common case (when running the built in server). So, test scripts can ask for a database from a common module. That module figures out where the application home is and loads the config. It's loaded in "make_test" mode by default. Since the dsn is not set the PG* environment variables are used to tell everything what database to use. When Catalyst then goes to load the config an environment variable is set that says to use "make_test". It's probably less complex actually than my description above. ;) -- Bill Moseley. mose...@hank.org Sent from my iMutt ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Using test database with TWAM?
On Thu, Apr 30, 2009 at 11:12:39AM -0500, Peter Karman wrote: > Jesse Sheidlower wrote on 04/30/2009 10:42 AM: > > > Unfortunately I don't know how to do this from a test script, > > and I don't know how to tell TWAM to run a particular app but > > with a different DBIC schema (in this case, the identical one > > but with a different name, pointing to the test db) instead. > > Or how to do the same thing, but with TWAM hitting the app > > with the external-server method (so I can test the Apache > > deployment). > > > > Would be grateful for any pointers. > > Here's what I do. > > I have 3 different domains (environments): dev, test and prod. I > indicate which one I want with an environment variable, which defaults > to 'dev' (so if I forget, I only affect development data). > > % MY_DOMAIN=test make test > > My base db class (I use RDBO but DBIC would work just as well) checks > that env var and picks a profile to use based on the domain value. > > I maintain 3 database instances, one for each domain. I basically take a > dump of my production data via cron on a regular basis, and then import > a recent dump to my test and dev dbs as needed. That's a one-line > system() call in my test scripts in a BEGIN block. And Zbigniew Lukasiak wrote: > Hmm - first question why a different DBIC schema? Why not use the > same schema only connected to a different dsn? > And if you go that path - then you can move the connection details > (dsn) from the Catalyst model code into a config file. The final step > would be to use different config files for the tests and for the real > app. Thanks--this is all very useful. I do not, in fact, have any reason for a different schema; a different DSN is all I need. I do keep the connection details in a config file. I guess my question then becomes the more prosaic one of detail, esp. as I haven't really done much with tests before: How do I populate this database from within my test script? How do I get my test script to use a different config file for the test database? And Catalyst-wise, how do I run TWAM against the version of my app that's using this other config-file setup, either from the test server, or externally? Thanks. I do know there's the bit in the Testing tutorial about using a test database, but the larger-scale things weren't really addressed. (I'll add them to the Tutorial, if I get good answers!) Jesse ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Using test database with TWAM?
On Thu, 2009-04-30 at 11:42 -0400, Jesse Sheidlower wrote: [snip] > I quickly hit a wall because I couldn't figure out how to run > this with a test database. Since I need to test modification > and deletion stuff, but have real data, I can't just run this > against my actual app. So I figured I would dump the sql from > my production app and have my test program load this into a > database called "test-db" or whatever, having the same format > and schema as the real db, and then just drop the test db at > the end of the test. The only way I managed to do this and keep my sanity was to use the "fat model" method and then test the model layer specifically (DBIC in my case) outside of catalyst. To help with this I started using with DBICx::TestDatabase http://search.cpan.org/~jrockway/DBICx-TestDatabase-0.02/lib/DBICx/TestDatabase.pm This worked great, until I ran into problems with code relying on features of MySQL (yes I know) I then cooked up a bit (ok a lot) of a hack that relies on MySQL allowing you to create a temporary table with the same name as an existing table, the real table gets hidden. Leaving you with an empty clone of your table that disappears when you close the connection. http://dev.mysql.com/doc/refman/5.1/en/create-table.html so now when I run my tests I clone an empty DB for the duration of the test run. You have to populate it with canned data but this gives you a very consistent way to test. Again you need to be doing your heavy lifting in your model (DBIC) for this to work well. How safe this is in the long run is anybody's guess but so far its working quite well. Just don't do it on a replicating master If your wondering why I went to all this trouble. We have several developers so running multiple tests at the same time caused all kinds of strange problems and nobody wanted to maintain a DB per developer! Iain. ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Using test database with TWAM?
On Thu, Apr 30, 2009 at 5:42 PM, Jesse Sheidlower wrote: > > I assume this is more of a general Perl testing question than > a Catalyst question, but > > I finally sat down to write real tests for one of my apps, > with the hopes that I'll do this regularly for everything. I > was using the Tutorial chapter on testing, and the docs for > Test::WWW::Mechanize::Catalyst, for ideas. > > I quickly hit a wall because I couldn't figure out how to run > this with a test database. Since I need to test modification > and deletion stuff, but have real data, I can't just run this > against my actual app. So I figured I would dump the sql from > my production app and have my test program load this into a > database called "test-db" or whatever, having the same format > and schema as the real db, and then just drop the test db at > the end of the test. > > Unfortunately I don't know how to do this from a test script, > and I don't know how to tell TWAM to run a particular app but > with a different DBIC schema (in this case, the identical one > but with a different name, pointing to the test db) instead. > Or how to do the same thing, but with TWAM hitting the app > with the external-server method (so I can test the Apache > deployment). > > Would be grateful for any pointers. Hmm - first question why a different DBIC schema? Why not use the same schema only connected to a different dsn? And if you go that path - then you can move the connection details (dsn) from the Catalyst model code into a config file. The final step would be to use different config files for the tests and for the real app. -- Zbigniew Lukasiak http://brudnopis.blogspot.com/ http://perlalchemy.blogspot.com/ ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
Re: [Catalyst] Using test database with TWAM?
Jesse Sheidlower wrote on 04/30/2009 10:42 AM: > Unfortunately I don't know how to do this from a test script, > and I don't know how to tell TWAM to run a particular app but > with a different DBIC schema (in this case, the identical one > but with a different name, pointing to the test db) instead. > Or how to do the same thing, but with TWAM hitting the app > with the external-server method (so I can test the Apache > deployment). > > Would be grateful for any pointers. Here's what I do. I have 3 different domains (environments): dev, test and prod. I indicate which one I want with an environment variable, which defaults to 'dev' (so if I forget, I only affect development data). % MY_DOMAIN=test make test My base db class (I use RDBO but DBIC would work just as well) checks that env var and picks a profile to use based on the domain value. I maintain 3 database instances, one for each domain. I basically take a dump of my production data via cron on a regular basis, and then import a recent dump to my test and dev dbs as needed. That's a one-line system() call in my test scripts in a BEGIN block. Having the 3 database instances is also nice for schema dev work, as I can make changes in dev, test them with my code, and then deploy the changes to test domain, test code, repeat for prod (unless my tests are somehow destructive of data, in which case I have checks for that env var in my tests and only run in test/dev.) HTH pek -- Peter Karman . pe...@peknet.com . http://peknet.com/ ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/
[Catalyst] Using test database with TWAM?
I assume this is more of a general Perl testing question than a Catalyst question, but I finally sat down to write real tests for one of my apps, with the hopes that I'll do this regularly for everything. I was using the Tutorial chapter on testing, and the docs for Test::WWW::Mechanize::Catalyst, for ideas. I quickly hit a wall because I couldn't figure out how to run this with a test database. Since I need to test modification and deletion stuff, but have real data, I can't just run this against my actual app. So I figured I would dump the sql from my production app and have my test program load this into a database called "test-db" or whatever, having the same format and schema as the real db, and then just drop the test db at the end of the test. Unfortunately I don't know how to do this from a test script, and I don't know how to tell TWAM to run a particular app but with a different DBIC schema (in this case, the identical one but with a different name, pointing to the test db) instead. Or how to do the same thing, but with TWAM hitting the app with the external-server method (so I can test the Apache deployment). Would be grateful for any pointers. Thanks. Jesse Sheidlower ___ List: Catalyst@lists.scsys.co.uk Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/ Dev site: http://dev.catalyst.perl.org/