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