Trog-SQLite.t 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. use strict;
  2. use warnings;
  3. use Test::More;
  4. use Test::MockModule qw{strict};
  5. use Test::Deep;
  6. use Test::Fatal qw{exception};
  7. use FindBin;
  8. use lib "$FindBin::Bin/../lib";
  9. require_ok('Trog::SQLite') or BAIL_OUT("Can't find SUT");
  10. subtest 'dbh' => sub {
  11. my $readmock = Test::MockModule->new('File::Slurper');
  12. $readmock->redefine('read_text', sub { "SELECT me FROM candidates" });
  13. my $dbimock = Test::MockModule->new("DBI");
  14. $dbimock->redefine('connect', sub { bless({},'TrogDBD') });
  15. my $works = 0;
  16. no warnings qw{redefine once};
  17. local *TrogDBD::do = sub { $works };
  18. like(exception { Trog::SQLite::dbh('bogus','bogus') }, qr/ensure/i, "Failure to enforce schema throws");
  19. $works = 1;
  20. # Otherwise it works
  21. isa_ok(Trog::SQLite::dbh('bogus','bogus'),'TrogDBD');
  22. };
  23. subtest bulk_insert => sub {
  24. like(exception { Trog::SQLite::bulk_insert({},'bogus', [qw{a b c}], 'PROCASTINATE') }, qr/unsupported/i, "insert OR keyword consistency enforced");
  25. like(exception { Trog::SQLite::bulk_insert({},'bogus', []) }, qr/nonempty/, "keys must be provided");
  26. like(exception { Trog::SQLite::bulk_insert({},'bogus',[qw{a b c}],'IGNORE',qw{jello}) }, qr/multiple of/i, "sufficient values must be provided");
  27. my $smt;
  28. my $dbh = bless({},'TrogDBH');
  29. no warnings qw{redefine once};
  30. local *TrogDBH::prepare = sub { $smt .= $_[1]; return bless({},'TrogSMT') };
  31. local *TrogSMT::execute = sub {};
  32. is(exception { Trog::SQLite::bulk_insert($dbh,'bogus', [qw{moo cows}], 'IGNORE', qw{a b c d}) }, undef, "can do bulk insert");
  33. is($smt, "INSERT OR IGNORE INTO bogus (moo,cows) VALUES (?,?),(?,?)", "Expected query prepared");
  34. # Million insert
  35. $smt='';
  36. my $keys = [("a") x 10];
  37. my @values = ("b") x (10**6);
  38. Trog::SQLite::bulk_insert($dbh,'bogus', $keys, 'IGNORE', @values);
  39. my $expected = "INSERT OR IGNORE INTO bogus (a,a,a,a,a,a,a,a,a,a) VALUES (?,?,?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?,?,?),(?,?,?,?,?,?,?,?,?,?)INSERT OR IGNORE INTO bogus (a,a,a,a,a,a,a,a,a,a) VALUES (?,?,?,?,?,?,?,?,?,?)";
  40. is($smt,$expected, "As expected, only two statements are necessary to be prepared, no matter how many rows to insert.");
  41. };
  42. done_testing;