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