7 my $schema = DBICTest->init_schema();
11 # select from a class with resultset_attributes
12 my $resultset = $schema->resultset('BooksInLibrary');
13 is($resultset, 3, "select from a class with resultset_attributes okay");
15 # now test out selects through a resultset
16 my $owner = $schema->resultset('Owners')->find({name => "Newton"});
17 my $programming_perl = $owner->books->find_or_create({ title => "Programming Perl" });
18 is($programming_perl->id, 1, 'select from a resultset with find_or_create for existing entry ok');
22 $see_spot = eval { $owner->books->find_or_create({ title => "See Spot Run" }) };
24 ok(!$@, 'find_or_create on resultset with attribute for non-existent entry did not throw');
25 ok(defined $see_spot, 'successfully did insert on resultset with attribute for non-existent entry');
27 my $see_spot_rs = $owner->books->search({ title => "See Spot Run" });
28 eval { $see_spot_rs->delete(); };
30 ok(!$@, 'delete on resultset with attribute did not throw');
31 is($see_spot_rs->count(), 0, 'delete on resultset with attributes succeeded');
34 my $collection = $schema->resultset('Collection')->search({collectionid => 1});
35 my $pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});
36 my $pointy_count = $pointy_objects->count();
37 is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from resultset count correct');
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();
42 is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from row count correct');
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' } });
47 is($pointy_objects->count(), 2, 'many_to_many explicit query through linking table with where starting from row count correct');
49 $collection = $schema->resultset('Collection')->find(1);
50 $pointy_objects = $collection->pointy_objects();
51 $pointy_count = $pointy_objects->count();
52 is($pointy_count, 2, 'many_to_many resultset with where in resultset attrs count correct');
54 # add_to_$rel on many_to_many with where containing a required field
55 eval {$collection->add_to_pointy_objects({ value => "Nail" }) };
57 ok( !$@, 'many_to_many add_to_$rel($hash) with where in relationship attrs did not throw');
58 is($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();
61 my $pen = $schema->resultset('TypedObject')->create({ value => "Pen", type => "pointy"});
62 eval {$collection->add_to_pointy_objects($pen)};
64 ok( !$@, 'many_to_many add_to_$rel($object) with where in relationship attrs did not throw');
65 is($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();
68 my $round_objects = $collection->round_objects();
69 my $round_count = $round_objects->count();
70 eval {$collection->add_to_objects({ value => "Wheel", type => "round" })};
72 ok( !$@, 'many_to_many add_to_$rel($hash) did not throw');
73 is($round_objects->count, $round_count+1, 'many_to_many add_to_$rel($hash) count correct');
76 $round_count = $round_objects->count();
77 $pointy_count = $pointy_objects->count();
78 my @all_pointy_objects = $pointy_objects->all;
79 # doing a set on pointy objects with its current set should not change any counts
80 eval {$collection->set_pointy_objects(\@all_pointy_objects)};
82 ok( !$@, 'many_to_many set_$rel(\@objects) did not throw');
83 is($pointy_objects->count, $pointy_count, 'many_to_many set_$rel($hash) count correct');
84 is($round_objects->count, $round_count, 'many_to_many set_$rel($hash) other rel count correct');