Class-DBI-Sweet

 view release on metacpan or  search on metacpan

t/cdbi-t/10-mysql.t  view on Meta::CPAN


	my $first = $s1->filmids->first;
	ok !ref $first, "First is not a reference";
	is $first, $filmid[0], "But it's the same as filmid[0]";
}

{    # cascades correctly
	my $lenin  = MyFilm->insert({ title    => "Leningrad Cowboys Go America" });
	my $pimme  = MyStar->insert({ name     => "Pimme Korhonen" });
	my $cowboy = MyStarLink->insert({ film => $lenin, star => $pimme });
	$lenin->delete;
	is MyStar->search(name => 'Pimme Korhonen')->count, 1, "Pimme still exists";
	is MyStarLink->search(star => $pimme->id)->count, 0, "But in no films";
}

{
	ok MyStar->has_many(filmids_mcpk => [ MyStarLinkMCPK => 'film', 'id' ]),
		"**** Multi-map via MCPK";
	my @filmid = $s1->filmids_mcpk;
	ok !ref $filmid[0], "Film-id is not a reference";

	my $first = $s1->filmids_mcpk->first;
	ok !ref $first, "First is not a reference";
	is $first, $filmid[0], "But it's the same as filmid[0]";
}

{
	ok my $f0 = MyFilm->insert({ filmid => 0, title => "Year 0" }),
		"Create with explicit id = 0";
	isa_ok $f0 => 'MyFilm';
	is $f0->id, 0, "ID of 0";
}

{    # create doesn't mess with my hash.
	my %info = (Name => "Catherine Wilkening");
	my $cw = MyStar->find_or_create(\%info);
	is scalar keys %info, 1, "Our hash still has only one key";
	is $info{Name}, "Catherine Wilkening", "Still same name";
}

{
	MyFilm->set_sql(
		retrieve_all_sorted => "SELECT __ESSENTIAL__ FROM __TABLE__ ORDER BY %s");

	sub MyFilm::retrieve_all_sorted_by {
		my ($class, $order_by) = @_;
		return $class->sth_to_objects($class->sql_retrieve_all_sorted($order_by));
	}

	my @all = MyFilm->retrieve_all_sorted_by("title");
	is @all, 3, "3 films";
	ok $all[2]->title gt $all[1]->title && $all[1]->title gt $all[0]->title,
		"sorted by title";
}

{

	package Class::DBI::Search::Test::Limited;
	use base 'Class::DBI::Search::Basic';

	sub fragment {
		my $self = shift;
		my $frag = $self->SUPER::fragment;
		if (defined(my $limit = $self->opt('limit'))) {
			$frag .= " LIMIT $limit";
		}
		return $frag;
	}

	package main;

	MyFilm->add_searcher(search => "Class::DBI::Search::Test::Limited");

	my @common = map MyFilm->insert({ title => "Common Title" }), 1 .. 3;
	{
		my @ltd = MyFilm->search(
			title => "Common Title",
			{
				order_by => 'filmid',
				limit    => 1
			}
		);
		is @ltd, 1, "Limit to one film";
		is $ltd[0]->id, $common[0]->id, "The correct one";
	}

	{
		my @ltd = MyFilm->search(
			title => "Common Title",
			{
				order_by => 'filmid',
				limit    => "1,1"
			}
		);
		is @ltd, 1, "Limit to middle film";
		is $ltd[0]->id, $common[1]->id, " - correctly";
	}

}



( run in 1.406 second using v1.01-cache-2.11-cpan-364913b4093 )