Database-BI

 view release on metacpan or  search on metacpan

t/integration.t  view on Meta::CPAN

use strict;
use warnings;

# ---------------------------------------------------------------------------
# Integration test suite for Database::BI
#
# Focus: cross-module, multi-step workflows where Database::BI (routing/config),
# Database::BI::Controller::Dashboard (HTTP actions), and
# Database::BI::Model::DataSource (data access) all interact.
#
# What is NOT here (covered by dedicated suites):
#   - Individual filter operators             -> t/filter.t
#   - Single-endpoint status codes            -> t/unit.t
#   - Internal helpers (white-box)            -> t/function.t
#   - Security / hostile inputs               -> t/cgi_security.t
# ---------------------------------------------------------------------------

use Test::Most;
use Test::Mojo;
use Test::Without::Module;
use Readonly;
use File::Spec     ();
use File::Temp     qw(tempdir tempfile);
use Mojo::File     ();
use Mojo::Util     qw(url_escape);
use Scalar::Util   qw(reftype refaddr);
use DBI            ();

# ---------------------------------------------------------------------------
# Constants derived from known sample data (sales.csv, products.psv).
# Keep in sync with the data/ directory.
# ---------------------------------------------------------------------------
Readonly my $SALES_CSV   => File::Spec->rel2abs('data/sales.csv');
Readonly my $PRODUCTS_PSV => File::Spec->rel2abs('data/products.psv');
Readonly my $CATALOG_XML  => File::Spec->rel2abs('data/catalog.xml');

# Known row counts (as of the shipped sample data):
Readonly my $SALES_ROW_COUNT    => 6;
Readonly my $PRODUCTS_ROW_COUNT => 4;

# Known column names present in sales.csv (integration assertions):
Readonly my @SALES_COLS => qw(id product region sales_rep amount sale_date);

# ---------------------------------------------------------------------------
# Bootstrap: Test::Mojo must be constructed BEFORE any direct use of
# DataSource so that Database::Abstraction modules are loaded at runtime
# (bypassing the Sub::Private CHECK-phase stash cleanup).
# ---------------------------------------------------------------------------
my $t = Test::Mojo->new('Database::BI');

# Redirect model_dir to a temp directory so tests never write to .bi/models.json
# in the workspace.  The variable must be file-scoped so the dir persists for
# the entire test process (CLEANUP => 1 deletes it when it goes out of scope).
my $TEST_MODEL_DIR = tempdir(CLEANUP => 1);
$t->app->config->{model_dir} = $TEST_MODEL_DIR;

# Guard: most integration subtests depend on the sample data files.
plan skip_all => 'data/sales.csv not found'    unless -f $SALES_CSV;
plan skip_all => 'data/products.psv not found' unless -f $PRODUCTS_PSV;

require Database::BI::Model::DataSource;    # already in %INC; stash intact

# ---------------------------------------------------------------------------
# Minimal i18n spy object used in Section 5.
# Implements maketext() so it is accepted by DataSource's i18n validation.
# ---------------------------------------------------------------------------
{
	package MockI18n;
	sub new { bless { log => [] }, shift }
	sub maketext {
		my ($self, $key, @args) = @_;
		push @{ $self->{log} }, { key => $key, args => \@args };
		# Delegate to the built-in message dict so error text is still human-readable.



( run in 1.004 second using v1.01-cache-2.11-cpan-16aabcbfbcb )