X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F100populate.t;h=e179931281e6933ebc686671684d5486b5b5cade;hb=257a1d3b8fb3d5509b132a9497bfe34662e8d29e;hp=51d3fcf3b031cd91294adc8eea269513e8688a91;hpb=d35a6fedb511b76dc5406f8ec5ced9daf84423cd;p=dbsrgits%2FDBIx-Class.git diff --git a/t/100populate.t b/t/100populate.t index 51d3fcf..e179931 100644 --- a/t/100populate.t +++ b/t/100populate.t @@ -5,8 +5,7 @@ use Test::More; use Test::Exception; use lib qw(t/lib); use DBICTest; - -plan tests => 23; +use Path::Class::File (); my $schema = DBICTest->init_schema(); @@ -39,7 +38,6 @@ throws_ok ( sub { $schema->populate('CD', [ map { { - cdid => $i++, # without a PK the bulk insert does not engage - how come? artist => $artist->id, title => $_, year => 2009, @@ -117,3 +115,205 @@ is($link7->id, 7, 'Link 7 id'); is($link7->url, undef, 'Link 7 url'); is($link7->title, 'gtitle', 'Link 7 title'); +my $rs = $schema->resultset('Artist'); +$rs->delete; + +# test _execute_array_empty (insert_bulk with all literal sql) + +$rs->populate([ + (+{ + name => \"'DT'", + rank => \500, + charfield => \"'mtfnpy'", + }) x 5 +]); + +is((grep { + $_->name eq 'DT' && + $_->rank == 500 && + $_->charfield eq 'mtfnpy' +} $rs->all), 5, 'populate with all literal SQL'); + +$rs->delete; + +# test mixed binds with literal sql + +$rs->populate([ + (+{ + name => \"'DT'", + rank => 500, + charfield => \"'mtfnpy'", + }) x 5 +]); + +is((grep { + $_->name eq 'DT' && + $_->rank == 500 && + $_->charfield eq 'mtfnpy' +} $rs->all), 5, 'populate with all literal SQL'); + +$rs->delete; + +### + +throws_ok { + $rs->populate([ + { + artistid => 1, + name => 'foo1', + }, + { + artistid => 'foo', # this dies + name => 'foo2', + }, + { + artistid => 3, + name => 'foo3', + }, + ]); +} qr/slice/, 'bad slice'; + +is($rs->count, 0, 'populate is atomic'); + +# Trying to use a column marked as a bind in the first slice with literal sql in +# a later slice should throw. + +throws_ok { + $rs->populate([ + { + artistid => 1, + name => \"'foo'", + }, + { + artistid => \2, + name => \"'foo'", + } + ]); +} qr/bind expected/, 'literal sql where bind expected throws'; + +# ... and vice-versa. + +throws_ok { + $rs->populate([ + { + artistid => \1, + name => \"'foo'", + }, + { + artistid => 2, + name => \"'foo'", + } + ]); +} qr/literal SQL expected/i, 'bind where literal sql expected throws'; + +throws_ok { + $rs->populate([ + { + artistid => 1, + name => \"'foo'", + }, + { + artistid => 2, + name => \"'bar'", + } + ]); +} qr/inconsistent/, 'literal sql must be the same in all slices'; + +# the stringification has nothing to do with the artist name +# this is solely for testing consistency +my $fn = Path::Class::File->new ('somedir/somefilename.tmp'); +my $fn2 = Path::Class::File->new ('somedir/someotherfilename.tmp'); + +lives_ok { + $rs->populate([ + { + name => 'supplied before stringifying object', + }, + { + name => $fn, + } + ]); +} 'stringifying objects pass through'; + +# ... and vice-versa. + +lives_ok { + $rs->populate([ + { + name => $fn2, + }, + { + name => 'supplied after stringifying object', + }, + ]); +} 'stringifying objects pass through'; + +for ( + $fn, + $fn2, + 'supplied after stringifying object', + 'supplied before stringifying object' +) { + my $row = $rs->find ({name => $_}); + ok ($row, "Stringification test row '$_' properly inserted"); +} + +$rs->delete; + +# test stringification with ->create rather than Storage::insert_bulk as well + +lives_ok { + my @dummy = $rs->populate([ + { + name => 'supplied before stringifying object', + }, + { + name => $fn, + } + ]); +} 'stringifying objects pass through'; + +# ... and vice-versa. + +lives_ok { + my @dummy = $rs->populate([ + { + name => $fn2, + }, + { + name => 'supplied after stringifying object', + }, + ]); +} 'stringifying objects pass through'; + +for ( + $fn, + $fn2, + 'supplied after stringifying object', + 'supplied before stringifying object' +) { + my $row = $rs->find ({name => $_}); + ok ($row, "Stringification test row '$_' properly inserted"); +} + +lives_ok { + $schema->resultset('TwoKeys')->populate([{ + artist => 1, + cd => 5, + fourkeys_to_twokeys => [{ + f_foo => 1, + f_bar => 1, + f_hello => 1, + f_goodbye => 1, + autopilot => 'a', + },{ + f_foo => 2, + f_bar => 2, + f_hello => 2, + f_goodbye => 2, + autopilot => 'b', + }] + }]) +} 'multicol-PK has_many populate works'; + +done_testing;