Database-BI

 view release on metacpan or  search on metacpan

t/integration.t  view on Meta::CPAN

use strict;
use warnings;

# Caches go where CACHEDIR, CACHE_DIR or Object::Configure say before model_dir
# (Database::BI::CacheDir): without this, a run writes into the user's own cache.
delete @ENV{qw(CACHEDIR CACHE_DIR Database__BI__cache_dir Database__BI__Extract__cache_dir Database__BI__Model__Geocode__cache_dir)};

# ---------------------------------------------------------------------------
# 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.
#
# Sections 60 onward hold the workflows that cross every layer, independent
# objects in one process, every combination of the optional modules, permissions
# revoked under a running app, and upstreams that fail.
#
# 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 Test::Mockingbird qw(mock_scoped spy restore_all);
use Test::Permissions qw(can_revoke_create can_revoke_search with_revoked);
use Test::Returns;
use Readonly;
use File::Basename ();
use File::Spec     ();
use File::Temp     qw(tempdir tempfile);
use HTTP::Response ();
use LWP::UserAgent ();
use Mojo::File     ();
use Mojo::JSON     qw(decode_json encode_json);
use Mojo::Promise  ();
use Mojo::Util     qw(url_escape);
use Scalar::Util   qw(reftype refaddr);
use DBI            ();

# Mojolicious dispatches under a __DIE__ handler that throws an object.  When
# Test::Without::Module's @INC hook croaks under it, perl goes on through @INC and
# loads the module after all, so a test that hides a module and then makes a
# request sees it installed.  The hook has to die with no handler in the way.
{
	no warnings 'redefine';
	my $hook = \&Test::Without::Module::fake_module;
	*Test::Without::Module::fake_module = sub { local $SIG{__DIE__}; return $hook->(@_) };
}

# ---------------------------------------------------------------------------
# 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;



( run in 0.817 second using v1.01-cache-2.11-cpan-eb134014566 )