7 my $schema = DBICTest->init_schema();
9 # select from a class with resultset_attributes
10 my $resultset = $schema->resultset('BooksInLibrary');
11 is($resultset, 3, "select from a class with resultset_attributes okay");
13 $resultset = $resultset->search({}, { where => undef });
14 is($resultset, 3, "where condition not obliterated");
16 # now test out selects through a resultset
17 my $owner = $schema->resultset('Owners')->find({name => "Newton"});
18 my $programming_perl = $owner->books->find_or_create({ title => "Programming Perl" });
19 is($programming_perl->id, 1, 'select from a resultset with find_or_create for existing entry ok');
23 $see_spot = eval { $owner->books->find_or_create({ title => "See Spot Run" }) };
25 ok(!$@, 'find_or_create on resultset with attribute for non-existent entry did not throw');
26 ok(defined $see_spot, 'successfully did insert on resultset with attribute for non-existent entry');
28 my $see_spot_rs = $owner->books->search({ title => "See Spot Run" });
29 eval { $see_spot_rs->delete(); };
31 ok(!$@, 'delete on resultset with attribute did not throw');
32 is($see_spot_rs->count(), 0, 'delete on resultset with attributes succeeded');
35 my $collection = $schema->resultset('Collection')->search({collectionid => 1});
36 my $pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});
37 my $pointy_count = $pointy_objects->count();
38 is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from resultset count correct');
40 $collection = $schema->resultset('Collection')->find(1);
41 $pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});
42 $pointy_count = $pointy_objects->count();
43 is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from row count correct');
45 # use where on many_to_many query
46 $collection = $schema->resultset('Collection')->find(1);
47 $pointy_objects = $collection->search_related('collection_object')->search_related('object', {}, { where => { 'object.type' => 'pointy' } });
48 is($pointy_objects->count(), 2, 'many_to_many explicit query through linking table with where starting from row count correct');
50 $collection = $schema->resultset('Collection')->find(1);
51 $pointy_objects = $collection->pointy_objects();
52 $pointy_count = $pointy_objects->count();
53 is($pointy_count, 2, 'many_to_many resultset with where in resultset attrs count correct');
55 # add_to_$rel on many_to_many with where containing a required field
56 eval {$collection->add_to_pointy_objects({ value => "Nail" }) };
58 ok( !$@, 'many_to_many add_to_$rel($hash) with where in relationship attrs did not throw');
59 is($pointy_objects->count, $pointy_count+1, 'many_to_many add_to_$rel($hash) with where in relationship attrs count correct');
60 $pointy_count = $pointy_objects->count();
62 my $pen = $schema->resultset('TypedObject')->create({ value => "Pen", type => "pointy"});
63 eval {$collection->add_to_pointy_objects($pen)};
65 ok( !$@, 'many_to_many add_to_$rel($object) with where in relationship attrs did not throw');
66 is($pointy_objects->count, $pointy_count+1, 'many_to_many add_to_$rel($object) with where in relationship attrs count correct');
67 $pointy_count = $pointy_objects->count();
69 my $round_objects = $collection->round_objects();
70 my $round_count = $round_objects->count();
71 eval {$collection->add_to_objects({ value => "Wheel", type => "round" })};
73 ok( !$@, 'many_to_many add_to_$rel($hash) did not throw');
74 is($round_objects->count, $round_count+1, 'many_to_many add_to_$rel($hash) count correct');
77 $round_count = $round_objects->count();
78 $pointy_count = $pointy_objects->count();
79 my @all_pointy_objects = $pointy_objects->all;
80 # doing a set on pointy objects with its current set should not change any counts
81 eval {$collection->set_pointy_objects(\@all_pointy_objects)};
83 ok( !$@, 'many_to_many set_$rel(\@objects) did not throw');
84 is($pointy_objects->count, $pointy_count, 'many_to_many set_$rel($hash) count correct');
85 is($round_objects->count, $round_count, 'many_to_many set_$rel($hash) other rel count correct');