Commit | Line | Data |
70350518 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use lib qw(t/lib); |
6 | use DBICTest; |
7 | |
a47e1233 |
8 | my $schema = DBICTest->init_schema(); |
87f0da6a |
9 | |
368a5228 |
10 | plan tests => 36; |
11 | |
12 | is_deeply([ sort $schema->source('CD')->unique_constraint_names ], [ qw/cd_artist_title primary/ ], 'CD source has an automatically named unique constraint'); |
13 | is_deeply([ sort $schema->source('Producer')->unique_constraint_names ], [ qw/primary prod_name/ ], 'Producer source has a named unique constraint'); |
87f0da6a |
14 | |
15 | my $artistid = 1; |
16 | my $title = 'UNIQUE Constraint'; |
17 | |
18 | my $cd1 = $schema->resultset('CD')->find_or_create({ |
19 | artist => $artistid, |
20 | title => $title, |
21 | year => 2005, |
22 | }); |
23 | |
24 | my $cd2 = $schema->resultset('CD')->find( |
25 | { |
26 | artist => $artistid, |
27 | title => $title, |
28 | }, |
368a5228 |
29 | { key => 'cd_artist_title' } |
87f0da6a |
30 | ); |
31 | |
32 | is($cd2->get_column('artist'), $cd1->get_column('artist'), 'find by specific key: artist is correct'); |
33 | is($cd2->title, $cd1->title, 'title is correct'); |
34 | is($cd2->year, $cd1->year, 'year is correct'); |
35 | |
368a5228 |
36 | my $cd3 = $schema->resultset('CD')->find($artistid, $title, { key => 'cd_artist_title' }); |
c9bb4b2f |
37 | |
38 | is($cd3->get_column('artist'), $cd1->get_column('artist'), 'find by specific key, ordered columns: artist is correct'); |
39 | is($cd3->title, $cd1->title, 'title is correct'); |
40 | is($cd3->year, $cd1->year, 'year is correct'); |
41 | |
42 | my $cd4 = $schema->resultset('CD')->update_or_create( |
87f0da6a |
43 | { |
44 | artist => $artistid, |
45 | title => $title, |
46 | year => 2007, |
47 | }, |
48 | ); |
49 | |
c9bb4b2f |
50 | ok(! $cd4->is_changed, 'update_or_create without key: row is clean'); |
51 | is($cd4->cdid, $cd2->cdid, 'cdid is correct'); |
52 | is($cd4->get_column('artist'), $cd2->get_column('artist'), 'artist is correct'); |
53 | is($cd4->title, $cd2->title, 'title is correct'); |
54 | is($cd4->year, 2007, 'updated year is correct'); |
87f0da6a |
55 | |
c9bb4b2f |
56 | my $cd5 = $schema->resultset('CD')->update_or_create( |
87f0da6a |
57 | { |
58 | artist => $artistid, |
59 | title => $title, |
60 | year => 2007, |
61 | }, |
368a5228 |
62 | { key => 'cd_artist_title' } |
87f0da6a |
63 | ); |
64 | |
c9bb4b2f |
65 | ok(! $cd5->is_changed, 'update_or_create by specific key: row is clean'); |
66 | is($cd5->cdid, $cd2->cdid, 'cdid is correct'); |
67 | is($cd5->get_column('artist'), $cd2->get_column('artist'), 'artist is correct'); |
68 | is($cd5->title, $cd2->title, 'title is correct'); |
69 | is($cd5->year, 2007, 'updated year is correct'); |
87f0da6a |
70 | |
c9bb4b2f |
71 | my $cd6 = $schema->resultset('CD')->update_or_create( |
87f0da6a |
72 | { |
73 | cdid => $cd2->cdid, |
74 | artist => 1, |
75 | title => $cd2->title, |
76 | year => 2005, |
77 | }, |
78 | { key => 'primary' } |
79 | ); |
80 | |
c9bb4b2f |
81 | ok(! $cd6->is_changed, 'update_or_create by PK: row is clean'); |
82 | is($cd6->cdid, $cd2->cdid, 'cdid is correct'); |
83 | is($cd6->get_column('artist'), $cd2->get_column('artist'), 'artist is correct'); |
84 | is($cd6->title, $cd2->title, 'title is correct'); |
85 | is($cd6->year, 2005, 'updated year is correct'); |
87f0da6a |
86 | |
c9bb4b2f |
87 | my $cd7 = $schema->resultset('CD')->find_or_create( |
8dc40f3e |
88 | { |
89 | artist => $artistid, |
90 | title => $title, |
91 | year => 2010, |
92 | }, |
368a5228 |
93 | { key => 'cd_artist_title' } |
8dc40f3e |
94 | ); |
95 | |
c9bb4b2f |
96 | is($cd7->cdid, $cd1->cdid, 'find_or_create by specific key: cdid is correct'); |
97 | is($cd7->get_column('artist'), $cd1->get_column('artist'), 'artist is correct'); |
98 | is($cd7->title, $cd1->title, 'title is correct'); |
99 | is($cd7->year, $cd1->year, 'year is correct'); |
8dc40f3e |
100 | |
101 | my $artist = $schema->resultset('Artist')->find($artistid); |
c9bb4b2f |
102 | my $cd8 = $artist->find_or_create_related('cds', |
8dc40f3e |
103 | { |
8dc40f3e |
104 | title => $title, |
105 | year => 2020, |
106 | }, |
368a5228 |
107 | { key => 'cd_artist_title' } |
8dc40f3e |
108 | ); |
109 | |
c9bb4b2f |
110 | is($cd8->cdid, $cd1->cdid, 'find_or_create related by specific key: cdid is correct'); |
111 | is($cd8->get_column('artist'), $cd1->get_column('artist'), 'artist is correct'); |
112 | is($cd8->title, $cd1->title, 'title is correct'); |
113 | is($cd8->year, $cd1->year, 'year is correct'); |
8dc40f3e |
114 | |
c9bb4b2f |
115 | my $cd9 = $artist->update_or_create_related('cds', |
045120e6 |
116 | { |
045120e6 |
117 | title => $title, |
118 | year => 2021, |
119 | }, |
368a5228 |
120 | { key => 'cd_artist_title' } |
045120e6 |
121 | ); |
122 | |
c9bb4b2f |
123 | ok(! $cd9->is_changed, 'update_or_create by specific key: row is clean'); |
124 | is($cd9->cdid, $cd1->cdid, 'cdid is correct'); |
125 | is($cd9->get_column('artist'), $cd1->get_column('artist'), 'artist is correct'); |
126 | is($cd9->title, $cd1->title, 'title is correct'); |
127 | is($cd9->year, 2021, 'year is correct'); |
045120e6 |
128 | |