Trog-SQLite-TagIndex.t 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env perl
  2. use 5.040;
  3. use re '/aasx';
  4. use warnings FATAL => 'all';
  5. use Test::More;
  6. use Cwd ();
  7. use File::Copy ();
  8. use File::Path ();
  9. use File::Temp ();
  10. use FindBin;
  11. use FindBin::libs;
  12. use_ok('Trog::SQLite::TagIndex') or BAIL_OUT("Can't find SUT");
  13. # The index opens schema/ and data/ relative to the cwd, so it gets a root of
  14. # its own with the real schema in it.
  15. my $oldcwd = Cwd::getcwd();
  16. my $root = File::Temp::tempdir( CLEANUP => 1 );
  17. File::Path::make_path( "$root/schema", "$root/data" );
  18. File::Copy::copy( "$FindBin::Bin/../schema/flatfile.schema", "$root/schema/flatfile.schema" ) or die "Could not copy the schema: $!";
  19. chdir $root or die "Could not chdir to $root: $!";
  20. # A data model holding whatever posts it is given, answering read() and get()
  21. # the way build_index() and build_routes() ask.
  22. package Fake::Data {
  23. sub new { my ( $class, @posts ) = @_; return bless( { posts => \@posts }, $class ) }
  24. sub read { my ($self) = @_; return [ @{ $self->{posts} } ] }
  25. sub get { my ($self) = @_; return @{ $self->{posts} } }
  26. }
  27. # One that gets as far as the index, then falls over before the routes.
  28. package Broken::Data {
  29. use parent -norequire, 'Fake::Data';
  30. sub get { die "the data model fell over\n" }
  31. }
  32. sub post {
  33. my (%over) = @_;
  34. return {
  35. id => 'p-one',
  36. tags => [qw{blog public}],
  37. created => 1700000000,
  38. local_href => '/posts/p-one',
  39. method => 'GET',
  40. callback => 'Trog::Routes::HTML::posts',
  41. aliases => [],
  42. %over,
  43. };
  44. }
  45. sub index_rows {
  46. my $rows = Trog::SQLite::TagIndex::dbh()->selectall_arrayref( 'SELECT id, tag FROM posts ORDER BY id, tag', { Slice => {} } );
  47. return [ map { "$_->{id}:$_->{tag}" } @$rows ];
  48. }
  49. subtest 'add_post' => sub {
  50. my $data = Fake::Data->new();
  51. Trog::SQLite::TagIndex::add_post( post(), $data );
  52. Trog::SQLite::TagIndex::add_post( post(), $data );
  53. is_deeply( index_rows(), [qw{p-one:blog p-one:public}], 'saving a post twice indexes its tags once' );
  54. Trog::SQLite::TagIndex::add_post( post( tags => ['public'] ), $data );
  55. is_deeply( index_rows(), ['p-one:public'], 'and a tag the edit removed is gone' );
  56. is_deeply( [ Trog::SQLite::TagIndex::posts_for_tags('blog') ], [], 'so the post no longer turns up under it' );
  57. };
  58. subtest 'rebuild' => sub {
  59. my $dbh = Trog::SQLite::TagIndex::dbh();
  60. # What an index looks like after something other than tCMS changed the
  61. # posts: a post that is gone, and a route to it.
  62. Trog::SQLite::TagIndex::add_post( post( id => 'p-gone', local_href => '/posts/p-gone', aliases => ['/gone'] ), Fake::Data->new() );
  63. $dbh->do('INSERT INTO posts_index (post_id, post_time, tag_id) SELECT post_id, post_time, tag_id FROM posts_index');
  64. my $data = Fake::Data->new( post( tags => [qw{blog public}] ), post( id => 'p-two', tags => ['public'], local_href => '/posts/p-two' ) );
  65. Trog::SQLite::TagIndex::rebuild($data);
  66. is_deeply( index_rows(), [qw{p-one:blog p-one:public p-two:public}], 'the index holds exactly the posts there are, once each' );
  67. my %routes = Trog::SQLite::TagIndex::routes();
  68. is_deeply( [ sort keys %routes ], [qw{/posts/p-one /posts/p-two}], 'and the routes of those posts only' );
  69. is_deeply( { Trog::SQLite::TagIndex::aliases() }, {}, 'with the gone post\'s alias gone too' );
  70. my ($methods) = $dbh->selectrow_array('SELECT COUNT(*) FROM methods');
  71. ok( $methods, 'the HTTP methods the schema fills are kept' );
  72. Trog::SQLite::TagIndex::rebuild($data);
  73. is_deeply( index_rows(), [qw{p-one:blog p-one:public p-two:public}], 'running it again changes nothing' );
  74. };
  75. subtest 'rebuild is one transaction' => sub {
  76. my $before = index_rows();
  77. my $broken = Broken::Data->new( post() );
  78. ok( !eval { Trog::SQLite::TagIndex::rebuild($broken); 1 }, 'a rebuild that fails part way dies' );
  79. like( $@, qr/fell[ ]over/, 'with the error that stopped it' );
  80. is_deeply( index_rows(), $before, 'and leaves the index as it was' );
  81. };
  82. chdir $oldcwd;
  83. done_testing();