better money value comparison in tests
[dbsrgits/DBIx-Class.git] / t / 19quotes.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use IO::File;
6
7 use lib qw(t/lib);
8 use DBIC::SqlMakerTest;
9
10 BEGIN {
11     eval "use DBD::SQLite";
12     plan $@
13         ? ( skip_all => 'needs DBD::SQLite for testing' )
14         : ( tests => 7 );
15 }
16
17
18 use_ok('DBICTest');
19 use_ok('DBIC::DebugObj');
20 my $schema = DBICTest->init_schema();
21
22 #diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
23
24 $schema->storage->sql_maker->quote_char('`');
25 $schema->storage->sql_maker->name_sep('.');
26
27 my ($sql, @bind);
28 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
29 $schema->storage->debug(1);
30
31 my $rs;
32
33 $rs = $schema->resultset('CD')->search(
34            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
35            { join => 'artist' });
36 eval { $rs->count };
37 is_same_sql_bind(
38   $sql, \@bind,
39   "SELECT COUNT( * ) FROM `cd` `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
40   'got correct SQL for count query with quoting'
41 );
42
43 my $order = 'year DESC';
44 $rs = $schema->resultset('CD')->search({},
45             { 'order_by' => $order });
46 eval { $rs->first };
47 like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
48
49 $rs = $schema->resultset('CD')->search({},
50             { 'order_by' => \$order });
51 eval { $rs->first };
52 like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
53
54 $schema->storage->sql_maker->quote_char([qw/[ ]/]);
55 $schema->storage->sql_maker->name_sep('.');
56
57 $rs = $schema->resultset('CD')->search(
58            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
59            { join => 'artist' });
60 eval { $rs->count };
61 is_same_sql_bind(
62   $sql, \@bind,
63   "SELECT COUNT( * ) FROM [cd] [me]  JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
64   'got correct SQL for count query with bracket quoting'
65 );
66
67 my %data = (
68        name => 'Bill',
69        order => '12'
70 );
71
72 $schema->storage->sql_maker->quote_char('`');
73 $schema->storage->sql_maker->name_sep('.');
74
75 is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');