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