Factor SQL-standard deferred FK checks into a component
[dbsrgits/DBIx-Class.git] / t / multi_create / find_or_multicreate.t
CommitLineData
e084cb2b 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
8my $schema = DBICTest->init_schema( no_populate => 1 );
9
10my $t11 = $schema->resultset('Track')->find_or_create({
11 trackid => 1,
12 title => 'Track one cd one',
13 cd => {
14 year => 1,
15 title => 'CD one',
16 very_long_artist_relationship => {
17 name => 'Artist one',
18 }
19 }
20});
21
22my $t12 = $schema->resultset('Track')->find_or_create({
23 trackid => 2,
24 title => 'Track two cd one',
25 cd => {
26 title => 'CD one',
27 very_long_artist_relationship => {
28 name => 'Artist one',
29 }
30 }
31});
32
33# FIXME - MC should be smart enough to infer this on its own...
34$schema->resultset('Artist')->create({ name => 'Artist two' });
35
36my $t2 = $schema->resultset('Track')->find_or_create({
37 trackid => 3,
38 title => 'Track one cd one',
39 cd => {
40 year => 1,
41 title => 'CD one',
42 very_long_artist_relationship => {
43 name => 'Artist two',
44 }
45 }
46});
47
48is_deeply(
49 $schema->resultset('Artist')->search({}, {
50 prefetch => { cds => 'tracks' },
51 order_by => 'tracks.title',
52 })->all_hri,
53 [
54 { artistid => 1, charfield => undef, name => "Artist one", rank => 13, cds => [
55 { artist => 1, cdid => 1, genreid => undef, single_track => undef, title => "CD one", year => 1, tracks => [
56 { cd => 1, last_updated_at => undef, last_updated_on => undef, position => 1, title => "Track one cd one", trackid => 1 },
57 { cd => 1, last_updated_at => undef, last_updated_on => undef, position => 2, title => "Track two cd one", trackid => 2 },
58 ]},
59 ]},
60 { artistid => 2, charfield => undef, name => "Artist two", rank => 13, cds => [
61 { artist => 2, cdid => 2, genreid => undef, single_track => undef, title => "CD one", year => 1, tracks => [
62 { cd => 2, last_updated_at => undef, last_updated_on => undef, position => 1, title => "Track one cd one", trackid => 3 },
63 ]},
64 ]},
65 ],
66 'Expected state of database after several find_or_create rounds'
67);
68
69
70done_testing;
71