[Jifty-commit] jifty-dbi branch, master, updated. 0.60-25-g37feeb8

Jifty commits jifty-commit at lists.jifty.org
Wed Jul 14 14:10:10 EDT 2010


The branch, master has been updated
       via  37feeb8ca6ab925c8a4abbf459be15fd0190e238 (commit)
       via  a7dc56bb450db957ccea205af7baff1f4c87d8f0 (commit)
      from  2369f312f8b1f97c5b7209baebe88a829c26e1b9 (commit)

Summary of changes:
 lib/Jifty/DBI/Filter/base64.pm           |   10 +++-
 t/02searches_distinct_values.t           |    2 +-
 t/{06filter_yaml.t => 06filter_base64.t} |   72 ++++++++++++++++--------------
 3 files changed, 47 insertions(+), 37 deletions(-)
 copy t/{06filter_yaml.t => 06filter_base64.t} (50%)

- Log -----------------------------------------------------------------
commit a7dc56bb450db957ccea205af7baff1f4c87d8f0
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Wed Jul 14 14:11:13 2010 -0400

    Ensure encode_base64 doesn't choke on utf8

diff --git a/lib/Jifty/DBI/Filter/base64.pm b/lib/Jifty/DBI/Filter/base64.pm
index 5351fab..7783456 100644
--- a/lib/Jifty/DBI/Filter/base64.pm
+++ b/lib/Jifty/DBI/Filter/base64.pm
@@ -4,6 +4,7 @@ use warnings;
 use strict;
 
 use base qw|Jifty::DBI::Filter|;
+use Encode qw(encode_utf8 is_utf8);
 use MIME::Base64 ();
 
 =head1 NAME
@@ -17,8 +18,9 @@ This filter allow you to store arbitrary data in a column of type
 
 =head2 encode
 
-If value is defined, then encodes it using
-L<MIME::Base64/encode_base64>. Does nothing if value is not defined.
+If value is defined, then encodes it using L<MIME::Base64/encode_base64> after
+passing it through L<Encode/encode_utf8>.  Does nothing if value is not
+defined.
 
 =cut
 
@@ -28,7 +30,9 @@ sub encode {
     my $value_ref = $self->value_ref;
     return unless defined $$value_ref;
 
-    $$value_ref = MIME::Base64::encode_base64($$value_ref);
+    $$value_ref = MIME::Base64::encode_base64(
+        is_utf8($$value_ref) ? encode_utf8($$value_ref) : $$value_ref
+    );
 
     return 1;
 }
diff --git a/t/06filter_base64.t b/t/06filter_base64.t
new file mode 100644
index 0000000..93590f4
--- /dev/null
+++ b/t/06filter_base64.t
@@ -0,0 +1,120 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use Encode qw(decode_utf8 is_utf8);
+
+use Test::More;
+BEGIN { require "t/utils.pl" }
+our (@available_drivers);
+
+use constant TESTS_PER_DRIVER => 20;
+
+my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
+plan tests => $total;
+
+my $normal_data = "Hi there";
+my $perl_data   = "Hi there—";
+my $utf8_data   = decode_utf8($perl_data);
+
+foreach my $d (@available_drivers) {
+SKIP: {
+    unless (has_schema('TestApp::User', $d)) {
+        skip "No schema for '$d' driver", TESTS_PER_DRIVER;
+    }
+
+    unless (should_test($d)) {
+        skip "ENV is not defined for driver '$d'", TESTS_PER_DRIVER;
+    }
+
+    diag("start testing with '$d' handle") if $ENV{TEST_VERBOSE};
+
+    my $handle = get_handle($d);
+    connect_handle($handle);
+    isa_ok($handle->dbh, 'DBI::db');
+
+    {
+        my $ret = init_schema('TestApp::User', $handle);
+        isa_ok($ret, 'DBI::st', 'init schema');
+    }
+
+    # data ref, is_utf8 expected, base64 expected, handle
+    store_data( \$normal_data,  0, "SGkgdGhlcmU=\n",      $handle );
+    store_data( \$perl_data,    0, "SGkgdGhlcmXigJQ=\n",  $handle );
+    store_data( \$utf8_data,    1, "SGkgdGhlcmXigJQ=\n",  $handle );
+
+    cleanup_schema('TestApp', $handle);
+    disconnect_handle($handle);
+}
+}
+
+sub store_data {
+    my $data = shift;
+    my $isutf8 = shift;
+    my $expected = shift;
+    my $handle = shift;
+
+    my $utf8 = is_utf8($$data) ? 1 : 0;
+
+    ok $utf8 == $isutf8, "is_utf8 = $utf8 as expected";
+    
+    my $rec = TestApp::User->new( handle => $handle );
+    isa_ok($rec, 'Jifty::DBI::Record');
+
+    my $id;
+   
+    eval { $id = $rec->create( content => $$data ); };
+    ok($id, 'created record');
+    ok($rec->load($id), 'loaded record');
+    is($rec->id, $id, 'record id matches');
+    is($rec->__raw_value('content'), $expected, "got expected base64");
+}
+
+package TestApp::User;
+use base qw/ Jifty::DBI::Record /;
+
+1;
+
+sub schema_sqlite {
+
+<<EOF;
+CREATE table users (
+    id integer primary key,
+    content text
+)
+EOF
+
+}
+
+sub schema_mysql {
+
+<<EOF;
+CREATE TEMPORARY table users (
+    id integer auto_increment primary key,
+    content text
+)
+EOF
+
+}
+
+sub schema_pg {
+
+<<EOF;
+CREATE TEMPORARY table users (
+    id serial primary key,
+    content text
+)
+EOF
+
+}
+
+BEGIN {
+    use Jifty::DBI::Schema;
+
+    use Jifty::DBI::Record schema {
+    column content =>
+        type is 'text',
+        filters are qw/ Jifty::DBI::Filter::base64 /;
+    }
+}
+

commit 37feeb8ca6ab925c8a4abbf459be15fd0190e238
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Wed Jul 14 14:11:37 2010 -0400

    Fix test plan

diff --git a/t/02searches_distinct_values.t b/t/02searches_distinct_values.t
index 559badd..a961f28 100644
--- a/t/02searches_distinct_values.t
+++ b/t/02searches_distinct_values.t
@@ -9,7 +9,7 @@ use Test::More;
 BEGIN { require "t/utils.pl" }
 our (@available_drivers);
 
-use constant TESTS_PER_DRIVER => 47;
+use constant TESTS_PER_DRIVER => 10;
 
 my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
 plan tests => $total;

-----------------------------------------------------------------------


More information about the Jifty-commit mailing list