Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
359 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use strict; | ||
use warnings; | ||
use t::Util; | ||
use Test::More; | ||
|
||
my $test = mk_test 'insert_multi'; | ||
|
||
$test->( | ||
desc => 'basic', | ||
input => [ | ||
foo => [qw/bar baz/], | ||
[ [qw/hoge fuga/], [qw/fizz buzz/] ], | ||
], | ||
expects => { | ||
stmt => 'INSERT INTO `foo` (`bar`, `baz`) VALUES (?, ?), (?, ?)', | ||
bind => [qw/hoge fuga fizz buzz/], | ||
}, | ||
); | ||
|
||
$test->( | ||
desc => 'mismatch params', | ||
input => [ | ||
foo => [qw/bar baz/], | ||
[ [qw/hoge fuga/], [qw/fizz buzz fizzbuzz/], [qw//] ], | ||
], | ||
expects => { | ||
stmt => 'INSERT INTO `foo` (`bar`, `baz`) VALUES (?, ?), (?, ?), (?, ?)', | ||
bind => [qw/hoge fuga fizz buzz/, undef, undef], | ||
}, | ||
); | ||
|
||
$test->( | ||
desc => 'complex', | ||
input => [ | ||
foo => [qw/bar baz/], | ||
[ ['hoge', \'NOW()'], ['fuga', \['UNIX_TIMESTAMP(?)', '2012-12-12'] ] ], | ||
], | ||
expects => { | ||
stmt => 'INSERT INTO `foo` (`bar`, `baz`) VALUES (?, NOW()), (?, UNIX_TIMESTAMP(?))', | ||
bind => [qw/hoge fuga 2012-12-12/], | ||
}, | ||
); | ||
|
||
$test->( | ||
desc => 'insert ignore', | ||
input => [ | ||
foo => [qw/bar baz/], | ||
[ [qw/hoge fuga/], [qw/fizz buzz/] ], | ||
{ prefix => 'INSERT IGNORE INTO' }, | ||
], | ||
expects => { | ||
stmt => 'INSERT IGNORE INTO `foo` (`bar`, `baz`) VALUES (?, ?), (?, ?)', | ||
bind => [qw/hoge fuga fizz buzz/], | ||
}, | ||
); | ||
|
||
$test->( | ||
desc => 'on duplicate key update', | ||
input => [ | ||
foo => [qw/bar baz/], | ||
[ [qw/hoge fuga/], [qw/fizz buzz/] ], | ||
{ update => { bar => 'piyo' } }, | ||
], | ||
expects => { | ||
stmt => 'INSERT INTO `foo` (`bar`, `baz`) VALUES (?, ?), (?, ?) ON DUPLICATE KEY UPDATE `bar` = ?', | ||
bind => [qw/hoge fuga fizz buzz piyo/], | ||
}, | ||
); | ||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use strict; | ||
use warnings; | ||
use t::Util; | ||
use Test::More; | ||
|
||
my $test = mk_test 'insert_multi_from_hash'; | ||
|
||
$test->( | ||
desc => 'basic', | ||
input => [ | ||
foo => [ | ||
{ bar => 'hoge', baz => 'fuga' }, | ||
{ bar => 'fizz', baz => 'buzz' }, | ||
], | ||
], | ||
expects => { | ||
stmt => 'INSERT INTO `foo` (`bar`, `baz`) VALUES (?, ?), (?, ?)', | ||
bind => [qw/hoge fuga fizz buzz/], | ||
}, | ||
); | ||
|
||
$test->( | ||
desc => 'mismatch params', | ||
input => [ | ||
foo => [ | ||
{ bar => 'hoge', baz => 'fuga' }, | ||
{ bar => 'fizz', baz => 'buzz', xxx => 'yyy' }, | ||
{ }, | ||
], | ||
], | ||
expects => { | ||
stmt => 'INSERT INTO `foo` (`bar`, `baz`) VALUES (?, ?), (?, ?), (?, ?)', | ||
bind => [qw/hoge fuga fizz buzz/, undef, undef], | ||
}, | ||
); | ||
|
||
$test->( | ||
desc => 'complex', | ||
input => [ | ||
foo => [ | ||
{ bar => 'hoge', baz => \'NOW()' }, | ||
{ bar => 'fuga', baz => \['UNIX_TIMESTAMP(?)', '2012-12-12'] }, | ||
], | ||
], | ||
expects => { | ||
stmt => 'INSERT INTO `foo` (`bar`, `baz`) VALUES (?, NOW()), (?, UNIX_TIMESTAMP(?))', | ||
bind => [qw/hoge fuga 2012-12-12/], | ||
}, | ||
); | ||
|
||
$test->( | ||
desc => 'insert ignore', | ||
input => [ | ||
foo => [ | ||
{ bar => 'hoge', baz => 'fuga' }, | ||
{ bar => 'fizz', baz => 'buzz' }, | ||
], | ||
{ prefix => 'INSERT IGNORE INTO' }, | ||
], | ||
expects => { | ||
stmt => 'INSERT IGNORE INTO `foo` (`bar`, `baz`) VALUES (?, ?), (?, ?)', | ||
bind => [qw/hoge fuga fizz buzz/], | ||
}, | ||
); | ||
|
||
$test->( | ||
desc => 'on duplicate key update', | ||
input => [ | ||
foo => [ | ||
{ bar => 'hoge', baz => 'fuga' }, | ||
{ bar => 'fizz', baz => 'buzz' }, | ||
], | ||
{ update => { bar => 'piyo' } }, | ||
], | ||
expects => { | ||
stmt => 'INSERT INTO `foo` (`bar`, `baz`) VALUES (?, ?), (?, ?) ON DUPLICATE KEY UPDATE `bar` = ?', | ||
bind => [qw/hoge fuga fizz buzz piyo/], | ||
}, | ||
); | ||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use strict; | ||
use warnings; | ||
use Test::More; | ||
|
||
use SQL::Format; | ||
|
||
my $f = SQL::Format->new; | ||
subtest 'insert_multi no args' => sub { | ||
eval { $f->insert_multi }; | ||
like $@, qr/Usage: \$sqlf->insert_multi\(/; | ||
}; | ||
|
||
subtest 'insert_multi cols is not array' => sub { | ||
eval { $f->insert_multi(foo => {}) }; | ||
like $@, qr/Usage: \$sqlf->insert_multi\(/; | ||
}; | ||
|
||
subtest 'insert_multi values is not array' => sub { | ||
eval { $f->insert_multi(foo => [qw/bar baz/], {}) }; | ||
like $@, qr/Usage: \$sqlf->insert_multi\(/; | ||
}; | ||
|
||
subtest 'insert_multi_from_hash no args' => sub { | ||
eval { $f->insert_multi_from_hash }; | ||
like $@, qr/Usage: \$sqlf->insert_multi_from_hash\(/; | ||
}; | ||
|
||
subtest 'insert_multi_from_hash values is not array' => sub { | ||
eval { $f->insert_multi_from_hash(foo => {}) }; | ||
like $@, qr/Usage: \$sqlf->insert_multi_from_hash\(/; | ||
}; | ||
|
||
subtest 'insert_multi_from_hash values is not array in hash' => sub { | ||
eval { $f->insert_multi_from_hash(foo => [[]]) }; | ||
like $@, qr/Usage: \$sqlf->insert_multi_from_hash\(/; | ||
}; | ||
|
||
done_testing; |