Commit | Line | Data |
2284af7e |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use Test::Exception; |
b7743dab |
6 | use Test::Warn; |
2284af7e |
7 | use lib qw(t/lib); |
8 | use DBICTest; |
9 | use DBIC::SqlMakerTest; |
10 | |
11 | my $schema = DBICTest->init_schema(); |
12 | |
fb88ca2c |
13 | my $artist = $schema->resultset ('Artist')->find(1); |
2284af7e |
14 | |
15 | my $genre = $schema->resultset ('Genre') |
16 | ->create ({ name => 'par excellence' }); |
b7743dab |
17 | my $genre_cds = $genre->cds; |
2284af7e |
18 | |
b7743dab |
19 | is ($genre_cds->count, 0, 'No cds yet'); |
2284af7e |
20 | |
21 | # expect a create |
22 | $genre->update_or_create_related ('cds', { |
23 | artist => $artist, |
24 | year => 2009, |
25 | title => 'the best thing since sliced bread', |
26 | }); |
27 | |
28 | # verify cd was inserted ok |
b7743dab |
29 | is ($genre_cds->count, 1, 'One cd'); |
30 | my $cd = $genre_cds->first; |
2284af7e |
31 | is_deeply ( |
32 | { map { $_, $cd->get_column ($_) } qw/artist year title/ }, |
33 | { |
34 | artist => $artist->id, |
35 | year => 2009, |
36 | title => 'the best thing since sliced bread', |
37 | }, |
38 | 'CD created correctly', |
39 | ); |
40 | |
41 | # expect a year update on the only related row |
b7743dab |
42 | # (non-qunique column + unique column set as disambiguator) |
2284af7e |
43 | $genre->update_or_create_related ('cds', { |
44 | year => 2010, |
45 | title => 'the best thing since sliced bread', |
b7743dab |
46 | artist => 1, |
2284af7e |
47 | }); |
48 | |
49 | # re-fetch the cd, verify update |
50 | is ($genre->search_related( 'cds' )->count, 1, 'Still one cd'); |
b7743dab |
51 | $cd = $genre_cds->first; |
2284af7e |
52 | is_deeply ( |
53 | { map { $_, $cd->get_column ($_) } qw/artist year title/ }, |
54 | { |
55 | artist => $artist->id, |
56 | year => 2010, |
57 | title => 'the best thing since sliced bread', |
58 | }, |
59 | 'CD year column updated correctly', |
60 | ); |
61 | |
b7743dab |
62 | # expect a failing create: |
63 | # the unique constraint is not complete, and there is nothing |
64 | # in the database with such a year yet - insertion will fail due |
65 | # to missing artist fk |
66 | throws_ok { |
67 | $genre->update_or_create_related ('cds', { |
68 | year => 2020, |
69 | title => 'the best thing since sliced bread', |
70 | }) |
ed5550d3 |
71 | } qr/DBI Exception.+(?x: |
72 | \QNOT NULL constraint failed: cd.artist\E |
73 | | |
74 | \Qcd.artist may not be NULL\E |
75 | )/s, 'ambiguous find + create failed' |
76 | ; |
2284af7e |
77 | |
78 | # expect a create, after a failed search using *only* the |
79 | # *current* relationship and the unique column constraints |
80 | # (so no year) |
49eeb48d |
81 | $schema->is_executed_sql_bind( sub { |
82 | $genre->update_or_create_related ('cds', { |
83 | title => 'the best thing since vertical toasters', |
84 | artist => $artist, |
85 | year => 2012, |
86 | }); |
87 | }, [ |
88 | [ |
89 | 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track |
90 | FROM cd me |
91 | WHERE ( me.artist = ? AND me.genreid = ? AND me.title = ? ) |
92 | ', |
93 | 1, |
94 | 2, |
95 | "the best thing since vertical toasters", |
96 | ], |
97 | [ |
98 | 'INSERT INTO cd ( artist, genreid, title, year) VALUES ( ?, ?, ?, ? )', |
99 | 1, |
100 | 2, |
101 | "the best thing since vertical toasters", |
102 | 2012, |
103 | ], |
104 | ], 'expected select issued' ); |
2284af7e |
105 | |
106 | # a has_many search without a unique constraint makes no sense |
107 | # but I am not sure what to test for - leaving open |
b7743dab |
108 | |
109 | done_testing; |