Testing database (was Re: [jifty-devel] Tutorial questions)
Nelson Elhage
nelhage at bestpractical.com
Thu Nov 9 11:22:17 EST 2006
Nifty.
Is there a reason you're generating your own SQL, instead of
instatiating record objects and calling create() on them?
I do like the idea of maintaining test data in data files, as well as
making it per-test, instead of global.
- Nelson
On Thu, Nov 09, 2006 at 09:52:04AM -0500, Henry Baragar wrote:
> Hi,
>
> We want to use fixtures in Jifty similar to what Ruby does. We found an old
> message in the archive (below) which provides a solution.
>
> However, we find it easier to maintain the fixture data separately from the
> test code, particularly when there are complex linkages between the tables.
> Consequently, we have set up our system to load fixtures out of
> t/fixtures/*.yml using the following code:
>
> use YAML "LoadFile";
>
> # Load the fixtures
> sub load_fixtures {
> for my $file (map {<t/fixtures/$_.yml>} @_) {
> my ($table) = $file =~ /(\w+)[.]yml$/;
> load_yml($table,LoadFile($file));
> }
> }
>
> sub load_yml {
> my $table = shift;
> our $dbh = MyApp::Model::Table->_handle->dbh;
> for my $row (@_) {
> my %row = %$row;
> $dbh->do(insert_statement($table, keys %row), undef, values %row)
> or die $dbh->errstr;
> }
> }
>
> sub insert_statement {
> my $table = shift;
> my $cols = join ",", @_;
> my $binds = join ",", ("?") x @_;
> "insert into $table ($cols) values ($binds)";
> }
>
> A couple of notes:
>
> 1. We call load_fixtures as part of our setup in each test file
> as follows:
> load_fixtures(qw/model_ones model_twos/);
> or as:
> load_fixtures("model*");
>
> 2. We create the insert_statement every time in case different fixtures
> in a file list different fields.
>
> Regards,
> Henry
>
> On Wed Jan 4 14:10:46 EST 2006, Alex Vandiver wrote:
> [stuff deleted]
> > > 2) Fixtures - this is arguably the second most powerful feature of
> > > ActiveRecord (which is Ruby's ORM). If given a YAML file named
> > > appropriately, it will load that file into the table when asked. These
> > > are generally used for testing, but are really nice to have when
> > > resetting the dev database or for demos. I've also found them really
> > > handy for actually fleshing out how you want the schema to work.
> > Here's how we're doing this in one of our in-house Jifty apps -- this
> > can certainly be made better, but it's partially there. It's like
> > making fixtures, but in code rather than a data file.
> >
> > package MyApp::Test;
> > use base qw/Jifty::Test/;
> >
> > sub setup {
> > my $class = shift;
> > $class->SUPER::setup;
> >
> > my $ADMIN = MyApp::CurrentUser->superuser;
> >
> > my $widget = MyApp::Model::Thingy->new(current_user => $ADMIN);
> > $widget->create(
> > foo => "bar",
> > baz => "troz",
> > );
> > # And so on..
> > }
> >
> > # You can also:
> > sub test_config {
> > # Customize the config file for testing
> > my $class = shift;
> >
> > my ($config) = @_;
> > my $hash = $class->SUPER::test_Config($config);
> > $hash->{framework}{LogConfig} = "some/log4perl.conf";
> > return $hash;
> > }
> _______________________________________________
> jifty-devel mailing list
> jifty-devel at lists.jifty.org
> http://lists.jifty.org/cgi-bin/mailman/listinfo/jifty-devel
More information about the jifty-devel
mailing list