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