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 | |
8cfe052c |
9 | plan tests => 12; |
d4d6aae3 |
10 | |
11 | my $schema = DBICTest->init_schema(); |
12 | |
13 | # Test various new() invocations - this is all about backcompat, making |
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}); |
28 | eval { |
29 | $new_artist->insert; |
30 | $new_related_cd->insert; |
31 | }; |
32 | is ($@, '', 'Staged insertion successful'); |
33 | ok($new_artist->in_storage, 'artist inserted'); |
34 | ok($new_related_cd->in_storage, 'new_related_cd inserted'); |
35 | } |
36 | |
37 | { |
d3244de8 |
38 | my $new_artist = $schema->resultset("Artist")->new_result({ 'name' => 'Depeche Mode' }); |
8cfe052c |
39 | my $new_related_cd = $new_artist->new_related('cds', { 'title' => 'Leave Slightly Noisily', 'year' => 1982}); |
40 | eval { |
41 | $new_related_cd->insert; |
42 | }; |
43 | is ($@, '', 'CD insertion survives by finding artist'); |
44 | ok($new_artist->in_storage, 'artist inserted'); |
45 | ok($new_related_cd->in_storage, 'new_related_cd inserted'); |
46 | } |
47 | |
48 | { |
49 | my $new_artist = $schema->resultset("Artist")->new_result({ 'name' => 'Depeche Mode 2: Insertion Boogaloo' }); |
50 | my $new_related_cd = $new_artist->new_related('cds', { 'title' => 'Leave Loudly While Singing Off Key', 'year' => 1982}); |
d3244de8 |
51 | eval { |
52 | $new_related_cd->insert; |
53 | }; |
54 | is ($@, '', 'CD insertion survives by inserting artist'); |
55 | ok($new_artist->in_storage, 'artist inserted'); |
56 | ok($new_related_cd->in_storage, 'new_related_cd inserted'); |
57 | } |
58 | |
59 | { |
9ffcb5b8 |
60 | my $new_cd = $schema->resultset("CD")->new_result({}); |
61 | my $new_related_artist = $new_cd->new_related('artist', { 'name' => 'Marillion',}); |
62 | lives_ok ( |
63 | sub { |
64 | $new_related_artist->insert; |
65 | $new_cd->title( 'Misplaced Childhood' ); |
66 | $new_cd->year ( 1985 ); |
67 | $new_cd->artist( $new_related_artist ); # For exact backward compatibility |
68 | $new_cd->insert; |
69 | }, |
70 | 'Reversed staged insertion successful' |
71 | ); |
72 | ok($new_related_artist->in_storage, 'related artist inserted'); |
73 | ok($new_cd->in_storage, 'cd inserted'); |
74 | } |