Trog-SQLite-TagIndex.t 4.1 KB

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