applied patch from davinch: fix bug with create_multi not inserting non-storage objects
[dbsrgits/DBIx-Class.git] / t / 96multi_create.t
CommitLineData
33dd4e80 1use strict;
af2d42c0 2use warnings;
33dd4e80 3
9c6d6d93 4use Test::More qw(no_plan);
33dd4e80 5use lib qw(t/lib);
6use DBICTest;
7
8my $schema = DBICTest->init_schema();
9
33dd4e80 10my $cd2 = $schema->resultset('CD')->create({ artist =>
11 { name => 'Fred Bloggs' },
12 title => 'Some CD',
13 year => 1996
14 });
15
ac8e89d7 16is(ref $cd2->artist, 'DBICTest::Artist', 'Created CD and Artist object');
17is($cd2->artist->name, 'Fred Bloggs', 'Artist created correctly');
18
19my $artist = $schema->resultset('Artist')->create({ name => 'Fred 2',
20 cds => [
21 { title => 'Music to code by',
22 year => 2007,
23 },
24 ],
25 });
26is(ref $artist->cds->first, 'DBICTest::CD', 'Created Artist with CDs');
27is($artist->cds->first->title, 'Music to code by', 'CD created correctly');
af2d42c0 28
29# Add a new CD
30$artist->update({cds => [ $artist->cds,
31 { title => 'Yet another CD',
32 year => 2006,
33 },
34 ],
35 });
36is(($artist->cds->search({}, { order_by => 'year' }))[0]->title, 'Yet another CD', 'Updated and added another CD');
37
38my $newartist = $schema->resultset('Artist')->find_or_create({ name => 'Fred 2'});
39
40is($newartist->name, 'Fred 2', 'Retrieved the artist');
41
3d8ee6ab 42
af2d42c0 43my $newartist2 = $schema->resultset('Artist')->find_or_create({ name => 'Fred 3',
44 cds => [
45 { title => 'Noah Act',
46 year => 2007,
47 },
48 ],
49
50 });
51
52is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
3d8ee6ab 53
54
55CREATE_RELATED1 :{
56
57 my $artist = $schema->resultset('Artist')->first;
58
59 my $cd_result = $artist->create_related('cds', {
60
61 title => 'TestOneCD1',
62 year => 2007,
63 tracks => [
64
65 { position=>111,
66 title => 'TrackOne',
67 },
68 { position=>112,
69 title => 'TrackTwo',
70 }
71 ],
72
73 });
74
75 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
76 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
77
78 my $tracks = $cd_result->tracks;
79
80 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
81
82 foreach my $track ($tracks->all)
83 {
84 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
85 }
86}
87
88CREATE_RELATED2 :{
89
2ec8e594 90 my $artist = $schema->resultset('Artist')->first;
3d8ee6ab 91
2ec8e594 92 my $cd_result = $artist->create_related('cds', {
3d8ee6ab 93
2ec8e594 94 title => 'TestOneCD2',
3d8ee6ab 95 year => 2007,
96 tracks => [
97
98 { position=>111,
99 title => 'TrackOne',
100 },
101 { position=>112,
102 title => 'TrackTwo',
103 }
104 ],
105
9c6d6d93 106 liner_notes => { notes => 'I can haz liner notes?' },
107
3d8ee6ab 108 });
109
110 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
2ec8e594 111 ok( $cd_result->title eq "TestOneCD2", "Got Expected Title");
9c6d6d93 112 ok( $cd_result->notes eq 'I can haz liner notes?', 'Liner notes');
3d8ee6ab 113
114 my $tracks = $cd_result->tracks;
115
116 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
117
118 foreach my $track ($tracks->all)
119 {
120 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
121 }
122}
e5dddc05 123
124my $cdp = $schema->resultset('CD_to_Producer')->create({
125 cd => { artist => 1, title => 'foo', year => 2000 },
126 producer => { name => 'jorge' }
127 });
128
129ok($cdp, 'join table record created ok');
2bc3c81e 130
131SPECIAL_CASE: {
132 my $kurt_cobain = { name => 'Kurt Cobain' };
133
134 my $in_utero = $schema->resultset('CD')->new({
135 title => 'In Utero',
136 year => 1993
137 });
138
139 $kurt_cobain->{cds} = [ $in_utero ];
140
141
142 $schema->resultset('Artist')->populate([ $kurt_cobain ]); # %)
143 $a = $schema->resultset('Artist')->find({name => 'Kurt Cobain'});
144
145 is($a->name, 'Kurt Cobain', 'Artist insertion ok');
146 is($a->cds && $a->cds->first && $a->cds->first->title,
147 'In Utero', 'CD insertion ok');
148}
149
150SPECIAL_CASE2: {
151 my $pink_floyd = { name => 'Pink Floyd' };
152
153 my $the_wall = { title => 'The Wall', year => 1979 };
154
155 $pink_floyd->{cds} = [ $the_wall ];
156
157
158 $schema->resultset('Artist')->populate([ $pink_floyd ]); # %)
159 $a = $schema->resultset('Artist')->find({name => 'Pink Floyd'});
160
161 is($a->name, 'Pink Floyd', 'Artist insertion ok');
162 is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
163}