added TODO test: call accessors when create()ing a row
Moritz Onken [Sun, 9 Aug 2009 17:10:56 +0000 (17:10 +0000)]
t/create/set_column.t [new file with mode: 0644]
t/lib/DBICTest/Schema/Track.pm
t/lib/sqlite.sql
t/prefetch/double_prefetch.t
t/prefetch/grouped.t

diff --git a/t/create/set_column.t b/t/create/set_column.t
new file mode 100644 (file)
index 0000000..2cc462b
--- /dev/null
@@ -0,0 +1,47 @@
+use warnings;
+use strict;
+
+use Test::More;
+
+use lib qw(t/lib);
+use DBICTest;
+
+my $schema = DBICTest->init_schema();
+
+TODO: {
+    local $TODO = 'call accessors when calling create() or update()';
+
+    my $row =
+      $schema->resultset('Track')->new_result( { title => 'foo', cd => 1 } );
+    $row->increment(1);
+    $row->insert;
+    is( $row->increment, 2 );
+
+    $row =
+      $schema->resultset('Track')
+      ->create( { title => 'bar', cd => 1, increment => 1 } );
+    is( $row->increment, 2 );
+
+    # $row isa DBICTest::Schema::Track
+    $row->get_from_storage;
+    is( $row->increment, 2 );
+
+    $row->update( { increment => 3 } );
+    $row->get_from_storage;
+    is( $row->increment, 4 );
+
+    $row->increment(3);
+    $row->get_from_storage;
+    is( $row->increment, 4 );
+
+    eval {
+        $row =
+          $schema->resultset('Track')
+          ->create( { title => 'bar', cd => 2, set_increment => 1 } );
+    };
+    ok( !$@, 'lives ok' );
+    is( $row->increment, 1 );
+
+}
+
+done_testing;
index a6de595..948597d 100644 (file)
@@ -34,6 +34,11 @@ __PACKAGE__->add_columns(
     data_type => 'smalldatetime',
     is_nullable => 1
   },
+  increment => {
+      data_type => 'integer',
+      is_nullable => 1,
+      accessor => '_increment',
+  }
 );
 __PACKAGE__->set_primary_key('trackid');
 
@@ -50,4 +55,20 @@ __PACKAGE__->belongs_to( disc => 'DBICTest::Schema::CD' => 'cd');
 __PACKAGE__->might_have( cd_single => 'DBICTest::Schema::CD', 'single_track' );
 __PACKAGE__->might_have( lyrics => 'DBICTest::Schema::Lyrics', 'track_id' );
 
+sub increment {
+    my $self = shift;
+    if(@_) {
+        return $self->_increment($_[0] + 1);
+    }
+    return $self->_increment();
+}
+
+sub set_increment {
+    my $self = shift;
+    if(@_) {
+        return $self->_increment($_[0]);
+    }
+    return $self->_increment();
+}
+
 1;
index 20b8e5a..ce8be83 100644 (file)
@@ -285,7 +285,8 @@ CREATE TABLE track (
   title varchar(100) NOT NULL,
   last_updated_on datetime,
   last_updated_at datetime,
-  small_dt smalldatetime
+  small_dt smalldatetime,
+  increment integer
 );
 
 CREATE INDEX track_idx_cd ON track (cd);
index 6142098..f518f73 100644 (file)
@@ -23,8 +23,8 @@ is_same_sql(
   '(
     SELECT
       cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track,
-      single_track.trackid, single_track.cd, single_track.position, single_track.title, single_track.last_updated_on, single_track.last_updated_at, single_track.small_dt,
-      single_track_2.trackid, single_track_2.cd, single_track_2.position, single_track_2.title, single_track_2.last_updated_on, single_track_2.last_updated_at, single_track_2.small_dt,
+      single_track.trackid, single_track.cd, single_track.position, single_track.title, single_track.last_updated_on, single_track.last_updated_at, single_track.small_dt, single_track.increment,
+      single_track_2.trackid, single_track_2.cd, single_track_2.position, single_track_2.title, single_track_2.last_updated_on, single_track_2.last_updated_at, single_track_2.small_dt, single_track_2.increment,
       cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track
     FROM artist me
       LEFT JOIN cd cds ON cds.artist = me.artistid
index 92f383c..bd50a9d 100644 (file)
@@ -163,7 +163,7 @@ for ($cd_rs->all) {
     $most_tracks_rs->as_query,
     '(
       SELECT  me.cdid, me.track_count,
-              tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, tracks.small_dt,
+              tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, tracks.small_dt, tracks.increment,
               liner_notes.liner_id, liner_notes.notes
         FROM (
           SELECT me.cdid, COUNT( tracks.trackid ) AS track_count