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