Created
June 1, 2009 10:10
-
-
Save hidek/121335 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| package Schema::Base; | |
| use base qw/DBIx::Class/; | |
| __PACKAGE__->mk_classdata('_populate_data'); | |
| sub populate_data { | |
| my $self = shift; | |
| if (@_) { | |
| $self->_populate_data(@_); | |
| } else { | |
| $self->_populate_data; | |
| } | |
| } | |
| } | |
| { | |
| package Schema::Album; | |
| use base qw/Schema::Base/; | |
| __PACKAGE__->load_components(qw/Core/); | |
| __PACKAGE__->table('album'); | |
| __PACKAGE__->add_columns( | |
| id => { | |
| data_type => 'INTEGER', | |
| size => 11, | |
| is_nullable => 0, | |
| is_auto_increment => 1, | |
| }, | |
| title => { | |
| data_type => 'VARCHAR', | |
| size => '255', | |
| is_nullable => 0, | |
| }, | |
| ); | |
| __PACKAGE__->set_primary_key('id'); | |
| __PACKAGE__->has_many( | |
| tracks => 'Schema::Track', | |
| 'album_id', {order_by => 'position'} | |
| ); | |
| __PACKAGE__->populate_data( | |
| [['title'], ['album_1'], ['album_2'], ['album_3'],]); | |
| } | |
| { | |
| package Schema::Track; | |
| use base qw/Schema::Base/; | |
| __PACKAGE__->load_components(qw/Core/); | |
| __PACKAGE__->table('track'); | |
| __PACKAGE__->add_columns( | |
| id => { | |
| data_type => 'INTEGER', | |
| size => 11, | |
| is_nullable => 0, | |
| is_auto_increment => 1, | |
| }, | |
| album_id => { | |
| data_type => 'INTEGER', | |
| size => 11, | |
| is_nullable => 0, | |
| }, | |
| position => { | |
| data_type => 'TINYINT', | |
| size => 1, | |
| is_nullable => 0, | |
| }, | |
| title => { | |
| data_type => 'VARCHAR', | |
| size => '255', | |
| is_nullable => 0, | |
| }, | |
| ); | |
| __PACKAGE__->set_primary_key('id'); | |
| __PACKAGE__->add_unique_constraint( | |
| track_uq_album_id_position => [qw/album_id position/]); | |
| __PACKAGE__->belongs_to(album => 'Schema::Album', 'album_id'); | |
| __PACKAGE__->populate_data( | |
| [ | |
| [qw/album_id position title/], | |
| [1, 1, 'album_1_1'], | |
| [1, 2, 'album_1_2'], | |
| [1, 3, 'album_1_3'], | |
| [1, 4, 'album_1_4'], | |
| [1, 5, 'album_1_5'], | |
| ] | |
| ); | |
| } | |
| { | |
| package Schema; | |
| use base qw/DBIx::Class::Schema/; | |
| __PACKAGE__->load_classes(qw/Album Track/); | |
| } | |
| { | |
| package Schema::Test; | |
| sub init_schema { | |
| my $self = shift; | |
| my %args = @_; | |
| my $schema = Schema->connect('dbi:SQLite:dbname=:memory:'); | |
| $schema->create_ddl_dir('SQLite', undef, File::Spec->catdir('db')); | |
| $schema->deploy; | |
| for my $src ($schema->sources) { | |
| my $class = $schema->class($src); | |
| $schema->populate($class => $class->populate_data); | |
| } | |
| return $schema; | |
| } | |
| } | |
| { | |
| use strict; | |
| use Test::More qw(no_plan); | |
| my $schema = Schema::Test->init_schema; | |
| my $album = $schema->resultset('Album')->find({id => 1}); | |
| ok ($album->title eq 'album_1', 'album title'); | |
| my $i = 1; | |
| for my $track ($album->tracks) { | |
| ok ($track->title eq "album_1_$i") ; | |
| $i++; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment