File: 07-tree.t

package info (click to toggle)
libdbix-class-tree-nestedset-perl 0.10-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 196 kB
  • sloc: perl: 1,672; makefile: 2
file content (363 lines) | stat: -rw-r--r-- 10,590 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env perl
#
# $Id: $
# $Revision: $
# $Author: $
# $Source:  $
#
# $Log: $
#
use strict;
use warnings;

use Test::More;

use Data::Dumper;
use File::Temp 'tempfile';
use DBICx::TestDatabase;

use FindBin;
use lib "$FindBin::Bin/../lib";
use lib "$FindBin::Bin/tlib";

BEGIN { use_ok('TestSchema') }

my $schema = DBICx::TestDatabase->new('TestSchema');
isa_ok($schema, 'DBIx::Class::Schema');

my $trees = $schema->resultset('MultiTree');
isa_ok($trees, 'DBIx::Class::ResultSet');

my $root = $trees->create({ id => 1, content => 'root' });
isa_ok($root, 'DBIx::Class::Row');

is($root->id, 1, 'root has correct ID');
is($root->content, 'root', 'root has correct content');
is($root->parent, undef, 'root has no parent');
is($root->root->id, $root->id, 'root field gets set automatically');
is($root->descendants->count, 0, 'no descendants, initially');
is($root->nodes->count, 1, 'nodes include self');

ok( $root->is_root,   'is_root()');
ok( $root->is_leaf,   'is_leaf()');
ok(!$root->is_branch, 'is_branch()');

my $auto_inc = 1;

# Create a test tree
my $test_tree = [
    [1,  1,  20, 'root', undef, 0],
    [2,  2,  13, 'A',    1,     1],
    [3,  3,   4, 'B',    2,     2],
    [4,  5,  10, 'C',    2,     2],
    [5,  11, 12, 'D',    2,     2],
    [6,  6,  7,  'E',    4,     3],
    [7,  8,  9,  'F',    4,     3],
    [8,  14, 19, 'G',    1,     1],
    [9,  15, 16, 'H',    8,     2],
    [10, 17, 18, 'I',    8,     2],
];

my ($success, $child, $parent);

$root = create_tree(1, $test_tree);

test_tree($root, "Initial Tree", $test_tree);

# Test the 'is_root' interface
my $is_root = $root->is_root;
is($is_root, 1, "Root is root");

# Test for descendants of leaf nodes
for my $id (3,6,7,5,9,10) {
    $parent = $schema->resultset('MultiTree')->find($id);
    # list context
    my @descendants = $parent->descendants;
    is(scalar @descendants, 0, "List - descendants of leaf node $id");
    # scalar context
    $child  = $parent->descendants;
    is($child, 0, "Scalar - descendants of leaf node $id");
}

# Test for descendants of branches
for my $test ([1,[2,3,4,6,7,5,8,9,10]],[2,[3,4,6,7,5]],[4,[6,7]],[8,[9,10]]) {
    my $parent_id    = $test->[0];
    my @descendants_ids= @{$test->[1]};
    # list context
    $parent         = $schema->resultset('MultiTree')->find($parent_id);
    is($parent->descendants->count, scalar(@descendants_ids), "Number of descendants for $parent_id");

    my @descendants    = $parent->descendants;
    my $index = 0;
    for my $child (@descendants) {
        is($child->id, $descendants_ids[$index++], "List - Child $index of $parent_id");
    }
    # scalar context
    $parent         = $schema->resultset('MultiTree')->find($parent_id);
    my $descendants    = $parent->descendants;
    $index = 0;
    while (my $child = $descendants->next) {
        is($child->id, $descendants_ids[$index++], "Scalar - Child $index of $parent_id");
    }
}

# Test for ancestors of root
# list context
$child          = $schema->resultset('MultiTree')->find(1);
my @ancestors   = $child->ancestors;
is(@ancestors, 0, "Scalar - ancestors of root node");

# scalar context
$child          = $schema->resultset('MultiTree')->find(1);
my $ancestors   = $child->ancestors;
is ($ancestors, 0, "List - ancestors of root node");

