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
|
#!/usr/bin/perl
use warnings;
use strict;
use utf8;
use open qw(:std :utf8);
use lib qw(lib ../lib);
use Test::More tests => 50;
use Encode qw(decode encode);
BEGIN {
# Подготовка объекта тестирования для работы с utf8
my $builder = Test::More->builder;
binmode $builder->output, ":utf8";
binmode $builder->failure_output, ":utf8";
binmode $builder->todo_output, ":utf8";
use_ok 'DR::Tarantool::Spaces';
use_ok 'DR::Tarantool::Tuple';
}
my $s = DR::Tarantool::Spaces->new({
0 => {
name => 'test',
default_type => 'NUM',
fields => [
qw(a b c),
{
type => 'UTF8STR',
name => 'd'
},
{
type => 'NUM64',
name => 'a123',
},
{
type => 'STR',
name => 'abcd',
}
],
indexes => {
0 => [ qw(a b) ],
1 => 'd'
}
}
});
my $tp = new DR::Tarantool::Tuple( [ 'aa', 'bb', 'cc' ], $s->space('test') );
isa_ok $tp => 'DR::Tarantool::Tuple';
cmp_ok $tp->raw(0), '~~', 'aa', 'raw(0)';
cmp_ok $tp->a, '~~', 'aa', 'raw(0)';
cmp_ok $tp->raw(1), '~~', 'bb', 'raw(1)';
cmp_ok $tp->b, '~~', 'bb', 'raw(1)';
cmp_ok $tp->raw(2), '~~', 'cc', 'raw(2)';
cmp_ok $tp->c, '~~', 'cc', 'raw(2)';
ok @{ $tp->raw } ~~ @{[qw(aa bb cc)]}, 'raw';
cmp_ok $tp->raw(3), '~~', undef, 'raw(3)';
cmp_ok $tp->d, '~~', undef, 'raw(3)';
ok !eval { $tp->unknown; 1 }, 'unknown';
my $tp2 = $tp->next(['dd', 'ee']);
my $tp3 = $tp->next(['ff', 'gg']);
isa_ok $tp2 => 'DR::Tarantool::Tuple';
isa_ok $tp3 => 'DR::Tarantool::Tuple';
cmp_ok $tp2->raw(0), '~~', 'dd', 'tp2->raw(0)';
cmp_ok $tp2->raw(1), '~~', 'ee', 'tp2->raw(1)';
cmp_ok $tp3->raw(0), '~~', 'ff', 'tp3->raw(0)';
cmp_ok $tp3->raw(1), '~~', 'gg', 'tp3->raw(1)';
my $it = $tp->iter;
isa_ok $it => 'DR::Tarantool::Tuple::Iterator';
cmp_ok $it->count, '~~', 3, 'count';
$tp = $it->next;
cmp_ok $tp->raw(0), '~~', 'aa', 'raw(0)';
cmp_ok $tp->raw(1), '~~', 'bb', 'raw(1)';
$tp = $it->next;
cmp_ok $tp->raw(0), '~~', 'dd', 'raw(0)';
cmp_ok $tp->raw(1), '~~', 'ee', 'raw(1)';
$tp = $it->next;
cmp_ok $tp->raw(0), '~~', 'ff', 'raw(0)';
cmp_ok $tp->raw(1), '~~', 'gg', 'raw(1)';
$tp = $it->next;
cmp_ok $tp, '~~', undef, 'iterator finished';
while( my $t = $it->next ) {
isa_ok $t => 'DR::Tarantool::Tuple';
}
while( my $t = $it->next ) {
isa_ok $t => 'DR::Tarantool::Tuple';
}
$tp = new DR::Tarantool::Tuple( [ [ 'aa' ], [ 'bb' ], [ 'cc' ] ],
$s->space('test')
);
cmp_ok $tp->raw(0), '~~', 'aa', 'tuple[0]';
cmp_ok $tp->next->raw(0), '~~', 'bb', 'tuple[0]';
cmp_ok $tp->next->next->raw(0), '~~', 'cc', 'tuple[0]';
$tp = DR::Tarantool::Tuple->unpack(
[ pack('L<', 10), pack('L<', 20) ], $s->space('test')
);
isa_ok $tp => 'DR::Tarantool::Tuple';
cmp_ok $tp->raw(0), '~~', 10, 'raw(0)';
cmp_ok $tp->raw(1), '~~', 20, 'raw(1)';
$tp = new DR::Tarantool::Tuple( [ [ 'aa' ], [ 'bb' ], ], $s->space('test') );
isa_ok $tp => 'DR::Tarantool::Tuple';
cmp_ok $tp->iter->count, '~~', 2, 'create tuple list';
my $iter = $tp->iter;
isa_ok $iter => 'DR::Tarantool::Tuple::Iterator', 'iterator';
isa_ok $iter->next => 'DR::Tarantool::Tuple', 'no iterator class';
$iter = $tp->iter('TestItem');
isa_ok $iter => 'DR::Tarantool::Tuple::Iterator', 'iterator with TestItem';
$tp = $iter->next;
isa_ok $tp => 'TestItem';
isa_ok $tp->{tuple} => 'DR::Tarantool::Tuple';
cmp_ok $tp->{tuple}->raw(0), '~~', 'aa', 'tuple(0).raw(0)';
cmp_ok $iter->next->{tuple}->raw(0), '~~', 'bb', 'tuple(1).raw(0)';
$tp = DR::Tarantool::Tuple->new([ [ 'aa' ], [ 'bb' ], ], $s->space('test'));
$iter = $tp->iter;
undef $tp;
cmp_ok $iter->count, '~~', 2, 'iterator saves tuple ref';
# You have to use external tool to watch memory
while($ENV{LEAK_TEST}) {
$tp = DR::Tarantool::Tuple->new([ [ 'aa' ], [ 'bb' ], ], $s->space('test'));
$tp = $tp->iter('TestItem')->next;
}
package TestItem;
sub new {
my ($class, $tuple) = @_;
return bless { tuple => $tuple } => $class;
}
|