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 |
057d4713 |
10 | plan tests => 16;\r |
78060df8 |
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 |
057d4713 |
28 | my $see_spot_rs = $owner->books->search({ title => "See Spot Run" });\r |
29 | eval { $see_spot_rs->delete(); };\r |
30 | if ($@) { print $@ }\r |
31 | ok(!$@, 'delete on resultset with attribute did not throw');\r |
32 | is($see_spot_rs->count(), 0, 'delete on resultset with attributes succeeded');\r |
33 | \r |
78060df8 |
34 | # many_to_many tests\r |
35 | my $collection = $schema->resultset('Collection')->search({collectionid => 1});\r |
36 | my $pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});\r |
37 | my $pointy_count = $pointy_objects->count();\r |
38 | is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from resultset count correct');\r |
39 | \r |
40 | $collection = $schema->resultset('Collection')->find(1);\r |
41 | $pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});\r |
42 | $pointy_count = $pointy_objects->count();\r |
43 | is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from row count correct');\r |
44 | \r |
45 | # use where on many_to_many query\r |
46 | $collection = $schema->resultset('Collection')->find(1);\r |
47 | $pointy_objects = $collection->search_related('collection_object')->search_related('object', {}, { where => { 'object.type' => 'pointy' } });\r |
48 | is($pointy_objects->count(), 2, 'many_to_many explicit query through linking table with where starting from row count correct');\r |
49 | \r |
50 | $collection = $schema->resultset('Collection')->find(1);\r |
51 | $pointy_objects = $collection->pointy_objects();\r |
52 | $pointy_count = $pointy_objects->count();\r |
53 | is($pointy_count, 2, 'many_to_many resultset with where in resultset attrs count correct');\r |
54 | \r |
55 | # add_to_$rel on many_to_many with where containing a required field\r |
56 | eval {$collection->add_to_pointy_objects({ value => "Nail" }) };\r |
57 | if ($@) { print $@ }\r |
58 | ok( !$@, 'many_to_many add_to_$rel($hash) with where in relationship attrs did not throw');\r |
59 | is($pointy_objects->count, $pointy_count+1, 'many_to_many add_to_$rel($hash) with where in relationship attrs count correct');\r |
60 | $pointy_count = $pointy_objects->count();\r |
61 | \r |
62 | my $pen = $schema->resultset('TypedObject')->create({ value => "Pen", type => "pointy"});\r |
63 | eval {$collection->add_to_pointy_objects($pen)};\r |
64 | if ($@) { print $@ }\r |
65 | ok( !$@, 'many_to_many add_to_$rel($object) with where in relationship attrs did not throw');\r |
66 | is($pointy_objects->count, $pointy_count+1, 'many_to_many add_to_$rel($object) with where in relationship attrs count correct');\r |
67 | $pointy_count = $pointy_objects->count();\r |
68 | \r |
69 | my $round_objects = $collection->round_objects();\r |
70 | my $round_count = $round_objects->count();\r |
71 | eval {$collection->add_to_objects({ value => "Wheel", type => "round" })};\r |
72 | if ($@) { print $@ }\r |
73 | ok( !$@, 'many_to_many add_to_$rel($hash) did not throw');\r |
74 | is($round_objects->count, $round_count+1, 'many_to_many add_to_$rel($hash) count correct');\r |