# Test for ancestors
for my $test ([2,[1]],[3,[2,1]],[4,[2,1]],[6,[4,2,1]],[7,[4,2,1]],[5,[2,1]],[9,[8,1]],[10,[8,1]],[8,[1]]) {
    my $child_id    = $test->[0];
    my @ancestor_ids= @{$test->[1]};
    # list context
    $child          = $schema->resultset('MultiTree')->find($child_id);
    my @ancestors   = $child->ancestors;
    my $index = 0;
    for my $ancestor (@ancestors) {
        is($ancestor->id, $ancestor_ids[$index++], "List - Ancestor $index of $child_id");
    }
    # scalar context
    $child          = $schema->resultset('MultiTree')->find($child_id);
    my $ancestors   = $child->ancestors;
    $index = 0;
    while (my $ancestor = $ancestors->next) {
        is($ancestor->id, $ancestor_ids[$index++], "Scalar - Ancestor $index of $child_id");
    }
}

done_testing();
exit;

#### insert tests ####
# Insert a left child of the root node
$root = create_tree($test_tree);
$root->prepend_child({
    parent_id   => 11,
    name        => 'left_child',
});


$child = $schema->resultset('MultiTree')->create({
    id              => 11,
    title           => 'left child',
});




$success = $root->insert_left_child($child);
ok($success, "Left child inserted to root");
test_tree("Left Child to Root",[
    [1,  1,  22, 'root'],
    [2,  4,  15, 'A'],
    [3,  5,   6, 'B'],
    [4,  7,  12, 'C'],
    [5,  13, 14, 'D'],
    [6,  8,  9,  'E'],
    [7,  10, 11, 'F'],
    [8,  16, 21, 'G'],
    [9,  17, 18, 'H'],
    [10, 19, 20, 'I'],
    [11,  2,  3, 'left child'],
]);


# Insert a left child of a leaf node
$root = create_tree($test_tree);
$child = $schema->resultset('MultiTree')->create({
    id              => 11,
    title           => 'left child',
});
($parent) = $schema->resultset('MultiTree')->search({id => 10,});
$success = $parent->insert_left_child($child);
ok($success, "Left child inserted to 10");
test_tree("Left Child to Leaf", [
    [1,  1,  22, 'root'],
    [2,  2,  13, 'A'],
    [3,  3,   4, 'B'],
    [4,  5,  10, 'C'],
    [5,  11, 12, 'D'],
    [6,  6,  7,  'E'],
    [7,  8,  9,  'F'],
    [8,  14, 21, 'G'],
    [9,  15, 16, 'H'],
    [10, 17, 20, 'I'],
    [11, 18, 19, 'left child'],
]);

# Insert a left child of a non leaf node
$root = create_tree($test_tree);
$child = $schema->resultset('MultiTree')->create({
    id              => 11,
    title           => 'left child',
});
($parent) = $schema->resultset('MultiTree')->search({id => 8,});
$success = $parent->insert_left_child($child);
ok($success, "Left child inserted to 8");
test_tree("Left Child to non Leaf",[
    [1,  1,  22, 'root'],
    [2,  2,  13, 'A'],
    [3,  3,   4, 'B'],
    [4,  5,  10, 'C'],
    [5,  11, 12, 'D'],
    [6,  6,  7,  'E'],
    [7,  8,  9,  'F'],
    [8,  14, 21, 'G'],
    [9,  17, 18, 'H'],
    [10, 19, 20, 'I'],
    [11, 15, 16, 'left child'],
]);

#### insert_right_child tests ####
# Insert a right child of the root node
$root = create_tree($test_tree);
$child = $schema->resultset('MultiTree')->create({
    id              => 11,
    title           => 'right child',
});
$success = $root->insert_right_child($child);
ok($success, "Right child inserted to root");
test_tree("Right Child to Root",[
    [1,  1,  22, 'root'],
    [2,  2,  13, 'A'],
    [3,  3,   4, 'B'],
    [4,  5,  10, 'C'],
    [5,  11, 12, 'D'],
    [6,  6,  7,  'E'],
    [7,  8,  9,  'F'],
    [8,  14, 19, 'G'],
    [9,  15, 16, 'H'],
    [10, 17, 18, 'I'],
    [11, 20, 21, 'right child'],
]);

