moved tests to compose_namespace instead of compose_connection, marked compose_connec...
[dbsrgits/DBIx-Class.git] / t / 19quotes_newstyle.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use IO::File;
6
7 BEGIN {
8     eval "use DBD::SQLite";
9     plan $@
10         ? ( skip_all => 'needs DBD::SQLite for testing' )
11         : ( tests => 6 );
12 }
13
14 use lib qw(t/lib);
15
16 use_ok('DBICTest');
17 my $schema = DBICTest->init_schema();
18
19 my $orig_debugcb = $schema->storage->debugcb;
20 my $orig_debug = $schema->storage->debug;
21
22 diag('Testing against ' . join(' ', map { $schema->storage->dbh->get_info($_) } qw/17 18/));
23
24 my $dsn = $schema->storage->connect_info->[0];
25 $schema->connection($dsn, { quote_char => '`', name_sep => '.' });
26
27 my $sql = '';
28 $schema->storage->debugcb(sub { $sql = $_[1] });
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 like($sql, qr/\QSELECT COUNT( * ) FROM `cd` `me`  JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )\E/, 'got correct SQL for count query with quoting');
38
39 my $order = 'year DESC';
40 $rs = $schema->resultset('CD')->search({},
41             { 'order_by' => $order });
42 eval { $rs->first };
43 like($sql, qr/ORDER BY `\Q${order}\E`/, 'quoted ORDER BY with DESC (should use a scalarref anyway)');
44
45 $rs = $schema->resultset('CD')->search({},
46             { 'order_by' => \$order });
47 eval { $rs->first };
48 like($sql, qr/ORDER BY \Q${order}\E/, 'did not quote ORDER BY with scalarref');
49
50 $schema->connection($dsn, { quote_char => [qw/[ ]/], name_sep => '.' });
51 $schema->storage->debugcb(sub { $sql = $_[1] });
52 $schema->storage->debug(1);
53
54 $rs = $schema->resultset('CD')->search(
55            { 'me.year' => 2001, 'artist.name' => 'Caterwauler McCrae' },
56            { join => 'artist' });
57 eval { $rs->count };
58 like($sql, qr/\QSELECT COUNT( * ) FROM [cd] [me]  JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )\E/, 'got correct SQL for count query with bracket quoting');
59
60 my %data = (
61        name => 'Bill',
62        order => '12'
63 );
64
65 $schema->connection($dsn, { quote_char => '`', name_sep => '.' });
66
67 is($schema->storage->sql_maker->update('group', \%data), 'UPDATE `group` SET `name` = ?, `order` = ?', 'quoted table names for UPDATE');
68
69 $schema->storage->debugcb($orig_debugcb);
70 $schema->storage->debug($orig_debug);