Fix tests failing due to unspecified resultset retrieval order
[dbsrgits/DBIx-Class.git] / t / multi_create / in_memory.t
CommitLineData
d4d6aae3 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
6use lib qw(t/lib);
7use DBICTest;
8
d4d6aae3 9my $schema = DBICTest->init_schema();
10
8273e845 11# Test various new() invocations - this is all about backcompat, making
d4d6aae3 12# sure that insert() still works as expected by legacy code.
13#
14# What we essentially do is multi-instantiate objects, making sure nothing
15# gets inserted. Then we add some more objects to the mix either via
16# new_related() or by setting an accessor directly (or both) - again
17# expecting no inserts. Then after calling insert() on the starter object
a6a1ec32 18# we expect everything supplied to new() to get inserted, as well as any
19# relations whose PK's are necessary to complete the objects supplied
20# to new(). All other objects should be insert()able afterwards too.
d4d6aae3 21
22
9ffcb5b8 23{
24 my $new_artist = $schema->resultset("Artist")->new_result({ 'name' => 'Depeche Mode' });
25 my $new_related_cd = $new_artist->new_related('cds', { 'title' => 'Leave in Silence', 'year' => 1982});
0a03206a 26 lives_ok {
9ffcb5b8 27 $new_artist->insert;
28 $new_related_cd->insert;
0a03206a 29 } 'Staged insertion successful';
9ffcb5b8 30 ok($new_artist->in_storage, 'artist inserted');
31 ok($new_related_cd->in_storage, 'new_related_cd inserted');
32}
33
34{
0a03206a 35 my $new_artist = $schema->resultset("Artist")->new_result({ 'name' => 'Mode Depeche' });
8cfe052c 36 my $new_related_cd = $new_artist->new_related('cds', { 'title' => 'Leave Slightly Noisily', 'year' => 1982});
0a03206a 37 lives_ok {
8cfe052c 38 $new_related_cd->insert;
0a03206a 39 } 'CD insertion survives by finding artist';
8cfe052c 40 ok($new_artist->in_storage, 'artist inserted');
41 ok($new_related_cd->in_storage, 'new_related_cd inserted');
42}
43
44{
74608195 45 my $new_cd = $schema->resultset('CD')->new ({ 'title' => 'Leave Loudly While Singing Off Key', 'year' => 1982});
46 my $new_artist = $schema->resultset("Artist")->new ({ 'name' => 'Depeche Mode 2: Insertion Boogaloo' });
47 $new_cd->artist ($new_artist);
48
0a03206a 49 lives_ok {
74608195 50 $new_cd->insert;
0a03206a 51 } 'CD insertion survives by inserting artist';
74608195 52 ok($new_cd->in_storage, 'new_related_cd inserted');
53 ok($new_artist->in_storage, 'artist inserted');
54
55 my $retrieved_cd = $schema->resultset('CD')->find ({ 'title' => 'Leave Loudly While Singing Off Key'});
56 ok ($retrieved_cd, 'CD found in db');
57 is ($retrieved_cd->artist->name, 'Depeche Mode 2: Insertion Boogaloo', 'Correct artist attached to cd');
58}
59
0a03206a 60{
61 my $new_cd = $schema->resultset('CD')->new ({ 'title' => 'Leave screaming Off Key in the nude', 'year' => 1982});
62 my $new_related_artist = $new_cd->new_related( artist => { 'name' => 'Depeche Mode 3: Insertion Boogaloo' });
63 lives_ok {
64 $new_related_artist->insert;
65 $new_cd->insert;
66 } 'CD insertion survives after inserting artist';
67 ok($new_cd->in_storage, 'cd inserted');
68 ok($new_related_artist->in_storage, 'artist inserted');
69
70 my $retrieved_cd = $schema->resultset('CD')->find ({ 'title' => 'Leave screaming Off Key in the nude'});
71 ok ($retrieved_cd, 'CD found in db');
72 is ($retrieved_cd->artist->name, 'Depeche Mode 3: Insertion Boogaloo', 'Correct artist attached to cd');
73}
74
5b71a733 75# test both sides of a 1:(1|0)
76{
77 for my $reldir ('might_have', 'belongs_to') {
fb88ca2c 78 my $artist = $schema->resultset('Artist')->find(1);
5b71a733 79
80 my $new_track = $schema->resultset('Track')->new ({
81 title => "$reldir: First track of latest cd",
82 cd => {
83 title => "$reldir: Latest cd",
84 year => 2666,
85 artist => $artist,
86 },
87 });
88
89 my $new_single = $schema->resultset('CD')->new ({
90 artist => $artist,
91 title => "$reldir: Awesome first single",
92 year => 2666,
93 });
94
95 if ($reldir eq 'might_have') {
96 $new_track->cd_single ($new_single);
97 $new_track->insert;
98 }
99 else {
100 $new_single->single_track ($new_track);
101 $new_single->insert;
102 }
103
104 ok ($new_single->in_storage, "$reldir single inserted");
105 ok ($new_track->in_storage, "$reldir track inserted");
106
107 my $new_cds = $artist->search_related ('cds',
108 { year => '2666' },
109 { prefetch => 'tracks', order_by => 'cdid' }
110 );
111
112 is_deeply (
113 [$new_cds->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator'})->all ],
114 [
115 {
116 artist => 1,
0a03206a 117 cdid => 10,
5b71a733 118 genreid => undef,
119 single_track => undef,
120 title => "$reldir: Latest cd",
121 tracks => [
122 {
0a03206a 123 cd => 10,
5b71a733 124 last_updated_at => undef,
125 last_updated_on => undef,
126 position => 1,
5b71a733 127 title => "$reldir: First track of latest cd",
128 trackid => 19
129 }
130 ],
131 year => 2666
132 },
133 {
134 artist => 1,
0a03206a 135 cdid => 11,
5b71a733 136 genreid => undef,
137 single_track => 19,
138 title => "$reldir: Awesome first single",
139 tracks => [],
140 year => 2666
141 },
142 ],
143 'Expected rows created in database',
144 );
145
146 $new_cds->delete_all;
147 }
148}
149
74608195 150{
9ffcb5b8 151 my $new_cd = $schema->resultset("CD")->new_result({});
152 my $new_related_artist = $new_cd->new_related('artist', { 'name' => 'Marillion',});
153 lives_ok (
154 sub {
155 $new_related_artist->insert;
156 $new_cd->title( 'Misplaced Childhood' );
157 $new_cd->year ( 1985 );
158 $new_cd->artist( $new_related_artist ); # For exact backward compatibility
159 $new_cd->insert;
160 },
161 'Reversed staged insertion successful'
162 );
163 ok($new_related_artist->in_storage, 'related artist inserted');
164 ok($new_cd->in_storage, 'cd inserted');
165}
74608195 166
167done_testing;