Test for restricted prefetch (now passing again after previous commit)
[dbsrgits/DBIx-Class.git] / t / 46where_attribute.t
CommitLineData
65c2b042 1use strict;
2use warnings;
3
4use Test::More;
65c2b042 5use lib qw(t/lib);
6use DBICTest;
7my $schema = DBICTest->init_schema();
8
d2d82857 9plan tests => 19;
65c2b042 10
11# select from a class with resultset_attributes
12my $resultset = $schema->resultset('BooksInLibrary');
13is($resultset, 3, "select from a class with resultset_attributes okay");
14
15# now test out selects through a resultset
16my $owner = $schema->resultset('Owners')->find({name => "Newton"});
17my $programming_perl = $owner->books->find_or_create({ title => "Programming Perl" });
18is($programming_perl->id, 1, 'select from a resultset with find_or_create for existing entry ok');
19
20# and inserts?
21my $see_spot;
22$see_spot = eval { $owner->books->find_or_create({ title => "See Spot Run" }) };
23if ($@) { print $@ }
24ok(!$@, 'find_or_create on resultset with attribute for non-existent entry did not throw');
25ok(defined $see_spot, 'successfully did insert on resultset with attribute for non-existent entry');
26
27my $see_spot_rs = $owner->books->search({ title => "See Spot Run" });
28eval { $see_spot_rs->delete(); };
29if ($@) { print $@ }
30ok(!$@, 'delete on resultset with attribute did not throw');
31is($see_spot_rs->count(), 0, 'delete on resultset with attributes succeeded');
32
33# many_to_many tests
34my $collection = $schema->resultset('Collection')->search({collectionid => 1});
35my $pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});
36my $pointy_count = $pointy_objects->count();
37is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from resultset count correct');
38
39$collection = $schema->resultset('Collection')->find(1);
40$pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});
41$pointy_count = $pointy_objects->count();
42is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from row count correct');
43
44# use where on many_to_many query
45$collection = $schema->resultset('Collection')->find(1);
46$pointy_objects = $collection->search_related('collection_object')->search_related('object', {}, { where => { 'object.type' => 'pointy' } });
47is($pointy_objects->count(), 2, 'many_to_many explicit query through linking table with where starting from row count correct');
48
49$collection = $schema->resultset('Collection')->find(1);
50$pointy_objects = $collection->pointy_objects();
51$pointy_count = $pointy_objects->count();
52is($pointy_count, 2, 'many_to_many resultset with where in resultset attrs count correct');
53
54# add_to_$rel on many_to_many with where containing a required field
55eval {$collection->add_to_pointy_objects({ value => "Nail" }) };
56if ($@) { print $@ }
57ok( !$@, 'many_to_many add_to_$rel($hash) with where in relationship attrs did not throw');
58is($pointy_objects->count, $pointy_count+1, 'many_to_many add_to_$rel($hash) with where in relationship attrs count correct');
59$pointy_count = $pointy_objects->count();
60
61my $pen = $schema->resultset('TypedObject')->create({ value => "Pen", type => "pointy"});
62eval {$collection->add_to_pointy_objects($pen)};
63if ($@) { print $@ }
64ok( !$@, 'many_to_many add_to_$rel($object) with where in relationship attrs did not throw');
65is($pointy_objects->count, $pointy_count+1, 'many_to_many add_to_$rel($object) with where in relationship attrs count correct');
66$pointy_count = $pointy_objects->count();
67
68my $round_objects = $collection->round_objects();
69my $round_count = $round_objects->count();
70eval {$collection->add_to_objects({ value => "Wheel", type => "round" })};
71if ($@) { print $@ }
72ok( !$@, 'many_to_many add_to_$rel($hash) did not throw');
73is($round_objects->count, $round_count+1, 'many_to_many add_to_$rel($hash) count correct');
d2d82857 74
75# test set_$rel
76$round_count = $round_objects->count();
77$pointy_count = $pointy_objects->count();
78my @all_pointy_objects = $pointy_objects->all;
79# doing a set on pointy objects with its current set should not change any counts
80eval {$collection->set_pointy_objects(\@all_pointy_objects)};
81if ($@) { print $@ }
82ok( !$@, 'many_to_many set_$rel(\@objects) did not throw');
83is($pointy_objects->count, $pointy_count, 'many_to_many set_$rel($hash) count correct');
84is($round_objects->count, $round_count, 'many_to_many set_$rel($hash) other rel count correct');