Created
December 7, 2009 23:21
-
-
Save dhoss/251224 to your computer and use it in GitHub Desktop.
This file contains 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
## run as such: | |
## squishface:DBIx-Class-Tree-Recursive dhoss$ perl -Ilib t/basic.t | |
## gives me this: | |
## DBIx::Class::ResultSet::create(): DBI Exception: DBD::SQLite::st execute failed: nested.path may not ## be NULL [for Statement "INSERT INTO nested ( name) VALUES ( ? )"] at t/basic.t line 24 | |
use strict; | |
use warnings; | |
use Test::More; | |
use lib "t/lib"; | |
use Schema; | |
my $schema = Schema->connect ('dbi:SQLite::memory:'); | |
$schema->deploy; | |
my $rs = $schema->resultset ('Test'); | |
my $chief = $rs->create ({ | |
name => 'grand chief', | |
children => [ | |
{ | |
name => 'subordinate 1', | |
children => [ | |
map { { name => 'rookie 1.' . $_ } } qw/1 2 3/, | |
], | |
}, | |
{ | |
name => 'subordinate 2', | |
children => [ | |
map { { name => 'rookie 2.' . $_ } } qw/1 2/, | |
], | |
}, | |
] | |
}); | |
dump_rs ($rs, 'initial state'); | |
$rs->find ({name => 'rookie 1.2'})->update ({ parent_id => $chief->id }); | |
dump_rs ($rs, 'promote a rookie to a subordinate'); | |
$rs->find ({name => 'rookie 1.2'})->move_to(1); | |
dump_rs ($rs, 'make this same rookie 1st subordinate'); | |
$rs->find ({name => 'rookie 1.2'})->move_to_group(undef, 1); | |
dump_rs ($rs, 'damn he is good - promote him to FIRST chief (this time use move_to_group)'); | |
$rs->find ({name => 'rookie 1.2'})->move_to(2); | |
dump_rs ($rs, 'not that good - make 2nd chief'); | |
my $sub2id = $rs->find ({name => 'subordinate 2'})->id; | |
$rs->find ({name => 'rookie 1.2'})->move_to_group($sub2id); | |
dump_rs ($rs, 'This guy is retarded, demote to last subordinate of 2nd subordinate'); | |
print "How many parents does the idiot have: " . $rs->find ({name => 'rookie 1.2'})->all_parents->count; | |
print "\n"; | |
print "How many children does the chief have besides the retard: " . $chief->all_children | |
->search({ name => { '!=', 'rookie 1.2' }})->count; | |
print "\n"; | |
sub dump_rs { | |
my ($rs, $desc) = @_; | |
print "-------\n$desc\n-------\n"; | |
print join ("\t", qw/id unit_name parent path/, "\n"); | |
for ($rs->cursor->all) { | |
print join ("\t", map { defined $_ ? $_ : 'NULL' } @$_, "\n"); | |
} | |
print "\n\n"; | |
} | |
done_testing; |
This file contains 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; | |
use base qw/DBIx::Class::Schema/; | |
__PACKAGE__->load_namespaces; | |
1; |
This file contains 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::Result::Test; | |
use base qw/DBIx::Class/; | |
__PACKAGE__->load_components(qw/Core Tree::Ordered::MatPath/); | |
__PACKAGE__->table ('nested'); | |
__PACKAGE__->add_columns ( | |
id => { data_type => 'int', is_auto_increment => 1 }, | |
name => { data_type => 'varchar' }, | |
parent_id => { data_type => 'int', is_nullable => 1 }, | |
path => { data_type => 'varchar' }, | |
); | |
__PACKAGE__->set_primary_key ('id'); | |
__PACKAGE__->has_many ('children', __PACKAGE__, 'parent_id'); | |
__PACKAGE__->belongs_to ('parent', __PACKAGE__, 'parent_id'); | |
__PACKAGE__->position_column ('path'); | |
__PACKAGE__->grouping_column ('parent_id'); | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment