318225caa50704bb5412129b6366e164952e1288
[ZOOM-Perl-moved-to-github.git] / t / 26-packages.t
1 # $Id: 26-packages.t,v 1.9 2008-09-29 15:49:18 mike Exp $
2
3 # Before `make install' is performed this script should be runnable with
4 # `make test'. After `make install' it should work as `perl 26-packages.t'
5
6 use strict;
7 use warnings;
8 use Test::More tests => 39;
9
10 BEGIN { use_ok('ZOOM') };
11
12
13 # We will create, and destroy, a new database with a random name
14 my $host = "z3950.indexdata.com:2100";
15 my $dbname = join("", map { chr(ord("a") + int(rand(26))) } 1..10);
16
17 # Connect anonymously, and expect this to fail
18 my $conn = makeconn($host, undef, undef, 1011);
19
20 # Connect as a user, but with incorrect password -- expect failure
21 $conn->destroy();
22 $conn = makeconn($host, "user", "badpw", 1011);
23
24 # Connect as a non-privileged user with correct password
25 $conn->destroy();
26 $conn = makeconn($host, "user", "frog", 0);
27
28 # Non-privileged user can't create database
29 makedb($conn, $dbname, 223);
30
31 # Connect as a privileged user with correct password, check DB is absent
32 $conn->destroy();
33 $conn = makeconn($host, "admin", "fish", 0);
34 $conn->option(databaseName => $dbname);
35 count_hits($conn, $dbname, "the", 109);
36
37 # Now create the database and check that it is present but empty
38 makedb($conn, $dbname, 0);
39 count_hits($conn, $dbname, "the", 114);
40
41 # Trying to create the same database again will fail EEXIST
42 makedb($conn, $dbname, 224);
43
44 # Add a single record, and check that it can be found
45 updaterec($conn, 1, content_of("samples/records/esdd0006.grs"), 0);
46 count_hits($conn, $dbname, "the", 0, 1);
47
48 # Add the same record with the same ID: overwrite => no change
49 updaterec($conn, 1, content_of("samples/records/esdd0006.grs"), 0);
50 count_hits($conn, $dbname, "the", 0, 1);
51
52 # Add it again record with different ID => new copy added
53 updaterec($conn, 2, content_of("samples/records/esdd0006.grs"), 0);
54 count_hits($conn, $dbname, "the", 0, 2);
55
56 # Now drop the newly-created database
57 dropdb($conn, $dbname, 0);
58
59 # A second dropping should fail, as the database is no longer there.
60 dropdb($conn, $dbname, 235);
61
62
63 sub makeconn {
64     my($host, $user, $password, $expected_error) = @_;
65
66     my $options = new ZOOM::Options();
67     $options->option(user => $user)
68         if defined $user;
69     $options->option(password => $password)
70         if defined $password;
71
72     my $conn;
73     eval { $conn = create ZOOM::Connection($options) };
74     ok(!$@, "unconnected connection object created");
75
76     eval { $conn->connect($host, 0) };
77     my($errcode, $errmsg, $addinfo) = maybe_error($@);
78
79     ok($errcode == $expected_error,
80        "connection to '$host'" . ($errcode ? " refused ($errcode)" : ""));
81
82     return $conn;
83 }
84
85
86 sub makedb {
87     my($conn, $dbname, $expected_error) = @_;
88
89     my $p = $conn->package();
90     # Inspection of the ZOOM-C code shows that this can never fail, in fact.
91     ok(defined $p, "created package");
92
93     $p->option(databaseName => $dbname);
94     my $val = $p->option("databaseName");
95     ok($val eq $dbname, "package option retrieved as expected");
96
97     eval { $p->send("create") };
98     my($errcode, $errmsg, $addinfo) = maybe_error($@);
99     ok($errcode == $expected_error, "database creation '$dbname'" .
100        ($errcode ? " refused ($errcode)" : ""));
101
102     # Now we can inspect the package options to find out more about
103     # how the server dealt with the request.  However, it seems that
104     # the "package database" described in the standard is not used,
105     # and that the only options we can inspect are the following:
106     $val = $p->option("targetReference");
107     $val = $p->option("xmlUpdateDoc");
108     # ... and we know nothing about expected or actual values.
109
110     $p->destroy();
111     ok(1, "destroyed createdb package");
112 }
113
114
115 sub dropdb {
116     my($conn, $dbname, $expected_error) = @_;
117
118     my $p = $conn->package();
119     # No need to keep ok()ing this, or checking the option-setting
120     $p->option(databaseName => $dbname);
121     eval { $p->send("drop") };
122     my($errcode, $errmsg, $addinfo) = maybe_error($@);
123     ok($errcode == $expected_error,
124        "database drop '$dbname'"  . ($errcode ? " refused $errcode" : ""));
125
126     $p->destroy();
127     ok(1, "destroyed dropdb package");
128 }
129
130
131 # We always use "specialUpdate", which adds a record or replaces it if
132 # it's already there.  By contrast, "insert" fails if the record
133 # already exists, and "replace" fails if it does not.
134 #
135 sub updaterec {
136     my($conn, $id, $file, $expected_error) = @_;
137
138     my $p = $conn->package();
139     $p->option(action => "specialUpdate");
140     $p->option(recordIdOpaque => $id);
141     $p->option(record => $file);
142
143     eval { $p->send("update") };
144     my($errcode, $errmsg, $addinfo) = maybe_error($@);
145     ok($errcode == $expected_error, "record update $id" .
146        ($errcode ? " failed $errcode '$errmsg' ($addinfo)" : ""));
147
148     $p->destroy();
149     ok(1, "destroyed update package");
150 }
151
152
153 sub count_hits {
154     my($conn, $dbname, $query, $expected_error, $expected_count) = @_;
155
156     my $rs;
157     eval { $rs = $conn->search_pqf($query) };
158     my($errcode, $errmsg, $addinfo) = maybe_error($@);
159     ok($errcode == $expected_error, "database '$dbname' " .
160        ($errcode == 0 ? "can be searched" : "not searchable ($errcode)"));
161
162     return if $errcode != 0;
163     my $n = $rs->size($rs);
164     ok($n == $expected_count,
165        "database '$dbname' has $n records (expected $expected_count)");
166 }
167
168
169 sub content_of {
170     my($filename) = @_;
171
172     use IO::File;
173     my $f = new IO::File("<$filename")
174         or die "can't open file '$filename': $!";
175     my $text = join("", <$f>);
176     $f->close();
177
178     return $text;
179 }
180
181
182 # Return the elements of an exception as separate scalars
183 sub maybe_error {
184     my ($x) = @_;
185
186     if ($x && $x->isa("ZOOM::Exception")) {
187         return ($x->code(),
188                 $x->message(),
189                 $x->addinfo());
190     } else {
191         return (0, undef, undef);
192     }
193 }
194
195
196 # To investigate the set of databases created, use Explain Classic:
197 #
198 #       $ yaz-client -u admin/fish test.indexdata.com:2118/IR-Explain-1
199 #       Z> find @attr exp1 1=1 databaseinfo
200 #       Z> format xml
201 #       Z> show 3
202 #
203 # It seems that Explain still knows about dropped databases.