Complete rewrite as OO version of 16-packages.t
[ZOOM-Perl-moved-to-github.git] / t / 26-packages.t
1 # $Id: 26-packages.t,v 1.2 2005-12-09 10:33: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 # 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 = "localhost:9999";
16 #my $host = "indexdata.com/gils";
17 my $dbname = join("", map { chr(ord("a") + int(rand(26))) } 1..10);
18
19 # Connect anonymously, and expect this to fail
20 my $conn = makeconn($host, undef, undef, 1011);
21
22 # Connect as a user, but with incorrect password -- expect failure
23 $conn->destroy();
24 $conn = makeconn($host, "user", "badpw", 1011);
25
26 # Connect as a non-privileged user with correct password
27 $conn->destroy();
28 $conn = makeconn($host, "user", "frog", 0);
29
30 # Non-privileged user can't create database
31 makedb($conn, $dbname, 223);
32
33 # Connect as a privileged user with correct password, check DB is absent
34 $conn->destroy();
35 $conn = makeconn($host, "admin", "fish", 0);
36 $conn->option(databaseName => $dbname);
37 count_hits($conn, "the", 109);
38
39 # Now create the database and check that it is present but empty
40 makedb($conn, $dbname, 0);
41 count_hits($conn, "the", 0, 0);
42
43 # Trying to create the same database again will fail EEXIST
44 makedb($conn, $dbname, 224);
45
46 # Add a single record, and check that it can be found
47 updaterec($conn, 1, content_of("samples/records/esdd0006.grs"), 0);
48 count_hits($conn, "the", 0, 1);
49
50 # Add the same record with the same ID: overwrite => no change
51 updaterec($conn, 1, content_of("samples/records/esdd0006.grs"), 0);
52 count_hits($conn, "the", 0, 1);
53
54 # Add it again record with different ID => new copy added
55 updaterec($conn, 2, content_of("samples/records/esdd0006.grs"), 0);
56 count_hits($conn, "the", 0, 2);
57
58 # Now drop the newly-created database
59 dropdb($conn, $dbname, 0);
60
61 # A second dropping should fail, but does not do so -- I think that
62 # "drop" is an always-"successful" no-op.  Yuck.
63 dropdb($conn, $dbname, 0);
64
65
66 sub makeconn {
67     my($host, $user, $password, $expected_error) = @_;
68
69     my $options = new ZOOM::Options();
70     $options->option(user => $user)
71         if defined $user;
72     $options->option(password => $password)
73         if defined $password;
74
75     my $conn;
76     eval { $conn = create ZOOM::Connection($options) };
77     ok(!$@, "unconnected connection object created");
78
79     eval { $conn->connect($host, 0) };
80     my($errcode, $errmsg, $addinfo) = maybe_error($@);
81
82     ok($errcode == $expected_error,
83        "connection to '$host'" . ($errcode ? " refused ($errcode)" : ""));
84
85     return $conn;
86 }
87
88
89 sub makedb {
90     my($conn, $dbname, $expected_error) = @_;
91
92     my $p = $conn->package();
93     # Inspection of the ZOOM-C code shows that this can never fail, in fact.
94     ok(defined $p, "created package");
95
96     $p->option(databaseName => $dbname);
97     my $val = $p->option("databaseName");
98     ok($val eq $dbname, "package option retrieved as expected");
99
100     eval { $p->send("create") };
101     my($errcode, $errmsg, $addinfo) = maybe_error($@);
102     ok($errcode == $expected_error, "database creation '$dbname'" .
103        ($errcode ? " refused ($errcode)" : ""));
104
105     # Now we can inspect the package options to find out more about
106     # how the server dealt with the request.  However, it seems that
107     # the "package database" described in the standard is not used,
108     # and that the only options we can inspect are the following:
109     $val = $p->option("targetReference");
110     $val = $p->option("xmlUpdateDoc");
111     # ... and we know nothing about expected or actual values.
112
113     $p->destroy();
114     ok(1, "destroyed createdb package");
115 }
116
117
118 sub dropdb {
119     my($conn, $dbname, $expected_error) = @_;
120
121     my $p = $conn->package();
122     # No need to keep ok()ing this, or checking the option-setting
123     $p->option(databaseName => $dbname);
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 }