Fix silent failures on autoinc PK without an is_auto_increment attribute
[dbsrgits/DBIx-Class.git] / t / 46where_attribute.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Warn;
8
9 use DBICTest;
10 my $schema = DBICTest->init_schema();
11
12 # select from a class with resultset_attributes
13 my $resultset = $schema->resultset('BooksInLibrary');
14 is($resultset, 3, "select from a class with resultset_attributes okay");
15
16 $resultset = $resultset->search({}, { where => undef });
17 is($resultset, 3, "where condition not obliterated");
18
19 # now test out selects through a resultset
20 my $owner = $schema->resultset('Owners')->find({name => "Newton"});
21 my $programming_perl = $owner->books->find_or_create({ title => "Programming Perl" });
22 is($programming_perl->id, 1, 'select from a resultset with find_or_create for existing entry ok');
23
24 # and inserts?
25 my $see_spot;
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 };
31 is ($@, '',  'find_or_create on resultset with attribute for non-existent entry did not throw');
32 ok(defined $see_spot, 'successfully did insert on resultset with attribute for non-existent entry');
33
34 my $see_spot_rs = $owner->books->search({ title => "See Spot Run" });
35 eval { $see_spot_rs->delete(); };
36 if ($@) { print $@ }
37 ok(!$@, 'delete on resultset with attribute did not throw');
38 is($see_spot_rs->count(), 0, 'delete on resultset with attributes succeeded');
39
40 # many_to_many tests
41 my $collection = $schema->resultset('Collection')->search({collectionid => 1});
42 my $pointy_objects = $collection->search_related('collection_object')->search_related('object', { type => "pointy"});
43 my $pointy_count = $pointy_objects->count();
44 is($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();
49 is($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' } });
54 is($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();
59 is($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
62 eval {$collection->add_to_pointy_objects({ value => "Nail" }) };
63 if ($@) { print $@ }
64 ok( !$@, 'many_to_many add_to_$rel($hash) with where in relationship attrs did not throw');
65 is($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
68 my $pen = $schema->resultset('TypedObject')->create({ value => "Pen", type => "pointy"});
69 eval {$collection->add_to_pointy_objects($pen)};
70 if ($@) { print $@ }
71 ok( !$@, 'many_to_many add_to_$rel($object) with where in relationship attrs did not throw');
72 is($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
75 my $round_objects = $collection->round_objects();
76 my $round_count = $round_objects->count();
77 eval {$collection->add_to_objects({ value => "Wheel", type => "round" })};
78 if ($@) { print $@ }
79 ok( !$@, 'many_to_many add_to_$rel($hash) did not throw');
80 is($round_objects->count, $round_count+1, 'many_to_many add_to_$rel($hash) count correct');
81
82 # test set_$rel
83 $round_count = $round_objects->count();
84 $pointy_count = $pointy_objects->count();
85 my @all_pointy_objects = $pointy_objects->all;
86 # doing a set on pointy objects with its current set should not change any counts
87 eval {$collection->set_pointy_objects(\@all_pointy_objects)};
88 if ($@) { print $@ }
89 ok( !$@, 'many_to_many set_$rel(\@objects) did not throw');
90 is($pointy_objects->count, $pointy_count, 'many_to_many set_$rel($hash) count correct');
91 is($round_objects->count, $round_count, 'many_to_many set_$rel($hash) other rel count correct');
92
93 done_testing;