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