Merge 'trunk' into 'DBIx-Class-current'
[dbsrgits/DBIx-Class-Historic.git] / t / run / 20unique.tl
CommitLineData
87f0da6a 1sub run_tests {
2my $schema = shift;
3
c9bb4b2f 4plan tests => 34;
87f0da6a 5
6my $artistid = 1;
7my $title = 'UNIQUE Constraint';
8
9my $cd1 = $schema->resultset('CD')->find_or_create({
10 artist => $artistid,
11 title => $title,
12 year => 2005,
13});
14
15my $cd2 = $schema->resultset('CD')->find(
16 {
17 artist => $artistid,
18 title => $title,
19 },
20 { key => 'artist_title' }
21);
22
23is($cd2->get_column('artist'), $cd1->get_column('artist'), 'find by specific key: artist is correct');
24is($cd2->title, $cd1->title, 'title is correct');
25is($cd2->year, $cd1->year, 'year is correct');
26
c9bb4b2f 27my $cd3 = $schema->resultset('CD')->find($artistid, $title, { key => 'artist_title' });
28
29is($cd3->get_column('artist'), $cd1->get_column('artist'), 'find by specific key, ordered columns: artist is correct');
30is($cd3->title, $cd1->title, 'title is correct');
31is($cd3->year, $cd1->year, 'year is correct');
32
33my $cd4 = $schema->resultset('CD')->update_or_create(
87f0da6a 34 {
35 artist => $artistid,
36 title => $title,
37 year => 2007,
38 },
39);
40
c9bb4b2f 41ok(! $cd4->is_changed, 'update_or_create without key: row is clean');
42is($cd4->cdid, $cd2->cdid, 'cdid is correct');
43is($cd4->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
44is($cd4->title, $cd2->title, 'title is correct');
45is($cd4->year, 2007, 'updated year is correct');
87f0da6a 46
c9bb4b2f 47my $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 56ok(! $cd5->is_changed, 'update_or_create by specific key: row is clean');
57is($cd5->cdid, $cd2->cdid, 'cdid is correct');
58is($cd5->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
59is($cd5->title, $cd2->title, 'title is correct');
60is($cd5->year, 2007, 'updated year is correct');
87f0da6a 61
c9bb4b2f 62my $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 72ok(! $cd6->is_changed, 'update_or_create by PK: row is clean');
73is($cd6->cdid, $cd2->cdid, 'cdid is correct');
74is($cd6->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
75is($cd6->title, $cd2->title, 'title is correct');
76is($cd6->year, 2005, 'updated year is correct');
87f0da6a 77
c9bb4b2f 78my $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 87is($cd7->cdid, $cd1->cdid, 'find_or_create by specific key: cdid is correct');
88is($cd7->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
89is($cd7->title, $cd1->title, 'title is correct');
90is($cd7->year, $cd1->year, 'year is correct');
8dc40f3e 91
92my $artist = $schema->resultset('Artist')->find($artistid);
c9bb4b2f 93my $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 102is($cd8->cdid, $cd1->cdid, 'find_or_create related by specific key: cdid is correct');
103is($cd8->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
104is($cd8->title, $cd1->title, 'title is correct');
105is($cd8->year, $cd1->year, 'year is correct');
8dc40f3e 106
c9bb4b2f 107my $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 116ok(! $cd9->is_changed, 'update_or_create by specific key: row is clean');
117is($cd9->cdid, $cd1->cdid, 'cdid is correct');
118is($cd9->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
119is($cd9->title, $cd1->title, 'title is correct');
120is($cd9->year, 2021, 'year is correct');
045120e6 121
87f0da6a 122}
123
1241;