revert previous revision
[dbsrgits/DBIx-Class.git] / t / bindtype_columns.t
index 2100cca..1462d9b 100644 (file)
@@ -5,37 +5,84 @@ use Test::More;
 use lib qw(t/lib);
 use DBICTest;
 
-my $schema = DBICTest->init_schema();
-
-plan tests => 2;
-
-# figure out if we've got a version of sqlite that is older than 3.2.6, in
-# which case COUNT(DISTINCT()) doesn't work
-my $is_broken_sqlite = 0;
-my ($sqlite_major_ver,$sqlite_minor_ver,$sqlite_patch_ver) =
-    split /\./, $schema->storage->dbh->get_info(18);
-if( $schema->storage->dbh->get_info(17) eq 'SQLite' &&
-    ( ($sqlite_major_ver < 3) ||
-      ($sqlite_major_ver == 3 && $sqlite_minor_ver < 2) ||
-      ($sqlite_major_ver == 3 && $sqlite_minor_ver == 2 && $sqlite_patch_ver < 6) ) ) {
-    $is_broken_sqlite = 1;
+my ($dsn, $dbuser, $dbpass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
+
+plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
+  unless ($dsn && $dbuser);
+  
+plan tests => 6;
+
+my $schema = DBICTest::Schema->connection($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
+
+my $dbh = $schema->storage->dbh;
+
+{
+    local $SIG{__WARN__} = sub {};
+    $dbh->do('DROP TABLE IF EXISTS bindtype_test');
+
+    # the blob/clob are for reference only, will be useful when we switch to SQLT and can test Oracle along the way
+    $dbh->do(qq[
+        CREATE TABLE bindtype_test 
+        (
+            id              serial       NOT NULL   PRIMARY KEY,
+            bytea           bytea        NULL,
+            blob            bytea        NULL,
+            clob            text         NULL
+        );
+    ],{ RaiseError => 1, PrintError => 1 });
 }
 
+my $big_long_string    = "\x00\x01\x02 abcd" x 125000;
 
-#Bindtest
+my $new;
+# test inserting a row
 {
-       my $new = $schema->resultset("Artist")->new({
-       
-               artistid=>25,
-               name=>'JohnNapiorkowski',
-       });
-       
-       $new->update_or_insert;
-       
-       my $resultset = $schema->resultset("Artist")->find({artistid=>25});
-       
-       is($resultset->id, 25, 'Testing New ID');
-       is($resultset->name, 'JohnNapiorkowski', 'Testing New Name');
+  $new = $schema->resultset('BindType')->create({ bytea => $big_long_string });
+
+  ok($new->id, "Created a bytea row");
+  is($new->bytea,      $big_long_string, "Set the blob correctly.");
 }
 
+# test retrieval of the bytea column
+{
+  my $row = $schema->resultset('BindType')->find({ id => $new->id });
+  is($row->get_column('bytea'), $big_long_string, "Created the blob correctly.");
+}
+
+TODO: {
+  local $TODO =
+    'Passing bind attributes to $sth->bind_param() should be implemented (it only works in $storage->insert ATM)';
+
+  my $rs = $schema->resultset('BindType')->search({ bytea => $big_long_string });
+
+  # search on the bytea column (select)
+  {
+    my $row = $rs->first;
+    is($row ? $row->id : undef, $new->id, "Found the row searching on the bytea column.");
+  }
+
+  # search on the bytea column (update)
+  {
+    my $new_big_long_string = $big_long_string . "2";
+    $schema->txn_do(sub {
+      $rs->update({ bytea => $new_big_long_string });
+      my $row = $schema->resultset('BindType')->find({ id => $new->id });
+      is($row ? $row->get_column('bytea') : undef, $new_big_long_string,
+        "Updated the row correctly (searching on the bytea column)."
+      );
+      $schema->txn_rollback;
+    });
+  }
+
+  # search on the bytea column (delete)
+  {
+    $schema->txn_do(sub {
+      $rs->delete;
+      my $row = $schema->resultset('BindType')->find({ id => $new->id });
+      is($row, undef, "Deleted the row correctly (searching on the bytea column).");
+      $schema->txn_rollback;
+    });
+  }
+}
 
+$dbh->do("DROP TABLE bindtype_test");