Institute a central "load this first in testing" package
[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;
c0329273 7
65c2b042 8use DBICTest;
9my $schema = DBICTest->init_schema();
10
65c2b042 11# select from a class with resultset_attributes
12my $resultset = $schema->resultset('BooksInLibrary');
13is($resultset, 3, "select from a class with resultset_attributes okay");
14
c704015d 15$resultset = $resultset->search({}, { where => undef });
16is($resultset, 3, "where condition not obliterated");
17
65c2b042 18# now test out selects through a resultset
19my $owner = $schema->resultset('Owners')->find({name => "Newton"});
20my $programming_perl = $owner->books->find_or_create({ title => "Programming Perl" });
21is($programming_perl->id, 1, 'select from a resultset with find_or_create for existing entry ok');
22
23# and inserts?
24my $see_spot;
25$see_spot = eval { $owner->books->find_or_create({ title => "See Spot Run" }) };
26if ($@) { print $@ }
27ok(!$@, 'find_or_create on resultset with attribute for non-existent entry did not throw');
28ok(defined $see_spot, 'successfully did insert on resultset with attribute for non-existent entry');
29
30my $see_spot_rs = $owner->books->search({ title => "See Spot Run" });
31eval { $see_spot_rs->delete(); };
32if ($@) { print $@ }
33ok(!$@, 'delete on resultset with attribute did not throw');
34is($see_spot_rs->count(), 0, 'delete on resultset with attributes succeeded');
35
36# many_to_many tests
37my $collection = $schema->resultset('Collection')->search({collectionid => 1});
38my $pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});
39my $pointy_count = $pointy_objects->count();
40is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from resultset count correct');
41
42$collection = $schema->resultset('Collection')->find(1);
43$pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});
44$pointy_count = $pointy_objects->count();
45is($pointy_count, 2, 'many_to_many explicit query through linking table with query starting from row count correct');
46
47# use where on many_to_many query
48$collection = $schema->resultset('Collection')->find(1);
49$pointy_objects = $collection->search_related('collection_object')->search_related('object', {}, { where => { 'object.type' => 'pointy' } });
50is($pointy_objects->count(), 2, 'many_to_many explicit query through linking table with where starting from row count correct');
51
52$collection = $schema->resultset('Collection')->find(1);
53$pointy_objects = $collection->pointy_objects();
54$pointy_count = $pointy_objects->count();
55is($pointy_count, 2, 'many_to_many resultset with where in resultset attrs count correct');
56
57# add_to_$rel on many_to_many with where containing a required field
58eval {$collection->add_to_pointy_objects({ value => "Nail" }) };
59if ($@) { print $@ }
60ok( !$@, 'many_to_many add_to_$rel($hash) with where in relationship attrs did not throw');
61is($pointy_objects->count, $pointy_count+1, 'many_to_many add_to_$rel($hash) with where in relationship attrs count correct');
62$pointy_count = $pointy_objects->count();
63
64my $pen = $schema->resultset('TypedObject')->create({ value => "Pen", type => "pointy"});
65eval {$collection->add_to_pointy_objects($pen)};
66if ($@) { print $@ }
67ok( !$@, 'many_to_many add_to_$rel($object) with where in relationship attrs did not throw');
68is($pointy_objects->count, $pointy_count+1, 'many_to_many add_to_$rel($object) with where in relationship attrs count correct');
69$pointy_count = $pointy_objects->count();
70
71my $round_objects = $collection->round_objects();
72my $round_count = $round_objects->count();
73eval {$collection->add_to_objects({ value => "Wheel", type => "round" })};
74if ($@) { print $@ }
75ok( !$@, 'many_to_many add_to_$rel($hash) did not throw');
76is($round_objects->count, $round_count+1, 'many_to_many add_to_$rel($hash) count correct');
d2d82857 77
78# test set_$rel
79$round_count = $round_objects->count();
80$pointy_count = $pointy_objects->count();
81my @all_pointy_objects = $pointy_objects->all;
82# doing a set on pointy objects with its current set should not change any counts
83eval {$collection->set_pointy_objects(\@all_pointy_objects)};
84if ($@) { print $@ }
85ok( !$@, 'many_to_many set_$rel(\@objects) did not throw');
86is($pointy_objects->count, $pointy_count, 'many_to_many set_$rel($hash) count correct');
87is($round_objects->count, $round_count, 'many_to_many set_$rel($hash) other rel count correct');
c704015d 88
89done_testing;