Comment.
[ZOOM-Perl-moved-to-github.git] / t / 26-packages.t
1 # $Id: 26-packages.t,v 1.6 2005-12-14 09:30:48 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 # We will create, and destroy, a new database with a random name
14 my $host = "test.indexdata.com:2118";
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, "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, "the", 0, 0);
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, "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, "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, "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 ### But at present, it's "always successful" (though not really)
61 dropdb($conn, $dbname, 0);
62
63
64 sub makeconn {
65     my($host, $user, $password, $expected_error) = @_;
66
67     my $options = new ZOOM::Options();
68     $options->option(user => $user)
69         if defined $user;
70     $options->option(password => $password)
71         if defined $password;
72
73     my $conn;
74     eval { $conn = create ZOOM::Connection($options) };
75     ok(!$@, "unconnected connection object created");
76
77     eval { $conn->connect($host, 0) };
78     my($errcode, $errmsg, $addinfo) = maybe_error($@);
79
80     ok($errcode == $expected_error,
81        "connection to '$host'" . ($errcode ? " refused ($errcode)" : ""));
82
83     return $conn;
84 }
85
86
87 sub makedb {
88     my($conn, $dbname, $expected_error) = @_;
89
90     my $p = $conn->package();
91     # Inspection of the ZOOM-C code shows that this can never fail, in fact.
92     ok(defined $p, "created package");
93
94     $p->option(databaseName => $dbname);
95     my $val = $p->option("databaseName");
96     ok($val eq $dbname, "package option retrieved as expected");
97
98     eval { $p->send("create") };
99     my($errcode, $errmsg, $addinfo) = maybe_error($@);
100     ok($errcode == $expected_error, "database creation '$dbname'" .
101        ($errcode ? " refused ($errcode)" : ""));
102
103     # Now we can inspect the package options to find out more about
104     # how the server dealt with the request.  However, it seems that
105     # the "package database" described in the standard is not used,
106     # and that the only options we can inspect are the following:
107     $val = $p->option("targetReference");
108     $val = $p->option("xmlUpdateDoc");
109     # ... and we know nothing about expected or actual values.
110
111     $p->destroy();
112     ok(1, "destroyed createdb package");
113 }
114
115
116 sub dropdb {
117     my($conn, $dbname, $expected_error) = @_;
118
119     my $p = $conn->package();
120     # No need to keep ok()ing this, or checking the option-setting
121     $p->option(databaseName => $dbname);
122     ### Don't send the package at the moment -- it corrupts Zebra
123     #$p->send("drop");
124     my($errcode, $errmsg, $addinfo) = maybe_error($@);
125     ok($errcode == $expected_error,
126        "database drop '$dbname'"  . ($errcode ? " refused $errcode" : ""));
127
128     $p->destroy();
129     ok(1, "destroyed dropdb package");
130 }
131
132
133 # We always use "specialUpdate", which adds a record or replaces it if
134 # it's already there.  By contrast, "insert" fails if the record
135 # already exists, and "replace" fails if it does not.
136 #
137 sub updaterec {
138     my($conn, $id, $file, $expected_error) = @_;
139
140     my $p = $conn->package();
141     $p->option(action => "specialUpdate");
142     $p->option(recordIdOpaque => $id);
143     $p->option(record => $file);
144
145     eval { $p->send("update") };
146     my($errcode, $errmsg, $addinfo) = maybe_error($@);
147     ok($errcode == $expected_error, "record update $id" .
148        ($errcode ? " failed $errcode '$errmsg' ($addinfo)" : ""));
149
150     $p->destroy();
151     ok(1, "destroyed update package");
152 }
153
154
155 sub count_hits {
156     my($conn, $query, $expected_error, $expected_count) = @_;
157
158     my $rs;
159     eval { $rs = $conn->search_pqf($query) };
160     my($errcode, $errmsg, $addinfo) = maybe_error($@);
161     ok($errcode == $expected_error, "database '$dbname' " .
162        ($errcode == 0 ? "can be searched" : "not searchable ($errcode)"));
163
164     return if $errcode != 0;
165     my $n = $rs->size($rs);
166     ok($n == $expected_count,
167        "database '$dbname' has $n records (expected $expected_count)");
168 }
169
170
171 sub content_of {
172     my($filename) = @_;
173
174     use IO::File;
175     my $f = new IO::File("<$filename")
176         or die "can't open file '$filename': $!";
177     my $text = join("", <$f>);
178     $f->close();
179
180     return $text;
181 }
182
183
184 # Return the elements of an exception as separate scalars
185 sub maybe_error {
186     my ($x) = @_;
187
188     if ($x && $x->isa("ZOOM::Exception")) {
189         return ($x->code(),
190                 $x->message(),
191                 $x->addinfo());
192     } else {
193         return (0, undef, undef);
194     }
195 }