Commit | Line | Data |
78060df8 |
1 | use strict;\r |
2 | use warnings;\r |
3 | \r |
4 | use Test::More;\r |
5 | use Data::Dumper;\r |
6 | use lib qw(t/lib);\r |
7 | use DBICTest;\r |
8 | my $schema = DBICTest->init_schema();\r |
9 | \r |
10 | plan tests => 14;\r |
11 | \r |
12 | # select from a class with resultset_attributes\r |
13 | my $resultset = $schema->resultset('BooksInLibrary');\r |
14 | is($resultset, 3, "select from a class with resultset_attributes okay");\r |
15 | \r |
16 | # now test out selects through a resultset\r |
17 | my $owner = $schema->resultset('Owners')->find({name => "Newton"});\r |
18 | my $programming_perl = $owner->books->find_or_create({ title => "Programming Perl" });\r |
19 | is($programming_perl->id, 1, 'select from a resultset with find_or_create for existing entry ok');\r |
20 | \r |
21 | # and inserts?\r |
22 | my $see_spot;\r |
23 | $see_spot = eval { $owner->books->find_or_create({ title => "See Spot Run" }) };\r |
24 | if ($@) { print $@ }\r |
25 | ok(!$@, 'find_or_create on resultset with attribute for non-existent entry did not throw');\r |
26 | ok(defined $see_spot, 'successfully did insert on resultset with attribute for non-existent entry');\r |
27 | \r |
28 | # many_to_many tests\r |
29 | my $collection = $schema->resultset('Collection')->search({collectionid => 1});\r |
30 | my $pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});\r |
31 | my $pointy_count = $pointy_objects->count();\r |
32 | is($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 |
37 | is($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 |
42 | is($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 |
47 | is($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 |
50 | eval {$collection->add_to_pointy_objects({ value => "Nail" }) };\r |
51 | if ($@) { print $@ }\r |
52 | ok( !$@, 'many_to_many add_to_$rel($hash) with where in relationship attrs did not throw');\r |
53 | is($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 |
56 | my $pen = $schema->resultset('TypedObject')->create({ value => "Pen", type => "pointy"});\r |
57 | eval {$collection->add_to_pointy_objects($pen)};\r |
58 | if ($@) { print $@ }\r |
59 | ok( !$@, 'many_to_many add_to_$rel($object) with where in relationship attrs did not throw');\r |
60 | is($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 |
63 | my $round_objects = $collection->round_objects();\r |
64 | my $round_count = $round_objects->count();\r |
65 | eval {$collection->add_to_objects({ value => "Wheel", type => "round" })};\r |
66 | if ($@) { print $@ }\r |
67 | ok( !$@, 'many_to_many add_to_$rel($hash) did not throw');\r |
68 | is($round_objects->count, $round_count+1, 'many_to_many add_to_$rel($hash) count correct');\r |