Commit | Line | Data |
9aa34b29 |
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 | |
9aa34b29 |
9 | my $schema = DBICTest->init_schema(); |
10 | |
58e39c86 |
11 | # For fully intuitive multicreate any relationships in a chain |
12 | # that do not exist for one reason or another should be created, |
13 | # even if the preceeding relationship already exists. |
14 | # |
15 | # To get this to work a minor rewrite of find() is necessary, and |
16 | # more importantly some sort of recursive_insert() call needs to |
17 | # be available. The way things will work then is: |
18 | # *) while traversing the hierarchy code calls find_or_create() |
19 | # *) this in turn calls find(%\nested_dataset) |
20 | # *) this should return not only the existing object, but must |
21 | # also attach all non-existing (in fact maybe existing) related |
22 | # bits of data to it, with in_storage => 0 |
23 | # *) then before returning the result of the succesful find(), we |
24 | # simply call $obj->recursive_insert and all is dandy |
25 | # |
26 | # Since this will not be a very clean solution, todoifying for the |
27 | # time being until an actual need arises |
28 | # |
29 | # ribasushi |
30 | |
31 | TODO: { my $f = __FILE__; local $TODO = "See comment at top of $f for discussion of the TODO"; |
32 | |
9aa34b29 |
33 | { |
34 | my $counts; |
35 | $counts->{$_} = $schema->resultset($_)->count for qw/Track CD Genre/; |
36 | |
37 | lives_ok (sub { |
38 | my $existing_nogen_cd = $schema->resultset('CD')->search ( |
39 | { 'genre.genreid' => undef }, |
40 | { join => 'genre' }, |
41 | )->first; |
42 | |
43 | $schema->resultset('Track')->create ({ |
44 | title => 'Sugar-coated', |
45 | cd => { |
46 | title => $existing_nogen_cd->title, |
47 | genre => { |
48 | name => 'sugar genre', |
49 | } |
50 | } |
51 | }); |
52 | |
53 | is ($schema->resultset('Track')->count, $counts->{Track} + 1, '1 new track'); |
54 | is ($schema->resultset('CD')->count, $counts->{CD}, 'No new cds'); |
55 | is ($schema->resultset('Genre')->count, $counts->{Genre} + 1, '1 new genre'); |
56 | |
57 | is ($existing_nogen_cd->genre->title, 'sugar genre', 'Correct genre assigned to CD'); |
58e39c86 |
58 | }, 'create() did not throw'); |
9aa34b29 |
59 | } |
9aa34b29 |
60 | { |
61 | my $counts; |
62 | $counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Producer/; |
63 | |
64 | lives_ok (sub { |
65 | my $artist = $schema->resultset('Artist')->first; |
66 | my $producer = $schema->resultset('Producer')->create ({ name => 'the queen of england' }); |
67 | |
68 | $schema->resultset('CD')->create ({ |
69 | artist => $artist, |
70 | title => 'queen1', |
71 | year => 2007, |
72 | cd_to_producer => [ |
73 | { |
74 | producer => { |
75 | name => $producer->name, |
76 | producer_to_cd => [ |
77 | { |
78 | cd => { |
79 | title => 'queen2', |
80 | year => 2008, |
81 | artist => $artist, |
82 | }, |
83 | }, |
84 | ], |
85 | }, |
86 | }, |
87 | ], |
88 | }); |
89 | |
90 | is ($schema->resultset('Artist')->count, $counts->{Artist}, 'No new artists'); |
91 | is ($schema->resultset('Producer')->count, $counts->{Producer} + 1, '1 new producers'); |
92 | is ($schema->resultset('CD')->count, $counts->{CD} + 2, '2 new cds'); |
93 | |
94 | is ($producer->cds->count, 2, 'CDs assigned to correct producer'); |
95 | is_deeply ( |
96 | [ $producer->cds->search ({}, { order_by => 'title' })->get_column('title')->all], |
97 | [ qw/queen1 queen2/ ], |
98 | 'Correct cd names', |
99 | ); |
58e39c86 |
100 | }, 'create() did not throw'); |
101 | } |
102 | |
9aa34b29 |
103 | } |
104 | |
58e39c86 |
105 | done_testing; |