Introducing DBIx::Class::Schema::SanityChecker
[dbsrgits/DBIx-Class.git] / t / multi_create / existing_in_chain.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
9aa34b29 3use strict;
4use warnings;
5
6use Test::More;
7use Test::Exception;
c0329273 8
9aa34b29 9use DBICTest;
10
9aa34b29 11my $schema = DBICTest->init_schema();
12
58e39c86 13# For fully intuitive multicreate any relationships in a chain
14# that do not exist for one reason or another should be created,
15# even if the preceeding relationship already exists.
16#
17# To get this to work a minor rewrite of find() is necessary, and
8273e845 18# more importantly some sort of recursive_insert() call needs to
58e39c86 19# be available. The way things will work then is:
20# *) while traversing the hierarchy code calls find_or_create()
21# *) this in turn calls find(%\nested_dataset)
22# *) this should return not only the existing object, but must
23# also attach all non-existing (in fact maybe existing) related
24# bits of data to it, with in_storage => 0
25# *) then before returning the result of the succesful find(), we
26# simply call $obj->recursive_insert and all is dandy
27#
28# Since this will not be a very clean solution, todoifying for the
29# time being until an actual need arises
30#
31# ribasushi
32
9fd5c112 33my $TODO_msg = "See comment at top of @{[ __FILE__ ]} for discussion of the TODO";
58e39c86 34
9aa34b29 35{
36 my $counts;
37 $counts->{$_} = $schema->resultset($_)->count for qw/Track CD Genre/;
38
9fd5c112 39 my $existing_nogen_cd = $schema->resultset('CD')->search (
40 { 'genre.genreid' => undef },
41 { join => 'genre' },
42 )->first;
43
44 $schema->resultset('Track')->create ({
45 title => 'Sugar-coated',
46 cd => {
47 title => $existing_nogen_cd->title,
48 genre => {
49 name => 'sugar genre',
9aa34b29 50 }
9fd5c112 51 }
52 });
9aa34b29 53
9fd5c112 54 is ($schema->resultset('Track')->count, $counts->{Track} + 1, '1 new track');
55 is ($schema->resultset('CD')->count, $counts->{CD}, 'No new cds');
9aa34b29 56
9fd5c112 57 TODO: {
58 todo_skip $TODO_msg, 1;
59 is ($schema->resultset('Genre')->count, $counts->{Genre} + 1, '1 new genre');
9aa34b29 60 is ($existing_nogen_cd->genre->title, 'sugar genre', 'Correct genre assigned to CD');
9fd5c112 61 }
9aa34b29 62}
9fd5c112 63
9aa34b29 64{
65 my $counts;
66 $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Producer/;
67
68 lives_ok (sub {
69 my $artist = $schema->resultset('Artist')->first;
70 my $producer = $schema->resultset('Producer')->create ({ name => 'the queen of england' });
71
72 $schema->resultset('CD')->create ({
73 artist => $artist,
74 title => 'queen1',
75 year => 2007,
76 cd_to_producer => [
77 {
78 producer => {
79 name => $producer->name,
80 producer_to_cd => [
81 {
82 cd => {
83 title => 'queen2',
84 year => 2008,
85 artist => $artist,
86 },
87 },
88 ],
89 },
90 },
91 ],
92 });
93
94 is ($schema->resultset('Artist')->count, $counts->{Artist}, 'No new artists');
95 is ($schema->resultset('Producer')->count, $counts->{Producer} + 1, '1 new producers');
9fd5c112 96
97 local $TODO = $TODO_msg;
9aa34b29 98 is ($schema->resultset('CD')->count, $counts->{CD} + 2, '2 new cds');
99
100 is ($producer->cds->count, 2, 'CDs assigned to correct producer');
101 is_deeply (
102 [ $producer->cds->search ({}, { order_by => 'title' })->get_column('title')->all],
103 [ qw/queen1 queen2/ ],
104 'Correct cd names',
105 );
58e39c86 106 }, 'create() did not throw');
107}
108
58e39c86 109done_testing;