Trog-Data.t 2.2 KB

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