[Jifty-commit] r2614 - in Jifty-DBI/branches/od: . t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Fri Jan 26 19:35:04 EST 2007


Author: clkao
Date: Fri Jan 26 19:35:04 2007
New Revision: 2614

Added:
   Jifty-DBI/branches/od/t/15types.t
Modified:
   Jifty-DBI/branches/od/MANIFEST
   Jifty-DBI/branches/od/Makefile.PL
   Jifty-DBI/branches/od/lib/Jifty/DBI/Schema.pm

Log:
First draft of bundled type support.

Modified: Jifty-DBI/branches/od/MANIFEST
==============================================================================
--- Jifty-DBI/branches/od/MANIFEST	(original)
+++ Jifty-DBI/branches/od/MANIFEST	Fri Jan 26 19:35:04 2007
@@ -74,6 +74,7 @@
 t/12prefetch.t
 t/13collection.t
 t/14handle-pg.t
+t/15types.t
 t/pod.t
 t/testmodels.pl
 t/utils.pl

Modified: Jifty-DBI/branches/od/Makefile.PL
==============================================================================
--- Jifty-DBI/branches/od/Makefile.PL	(original)
+++ Jifty-DBI/branches/od/Makefile.PL	Fri Jan 26 19:35:04 2007
@@ -17,7 +17,7 @@
 requires('Encode'  => 2.10);
 requires('Exporter::Lite');
 requires('Lingua::EN::Inflect');
-requires('Object::Declare' => 0.21);
+requires('Object::Declare' => 0.22);
 requires('UNIVERSAL::require');
 build_requires('Test::More' => 0.52);
 build_requires('DBD::SQLite');

Modified: Jifty-DBI/branches/od/lib/Jifty/DBI/Schema.pm
==============================================================================
--- Jifty-DBI/branches/od/lib/Jifty/DBI/Schema.pm	(original)
+++ Jifty-DBI/branches/od/lib/Jifty/DBI/Schema.pm	Fri Jan 26 19:35:04 2007
@@ -42,6 +42,9 @@
 
 use Carp qw/croak carp/;
 use Scalar::Defer;
+use base qw(Class::Data::Inheritable);
+__PACKAGE__->mk_classdata('TYPES' => {});
+
 use Object::Declare (
     mapping => {
         column => sub { Jifty::DBI::Column->new({@_}) } ,
@@ -55,7 +58,14 @@
         filters     => 'input_filters',
     },
     copula  => {
-        is          => '',
+        is          => sub { return @_ if $#_;
+                             my $typehandler = __PACKAGE__->TYPES->{$_[0]};
+                             # XXX: when we have a type name
+                             # convention, give a warning when it
+                             # looks like a type name but not found
+                             return ($_[0] => 1) unless $typehandler;
+                             return $typehandler->();
+			 },
         are         => '',
         as          => '',
         ajax        => 'ajax_',
@@ -309,6 +319,13 @@
     # (We may not *have* a caller(1) if the user is executing a .pm file.)
 }
 
+sub register_types {
+    my $class = shift;
+    while (my ($type, $sub) = splice(@_, 0, 2)) {
+        $class->TYPES->{$type} = $sub;
+    }
+}
+
 1;
 
 __END__

Added: Jifty-DBI/branches/od/t/15types.t
==============================================================================
--- (empty file)
+++ Jifty-DBI/branches/od/t/15types.t	Fri Jan 26 19:35:04 2007
@@ -0,0 +1,133 @@
+#!/usr/bin/env perl -w
+
+use strict;
+
+use Test::More;
+BEGIN { require "t/utils.pl" }
+our (@available_drivers);
+
+use constant TESTS_PER_DRIVER => 16;
+
+my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
+plan tests => $total;
+
+use DateTime ();
+
+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', "Inserted the schema. got a statement handle back" );}
+
+        my $rec = TestApp::User->new( handle => $handle );
+        isa_ok($rec, 'Jifty::DBI::Record');
+
+        my $now = time;
+        my $today = DateTime->from_epoch( epoch => $now )->truncate( to => 'day' )->epoch;
+        my $min_of_day = DateTime->from_epoch( epoch => $now )->truncate( to => 'minute' );
+        my $dt = DateTime->from_epoch( epoch => $now );
+        my ($id) = $rec->create( created => $dt, event_on => $dt, event_stops => $min_of_day );
+        ok($id, "Successfuly created ticket");
+        ok($rec->load($id), "Loaded the record");
+        is($rec->id, $id, "The record has its id");
+        isa_ok($rec->created, 'DateTime' );
+        is( $rec->created->epoch, $now, "Correct value");
+        isa_ok($rec->event_on, 'DateTime' );
+        is( $rec->event_on->epoch, $today, "Correct value");
+        isa_ok($rec->event_stops, 'DateTime' );
+        is( $rec->event_stops->minute, $min_of_day->minute, "Correct value");
+        is( $rec->event_stops->hour, $min_of_day->hour, "Correct value");
+
+        # undef/NULL
+        $rec->set_created;
+        is($rec->created, undef, "Set undef value" );
+
+        # from string
+        require POSIX;
+        $rec->set_created( POSIX::strftime( "%Y-%m-%d %H:%M:%S", gmtime($now) ) );
+        isa_ok($rec->created, 'DateTime' );
+        is( $rec->created->epoch, $now, "Correct value");
+
+        cleanup_schema( 'TestApp', $handle );
+        disconnect_handle( $handle );
+}
+}
+
+package TestApp::User;
+use base qw/Jifty::DBI::Record/;
+
+1;
+
+sub schema_sqlite {
+
+<<EOF;
+CREATE table users (
+        id integer primary key,
+        created datetime,
+        event_on date,
+        event_stops time
+)
+EOF
+
+}
+
+sub schema_mysql {
+
+<<EOF;
+CREATE TEMPORARY table users (
+        id integer auto_increment primary key,
+        created datetime,
+        event_on date,
+        event_stops time
+)
+EOF
+
+}
+
+sub schema_pg {
+
+<<EOF;
+CREATE TEMPORARY table users (
+        id serial primary key,
+        created timestamp,
+        event_on date,
+        event_stops time
+)
+EOF
+
+}
+
+BEGIN {
+use Jifty::DBI::Schema;
+Jifty::DBI::Schema->register_types(
+    Date =>
+        sub { type is 'date', input_filters are qw/Jifty::DBI::Filter::Date/ },
+    Time =>
+        sub { type is 'time', input_filters are qw/Jifty::DBI::Filter::Time/ },
+    DateTime => sub {
+        type is 'datetime',
+        input_filters are qw/Jifty::DBI::Filter::DateTime/
+    }
+);
+}
+
+use Jifty::DBI::Record schema {
+    column created     => is DateTime;
+    column event_on    => is Date;
+    column event_stops => is Time;
+};
+
+
+1;
+


More information about the Jifty-commit mailing list