Commit | Line | Data |
d4d6aae3 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use Test::Exception; |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
8 | |
d4d6aae3 |
9 | my $schema = DBICTest->init_schema(); |
10 | |
11 | # Test various new() invocations - this is all about backcompat, making |
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}); |
26 | eval { |
27 | $new_artist->insert; |
28 | $new_related_cd->insert; |
29 | }; |
30 | is ($@, '', 'Staged insertion successful'); |
31 | ok($new_artist->in_storage, 'artist inserted'); |
32 | ok($new_related_cd->in_storage, 'new_related_cd inserted'); |
33 | } |
34 | |
35 | { |
d3244de8 |
36 | my $new_artist = $schema->resultset("Artist")->new_result({ 'name' => 'Depeche Mode' }); |
8cfe052c |
37 | my $new_related_cd = $new_artist->new_related('cds', { 'title' => 'Leave Slightly Noisily', 'year' => 1982}); |
38 | eval { |
39 | $new_related_cd->insert; |
40 | }; |
41 | is ($@, '', 'CD insertion survives by finding artist'); |
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 | |
51 | eval { |
52 | $new_cd->insert; |
53 | }; |
54 | is ($@, '', 'CD insertion survives by inserting artist'); |
55 | ok($new_cd->in_storage, 'new_related_cd inserted'); |
56 | ok($new_artist->in_storage, 'artist inserted'); |
57 | |
58 | my $retrieved_cd = $schema->resultset('CD')->find ({ 'title' => 'Leave Loudly While Singing Off Key'}); |
59 | ok ($retrieved_cd, 'CD found in db'); |
60 | is ($retrieved_cd->artist->name, 'Depeche Mode 2: Insertion Boogaloo', 'Correct artist attached to cd'); |
61 | } |
62 | |
5b71a733 |
63 | # test both sides of a 1:(1|0) |
64 | { |
65 | for my $reldir ('might_have', 'belongs_to') { |
66 | my $artist = $schema->resultset('Artist')->next; |
67 | |
68 | my $new_track = $schema->resultset('Track')->new ({ |
69 | title => "$reldir: First track of latest cd", |
70 | cd => { |
71 | title => "$reldir: Latest cd", |
72 | year => 2666, |
73 | artist => $artist, |
74 | }, |
75 | }); |
76 | |
77 | my $new_single = $schema->resultset('CD')->new ({ |
78 | artist => $artist, |
79 | title => "$reldir: Awesome first single", |
80 | year => 2666, |
81 | }); |
82 | |
83 | if ($reldir eq 'might_have') { |
84 | $new_track->cd_single ($new_single); |
85 | $new_track->insert; |
86 | } |
87 | else { |
88 | $new_single->single_track ($new_track); |
89 | $new_single->insert; |
90 | } |
91 | |
92 | ok ($new_single->in_storage, "$reldir single inserted"); |
93 | ok ($new_track->in_storage, "$reldir track inserted"); |
94 | |
95 | my $new_cds = $artist->search_related ('cds', |
96 | { year => '2666' }, |
97 | { prefetch => 'tracks', order_by => 'cdid' } |
98 | ); |
99 | |
100 | is_deeply ( |
101 | [$new_cds->search ({}, { result_class => 'DBIx::Class::ResultClass::HashRefInflator'})->all ], |
102 | [ |
103 | { |
104 | artist => 1, |
105 | cdid => 9, |
106 | genreid => undef, |
107 | single_track => undef, |
108 | title => "$reldir: Latest cd", |
109 | tracks => [ |
110 | { |
111 | cd => 9, |
112 | last_updated_at => undef, |
113 | last_updated_on => undef, |
114 | position => 1, |
115 | small_dt => undef, |
116 | title => "$reldir: First track of latest cd", |
117 | trackid => 19 |
118 | } |
119 | ], |
120 | year => 2666 |
121 | }, |
122 | { |
123 | artist => 1, |
124 | cdid => 10, |
125 | genreid => undef, |
126 | single_track => 19, |
127 | title => "$reldir: Awesome first single", |
128 | tracks => [], |
129 | year => 2666 |
130 | }, |
131 | ], |
132 | 'Expected rows created in database', |
133 | ); |
134 | |
135 | $new_cds->delete_all; |
136 | } |
137 | } |
138 | |
74608195 |
139 | { |
9ffcb5b8 |
140 | my $new_cd = $schema->resultset("CD")->new_result({}); |
141 | my $new_related_artist = $new_cd->new_related('artist', { 'name' => 'Marillion',}); |
142 | lives_ok ( |
143 | sub { |
144 | $new_related_artist->insert; |
145 | $new_cd->title( 'Misplaced Childhood' ); |
146 | $new_cd->year ( 1985 ); |
147 | $new_cd->artist( $new_related_artist ); # For exact backward compatibility |
148 | $new_cd->insert; |
149 | }, |
150 | 'Reversed staged insertion successful' |
151 | ); |
152 | ok($new_related_artist->in_storage, 'related artist inserted'); |
153 | ok($new_cd->in_storage, 'cd inserted'); |
154 | } |
74608195 |
155 | |
156 | done_testing; |