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