Testing database (was Re: [jifty-devel] Tutorial questions)
Henry Baragar
Henry.Baragar at instantiated.ca
Thu Nov 9 12:20:27 EST 2006
On Thursday, November 09 2006 11:22 am, Nelson Elhage wrote:
> Nifty.
>
> Is there a reason you're generating your own SQL, instead of
> instatiating record objects and calling create() on them?
>
A couple of reasons (not necessarily very good ones):
1. I don't know how to convert table names to class names
(or vice-versa)
2. Cut'n'paste and minor edits of code that was written for
another application/purpose
3. I was following the Ruby example of not going through the
model for loading fixtures
I am open to suggestions for improvements.
Regards,
Henry
> 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
> >
More information about the jifty-devel
mailing list