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