292dd6b7c4a7adca3885fd3fc7165592bc8e1d8d
[dbsrgits/DBIx-Class.git] / t / multi_create / existing_in_chain.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 my $schema = DBICTest->init_schema();
10
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
16 # more importantly some sort of recursive_insert() call needs to
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
31 my $TODO_msg = "See comment at top of @{[ __FILE__ ]} for discussion of the TODO";
32
33 {
34   my $counts;
35   $counts->{$_} = $schema->resultset($_)->count for qw/Track CD Genre/;
36
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',
48       }
49     }
50   });
51
52   is ($schema->resultset('Track')->count, $counts->{Track} + 1, '1 new track');
53   is ($schema->resultset('CD')->count, $counts->{CD}, 'No new cds');
54
55   TODO: {
56     todo_skip $TODO_msg, 1;
57     is ($schema->resultset('Genre')->count, $counts->{Genre} + 1, '1 new genre');
58     is ($existing_nogen_cd->genre->title,  'sugar genre', 'Correct genre assigned to CD');
59   }
60 }
61
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');
94
95     local $TODO = $TODO_msg;
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     );
104   }, 'create() did not throw');
105 }
106
107 done_testing;