Introducing DBIx::Class::Schema::SanityChecker
[dbsrgits/DBIx-Class.git] / t / multi_create / existing_in_chain.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8
9 use DBICTest;
10
11 my $schema = DBICTest->init_schema();
12
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
18 # more importantly some sort of recursive_insert() call needs to
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
33 my $TODO_msg = "See comment at top of @{[ __FILE__ ]} for discussion of the TODO";
34
35 {
36   my $counts;
37   $counts->{$_} = $schema->resultset($_)->count for qw/Track CD Genre/;
38
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',
50       }
51     }
52   });
53
54   is ($schema->resultset('Track')->count, $counts->{Track} + 1, '1 new track');
55   is ($schema->resultset('CD')->count, $counts->{CD}, 'No new cds');
56
57   TODO: {
58     todo_skip $TODO_msg, 1;
59     is ($schema->resultset('Genre')->count, $counts->{Genre} + 1, '1 new genre');
60     is ($existing_nogen_cd->genre->title,  'sugar genre', 'Correct genre assigned to CD');
61   }
62 }
63
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');
96
97     local $TODO = $TODO_msg;
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     );
106   }, 'create() did not throw');
107 }
108
109 done_testing;