0.08001 because I'm an idiot
[dbsrgits/DBIx-Class.git] / t / 96multi_create.t
CommitLineData
33dd4e80 1use strict;
af2d42c0 2use warnings;
33dd4e80 3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
8my $schema = DBICTest->init_schema();
9
2ec8e594 10plan tests => 17;
33dd4e80 11
12my $cd2 = $schema->resultset('CD')->create({ artist =>
13 { name => 'Fred Bloggs' },
14 title => 'Some CD',
15 year => 1996
16 });
17
ac8e89d7 18is(ref $cd2->artist, 'DBICTest::Artist', 'Created CD and Artist object');
19is($cd2->artist->name, 'Fred Bloggs', 'Artist created correctly');
20
21my $artist = $schema->resultset('Artist')->create({ name => 'Fred 2',
22 cds => [
23 { title => 'Music to code by',
24 year => 2007,
25 },
26 ],
27 });
28is(ref $artist->cds->first, 'DBICTest::CD', 'Created Artist with CDs');
29is($artist->cds->first->title, 'Music to code by', 'CD created correctly');
af2d42c0 30
31# Add a new CD
32$artist->update({cds => [ $artist->cds,
33 { title => 'Yet another CD',
34 year => 2006,
35 },
36 ],
37 });
38is(($artist->cds->search({}, { order_by => 'year' }))[0]->title, 'Yet another CD', 'Updated and added another CD');
39
40my $newartist = $schema->resultset('Artist')->find_or_create({ name => 'Fred 2'});
41
42is($newartist->name, 'Fred 2', 'Retrieved the artist');
43
3d8ee6ab 44
af2d42c0 45my $newartist2 = $schema->resultset('Artist')->find_or_create({ name => 'Fred 3',
46 cds => [
47 { title => 'Noah Act',
48 year => 2007,
49 },
50 ],
51
52 });
53
54is($newartist2->name, 'Fred 3', 'Created new artist with cds via find_or_create');
3d8ee6ab 55
56
57CREATE_RELATED1 :{
58
59 my $artist = $schema->resultset('Artist')->first;
60
61 my $cd_result = $artist->create_related('cds', {
62
63 title => 'TestOneCD1',
64 year => 2007,
65 tracks => [
66
67 { position=>111,
68 title => 'TrackOne',
69 },
70 { position=>112,
71 title => 'TrackTwo',
72 }
73 ],
74
75 });
76
77 ok( $cd_result && ref $cd_result eq 'DBICTest::CD', "Got Good CD Class");
78 ok( $cd_result->title eq "TestOneCD1", "Got Expected Title");
79
80 my $tracks = $cd_result->tracks;
81
82 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
83
84 foreach my $track ($tracks->all)
85 {
86 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
87 }
88}
89
90CREATE_RELATED2 :{
91
2ec8e594 92 my $artist = $schema->resultset('Artist')->first;
3d8ee6ab 93
2ec8e594 94 my $cd_result = $artist->create_related('cds', {
3d8ee6ab 95
2ec8e594 96 title => 'TestOneCD2',
3d8ee6ab 97 year => 2007,
98 tracks => [
99
100 { position=>111,
101 title => 'TrackOne',
102 },
103 { position=>112,
104 title => 'TrackTwo',
105 }
106 ],
107
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");
3d8ee6ab 112
113 my $tracks = $cd_result->tracks;
114
115 ok( ref $tracks eq "DBIx::Class::ResultSet", "Got Expected Tracks ResultSet");
116
117 foreach my $track ($tracks->all)
118 {
119 ok( $track && ref $track eq 'DBICTest::Track', 'Got Expected Track Class');
120 }
121}