Merge 'DBIx-Class-current' into 'reorganize_tests'
[dbsrgits/DBIx-Class.git] / t / 80unique.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
8my $schema = DBICTest::init_schema();
87f0da6a 9
c9bb4b2f 10plan tests => 34;
87f0da6a 11
12my $artistid = 1;
13my $title = 'UNIQUE Constraint';
14
15my $cd1 = $schema->resultset('CD')->find_or_create({
16 artist => $artistid,
17 title => $title,
18 year => 2005,
19});
20
21my $cd2 = $schema->resultset('CD')->find(
22 {
23 artist => $artistid,
24 title => $title,
25 },
26 { key => 'artist_title' }
27);
28
29is($cd2->get_column('artist'), $cd1->get_column('artist'), 'find by specific key: artist is correct');
30is($cd2->title, $cd1->title, 'title is correct');
31is($cd2->year, $cd1->year, 'year is correct');
32
c9bb4b2f 33my $cd3 = $schema->resultset('CD')->find($artistid, $title, { key => 'artist_title' });
34
35is($cd3->get_column('artist'), $cd1->get_column('artist'), 'find by specific key, ordered columns: artist is correct');
36is($cd3->title, $cd1->title, 'title is correct');
37is($cd3->year, $cd1->year, 'year is correct');
38
39my $cd4 = $schema->resultset('CD')->update_or_create(
87f0da6a 40 {
41 artist => $artistid,
42 title => $title,
43 year => 2007,
44 },
45);
46
c9bb4b2f 47ok(! $cd4->is_changed, 'update_or_create without key: row is clean');
48is($cd4->cdid, $cd2->cdid, 'cdid is correct');
49is($cd4->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
50is($cd4->title, $cd2->title, 'title is correct');
51is($cd4->year, 2007, 'updated year is correct');
87f0da6a 52
c9bb4b2f 53my $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 62ok(! $cd5->is_changed, 'update_or_create by specific key: row is clean');
63is($cd5->cdid, $cd2->cdid, 'cdid is correct');
64is($cd5->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
65is($cd5->title, $cd2->title, 'title is correct');
66is($cd5->year, 2007, 'updated year is correct');
87f0da6a 67
c9bb4b2f 68my $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 78ok(! $cd6->is_changed, 'update_or_create by PK: row is clean');
79is($cd6->cdid, $cd2->cdid, 'cdid is correct');
80is($cd6->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
81is($cd6->title, $cd2->title, 'title is correct');
82is($cd6->year, 2005, 'updated year is correct');
87f0da6a 83
c9bb4b2f 84my $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 93is($cd7->cdid, $cd1->cdid, 'find_or_create by specific key: cdid is correct');
94is($cd7->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
95is($cd7->title, $cd1->title, 'title is correct');
96is($cd7->year, $cd1->year, 'year is correct');
8dc40f3e 97
98my $artist = $schema->resultset('Artist')->find($artistid);
c9bb4b2f 99my $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 108is($cd8->cdid, $cd1->cdid, 'find_or_create related by specific key: cdid is correct');
109is($cd8->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
110is($cd8->title, $cd1->title, 'title is correct');
111is($cd8->year, $cd1->year, 'year is correct');
8dc40f3e 112
c9bb4b2f 113my $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 122ok(! $cd9->is_changed, 'update_or_create by specific key: row is clean');
123is($cd9->cdid, $cd1->cdid, 'cdid is correct');
124is($cd9->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
125is($cd9->title, $cd1->title, 'title is correct');
126is($cd9->year, 2021, 'year is correct');
045120e6 127