Merge 'trunk' into 'multicreate_fixes'
[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
9plan 'no_plan';
10
11my $schema = DBICTest->init_schema();
12
13{
14 my $counts;
15 $counts->{$_} = $schema->resultset($_)->count for qw/Track CD Genre/;
16
17 lives_ok (sub {
18 my $existing_nogen_cd = $schema->resultset('CD')->search (
19 { 'genre.genreid' => undef },
20 { join => 'genre' },
21 )->first;
22
23 $schema->resultset('Track')->create ({
24 title => 'Sugar-coated',
25 cd => {
26 title => $existing_nogen_cd->title,
27 genre => {
28 name => 'sugar genre',
29 }
30 }
31 });
32
33 is ($schema->resultset('Track')->count, $counts->{Track} + 1, '1 new track');
34 is ($schema->resultset('CD')->count, $counts->{CD}, 'No new cds');
35 is ($schema->resultset('Genre')->count, $counts->{Genre} + 1, '1 new genre');
36
37 is ($existing_nogen_cd->genre->title, 'sugar genre', 'Correct genre assigned to CD');
38 });
39}
40
41{
42 my $counts;
43 $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Producer/;
44
45 lives_ok (sub {
46 my $artist = $schema->resultset('Artist')->first;
47 my $producer = $schema->resultset('Producer')->create ({ name => 'the queen of england' });
48
49 $schema->resultset('CD')->create ({
50 artist => $artist,
51 title => 'queen1',
52 year => 2007,
53 cd_to_producer => [
54 {
55 producer => {
56 name => $producer->name,
57 producer_to_cd => [
58 {
59 cd => {
60 title => 'queen2',
61 year => 2008,
62 artist => $artist,
63 },
64 },
65 ],
66 },
67 },
68 ],
69 });
70
71 is ($schema->resultset('Artist')->count, $counts->{Artist}, 'No new artists');
72 is ($schema->resultset('Producer')->count, $counts->{Producer} + 1, '1 new producers');
73 is ($schema->resultset('CD')->count, $counts->{CD} + 2, '2 new cds');
74
75 is ($producer->cds->count, 2, 'CDs assigned to correct producer');
76 is_deeply (
77 [ $producer->cds->search ({}, { order_by => 'title' })->get_column('title')->all],
78 [ qw/queen1 queen2/ ],
79 'Correct cd names',
80 );
81 });
82}
83
841;