add test for populate with literal sql mixed with binds, improve error messages
[dbsrgits/DBIx-Class.git] / t / 100populate.t
index aa3880e..9e7bc88 100644 (file)
@@ -114,9 +114,11 @@ is($link7->id, 7, 'Link 7 id');
 is($link7->url, undef, 'Link 7 url');
 is($link7->title, 'gtitle', 'Link 7 title');
 
-# test _execute_array_empty (insert_bulk with all literal sql)
 my $rs = $schema->resultset('Artist');
 $rs->delete;
+
+# test _execute_array_empty (insert_bulk with all literal sql)
+
 $rs->populate([
     (+{
         name => \"'DT'",
@@ -133,6 +135,26 @@ is((grep {
 
 $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([
         {
@@ -152,4 +174,48 @@ throws_ok {
 
 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';
+
 done_testing;