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