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