1 # $Id: 26-packages.t,v 1.4 2005-12-13 17:35:20 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 = "test.indexdata.com:8017";
16 my $dbname = join("", map { chr(ord("a") + int(rand(26))) } 1..10);
18 # Connect anonymously, and expect this to fail
19 my $conn = makeconn($host, undef, undef, 1011);
21 # Connect as a user, but with incorrect password -- expect failure
23 $conn = makeconn($host, "user", "badpw", 1011);
25 # Connect as a non-privileged user with correct password
27 $conn = makeconn($host, "user", "frog", 0);
29 # Non-privileged user can't create database
30 makedb($conn, $dbname, 223);
32 # Connect as a privileged user with correct password, check DB is absent
34 $conn = makeconn($host, "admin", "fish", 0);
35 $conn->option(databaseName => $dbname);
36 count_hits($conn, "the", 109);
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);
42 # Trying to create the same database again will fail EEXIST
43 makedb($conn, $dbname, 224);
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);
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);
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);
57 # Now drop the newly-created database
58 dropdb($conn, $dbname, 0);
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);
66 my($host, $user, $password, $expected_error) = @_;
68 my $options = new ZOOM::Options();
69 $options->option(user => $user)
71 $options->option(password => $password)
75 eval { $conn = create ZOOM::Connection($options) };
76 ok(!$@, "unconnected connection object created");
78 eval { $conn->connect($host, 0) };
79 my($errcode, $errmsg, $addinfo) = maybe_error($@);
81 ok($errcode == $expected_error,
82 "connection to '$host'" . ($errcode ? " refused ($errcode)" : ""));
89 my($conn, $dbname, $expected_error) = @_;
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");
95 $p->option(databaseName => $dbname);
96 my $val = $p->option("databaseName");
97 ok($val eq $dbname, "package option retrieved as expected");
99 eval { $p->send("create") };
100 my($errcode, $errmsg, $addinfo) = maybe_error($@);
101 ok($errcode == $expected_error, "database creation '$dbname'" .
102 ($errcode ? " refused ($errcode)" : ""));
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.
113 ok(1, "destroyed createdb package");
118 my($conn, $dbname, $expected_error) = @_;
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
125 my($errcode, $errmsg, $addinfo) = maybe_error($@);
126 ok($errcode == $expected_error,
127 "database drop '$dbname'" . ($errcode ? " refused $errcode" : ""));
130 ok(1, "destroyed dropdb package");
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.
139 my($conn, $id, $file, $expected_error) = @_;
141 my $p = $conn->package();
142 $p->option(action => "specialUpdate");
143 $p->option(recordIdOpaque => $id);
144 $p->option(record => $file);
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)" : ""));
152 ok(1, "destroyed update package");
157 my($conn, $query, $expected_error, $expected_count) = @_;
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)"));
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)");
176 my $f = new IO::File("<$filename")
177 or die "can't open file '$filename': $!";
178 my $text = join("", <$f>);
185 # Return the elements of an exception as separate scalars
189 if ($x && $x->isa("ZOOM::Exception")) {
194 return (0, undef, undef);