1 # $Id: 26-packages.t,v 1.3 2005-12-13 13:23:45 mike Exp $
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'
8 use Test::More tests => 40;
10 BEGIN { use_ok('ZOOM') };
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);
19 # Connect anonymously, and expect this to fail
20 my $conn = makeconn($host, undef, undef, 1011);
22 # Connect as a user, but with incorrect password -- expect failure
24 $conn = makeconn($host, "user", "badpw", 1011);
26 # Connect as a non-privileged user with correct password
28 $conn = makeconn($host, "user", "frog", 0);
30 # Non-privileged user can't create database
31 makedb($conn, $dbname, 223);
33 # Connect as a privileged user with correct password, check DB is absent
35 $conn = makeconn($host, "admin", "fish", 0);
36 $conn->option(databaseName => $dbname);
37 count_hits($conn, "the", 109);
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);
43 # Trying to create the same database again will fail EEXIST
44 makedb($conn, $dbname, 224);
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);
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);
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);
58 # Now drop the newly-created database
59 dropdb($conn, $dbname, 0);
61 # A second dropping should fail, as the database is no longer there.
62 ### But at present, it's "always successful" (though not really)
63 dropdb($conn, $dbname, 0);
67 my($host, $user, $password, $expected_error) = @_;
69 my $options = new ZOOM::Options();
70 $options->option(user => $user)
72 $options->option(password => $password)
76 eval { $conn = create ZOOM::Connection($options) };
77 ok(!$@, "unconnected connection object created");
79 eval { $conn->connect($host, 0) };
80 my($errcode, $errmsg, $addinfo) = maybe_error($@);
82 ok($errcode == $expected_error,
83 "connection to '$host'" . ($errcode ? " refused ($errcode)" : ""));
90 my($conn, $dbname, $expected_error) = @_;
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");
96 $p->option(databaseName => $dbname);
97 my $val = $p->option("databaseName");
98 ok($val eq $dbname, "package option retrieved as expected");
100 eval { $p->send("create") };
101 my($errcode, $errmsg, $addinfo) = maybe_error($@);
102 ok($errcode == $expected_error, "database creation '$dbname'" .
103 ($errcode ? " refused ($errcode)" : ""));
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.
114 ok(1, "destroyed createdb package");
119 my($conn, $dbname, $expected_error) = @_;
121 my $p = $conn->package();
122 # No need to keep ok()ing this, or checking the option-setting
123 $p->option(databaseName => $dbname);
124 ### Don't send the package at the moment -- it corrupts Zebra
126 my($errcode, $errmsg, $addinfo) = maybe_error($@);
127 ok($errcode == $expected_error,
128 "database drop '$dbname'" . ($errcode ? " refused $errcode" : ""));
131 ok(1, "destroyed dropdb package");
135 # We always use "specialUpdate", which adds a record or replaces it if
136 # it's already there. By contrast, "insert" fails if the record
137 # already exists, and "replace" fails if it does not.
140 my($conn, $id, $file, $expected_error) = @_;
142 my $p = $conn->package();
143 $p->option(action => "specialUpdate");
144 $p->option(recordIdOpaque => $id);
145 $p->option(record => $file);
147 eval { $p->send("update") };
148 my($errcode, $errmsg, $addinfo) = maybe_error($@);
149 ok($errcode == $expected_error, "record update $id" .
150 ($errcode ? " failed $errcode '$errmsg' ($addinfo)" : ""));
153 ok(1, "destroyed update package");
158 my($conn, $query, $expected_error, $expected_count) = @_;
161 eval { $rs = $conn->search_pqf($query) };
162 my($errcode, $errmsg, $addinfo) = maybe_error($@);
163 ok($errcode == $expected_error, "database '$dbname' " .
164 ($errcode == 0 ? "can be searched" : "not searchable ($errcode)"));
166 return if $errcode != 0;
167 my $n = $rs->size($rs);
168 ok($n == $expected_count,
169 "database '$dbname' has $n records (expected $expected_count)");
177 my $f = new IO::File("<$filename")
178 or die "can't open file '$filename': $!";
179 my $text = join("", <$f>);
186 # Return the elements of an exception as separate scalars
190 if ($x && $x->isa("ZOOM::Exception")) {
195 return (0, undef, undef);