Changes/author for a1e1a51
[dbsrgits/DBIx-Class.git] / t / 46where_attribute.t
index e17b518..a54adb6 100644 (file)
@@ -1,74 +1,84 @@
-use strict;\r
-use warnings;\r
-\r
-use Test::More;\r
-use Data::Dumper;\r
-use lib qw(t/lib);\r
-use DBICTest;\r
-my $schema = DBICTest->init_schema();\r
-\r
-plan tests => 16;\r
-\r
-# select from a class with resultset_attributes\r
-my $resultset = $schema->resultset('BooksInLibrary');\r
-is($resultset, 3, "select from a class with resultset_attributes okay");\r
-\r
-# now test out selects through a resultset\r
-my $owner = $schema->resultset('Owners')->find({name => "Newton"});\r
-my $programming_perl = $owner->books->find_or_create({ title => "Programming Perl" });\r
-is($programming_perl->id, 1, 'select from a resultset with find_or_create for existing entry ok');\r
-\r
-# and inserts?\r
-my $see_spot;\r
-$see_spot = eval { $owner->books->find_or_create({ title => "See Spot Run" }) };\r
-if ($@) { print $@ }\r
-ok(!$@, 'find_or_create on resultset with attribute for non-existent entry did not throw');\r
-ok(defined $see_spot, 'successfully did insert on resultset with attribute for non-existent entry');\r
-\r
-my $see_spot_rs = $owner->books->search({ title => "See Spot Run" });\r
-eval { $see_spot_rs->delete(); };\r
-if ($@) { print $@ }\r
-ok(!$@, 'delete on resultset with attribute did not throw');\r
-is($see_spot_rs->count(), 0, 'delete on resultset with attributes succeeded');\r
-\r
-# many_to_many tests\r
-my $collection = $schema->resultset('Collection')->search({collectionid => 1});\r
-my $pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});\r
-my $pointy_count = $pointy_objects->count();\r
-is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from resultset count correct');\r
-\r
-$collection = $schema->resultset('Collection')->find(1);\r
-$pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});\r
-$pointy_count = $pointy_objects->count();\r
-is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from row count correct');\r
-\r
-# use where on many_to_many query\r
-$collection = $schema->resultset('Collection')->find(1);\r
-$pointy_objects = $collection->search_related('collection_object')->search_related('object', {}, { where => { 'object.type' => 'pointy' } });\r
-is($pointy_objects->count(), 2, 'many_to_many explicit query through linking table with where starting from row count correct');\r
-\r
-$collection = $schema->resultset('Collection')->find(1);\r
-$pointy_objects = $collection->pointy_objects();\r
-$pointy_count = $pointy_objects->count();\r
-is($pointy_count, 2, 'many_to_many resultset with where in resultset attrs count correct');\r
-\r
-# add_to_$rel on many_to_many with where containing a required field\r
-eval {$collection->add_to_pointy_objects({ value => "Nail" }) };\r
-if ($@) { print $@ }\r
-ok( !$@, 'many_to_many add_to_$rel($hash) with where in relationship attrs did not throw');\r
-is($pointy_objects->count, $pointy_count+1, 'many_to_many add_to_$rel($hash) with where in relationship attrs count correct');\r
-$pointy_count = $pointy_objects->count();\r
-\r
-my $pen = $schema->resultset('TypedObject')->create({ value => "Pen", type => "pointy"});\r
-eval {$collection->add_to_pointy_objects($pen)};\r
-if ($@) { print $@ }\r
-ok( !$@, 'many_to_many add_to_$rel($object) with where in relationship attrs did not throw');\r
-is($pointy_objects->count, $pointy_count+1, 'many_to_many add_to_$rel($object) with where in relationship attrs count correct');\r
-$pointy_count = $pointy_objects->count();\r
-\r
-my $round_objects = $collection->round_objects();\r
-my $round_count = $round_objects->count();\r
-eval {$collection->add_to_objects({ value => "Wheel", type => "round" })};\r
-if ($@) { print $@ }\r
-ok( !$@, 'many_to_many add_to_$rel($hash) did not throw');\r
-is($round_objects->count, $round_count+1, 'many_to_many add_to_$rel($hash) count correct');\r
+use strict;
+use warnings;
+
+use Test::More;
+use lib qw(t/lib);
+use DBICTest;
+my $schema = DBICTest->init_schema();
+
+plan tests => 19;
+
+# select from a class with resultset_attributes
+my $resultset = $schema->resultset('BooksInLibrary');
+is($resultset, 3, "select from a class with resultset_attributes okay");
+
+# now test out selects through a resultset
+my $owner = $schema->resultset('Owners')->find({name => "Newton"});
+my $programming_perl = $owner->books->find_or_create({ title => "Programming Perl" });
+is($programming_perl->id, 1, 'select from a resultset with find_or_create for existing entry ok');
+
+# and inserts?
+my $see_spot;
+$see_spot = eval { $owner->books->find_or_create({ title => "See Spot Run" }) };
+if ($@) { print $@ }
+ok(!$@, 'find_or_create on resultset with attribute for non-existent entry did not throw');
+ok(defined $see_spot, 'successfully did insert on resultset with attribute for non-existent entry');
+
+my $see_spot_rs = $owner->books->search({ title => "See Spot Run" });
+eval { $see_spot_rs->delete(); };
+if ($@) { print $@ }
+ok(!$@, 'delete on resultset with attribute did not throw');
+is($see_spot_rs->count(), 0, 'delete on resultset with attributes succeeded');
+
+# many_to_many tests
+my $collection = $schema->resultset('Collection')->search({collectionid => 1});
+my $pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});
+my $pointy_count = $pointy_objects->count();
+is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from resultset count correct');
+
+$collection = $schema->resultset('Collection')->find(1);
+$pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});
+$pointy_count = $pointy_objects->count();
+is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from row count correct');
+
+# use where on many_to_many query
+$collection = $schema->resultset('Collection')->find(1);
+$pointy_objects = $collection->search_related('collection_object')->search_related('object', {}, { where => { 'object.type' => 'pointy' } });
+is($pointy_objects->count(), 2, 'many_to_many explicit query through linking table with where starting from row count correct');
+
+$collection = $schema->resultset('Collection')->find(1);
+$pointy_objects = $collection->pointy_objects();
+$pointy_count = $pointy_objects->count();
+is($pointy_count, 2, 'many_to_many resultset with where in resultset attrs count correct');
+
+# add_to_$rel on many_to_many with where containing a required field
+eval {$collection->add_to_pointy_objects({ value => "Nail" }) };
+if ($@) { print $@ }
+ok( !$@, 'many_to_many add_to_$rel($hash) with where in relationship attrs did not throw');
+is($pointy_objects->count, $pointy_count+1, 'many_to_many add_to_$rel($hash) with where in relationship attrs count correct');
+$pointy_count = $pointy_objects->count();
+
+my $pen = $schema->resultset('TypedObject')->create({ value => "Pen", type => "pointy"});
+eval {$collection->add_to_pointy_objects($pen)};
+if ($@) { print $@ }
+ok( !$@, 'many_to_many add_to_$rel($object) with where in relationship attrs did not throw');
+is($pointy_objects->count, $pointy_count+1, 'many_to_many add_to_$rel($object) with where in relationship attrs count correct');
+$pointy_count = $pointy_objects->count();
+
+my $round_objects = $collection->round_objects();
+my $round_count = $round_objects->count();
+eval {$collection->add_to_objects({ value => "Wheel", type => "round" })};
+if ($@) { print $@ }
+ok( !$@, 'many_to_many add_to_$rel($hash) did not throw');
+is($round_objects->count, $round_count+1, 'many_to_many add_to_$rel($hash) count correct');
+
+# test set_$rel
+$round_count = $round_objects->count();
+$pointy_count = $pointy_objects->count();
+my @all_pointy_objects = $pointy_objects->all;
+# doing a set on pointy objects with its current set should not change any counts
+eval {$collection->set_pointy_objects(\@all_pointy_objects)};
+if ($@) { print $@ }
+ok( !$@, 'many_to_many set_$rel(\@objects) did not throw');
+is($pointy_objects->count, $pointy_count, 'many_to_many set_$rel($hash) count correct');
+is($round_objects->count, $round_count, 'many_to_many set_$rel($hash) other rel count correct');