Database-BI
view release on metacpan or search on metacpan
# shows nothing and reports nothing, which reads exactly like a card with no
# data; a branch that dies takes the whole canvas down for one card's sake.
# Both look like ordinary code, and each is only reachable through the
# condition that produces it, so the dispatcher is read as a program here:
# its statements, and what each one answers with.
# ---------------------------------------------------------------------------
subtest '3.3: a card that cannot be drawn answers with an error' => sub {
plan skip_all => 'the controller source is not in this checkout'
unless -f $src->{file};
my $body = _sub_code($src, '_render_visual_markup');
plan skip_all => 'the card renderer is not in this controller'
unless defined $body;
$body = join "\n", @$body;
ok(length $body, 'the renderer has a body to read');
my $statements = _statements($body);
ok(scalar @$statements > 20, 'the renderer is read as the statements it is made of');
# Each return is one of the two answers. An error answer also carries a
# message: an empty one is a blank card with an explanation-shaped hole.
my (@error, @drawn, @bare, @other);
for my $st (@$statements) {
next unless $st =~ /\breturn\b/;
if ($st =~ /\breturn\s*;/) { push @bare, $st }
elsif ($st =~ /\berror\s*=>/) { push @error, $st }
elsif ($st =~ /\bhtml\s*=>/) { push @drawn, $st }
else { push @other, $st }
}
is scalar @bare, 0, 'no branch of the dispatcher returns nothing at all'
or diag explain \@bare;
ok(scalar @error, 'a card says why it could not be drawn');
ok(scalar @drawn, 'and hands back the html when it can');
is scalar @other, 0, 'every other return is one of those two answers'
or diag explain \@other;
my @empty = grep { /\berror\s*=>\s*(?:''|""|undef\b|0\b)/ } @error;
is scalar @empty, 0, 'and each of those answers carries a message'
or diag explain \@empty;
# A kind nobody implemented is a card's error like any other, not a card
# that draws nothing.
my @returns = grep { /\breturn\b/ } @$statements;
like($returns[-1], qr/\bUnknown visual kind\b/,
'as is a kind the renderer has no branch for');
# A card's failure is its own: the renderer answers rather than dying, which
# would take the page down for one card's sake.
unlike($body, qr/^\s*(?:die|exit)\b|\b(?:croak|confess)\b/m,
'and none of it dies');
};
# ---------------------------------------------------------------------------
# A card is never filtered by its own cross-filter
#
# A click on a card marks it as the filter's source (?cv=), and every other
# card is narrowed by what was clicked (?cf=). The source is not narrowed: it
# stays whole, which is what makes the click readable -- you see the thing you
# clicked, beside everything it changed. Two places draw a card -- the page's
# loop, and the endpoint that adds or repairs one -- so both are read here, and
# so is the context both share: the specs are handed out unapplied, because a
# context that applied them could not know which card, if any, is the source.
# ---------------------------------------------------------------------------
subtest '3.3: a card is never filtered by its own cross-filter' => sub {
plan skip_all => 'the controller source is not in this checkout'
unless -f $src->{file};
# -- the page's loop ---------------------------------------------------
my $canvas = _sub_code($src, 'canvas_render');
ok($canvas, 'the canvas renderer is there to read');
my $applied = _lines_like($canvas, qr/_canvas_cross_filter\(/);
is scalar @$applied, 1, 'a card is narrowed by the cross-filter on one line';
if (@$applied) {
like _guard_of($canvas, $applied->[0]),
qr/\$cf_active\s*&&\s*\$i\s*!=\s*\$cf_source\b/,
'and only inside a guard that says this card is not the source';
}
# The rows the pass is run on are the card's own copy: a cross-filter applied
# to $records would take the rows away from the source card and from every
# card after it, which is the same bug from the other end. The page-wide
# time range is the one filter that does narrow those rows, because it is the
# page's range rather than a card's click.
is scalar @{ _lines_like($canvas, qr/_apply_filter_spec\(\$records, \$cfs/) }, 0,
'no cross-filter is applied to the rows the whole page draws from';
my $on_records = _lines_like($canvas, qr/_apply_filter_spec\(\$records, /);
is scalar @$on_records, 1,
'whose one page-wide narrowing is the time range, applied once';
if (@$on_records) {
my ($in_loop) = grep { $_ < $on_records->[0] && $canvas->[$_] =~ /^\s*for\b/ }
reverse 0 .. $on_records->[0];
like((defined $in_loop ? $canvas->[$in_loop] : ''), qr/range_specs/,
'and it sits in the loop over the range the page named');
}
is scalar @{ _lines_like($canvas, qr/^\s*my \$vis_recs\s*=\s*\$records;/) }, 1,
'each card starts from those rows';
# The renderer is told its rows are narrowed by the same question, so it
# cannot decide for itself that a card wanted filtering.
my ($call_at) = @{ _lines_like($canvas, qr/_render_visual_html\(/) };
ok(defined $call_at, 'the card is drawn through the renderer');
my ($call_end) = defined $call_at
? grep { $_ > $call_at && $canvas->[$_] =~ /^\s*\);/ } 0 .. $#$canvas
: ();
if (defined $call_at && defined $call_end) {
my @asked = grep {
$canvas->[$_] =~ /\$cf_active\s*&&\s*\$i\s*!=\s*\$cf_source\s*\?\s*1\s*:\s*0/
} $call_at .. $call_end;
is scalar @asked, 1,
'and is told, by that same question, that its rows are already narrowed';
}
else {
fail('the renderer call has an argument list to read');
}
# The page still has to know which card made the filter: that is how the
# source is drawn as the source rather than as a card that failed to narrow.
is scalar @{ _lines_like($canvas, qr/cf_source\s*=>\s*\(\$i == \$cf_source \? 1 : 0\)/) },
1, 'the card that made the filter is marked as its source, once';
# -- the endpoint that draws one card ----------------------------------
my $patch = _sub_code($src, 'analyses_update_visual');
ok($patch, 'the endpoint that draws a single card is there to read');
my $patch_applied = _lines_like($patch, qr/_canvas_cross_filter\(/);
is scalar @$patch_applied, 1, 'it narrows a card in one place too';
my ($guard_at) = grep {$patch->[$_] =~ /\$ctx->\{cf_source\}\s*!=\s*\$index\b/}
0 .. $#$patch;
ok(defined $guard_at, 'and asks whether the card it is drawing is the source');
if (@$patch_applied && defined $guard_at) {
is(_guard_of($patch, $patch_applied->[0]), $patch->[$guard_at],
'narrowing it only on the branch where it is not');
# The flag handed to the renderer is set on that branch and no other:
# elsewhere it would tell the renderer to skip the SQL push-over for a
# card nothing had narrowed.
my ($indent) = $patch->[$guard_at] =~ /\A(\s*)/;
my ($block_end) = grep { $_ > $guard_at && $patch->[$_] =~ /\A\Q$indent\E\}/ }
0 .. $#$patch;
my ($flag_at) = @{ _lines_like($patch, qr/^\s*\$prefiltered = 1;/) };
ok(defined $block_end && defined $flag_at
&& $flag_at > $guard_at && $flag_at < $block_end,
'and only there is the renderer told the rows are narrowed');
}
else {
fail('the endpoint applies the cross-filter under a guard');
}
# -- the context both of them draw through -----------------------------
# cf_specs is returned unapplied on purpose: the caller is the only thing
# that knows which card is the source, so a context that narrowed the rows
# would narrow the source's too, with no way to put them back.
my $ctx = _sub_code($src, '_canvas_render_context');
ok($ctx, 'the context a card is drawn through is there to read');
is scalar @{ _lines_like($ctx, qr/_apply_filter_spec\(/) }, 0,
'and hands the cross-filters out unapplied, for the caller to place';
};
# ---------------------------------------------------------------------------
# The page is told what each card is
#
# A renderer's answer only reaches a reader if the page is built for it: a
# card's error is drawn in the card, the one card that needs to carry a head
# carries it, a card's own plan fails as that card rather than as the page, and
# a measure is refused before any kind is drawn. Each of those is a line that
# could be dropped without a healthy canvas noticing, so each is read here.
# ---------------------------------------------------------------------------
subtest '3.3: the page is told what each card is' => sub {
plan skip_all => 'the controller source is not in this checkout'
unless -f $src->{file};
plan skip_all => 'the canvas template is not in this checkout'
unless -f $tpl->{file};
# -- a card's error is drawn in the card -------------------------------
# Once by the page for the cards it already has, and once by the script for
# a card added while the page is up: both put the message where the reader
# is looking, and the message is escaped because it may quote a column name
# the reader typed.
my $in_card = _lines_like($tpl->{code}, qr/class="visual-error">\[\% v\.error/);
is scalar @$in_card, 1, "the template draws a card's error inside the card"
or diag explain $in_card;
if (@$in_card) {
like($tpl->{code}[ $in_card->[0] ] // '', qr/\| html %\]/,
'and escapes it rather than writing it as markup');
}
is scalar @{ _lines_like($tpl->{code},
qr/element\('div', \{ 'class': 'visual-error' \}, json\.error\)/) }, 1,
'as does a card added after the page is up';
# -- only a map card carries a head ------------------------------------
my $markup = _sub_code($src, '_render_visual_markup');
ok($markup, 'the card renderer is there to read');
my $heads = _lines_like($markup, qr/\bhead\s*=>/);
is scalar @$heads, 1, 'one kind of card hands the page a head';
if (@$heads) {
# The map branch, and no branch after it: a chart could otherwise put
# its stylesheet or its library on the page through a key nobody reads
# for it.
my ($map_at) = @{ _lines_like($markup, qr/^\s*if \(\$kind eq 'map'\)/) };
my ($next_kind) = grep { defined $map_at && $_ > $map_at
&& $markup->[$_] =~ /^\s*if \(\$kind eq / } 0 .. $#$markup;
ok(defined $map_at && $heads->[0] > $map_at
&& (!defined $next_kind || $heads->[0] < $next_kind),
'and that kind is the map');
}
my $sales_path = $t->app->home->child('data/sales.csv')->to_string;
plan skip_all => 'data/sales.csv not found' unless -f $sales_path;
my $spec = {
name => 'UndoRedo',
datasets => [ { spec => "path:$sales_path", alias => 'left' } ],
visuals => [ { kind => 'table', encode => {}, title => 'T' } ],
};
my $cid = $t->post_ok('/api/analyses',
{ 'Content-Type' => 'application/json' },
encode_json($spec)
)->status_is(201)->tx->res->json->{id};
$t->get_ok("/a/$cid")->status_is(200);
my $b = $t->tx->res->body;
like $b, qr/id="btn-undo"/, 'undo button present';
like $b, qr/id="btn-redo"/, 'redo button present';
like $b, qr/btn-undo.*disabled/s, 'undo button initially disabled';
like $b, qr/btn-redo.*disabled/s, 'redo button initially disabled';
like $b, qr/pushUndo/, 'pushUndo function defined in JS';
like $b, qr/initUndoRedo/, 'initUndoRedo called in JS';
};
subtest '3.6: bookmark panel present in canvas' => sub {
my $sales_path = $t->app->home->child('data/sales.csv')->to_string;
plan skip_all => 'data/sales.csv not found' unless -f $sales_path;
my $spec = {
name => 'BmCanvas',
datasets => [ { spec => "path:$sales_path", alias => 'left' } ],
visuals => [ { kind => 'table', encode => {}, title => 'T' } ],
};
my $cid = $t->post_ok('/api/analyses',
{ 'Content-Type' => 'application/json' },
encode_json($spec)
)->status_is(201)->tx->res->json->{id};
$t->get_ok("/a/$cid")->status_is(200);
my $b = $t->tx->res->body;
like $b, qr/id="btn-bm"/, 'bookmark toggle button present';
like $b, qr/id="bm-panel"/, 'bookmark panel present';
like $b, qr/bm-empty/, 'empty bookmark message shown (no bookmarks yet)';
like $b, qr/canvasBookmarks/, 'canvasBookmarks JS variable present';
unlike $b, qr/class="bm-item"/, 'no bookmark items shown before any are saved';
};
subtest '3.6: save bookmark via PATCH then verify on canvas' => sub {
my $sales_path = $t->app->home->child('data/sales.csv')->to_string;
plan skip_all => 'data/sales.csv not found' unless -f $sales_path;
my $spec = {
name => 'BmSave',
datasets => [ { spec => "path:$sales_path", alias => 'left' } ],
visuals => [ { kind => 'table', encode => {}, title => 'T' } ],
};
my $cid = $t->post_ok('/api/analyses',
{ 'Content-Type' => 'application/json' },
encode_json($spec)
)->status_is(201)->tx->res->json->{id};
# Save a bookmark by PATCHing the analyses endpoint directly
# (mirrors what the JS patchBookmarks() function does).
my $bm = { name => 'My view', state => '?f=region:eq:North', created => 1234567890 };
$t->patch_ok("/api/analyses/$cid",
{ 'Content-Type' => 'application/json' },
encode_json({ bookmarks => [$bm] })
)->status_is(200);
# Verify the bookmark is persisted in the stored analysis.
$t->get_ok("/api/analyses/$cid")->status_is(200);
my $stored = $t->tx->res->json;
is ref($stored->{bookmarks}), 'ARRAY', 'bookmarks key is array';
is scalar @{ $stored->{bookmarks} }, 1, 'one bookmark stored';
is $stored->{bookmarks}[0]{name}, 'My view', 'bookmark name correct';
is $stored->{bookmarks}[0]{state}, '?f=region:eq:North', 'bookmark state correct';
is $stored->{bookmarks}[0]{created}, 1234567890, 'bookmark created timestamp preserved';
# Verify the canvas renders the bookmark link.
$t->get_ok("/a/$cid")->status_is(200);
my $b = $t->tx->res->body;
like $b, qr/class="bm-item"/, 'saved bookmark item rendered';
like $b, qr/My view/, 'bookmark name visible';
like $b, qr{href="[^"]*\?f=region}, 'bookmark href contains state';
like $b, qr/class="bm-del-btn"/, 'delete button rendered on bookmark';
unlike $b, qr/id="bm-empty"/, 'empty-state message absent when bookmarks exist';
};
subtest '3.6: undo/redo JS stack key scoped to canvas id' => sub {
my $sales_path = $t->app->home->child('data/sales.csv')->to_string;
plan skip_all => 'data/sales.csv not found' unless -f $sales_path;
my $spec = {
name => 'StackKey',
datasets => [ { spec => "path:$sales_path", alias => 'left' } ],
visuals => [ { kind => 'table', encode => {}, title => 'T' } ],
};
my $cid = $t->post_ok('/api/analyses',
{ 'Content-Type' => 'application/json' },
encode_json($spec)
)->status_is(201)->tx->res->json->{id};
$t->get_ok("/a/$cid")->status_is(200);
my $b = $t->tx->res->body;
# The localStorage key must contain the canvas id so stacks from
# different analyses do not collide.
like $b, qr/bi:undo:/, 'localStorage key prefix present';
# The key includes the canvas id (either literal in JS or via anaId var).
like $b, qr/UNDO_KEY\s*=\s*'bi:undo:'\s*\+\s*anaId/, 'stack key uses anaId';
like $b, qr/MAX_STACK\s*=\s*50/, 'max stack size is 50';
};
# ---------------------------------------------------------------------------
# Phase 3.7 -- Print / PDF / PNG
# ---------------------------------------------------------------------------
subtest '3.7: print button and PNG download infrastructure present' => sub {
my $sales_path = $t->app->home->child('data/sales.csv')->to_string;
plan skip_all => 'data/sales.csv not found' unless -f $sales_path;
my $spec = {
( run in 1.424 second using v1.01-cache-2.11-cpan-eb134014566 )