Mark forgotten ::Row::id() method as indirect_sugar
[dbsrgits/DBIx-Class.git] / t / 46where_attribute.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
65c2b042 3use strict;
4use warnings;
5
6use Test::More;
7305f6f9 7use Test::Warn;
c0329273 8
65c2b042 9use DBICTest;
10my $schema = DBICTest->init_schema();
11
65c2b042 12# select from a class with resultset_attributes
13my $resultset = $schema->resultset('BooksInLibrary');
14is($resultset, 3, "select from a class with resultset_attributes okay");
15
c704015d 16$resultset = $resultset->search({}, { where => undef });
17is($resultset, 3, "where condition not obliterated");
18
65c2b042 19# now test out selects through a resultset
20my $owner = $schema->resultset('Owners')->find({name => "Newton"});
21my $programming_perl = $owner->books->find_or_create({ title => "Programming Perl" });
22is($programming_perl->id, 1, 'select from a resultset with find_or_create for existing entry ok');
23
24# and inserts?
25my $see_spot;
7305f6f9 26$see_spot = eval {
27 warnings_exist {
28 $owner->books->find_or_create({ title => "See Spot Run" })
29 } qr/Missing value for primary key column 'id' on BooksInLibrary - perhaps you forgot to set its 'is_auto_increment'/;
30};
31is ($@, '', 'find_or_create on resultset with attribute for non-existent entry did not throw');
65c2b042 32ok(defined $see_spot, 'successfully did insert on resultset with attribute for non-existent entry');
33
34my $see_spot_rs = $owner->books->search({ title => "See Spot Run" });
35eval { $see_spot_rs->delete(); };
36if ($@) { print $@ }
37ok(!$@, 'delete on resultset with attribute did not throw');
38is($see_spot_rs->count(), 0, 'delete on resultset with attributes succeeded');
39
40# many_to_many tests
41my $collection = $schema->resultset('Collection')->search({collectionid => 1});
42my $pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});
43my $pointy_count = $pointy_objects->count();
44is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from resultset count correct');
45
46$collection = $schema->resultset('Collection')->find(1);
47$pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});
48$pointy_count = $pointy_objects->count();
49is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from row count correct');
50
51# use where on many_to_many query
52$collection = $schema->resultset('Collection')->find(1);
53$pointy_objects = $collection->search_related('collection_object')->search_related('object', {}, { where => { 'object.type' => 'pointy' } });
54is($pointy_objects->count(), 2, 'many_to_many explicit query through linking table with where starting from row count correct');
55
56$collection = $schema->resultset('Collection')->find(1);
57$pointy_objects = $collection->pointy_objects();
58$pointy_count = $pointy_objects->count();
59is($pointy_count, 2, 'many_to_many resultset with where in resultset attrs count correct');
60
61# add_to_$rel on many_to_many with where containing a required field
62eval {$collection->add_to_pointy_objects({ value => "Nail" }) };
63if ($@) { print $@ }
64ok( !$@, 'many_to_many add_to_$rel($hash) with where in relationship attrs did not throw');
65is($pointy_objects->count, $pointy_count+1, 'many_to_many add_to_$rel($hash) with where in relationship attrs count correct');
66$pointy_count = $pointy_objects->count();
67
68my $pen = $schema->resultset('TypedObject')->create({ value => "Pen", type => "pointy"});
69eval {$collection->add_to_pointy_objects($pen)};
70if ($@) { print $@ }
71ok( !$@, 'many_to_many add_to_$rel($object) with where in relationship attrs did not throw');
72is($pointy_objects->count, $pointy_count+1, 'many_to_many add_to_$rel($object) with where in relationship attrs count correct');
73$pointy_count = $pointy_objects->count();
74
75my $round_objects = $collection->round_objects();
76my $round_count = $round_objects->count();
77eval {$collection->add_to_objects({ value => "Wheel", type => "round" })};
78if ($@) { print $@ }
79ok( !$@, 'many_to_many add_to_$rel($hash) did not throw');
80is($round_objects->count, $round_count+1, 'many_to_many add_to_$rel($hash) count correct');
d2d82857 81
82# test set_$rel
83$round_count = $round_objects->count();
84$pointy_count = $pointy_objects->count();
85my @all_pointy_objects = $pointy_objects->all;
86# doing a set on pointy objects with its current set should not change any counts
87eval {$collection->set_pointy_objects(\@all_pointy_objects)};
88if ($@) { print $@ }
89ok( !$@, 'many_to_many set_$rel(\@objects) did not throw');
90is($pointy_objects->count, $pointy_count, 'many_to_many set_$rel($hash) count correct');
91is($round_objects->count, $round_count, 'many_to_many set_$rel($hash) other rel count correct');
c704015d 92
93done_testing;