fixed up sqlt tests a bit and tried fixing 2 failing sqlt tests
[dbsrgits/DBIx-Class-Historic.git] / t / 80unique.t
CommitLineData
70350518 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
a47e1233 8my $schema = DBICTest->init_schema();
87f0da6a 9
368a5228 10plan tests => 36;
11
12is_deeply([ sort $schema->source('CD')->unique_constraint_names ], [ qw/cd_artist_title primary/ ], 'CD source has an automatically named unique constraint');
13is_deeply([ sort $schema->source('Producer')->unique_constraint_names ], [ qw/primary prod_name/ ], 'Producer source has a named unique constraint');
87f0da6a 14
15my $artistid = 1;
16my $title = 'UNIQUE Constraint';
17
18my $cd1 = $schema->resultset('CD')->find_or_create({
19 artist => $artistid,
20 title => $title,
21 year => 2005,
22});
23
24my $cd2 = $schema->resultset('CD')->find(
25 {
26 artist => $artistid,
27 title => $title,
28 },
368a5228 29 { key => 'cd_artist_title' }
87f0da6a 30);
31
32is($cd2->get_column('artist'), $cd1->get_column('artist'), 'find by specific key: artist is correct');
33is($cd2->title, $cd1->title, 'title is correct');
34is($cd2->year, $cd1->year, 'year is correct');
35
368a5228 36my $cd3 = $schema->resultset('CD')->find($artistid, $title, { key => 'cd_artist_title' });
c9bb4b2f 37
38is($cd3->get_column('artist'), $cd1->get_column('artist'), 'find by specific key, ordered columns: artist is correct');
39is($cd3->title, $cd1->title, 'title is correct');
40is($cd3->year, $cd1->year, 'year is correct');
41
42my $cd4 = $schema->resultset('CD')->update_or_create(
87f0da6a 43 {
44 artist => $artistid,
45 title => $title,
46 year => 2007,
47 },
48);
49
c9bb4b2f 50ok(! $cd4->is_changed, 'update_or_create without key: row is clean');
51is($cd4->cdid, $cd2->cdid, 'cdid is correct');
52is($cd4->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
53is($cd4->title, $cd2->title, 'title is correct');
54is($cd4->year, 2007, 'updated year is correct');
87f0da6a 55
c9bb4b2f 56my $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 65ok(! $cd5->is_changed, 'update_or_create by specific key: row is clean');
66is($cd5->cdid, $cd2->cdid, 'cdid is correct');
67is($cd5->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
68is($cd5->title, $cd2->title, 'title is correct');
69is($cd5->year, 2007, 'updated year is correct');
87f0da6a 70
c9bb4b2f 71my $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 81ok(! $cd6->is_changed, 'update_or_create by PK: row is clean');
82is($cd6->cdid, $cd2->cdid, 'cdid is correct');
83is($cd6->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
84is($cd6->title, $cd2->title, 'title is correct');
85is($cd6->year, 2005, 'updated year is correct');
87f0da6a 86
c9bb4b2f 87my $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 96is($cd7->cdid, $cd1->cdid, 'find_or_create by specific key: cdid is correct');
97is($cd7->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
98is($cd7->title, $cd1->title, 'title is correct');
99is($cd7->year, $cd1->year, 'year is correct');
8dc40f3e 100
101my $artist = $schema->resultset('Artist')->find($artistid);
c9bb4b2f 102my $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 110is($cd8->cdid, $cd1->cdid, 'find_or_create related by specific key: cdid is correct');
111is($cd8->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
112is($cd8->title, $cd1->title, 'title is correct');
113is($cd8->year, $cd1->year, 'year is correct');
8dc40f3e 114
c9bb4b2f 115my $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 123ok(! $cd9->is_changed, 'update_or_create by specific key: row is clean');
124is($cd9->cdid, $cd1->cdid, 'cdid is correct');
125is($cd9->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
126is($cd9->title, $cd1->title, 'title is correct');
127is($cd9->year, 2021, 'year is correct');
045120e6 128