Database-BI

 view release on metacpan or  search on metacpan

t/insights.t  view on Meta::CPAN

		'EU slash-date column (D/M/YYYY) is not reported as a numeric outlier');
}

{
	# DD-Mon-YYYY format (common in Oracle / older Excel exports).
	my @rows = map {
		{ Date => sprintf('%02d-Oct-2026', $_), Val => 100 + $_ }
	} 1..10;
	my $insights = Database::BI::Insights::compute(
		records  => \@rows,
		columns  => ['Date', 'Val'],
		spec_key => 'table:oracle',
	);
	my @date_outliers = grep {
		$_->{kind} eq 'outlier' && $_->{headline} =~ /\bDate\b/
	} @$insights;
	is(scalar(@date_outliers), 0,
		'DD-Mon-YYYY date column is not reported as a numeric outlier');
}

# ---------------------------------------------------------------------------
# Section 3: Insufficient data path
# ---------------------------------------------------------------------------

{
	# A dataset with one numeric column and only 1 row: nothing to report.
	my $insights = Database::BI::Insights::compute(
		records  => [{ amount => 100 }],
		columns  => ['amount'],
		spec_key => 'table:tiny',
	);
	ok(ref($insights) eq 'ARRAY', 'compute() returns arrayref for tiny input');
	is(scalar(@$insights), 0, 'no insights on a single-row dataset');
}

{
	# A dataset with only non-numeric columns: no outliers/movers.
	my $insights = Database::BI::Insights::compute(
		records  => [map { { name => "row$_", tag => 'x' } } 1..10],
		columns  => ['name', 'tag'],
		spec_key => 'table:nonnumeric',
	);
	is(scalar(@$insights), 0, 'no insights when no numeric columns');
}

{
	# Fewer than MIN_PERIODS_SEASON (8) periods: no seasonality insight.
	my @rows = map {
		{ sale_date => sprintf('2025-%02d-01', $_), amount => 100 + $_ }
	} 1..5;
	my $insights = Database::BI::Insights::compute(
		records  => \@rows,
		columns  => [qw(sale_date amount)],
		spec_key => 'table:few_periods',
	);
	my @season = grep { $_->{kind} eq 'seasonality' } @$insights;
	is(scalar(@season), 0, 'no seasonality insight with fewer than 8 periods');
}

# ---------------------------------------------------------------------------
# Section 4: HTTP endpoint tests (GET /insights)
# ---------------------------------------------------------------------------

{
	my $enc = url_escape("path:$FIXTURE");

	# Basic smoke: 200, page renders, no crash.
	$t->get_ok("/insights?l=path:${\url_escape($FIXTURE)}")
	  ->status_is(200)
	  ->content_like(qr/Automatic insights/i, 'page title present')
	  ->content_like(qr/insight-card/, 'at least one insight card rendered');

	# Outlier card present in HTML.
	$t->get_ok("/insights?l=path:${\url_escape($FIXTURE)}")
	  ->content_like(qr/outlier/i, 'outlier kind shown in HTML');

	# Every view_url in the page must be a root-relative path.
	$t->get_ok("/insights?l=path:${\url_escape($FIXTURE)}")
	  ->content_unlike(qr/view_url.*javascript:/i, 'no javascript: in any insight URL');

	# Missing l= returns 400.
	$t->get_ok('/insights')
	  ->status_is(400);

	# Unknown table returns 404.
	$t->get_ok('/insights?l=table:does_not_exist_xyz')
	  ->status_is(404);

	# f= filter is forwarded: filtering to North reduces data but still returns 200.
	$t->get_ok("/insights?l=path:${\url_escape($FIXTURE)}&f=region:eq:North")
	  ->status_is(200);
}

# ---------------------------------------------------------------------------
# Section 5: method_note() returns non-empty strings for all known keys
# ---------------------------------------------------------------------------

{
	for my $key (qw(iqr_fence pop_delta top_mover autocorr)) {
		my $note = Database::BI::Insights::method_note($key);
		ok(defined $note && length $note, "method_note('$key') returns a string");
	}
}

# ---------------------------------------------------------------------------
# Section 6: drill_url present and well-formed for outlier/period_delta/top_mover
# ---------------------------------------------------------------------------

{
	my @rows;
	open my $fh, '<', $FIXTURE or die "Cannot open $FIXTURE: $!";
	my $header = <$fh>;
	chomp $header;
	my @cols = split /,/, $header;
	while (my $line = <$fh>) {
		chomp $line;
		my @vals = split /,/, $line;
		my %row;
		@row{@cols} = @vals;
		push @rows, \%row;
	}



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