Database-BI

 view release on metacpan or  search on metacpan

t/mariadb.t  view on Meta::CPAN

			map { { entry => $_, %{ $self->{_fixture}{$_} } } }
			sort keys %{ $self->{_fixture} }
		];
	}

	sub _col_names {
		my $self  = shift;
		my $first = (values %{ $self->{_fixture} })[0];
		return [ sort('entry', keys %{$first}) ];
	}

	sub primary_key { return 'entry' }

	# What DataSource->mariadb_tables asks of information_schema.
	sub selectall_arrayref {
		my $self = shift;
		return [ map { [ 'orders', $_ ] } @{ $self->_col_names() } ];
	}

	sub prepare { my $self = shift; return $self->prepare_cached(@_) }

	sub prepare_cached {
		my ($self, $sql) = @_;
		return MockMariaDBSth->new(
			sql       => $sql,
			all_rows  => $self->_build_rows(),
			col_names => $self->_col_names(),
		);
	}

	sub column_info {
		my $self = shift;
		my @rows = map {
			{ COLUMN_NAME => $_, TYPE_NAME => 'VARCHAR', NULLABLE => 1, COLUMN_DEF => undef }
		} @{ $self->_col_names() };
		return MockMariaDBSth->new(sql => '_column_info', pre_rows => \@rows);
	}

	sub disconnect { return 1 }
	sub quote_identifier { return qq{"$_[1]"} }
}

sub _mock_dbh {
	my $dbh = MockMariaDBDbh->new(fixture => \%MARIADB_FIXTURE);
	return ($dbh, mock_scoped('DBI', connect => sub { $dbh }));
}

# ---------------------------------------------------------------------------
# InlineDA -- the non-MariaDB side of a join
# ---------------------------------------------------------------------------
{
	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 => [@INLINE_ROWS]);
}

# A DataSource over the mocked server, as _open_spec builds it.
sub _maria_source {
	return Database::BI::Model::DataSource->new(table => $TABLE, server => 'mock', mariadb => { database => 'test' });
}

# The rows behind a page, without reading them back out of its markup.
sub _export_rows {
	my ($query) = @_;
	$t->get_ok("/export?$query&format=json")->status_is(200);
	return decode_json($t->tx->res->body);
}

plan tests => 23;

# ---------------------------------------------------------------------------
# The DataSource methods
# ---------------------------------------------------------------------------
subtest 'M1: a DataSource holds a MariaDB-backed backend' => sub {
	plan tests => 3;
	my ($dbh, $guard) = _mock_dbh();
	my $src;
	lives_ok { $src = _maria_source() } 'it can be built';
	is($src->table_name, $TABLE, 'table_name');
	is($src->id_column, 'entry', 'id_column');
};

subtest 'M2: columns() lists the columns' => sub {
	plan tests => 2;
	my ($dbh, $guard) = _mock_dbh();
	my $cols = _maria_source()->columns;
	returns_is($cols, { type => 'arrayref' }, 'an arrayref');
	is_deeply([ sort @{$cols} ], [qw(amount entry region)], 'with every column');
};

subtest 'M3: fetch_all and selectall_arrayref return the rows' => sub {
	plan tests => 4;
	my ($dbh, $guard) = _mock_dbh();
	my $src = _maria_source();
	my $all = $src->fetch_all;
	returns_is($all, { type => 'arrayref', min => 4, max => 4 }, 'fetch_all: 4 rows');
	my ($row1) = grep { $_->{entry} eq 'row1' } @{$all};
	is($row1->{region}, 'North', 'with their values');



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