Additional tests for AdjacencyList.
Aran Deltac [Tue, 21 Mar 2006 20:55:19 +0000 (20:55 +0000)]
lib/DBIx/Class/Tree/AdjacencyList.pm
t/lib/DBICTest/Schema.pm
t/lib/DBICTest/Schema/Artist.pm
t/lib/DBICTest/Schema/Employee/AdjacencyList.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/Employee/Positioned.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/Employee/PositionedAdjacencyList.pm [new file with mode: 0644]
t/lib/sqlite.sql
t/run/04db.tl
t/run/26positioned.tl

index 4e9f28e..381b039 100644 (file)
@@ -91,9 +91,9 @@ sub parent {
     my $parent_column = $self->parent_column();
     if ($new_parent) {
         if (ref($new_parent)) {
-            $new_parent = $new_parent->id();
+            $new_parent = $new_parent->id() || 0;
         }
-        return 0 if ($new_parent == $self->get_column($parent_column));
+        return 0 if ($new_parent == ($self->get_column($parent_column)||0));
         my $positioned = $self->can('position_column');
         $self->move_last if ($positioned);
         $self->set_column( $parent_column => $new_parent );
@@ -165,7 +165,7 @@ sub _parent_clause {
     my( $self ) = @_;
     return (
         $self->parent_column() =>
-        $self->get_column($self->parent_column()) || 0
+        $self->get_column($self->parent_column())
     );
 }
 
index f2ee2d7..c0ef724 100644 (file)
@@ -7,6 +7,9 @@ no warnings qw/qw/;
 
 __PACKAGE__->load_classes(qw/
   Artist
+  Employee::Positioned
+  Employee::AdjacencyList
+  Employee::PositionedAdjacencyList
   CD
   #dummy
   Track
index 996f53c..f4c6706 100644 (file)
@@ -3,7 +3,7 @@ package # hide from PAUSE
 
 use base 'DBIx::Class::Core';
 
-__PACKAGE__->load_components('Positioned','PK::Auto');
+__PACKAGE__->load_components('PK::Auto');
 
 DBICTest::Schema::Artist->table('artist');
 DBICTest::Schema::Artist->add_columns(
@@ -16,17 +16,12 @@ DBICTest::Schema::Artist->add_columns(
     size      => 100,
     is_nullable => 1,
   },
-  position => {
-    data_type => 'integer',
-  },
 );
 DBICTest::Schema::Artist->set_primary_key('artistid');
-__PACKAGE__->position_column('position');
 
 __PACKAGE__->mk_classdata('field_name_for', {
     artistid    => 'primary key',
     name        => 'artist name',
-    position    => 'list position',
 });
 
 1;
diff --git a/t/lib/DBICTest/Schema/Employee/AdjacencyList.pm b/t/lib/DBICTest/Schema/Employee/AdjacencyList.pm
new file mode 100644 (file)
index 0000000..c0e4407
--- /dev/null
@@ -0,0 +1,39 @@
+package # hide from PAUSE 
+    DBICTest::Schema::Employee::AdjacencyList;
+
+use base 'DBIx::Class';
+
+__PACKAGE__->load_components(qw(
+    Tree::AdjacencyList
+    PK::Auto
+    Core
+));
+
+__PACKAGE__->table('employees_adjacencylist');
+
+__PACKAGE__->add_columns(
+    employee_id => {
+        data_type => 'integer',
+        is_auto_increment => 1
+    },
+    parent_id => {
+        data_type => 'integer',
+        is_nullable => 1,
+    },
+    name => {
+        data_type => 'varchar',
+        size      => 100,
+        is_nullable => 1,
+    },
+);
+
+__PACKAGE__->set_primary_key('employee_id');
+__PACKAGE__->parent_column('parent_id');
+
+__PACKAGE__->mk_classdata('field_name_for', {
+    employee_id => 'primary key',
+    parent_id   => 'parent id',
+    name        => 'employee name',
+});
+
+1;
diff --git a/t/lib/DBICTest/Schema/Employee/Positioned.pm b/t/lib/DBICTest/Schema/Employee/Positioned.pm
new file mode 100644 (file)
index 0000000..da7444e
--- /dev/null
@@ -0,0 +1,34 @@
+package # hide from PAUSE 
+    DBICTest::Schema::Employee::Positioned;
+
+use base 'DBIx::Class';
+
+__PACKAGE__->load_components(qw( Positioned PK::Auto Core ));
+
+__PACKAGE__->table('employees_positioned');
+
+__PACKAGE__->add_columns(
+    employee_id => {
+        data_type => 'integer',
+        is_auto_increment => 1
+    },
+    position => {
+        data_type => 'integer',
+    },
+    name => {
+        data_type => 'varchar',
+        size      => 100,
+        is_nullable => 1,
+    },
+);
+
+__PACKAGE__->set_primary_key('employee_id');
+__PACKAGE__->position_column('position');
+
+__PACKAGE__->mk_classdata('field_name_for', {
+    employee_id => 'primary key',
+    position    => 'list position',
+    name        => 'employee name',
+});
+
+1;
diff --git a/t/lib/DBICTest/Schema/Employee/PositionedAdjacencyList.pm b/t/lib/DBICTest/Schema/Employee/PositionedAdjacencyList.pm
new file mode 100644 (file)
index 0000000..86a2e58
--- /dev/null
@@ -0,0 +1,45 @@
+package # hide from PAUSE 
+    DBICTest::Schema::Employee::PositionedAdjacencyList;
+
+use base 'DBIx::Class';
+
+__PACKAGE__->load_components(qw(
+    Tree::AdjacencyList
+    Positioned
+    PK::Auto
+    Core
+));
+
+__PACKAGE__->table('employees_positioned_adjacencylist');
+
+__PACKAGE__->add_columns(
+    employee_id => {
+        data_type => 'integer',
+        is_auto_increment => 1
+    },
+    parent_id => {
+        data_type => 'integer',
+        is_nullable => 1,
+    },
+    position => {
+        data_type => 'integer',
+    },
+    name => {
+        data_type => 'varchar',
+        size      => 100,
+        is_nullable => 1,
+    },
+);
+
+__PACKAGE__->set_primary_key('employee_id');
+__PACKAGE__->position_column('position');
+__PACKAGE__->parent_column('parent_id');
+
+__PACKAGE__->mk_classdata('field_name_for', {
+    employee_id => 'primary key',
+    parent_id   => 'parent id',
+    position    => 'list position',
+    name        => 'employee name',
+});
+
+1;
index e7646c4..6cd3abe 100644 (file)
@@ -1,10 +1,19 @@
 -- 
 -- Created by SQL::Translator::Producer::SQLite
--- Created on Sun Mar 19 19:16:50 2006
+-- Created on Tue Mar 21 12:11:03 2006
 -- 
 BEGIN TRANSACTION;
 
 --
+-- Table: employees_positioned
+--
+CREATE TABLE employees_positioned (
+  employee_id INTEGER PRIMARY KEY NOT NULL,
+  position integer NOT NULL,
+  name varchar(100)
+);
+
+--
 -- Table: serialized
 --
 CREATE TABLE serialized (
@@ -13,12 +22,12 @@ CREATE TABLE serialized (
 );
 
 --
--- Table: twokeys
+-- Table: cd_to_producer
 --
-CREATE TABLE twokeys (
-  artist integer NOT NULL,
+CREATE TABLE cd_to_producer (
   cd integer NOT NULL,
-  PRIMARY KEY (artist, cd)
+  producer integer NOT NULL,
+  PRIMARY KEY (cd, producer)
 );
 
 --
@@ -30,12 +39,12 @@ CREATE TABLE liner_notes (
 );
 
 --
--- Table: cd_to_producer
+-- Table: employees_adjacencylist
 --
-CREATE TABLE cd_to_producer (
-  cd integer NOT NULL,
-  producer integer NOT NULL,
-  PRIMARY KEY (cd, producer)
+CREATE TABLE employees_adjacencylist (
+  employee_id INTEGER PRIMARY KEY NOT NULL,
+  parent_id integer,
+  name varchar(100)
 );
 
 --
@@ -43,8 +52,17 @@ CREATE TABLE cd_to_producer (
 --
 CREATE TABLE artist (
   artistid INTEGER PRIMARY KEY NOT NULL,
-  name varchar,
-  position INTEGER
+  name varchar(100)
+);
+
+--
+-- Table: employees_positioned_adjacencylist
+--
+CREATE TABLE employees_positioned_adjacencylist (
+  employee_id INTEGER PRIMARY KEY NOT NULL,
+  parent_id integer,
+  position integer NOT NULL,
+  name varchar(100)
 );
 
 --
@@ -57,17 +75,6 @@ CREATE TABLE self_ref_alias (
 );
 
 --
--- Table: fourkeys
---
-CREATE TABLE fourkeys (
-  foo integer NOT NULL,
-  bar integer NOT NULL,
-  hello integer NOT NULL,
-  goodbye integer NOT NULL,
-  PRIMARY KEY (foo, bar, hello, goodbye)
-);
-
---
 -- Table: cd
 --
 CREATE TABLE cd (
@@ -78,24 +85,6 @@ CREATE TABLE cd (
 );
 
 --
--- Table: artist_undirected_map
---
-CREATE TABLE artist_undirected_map (
-  id1 integer NOT NULL,
-  id2 integer NOT NULL,
-  PRIMARY KEY (id1, id2)
-);
-
---
--- Table: onekey
---
-CREATE TABLE onekey (
-  id INTEGER PRIMARY KEY NOT NULL,
-  artist integer NOT NULL,
-  cd integer NOT NULL
-);
-
---
 -- Table: track
 --
 CREATE TABLE track (
@@ -106,10 +95,11 @@ CREATE TABLE track (
 );
 
 --
--- Table: producer
+-- Table: treelike
 --
-CREATE TABLE producer (
-  producerid INTEGER PRIMARY KEY NOT NULL,
+CREATE TABLE treelike (
+  id INTEGER PRIMARY KEY NOT NULL,
+  parent integer NOT NULL,
   name varchar(100) NOT NULL
 );
 
@@ -131,11 +121,48 @@ CREATE TABLE self_ref (
 );
 
 --
--- Table: treelike
+-- Table: twokeys
 --
-CREATE TABLE treelike (
+CREATE TABLE twokeys (
+  artist integer NOT NULL,
+  cd integer NOT NULL,
+  PRIMARY KEY (artist, cd)
+);
+
+--
+-- Table: fourkeys
+--
+CREATE TABLE fourkeys (
+  foo integer NOT NULL,
+  bar integer NOT NULL,
+  hello integer NOT NULL,
+  goodbye integer NOT NULL,
+  PRIMARY KEY (foo, bar, hello, goodbye)
+);
+
+--
+-- Table: artist_undirected_map
+--
+CREATE TABLE artist_undirected_map (
+  id1 integer NOT NULL,
+  id2 integer NOT NULL,
+  PRIMARY KEY (id1, id2)
+);
+
+--
+-- Table: onekey
+--
+CREATE TABLE onekey (
   id INTEGER PRIMARY KEY NOT NULL,
-  parent integer NOT NULL,
+  artist integer NOT NULL,
+  cd integer NOT NULL
+);
+
+--
+-- Table: producer
+--
+CREATE TABLE producer (
+  producerid INTEGER PRIMARY KEY NOT NULL,
   name varchar(100) NOT NULL
 );
 
index f7d847c..4865d96 100644 (file)
@@ -45,11 +45,6 @@ my $test_type_info = {
         'data_type' => 'varchar',
         'is_nullable' => 0,
     },
-    'position' => {
-        'data_type' => 'INTEGER',
-        'is_nullable' => 0,
-        'size' => undef,
-    },
 };
 is_deeply($type_info, $test_type_info, 'columns_info_for - column data types');
 
index 997f970..31503b0 100644 (file)
@@ -2,62 +2,92 @@
 
 sub run_tests {
 
+    plan tests => 96;
     my $schema = shift;
-    my $artists = $schema->resultset("Artist");
 
-    $artists->delete();
-    $artists->create({ artistid=>1, name=>"Joe" });
-    $artists->create({ artistid=>2, name=>"Bob" });
-    $artists->create({ artistid=>3, name=>"Ted" });
-    $artists->create({ artistid=>4, name=>"Ned" });
-    $artists->create({ artistid=>5, name=>"Don" });
+    foreach my $class ( 'Positioned', 'PositionedAdjacencyList', 'AdjacencyList' ) {
 
-    $artists = $artists->search(undef,{order_by=>'position'});
+        my $employees = $schema->resultset('Employee::'.$class);
 
-    plan tests => 230;
+        if ($employees->result_class->can('position_column')) {
 
-    check_positions($schema);
+            $employees->delete();
+            foreach (1..5) {
+                $employees->create({ name=>'temp' });
+            }
 
-    my $artist;
+            $employees = $employees->search(undef,{order_by=>'position'});
+            ok( check_positions($employees), "$class: intial positions" );
 
-    foreach my $position (1..5) {
+            my $employee;
 
-        $artist = $artists->find({ position=>$position });
-        $artist->move_previous();
-        check_positions($schema);
+            foreach my $position (1..$employees->count()) {
 
-        $artist = $artists->find({ position=>$position });
-        $artist->move_next();
-        check_positions($schema);
+                $employee = $employees->find({ position=>$position });
+                $employee->move_previous();
+                ok( check_positions($employees), "$class: move_previous( $position )" );
 
-        $artist = $artists->find({ position=>$position });
-        $artist->move_first();
-        check_positions($schema);
+                $employee = $employees->find({ position=>$position });
+                $employee->move_next();
+                ok( check_positions($employees), "$class: move_next( $position )" );
 
-        $artist = $artists->find({ position=>$position });
-        $artist->move_last();
-        check_positions($schema);
+                $employee = $employees->find({ position=>$position });
+                $employee->move_first();
+                ok( check_positions($employees), "$class: move_first( $position )" );
 
-        foreach my $to_position (1..5) {
+                $employee = $employees->find({ position=>$position });
+                $employee->move_last();
+                ok( check_positions($employees), "$class: move_last( $position )" );
 
-            $artist = $artists->find({ position=>$position });
-            $artist->move_to($to_position);
-            check_positions($schema);
+                foreach my $to_position (1..$employees->count()) {
+                    $employee = $employees->find({ position=>$position });
+                    $employee->move_to($to_position);
+                    ok( check_positions($employees), "$class: move_to( $position => $to_position )" );
+                }
 
+            }
         }
+        if ($employees->result_class->can('parent_column')) {
 
+            $employees->delete();
+            my $mom = $employees->create({ name=>'temp' });
+            foreach (1..14) {
+                $employees->create({ name=>'temp', parent_id=>$mom->id() });
+            }
+
+            my $children = $mom->children();
+            ok( ($children->count()==14), 'correct number of children' );
+
+            my $grandma = $mom;
+            my @children = $children->all();
+            $mom = pop(@children);
+            foreach my $child (@children) {
+                $child->parent( $mom );
+            }
+
+            ok( ($mom->children->count() == 13), 'correct number of grandchildren' );
+
+            if ($employees->result_class->can('position_column')) {
+                # TODO: Test positioning within a tree.
+            }
+        }
     }
 
 }
 
 sub check_positions {
-    my $schema = shift;
-    my $artists = $schema->resultset("Artist")->search(undef,{order_by=>'position'});
+    my( $employees ) = @_;
+    $employees->reset();
     my $expected_position = 0;
-    while (my $artist = $artists->next()) {
+    my $is_ok = 1;
+    while (my $employee = $employees->next()) {
         $expected_position ++;
-        ok( ($artist->position()==$expected_position), 'default positions set as expected' );
+        if ($employee->position()!=$expected_position) {
+            $is_ok = 0;
+            last;
+        }
     }
+    return $is_ok;
 }
 
 1;