Port remaining tests to the Opt::Dep reposiory
[dbsrgits/DBIx-Class.git] / t / bind / attribute.t
CommitLineData
1c133e22 1use strict;
2use warnings;
3
4use Test::More;
5use lib qw(t/lib);
07a287ce 6use DBIC::SqlMakerTest;
7
8use_ok('DBICTest');
1c133e22 9
10my $schema = DBICTest->init_schema;
11
12BEGIN {
13 eval "use DBD::SQLite";
14 plan $@
15 ? ( skip_all => 'needs DBD::SQLite for testing' )
b8b55c8e 16 : ( tests => 13 );
1c133e22 17}
18
1c133e22 19my $where_bind = {
20 where => \'name like ?',
21 bind => [ 'Cat%' ],
22};
23
24my $rs;
25
26TODO: {
27 local $TODO = 'bind args order needs fixing (semifor)';
28
29 # First, the simple cases...
30 $rs = $schema->resultset('Artist')->search(
31 { artistid => 1 },
32 $where_bind,
33 );
34
35 is ( $rs->count, 1, 'where/bind combined' );
36
37 $rs= $schema->resultset('Artist')->search({}, $where_bind)
38 ->search({ artistid => 1});
39
40 is ( $rs->count, 1, 'where/bind first' );
0491b597 41
1c133e22 42 $rs = $schema->resultset('Artist')->search({ artistid => 1})
43 ->search({}, $where_bind);
44
45 is ( $rs->count, 1, 'where/bind last' );
46}
47
b8b55c8e 48{
49 # More complex cases, based primarily on the Cookbook
50 # "Arbitrary SQL through a custom ResultSource" technique,
51 # which seems to be the only place the bind attribute is
52 # documented. Breaking this technique probably breaks existing
53 # application code.
54 my $source = DBICTest::Artist->result_source_instance;
55 my $new_source = $source->new($source);
56 $new_source->source_name('Complex');
1c133e22 57
b8b55c8e 58 $new_source->name(\<<'');
59 ( SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year
60 FROM artist a
61 JOIN cd ON cd.artist = a.artistid
62 WHERE cd.year = ?)
1c133e22 63
b8b55c8e 64 $schema->register_extra_source('Complex' => $new_source);
1c133e22 65
b8b55c8e 66 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] });
67 is ( $rs->count, 1, 'cookbook arbitrary sql example' );
1c133e22 68
b8b55c8e 69 $rs = $schema->resultset('Complex')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
70 is ( $rs->count, 1, '...cookbook + search condition' );
1c133e22 71
b8b55c8e 72 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
73 ->search({ 'artistid' => 1 });
74 is ( $rs->count, 1, '...cookbook (bind first) + chained search' );
1c133e22 75
07a287ce 76 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
07a287ce 77 is_same_sql_bind(
af6aac2d 78 $rs->as_query,
0491b597 79 "(SELECT me.artistid, me.name, me.rank, me.charfield FROM (SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year FROM artist a JOIN cd ON cd.artist = a.artistid WHERE cd.year = ?) me WHERE title LIKE ?)",
07a287ce 80 [
81 [ '!!dummy' => '1999' ],
82 [ '!!dummy' => 'Spoon%' ]
83 ],
84 'got correct SQL'
b8b55c8e 85 );
86}
87
88{
89 # More complex cases, based primarily on the Cookbook
90 # "Arbitrary SQL through a custom ResultSource" technique,
91 # which seems to be the only place the bind attribute is
92 # documented. Breaking this technique probably breaks existing
93 # application code.
94
95 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] });
96 is ( $rs->count, 1, 'cookbook arbitrary sql example (in separate file)' );
97
98 $rs = $schema->resultset('CustomSql')->search({ 'artistid' => 1 }, { bind => [ 1999 ] });
99 is ( $rs->count, 1, '...cookbook (in separate file) + search condition' );
100
101 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })
102 ->search({ 'artistid' => 1 });
103 is ( $rs->count, 1, '...cookbook (bind first, in separate file) + chained search' );
dbf95416 104
b8b55c8e 105 $rs = $schema->resultset('CustomSql')->search({}, { bind => [ 1999 ] })->search({}, { where => \"title LIKE ?", bind => [ 'Spoon%' ] });
106 is_same_sql_bind(
107 $rs->as_query,
0491b597 108 "(SELECT me.artistid, me.name, me.rank, me.charfield FROM (SELECT a.*, cd.cdid AS cdid, cd.title AS title, cd.year AS year FROM artist a JOIN cd ON cd.artist = a.artistid WHERE cd.year = ?) me WHERE title LIKE ?)",
b8b55c8e 109 [
110 [ '!!dummy' => '1999' ],
111 [ '!!dummy' => 'Spoon%' ]
112 ],
113 'got correct SQL (cookbook arbitrary SQL, in separate file)'
114 );
07a287ce 115}
116
117TODO: {
1c133e22 118 local $TODO = 'bind args order needs fixing (semifor)';
119 $rs = $schema->resultset('Complex')->search({}, { bind => [ 1999 ] })
120 ->search({ 'artistid' => 1 }, {
121 where => \'title like ?',
122 bind => [ 'Spoon%' ] });
123 is ( $rs->count, 1, '...cookbook + chained search with extra bind' );
124}