Database-BI
view release on metacpan or search on metacpan
t/mariadb_live.t view on Meta::CPAN
# ---------------------------------------------------------------------------
# The application, with this server configured
# ---------------------------------------------------------------------------
my $data_dir = tempdir(CLEANUP => 1);
# What a Merge with a real file reads.
Mojo::File->new($data_dir)->child('csvtiers.csv')->spew("entry,csvtier\nrow1,csvgold\nrow2,csvsilver\n");
# The settings a mariadb_servers entry holds, from what _parse_server found.
my ($SERVER_DB) = $DSN =~ /database=([^;]+)/;
my ($SERVER_HOST) = $DSN =~ /host=([^;]+)/;
my ($SERVER_PORT) = $DSN =~ /port=([^;]+)/;
my %SERVER = (
database => $SERVER_DB,
host => $SERVER_HOST,
port => $SERVER_PORT,
username => $DB_USER,
password => $DB_PASS,
);
Readonly my $O => "mariadb:live/$ORDERS";
Readonly my $T => "mariadb:live/$TIERS";
my $t = Test::Mojo->new('Database::BI', {
data_dir => $data_dir,
model_dir => tempdir(CLEANUP => 1),
mariadb_servers => { live => { %SERVER } },
});
require Database::BI::Model::DataSource;
require Database::BI::Controller::Dashboard;
sub _maria_source {
my ($table, %opts) = @_;
return Database::BI::Model::DataSource->new(
table => $table, server => 'live', mariadb => { %SERVER, %opts },
);
}
sub _orders { return _maria_source($ORDERS) }
sub _export_rows {
my ($query) = @_;
$t->get_ok("/export?$query&format=json")->status_is(200);
return decode_json($t->tx->res->body);
}
sub _entries { return [ sort map { $_->{entry} } @{ $_[0] } ] }
# ---------------------------------------------------------------------------
# InlineDA -- the non-MariaDB side of some joins
# ---------------------------------------------------------------------------
{
package InlineDA;
sub new {
my ($class, %args) = @_;
return bless { _cols => $args{cols} // [], _rows => $args{rows} // [] }, $class;
}
sub columns { return $_[0]->{_cols} }
sub schema { return {} }
sub updated { return 1 }
sub set_logger { return $_[0] }
sub selectall_arrayref {
my ($self, $criteria) = @_;
my @rows = @{ $self->{_rows} };
for my $col (CORE::keys %{ $criteria // {} }) {
my $val = $criteria->{$col};
next if ref $val;
@rows = grep { defined $_->{$col} && $_->{$col} eq $val } @rows;
}
return \@rows;
}
}
sub _inline_da {
return InlineDA->new(
cols => [qw(entry tier)],
rows => [
{ entry => 'row1', tier => 'gold' },
{ entry => 'row2', tier => 'silver' },
{ entry => 'row3', tier => 'bronze' },
],
);
}
sub _join {
my (%extra) = @_;
return Database::Join->new(
databases => [ _orders(), _inline_da() ],
join_column => 'entry',
%extra,
);
}
plan tests => 25;
# ---------------------------------------------------------------------------
# The DataSource methods
# ---------------------------------------------------------------------------
subtest 'L1: a DataSource connects, and lists the columns' => sub {
plan tests => 3;
my $src = _orders();
is($src->table_name, $ORDERS, 'table_name');
my $cols = $src->columns;
returns_is($cols, { type => 'arrayref' }, 'columns() is an arrayref');
is_deeply([ sort @{$cols} ], [qw(amount day entry label region)], 'with every column');
};
subtest 'L2: fetch_all returns the rows, with the types the server sends' => sub {
plan tests => 6;
my $rows = _orders()->fetch_all;
returns_is($rows, { type => 'arrayref', min => 4, max => 4 }, 'all 4 rows');
my %by = map { $_->{entry} => $_ } @{$rows};
is($by{row1}{amount}, '10.50', 'a DECIMAL keeps its scale');
is($by{row1}{day}, '2026-01-01', 'a DATE is ISO');
ok(exists $by{row4}{amount}, 'a NULL column is still a key of the row');
ok(!defined $by{row4}{amount}, 'and has no value');
is($by{row2}{label}, "Zo\x{eb}", 'non-ASCII text comes back as characters');
};
( run in 1.927 second using v1.01-cache-2.11-cpan-0e0213f3164 )