Trog-Data.t 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. use strict;
  2. use warnings;
  3. use Test::More;
  4. use Test::MockModule;
  5. use Config::Simple ();
  6. use File::Temp ();
  7. use Path::Tiny ();
  8. use FindBin;
  9. use lib "$FindBin::Bin/../lib";
  10. use_ok('Trog::Data') or BAIL_OUT("Can't find SUT");
  11. # Ahead of the tests, which point its database somewhere else: model() requires
  12. # it, and a first load would put the real path back.
  13. use Trog::Data::SQLite ();
  14. no warnings qw{once};
  15. sub config_for {
  16. my ($model) = @_;
  17. my $conf = Config::Simple->new( syntax => 'ini' );
  18. $conf->param( 'general.data_model', $model );
  19. return $conf;
  20. }
  21. subtest 'model' => sub {
  22. my $dir = Path::Tiny->new( File::Temp::tempdir( CLEANUP => 1 ) );
  23. local $Trog::Data::flatfile_store = "$dir/files";
  24. local $Trog::Data::SQLite::dbname = "$dir/posts.sqlite";
  25. my @warned;
  26. my $mock = Test::MockModule->new('Trog::Data');
  27. $mock->redefine( WARN => sub { push( @warned, @_ ) } );
  28. is( Trog::Data::model( config_for('FlatFile') ), 'FlatFile', 'FlatFile is served as configured' );
  29. is( Trog::Data::model( config_for('SQLite') ), 'SQLite', 'a new site with no flat file store gets SQLite' );
  30. $dir->child('files')->mkpath;
  31. is( Trog::Data::model( config_for('SQLite') ), 'SQLite', 'and so does one whose store is empty' );
  32. is( scalar(@warned), 0, 'neither of which is worth a warning' );
  33. $dir->child( 'files', 'a-post' )->spew('{}');
  34. $dir->child( 'files', 'another' )->spew('{}');
  35. is( Trog::Data::model( config_for('SQLite') ), 'FlatFile', 'a site with flat file posts and no database keeps serving its posts' );
  36. is( scalar(@warned), 1, 'with a warning' );
  37. like( $warned[0] // '', qr{holds 2 post\(s\).*bin/migrate\.pl}a, 'saying how many, and how to move them' );
  38. $dir->child('posts.sqlite')->touch;
  39. @warned = ();
  40. is( Trog::Data::model( config_for('SQLite') ), 'SQLite', 'once the migration has written the database, it is served' );
  41. is( scalar(@warned), 0, 'without a warning' );
  42. is( Trog::Data::model( config_for('FlatFile') ), 'FlatFile', 'and a site configured for FlatFile is never moved' );
  43. };
  44. done_testing();