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'
6 use Test::More tests => 39;
8 BEGIN { use_ok('ZOOM') };
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);
15 # Connect anonymously, and expect this to fail
16 my $conn = makeconn($host, undef, undef, 1011);
18 # Connect as a user, but with incorrect password -- expect failure
20 $conn = makeconn($host, "user", "badpw", 1011);
22 # Connect as a non-privileged user with correct password
24 $conn = makeconn($host, "user", "frog", 0);
26 # Non-privileged user can't create database
27 makedb($conn, $dbname, 223);
29 # Connect as a privileged user with correct password, check DB is absent
31 $conn = makeconn($host, "admin", "fish", 0);
32 $conn->option(databaseName => $dbname);
33 count_hits($conn, $dbname, "the", 109);
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);
39 # Trying to create the same database again will fail EEXIST
40 makedb($conn, $dbname, 224);
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);
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);
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);
54 # Now drop the newly-created database
55 dropdb($conn, $dbname, 0);
57 # A second dropping should fail, as the database is no longer there.
58 dropdb($conn, $dbname, 235);
62 my($host, $user, $password, $expected_error) = @_;
64 my $options = new ZOOM::Options();
65 $options->option(user => $user)
67 $options->option(password => $password)
71 eval { $conn = create ZOOM::Connection($options) };
72 ok(!$@, "unconnected connection object created");
74 eval { $conn->connect($host, 0) };
75 my($errcode, $errmsg, $addinfo) = maybe_error($@);
77 ok($errcode == $expected_error,
78 "connection to '$host'" . ($errcode ? " refused ($errcode)" : ""));
85 my($conn, $dbname, $expected_error) = @_;
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");
91 $p->option(databaseName => $dbname);
92 my $val = $p->option("databaseName");
93 ok($val eq $dbname, "package option retrieved as expected");
95 eval { $p->send("create") };
96 my($errcode, $errmsg, $addinfo) = maybe_error($@);
97 ok($errcode == $expected_error, "database creation '$dbname'" .
98 ($errcode ? " refused ($errcode)" : ""));
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.
109 ok(1, "destroyed createdb package");
114 my($conn, $dbname, $expected_error) = @_;
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" : ""));
125 ok(1, "destroyed dropdb package");
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.
134 my($conn, $id, $file, $expected_error) = @_;
136 my $p = $conn->package();
137 $p->option(action => "specialUpdate");
138 $p->option(recordIdOpaque => $id);
139 $p->option(record => $file);
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)" : ""));
147 ok(1, "destroyed update package");
152 my($conn, $dbname, $query, $expected_error, $expected_count) = @_;
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)"));
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)");
171 my $f = new IO::File("<$filename")
172 or die "can't open file '$filename': $!";
173 my $text = join("", <$f>);
180 # Return the elements of an exception as separate scalars
184 if ($x && $x->isa("ZOOM::Exception")) {
189 return (0, undef, undef);
194 # To investigate the set of databases created, use Explain Classic:
196 # $ yaz-client -u admin/fish test.indexdata.com:2118/IR-Explain-1
197 # Z> find @attr exp1 1=1 databaseinfo
201 # It seems that Explain still knows about dropped databases.