LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I'm developing a DBIx distribution and need to check plenty of SQL in the tests.

My approach is to use SQLite as primary engine, since it comes with the DBD::SQLite module installed and that's not much of a prerequisite (it's even comes bundled with Strawberry Perl!)

My issue is where to put the temporary SQLite file...

Those SQLite files don't become big, but I'm not sure about writing stuff into the dist directory. On a side note:

I want to be able to switch to other DB-Servers while testing. (But now really author's side unless explicitly wanted by the user)

What's the best approach to make this configurable?

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re: CPAN test suites with SQL
by Corion (Patriarch) on Apr 14, 2024 at 10:35 UTC

    Why not keep the SQLite files in :memory: ?

    my $dbh = DBI->connect("dbi:SQLite:dbname=:memory:","","");

    Alternatively, have the filenames be created by File::Temp and clean up afterwards:

    use File::Temp 'tempfile'; my ($fh, $tempname) = tempfile(); close $fh; END { unlink $tempname }; my $dbh = DBI->connect("dbi:SQLite:dbname=$tempname","","");

      I've found Test::TempDir::Tiny to be pretty useful for this. It requires less code (it wraps File::Temp), it handles the cleanup, and also leaves the files on disk if the tests fail.

        Oh, nice one. I'll probably start using this.
      > Why not keep the SQLite files in :memory: ?

      Cool, didn't know that one!

      Should have asked earlier! :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        As for testing against other databases: You can, in some cases, just spin up a temporary instance. If i remember correctly, DBD::Pg does (or "did"?) this during testing for PostgreSQL by using a temporary directory. And i'm pretty sure you can start a mysqld instance the same way.

        PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP