Merge 'trunk' into 'replication_dedux'
[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
057d4713 10plan tests => 16;\r
78060df8 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
057d4713 28my $see_spot_rs = $owner->books->search({ title => "See Spot Run" });\r
29eval { $see_spot_rs->delete(); };\r
30if ($@) { print $@ }\r
31ok(!$@, 'delete on resultset with attribute did not throw');\r
32is($see_spot_rs->count(), 0, 'delete on resultset with attributes succeeded');\r
33\r
78060df8 34# many_to_many tests\r
35my $collection = $schema->resultset('Collection')->search({collectionid => 1});\r
36my $pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});\r
37my $pointy_count = $pointy_objects->count();\r
38is($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
43is($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
48is($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
53is($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
56eval {$collection->add_to_pointy_objects({ value => "Nail" }) };\r
57if ($@) { print $@ }\r
58ok( !$@, 'many_to_many add_to_$rel($hash) with where in relationship attrs did not throw');\r
59is($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
62my $pen = $schema->resultset('TypedObject')->create({ value => "Pen", type => "pointy"});\r
63eval {$collection->add_to_pointy_objects($pen)};\r
64if ($@) { print $@ }\r
65ok( !$@, 'many_to_many add_to_$rel($object) with where in relationship attrs did not throw');\r
66is($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
69my $round_objects = $collection->round_objects();\r
70my $round_count = $round_objects->count();\r
71eval {$collection->add_to_objects({ value => "Wheel", type => "round" })};\r
72if ($@) { print $@ }\r
73ok( !$@, 'many_to_many add_to_$rel($hash) did not throw');\r
74is($round_objects->count, $round_count+1, 'many_to_many add_to_$rel($hash) count correct');\r