File: test-browseable-functions-using-SQLite.t

package info (click to toggle)
libapache-session-browseable-perl 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 248 kB
  • ctags: 63
  • sloc: perl: 915; makefile: 2
file content (145 lines) | stat: -rw-r--r-- 4,083 bytes parent folder | download | duplicates (7)
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
# Complete tests with SQLite

use strict;
use warnings;
use Test::More;
use File::Temp qw(mktemp);

my $dbfile = mktemp('tmp.db_XXXX');

plan skip_all => "DBD::SQLite is needed for this test"
  unless eval {
    require DBI;
    require DBD::SQLite;
    1;
  };

plan skip_all => "DBD::SQLite error : $@"
  unless eval {
    my $dbh;
    $dbh = DBI->connect( "dbi:SQLite:dbname=$dbfile", "", "" )
      or die $dbh->errstr;
    $dbh->do(
'CREATE TABLE sessions(id char(32) not null primary key,a_session text,f1 text,f2 text);'
    )                                                 or die $dbh->errstr;
    $dbh->do('CREATE INDEX f1_idx ON sessions (f1);') or die $dbh->errstr;
    $dbh->do('CREATE INDEX f2_idx ON sessions (f2);') or die $dbh->errstr;
    $dbh->disconnect()                                or die $dbh->errstr;
  };

my @list = ( "aa" .. "ay", "aa" .. "ac" );
my $count = @list;

plan tests => 27 + 5 * $count;

use_ok('Apache::Session::Browseable::SQLite');

my %session;
my $args = { DataSource => "dbi:SQLite:$dbfile", Index => "f1 f2", };
foreach (@list) {
    ok( tie %session, 'Apache::Session::Browseable::SQLite',
        undef, $args, "Create session $_" );
    $session{f1} = "1_$_";
    $session{f2} = "2_$_";
    $session{f3} = "3_$_";
    $session{f4} = "4_$_";
    untie %session;
}

my $res;

# 1. Get simply all sessions
ok(
    $res =
      Apache::Session::Browseable::SQLite->get_key_from_all_sessions($args),
    'Get all sessions'
);
ok( count($res) == $count,
    "get_key_from_all_sessions returns $count sessions (" . count($res) . ")" );

# 2. Test searchOn() on an indexed field
ok( $res =
      Apache::Session::Browseable::SQLite->searchOn( $args, 'f1', '1_aa' ) );
ok( count($res) == 2, 'Get 2 "aa" sessions (' . count($res) . ")" );
ok( $res =
      Apache::Session::Browseable::SQLite->searchOn( $args, 'f1', '1_ad' ) );
ok( count($res) == 1, 'Get 1 "ad" sessions (' . count($res) . ")" );

# 3. Test searchOn() on an unindexed field
ok( $res =
      Apache::Session::Browseable::SQLite->searchOn( $args, 'f3', '3_aa' ) );
ok( count($res) == 2, 'Get 2 "aa" sessions (' . count($res) . ")" );
ok( $res =
      Apache::Session::Browseable::SQLite->searchOn( $args, 'f3', '3_ad' ) );
ok( count($res) == 1, 'Get 1 "ad" sessions (' . count($res) . ")" );

# 4. Test searchOnExpr() on an indexed field
ok( $res =
      Apache::Session::Browseable::SQLite->searchOnExpr( $args, 'f1', '*aa' ) );
ok( count($res) == 2, 'Get 2 "aa" sessions (' . count($res) . ")" );

# 5. Test searchOnExpr() on an unindexed field
ok( $res =
      Apache::Session::Browseable::SQLite->searchOnExpr( $args, 'f3', '*aa' ) );
ok( count($res) == 2, 'Get 2 "aa" sessions (' . count($res) . ")" );

# 6. Test get_key_from_all_sessions to request for indexed fields
ok(
    $res = Apache::Session::Browseable::SQLite->get_key_from_all_sessions(
        $args, [ 'f1', 'f2' ]
    )
);

while ( my ( $id, $entry ) = each %$res ) {
    ok( $entry->{f1} =~ /^1_(\w{2})$/ );
    my $t = $1;
    ok( $entry->{f2} =~ /^2_${t}$/ );
}

# 7. Test searchOn to request for indexed fields
ok(
    $res = Apache::Session::Browseable::SQLite->searchOn(
        $args, 'f1', '1_aa', 'f1', 'f2'
    )
);

while ( my ( $id, $entry ) = each %$res ) {
    ok( $entry->{f1} =~ /^1_(\w{2})$/ );
    my $t = $1;
    ok( $entry->{f2} =~ /^2_${t}$/ );
}

# 8. Test get_key_from_all_sessions to request for unindexed fields
ok(
    $res = Apache::Session::Browseable::SQLite->get_key_from_all_sessions(
        $args, [ 'f1', 'f3' ]
    )
);

while ( my ( $id, $entry ) = each %$res ) {
    ok( $entry->{f1} =~ /^1_(\w{2})$/ );
    my $t = $1;
    ok( $entry->{f3} =~ /^3_${t}$/ );
}

# 9. Test searchOn to request for unindexed fields
ok(
    $res = Apache::Session::Browseable::SQLite->searchOn(
        $args, 'f1', '1_aa', 'f1', 'f3'
    )
);

unlink $dbfile if ( -e $dbfile );

while ( my ( $id, $entry ) = each %$res ) {
    ok( $entry->{f1} =~ /^1_(\w{2})$/ );
    my $t = $1;
    ok( $entry->{f3} =~ /^3_${t}$/ );
}

sub count {
    my @c = keys %{ $_[0] };
    return scalar @c;
}

1;