Database-BI

 view release on metacpan or  search on metacpan

t/mutant_killers.t  view on Meta::CPAN

use File::Temp qw(tempdir);
use Readonly;
use Test::Mojo;
use Test::Most;
use Test::Returns qw(returns_is);

# ---------------------------------------------------------------------------
# DataSource must be loaded through the app, or Sub::Private cleans its stash
# (see the note at the top of t/unit.t).
# ---------------------------------------------------------------------------
my $t = Test::Mojo->new('Database::BI');
$t->app->config->{model_dir} = tempdir(CLEANUP => 1);

require Database::BI::Extract;
require Database::BI::Insights;
require Database::BI::Model::DataSource;
require Database::BI::Model::Geocode;
require Database::BI::Util;

Readonly my $DS       => 'Database::BI::Model::DataSource';
Readonly my $INSIGHTS => 'Database::BI::Insights';

Readonly my $SPEC      => 'table:sales';
Readonly my $PATH_SPEC => 'path:/tmp/sales.csv';
Readonly my $URL_SPEC  => 'url:http://example.com/t';
Readonly my $BASE_URL  => '/insights?l=table:sales';
Readonly my $AMOUNT    => 'amount';
Readonly my $DATE_COL  => 'day';
Readonly my $CAT_COL   => 'region';

# Thresholds the code has as private constants; the tests restate them, so that
# changing one is a decision the tests notice.
Readonly my $SAMPLE_SIZE       => 20;
Readonly my $MAX_OUTLIERS      => 5;
Readonly my $MAX_MOVERS        => 5;
Readonly my $MIN_SEASON        => 8;
Readonly my $FMT_INT_LIMIT     => 1_000_000_000_000;
Readonly my $BDB_LIMIT         => 100 * 1024 * 1024;

# ---------------------------------------------------------------------------
# Stand-ins for the backend and the cache
# ---------------------------------------------------------------------------
{
	package Local::Backend;

	sub new { my ($class, %args) = @_; return bless { calls => [], %args }, $class }
	sub selectall_arrayref {
		my ($self, @args) = @_;
		push @{ $self->{calls} }, [ 'selectall_arrayref', @args ];
		return $self->{rows};
	}
	sub selectall_hashref {
		my ($self) = @_;
		push @{ $self->{calls} }, [ 'selectall_hashref' ];
		die $self->{die} if $self->{die};
		return $self->{rows};
	}
	sub columns   { return $_[0]{columns} }
	sub count     { return $_[0]{count} }
	sub aggregate { my ($self, @args) = @_; return { aggregated => \@args } }
	sub updated   { return $_[0]{updated} }
	sub schema {
		my ($self) = @_;
		die 'no schema' if $self->{schema_dies};
		return $self->{schema};
	}
	sub dbi_source {
		my ($self) = @_;
		die 'no dbi' if $self->{dbi_dies};
		return $self->{dbi};
	}
}
{
	# A backend with none of the optional methods.
	package Local::BareBackend;

	sub new { return bless {}, shift }
}
{
	package Local::Cache;

	sub new { return bless { store => {}, sets => [], gets => 0 }, shift }
	sub get { my ($self, $key) = @_; $self->{gets}++; return $self->{store}{$key} }
	sub set {
		my ($self, @args) = @_;
		push @{ $self->{sets} }, \@args;
		$self->{store}{ $args[0] } = $args[1];
		return;
	}
}
{
	package Local::I18n;

	sub new { return bless {}, shift }
	sub maketext { my ($self, $key, @args) = @_; return join ':', 'translated', $key, @args }
}
{
	# A geocoder that knows where everything is.
	package Local::Geocoder;

	sub new            { return bless {}, shift }
	sub valid_location { return 1 }
	sub geocode_many {
		my ($self, $values) = @_;
		return { map { $_ => { lat => 1, lon => 2 } } @{$values} };
	}
}
{
	# Serves one remote file, whatever it is called, as Net::SFTP::Foreign does.
	package Local::SFTP;

	my %files;
	sub serve { my ($class, %serve) = @_; %files = %serve; return }
	sub new   { return bless { error => 0 }, shift }
	sub error { return $_[0]{error} }
	sub get {
		my ($self, $remote, $local) = @_;
		return unless exists $files{$remote};
		open my $fh, '>', $local or die "$local: $!";
		print {$fh} $files{$remote};
		close $fh or die "$local: $!";



( run in 0.764 second using v1.01-cache-2.11-cpan-0e0213f3164 )