# Insert a right child of a leaf node
$root = create_tree($test_tree);
$child = $schema->resultset('MultiTree')->create({
    id              => 11,
    title           => 'right child',
});
($parent) = $schema->resultset('MultiTree')->search({id => 9,});
$success = $parent->insert_right_child($child);
ok($success, "Right child inserted to 9");
test_tree("Right Child to Leaf",[
    [1,  1,  22, 'root'],
    [2,  2,  13, 'A'],
    [3,  3,   4, 'B'],
    [4,  5,  10, 'C'],
    [5,  11, 12, 'D'],
    [6,  6,  7,  'E'],
    [7,  8,  9,  'F'],
    [8,  14, 21, 'G'],
    [9,  15, 18, 'H'],
    [10, 19, 20, 'I'],
    [11, 16, 17, 'right child'],
]);

# Insert a right child to a non leaf node
$root = create_tree($test_tree);
$child = $schema->resultset('MultiTree')->create({
    id              => 11,
    title           => 'right child',
});
($parent) = $schema->resultset('MultiTree')->search({id => 8,});
$success = $parent->insert_right_child($child);
ok($success, "Right child inserted to 8");
test_tree("Right Child to non Leaf",[
    [1,  1,  22, 'root'],
    [2,  2,  13, 'A'],
    [3,  3,   4, 'B'],
    [4,  5,  10, 'C'],
    [5,  11, 12, 'D'],
    [6,  6,  7,  'E'],
    [7,  8,  9,  'F'],
    [8,  14, 21, 'G'],
    [9,  15, 16, 'H'],
    [10, 17, 18, 'I'],
    [11, 19, 20, 'right child'],
]);

# Test for ancestors
$root = create_tree($test_tree);
for my $test ([2,1],[3,2],[4,2],[6,4],[7,4],[5,2],[9,8],[10,8],[8,1]) {
    my $child_id    = $test->[0];
    my $parent_id   = $test->[1];
    my $child       = $schema->resultset('MultiTree')->find($child_id);
    my $parent      = $child->parent;
    is($parent->id, $parent_id, "Parent of $child_id is $parent_id");
}

# Test for ancestors
$root = create_tree($test_tree);
for my $test ([2,[1]],[3,[1,2]],[4,[1,2]],[6,[1,2,4]],[7,[1,2,4]],[5,[1,2]],[9,[1,8]],[10,[1,8]],[8,[1]]) {
    my $child_id    = $test->[0];
    my @ancestor_ids= @{$test->[1]};
    # list context
    $child          = $schema->resultset('MultiTree')->find($child_id);
    my @ancestors   = $child->ancestors;
    my $index = 0;
    for my $ancestor (@ancestors) {
        is($ancestor->id, $ancestor_ids[$index++], "List - Ancestor $index of $child_id");
    }
    # scalar context
    $child          = $schema->resultset('MultiTree')->find($child_id);
    my $ancestors   = $child->ancestors;
    $index = 0;
    while (my $ancestor = $ancestors->next) {
        is($ancestor->id, $ancestor_ids[$index++], "Scalar - Ancestor $index of $child_id");
    }
}



#============== subroutines ====================

# Test an array of nodes, each contains an array of id, lft and rgt
sub test_tree {
    my ( $root, $test_name, $array_ref) = @_;

    # left and right extents must be unique
    my @extents = [];
    for my $node (@$array_ref) {
        my ($id, $left, $right, $content) = @$node;
        my $node = $schema->resultset('MultiTree')->find($id);
        ok($node, "$test_name: Node $id found");
        is($node->lft, $left, "$test_name: Left Extent is $left");
        is($node->rgt, $right, "$test_name: Right Extent is $right");
        is($node->content, $content, "$test_name: Name is $content");
    }
}

# Create a complete tree
sub create_tree {
    my ($root_id, $array_ref) = @_;

    $auto_inc = $root->id + 1;
    $schema->resultset('MultiTree')->delete;

    for my $node (@$array_ref) {
        my ($id, $left, $right, $content, $parent, $level) = @$node;
        my $node = $schema->resultset('MultiTree')->create({
            id          => $id,
            root_id     => $root->id,
            lft         => $left,
            rgt         => $right,
            content     => $content,
            level       => $level,
        });
        if ($id >= $auto_inc) {
            $auto_inc = $id + 1;
        }
    }
    my $root = $schema->resultset('MultiTree')->find($root_id);
    return $root;
}