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