1 # $Id: 26-packages.t,v 1.5 2005-12-13 17:36:11 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 # We will create, and destroy, a new database with a random name
14 my $host = "test.indexdata.com:8017";
15 my $dbname = join("", map { chr(ord("a") + int(rand(26))) } 1..10);
17 # Connect anonymously, and expect this to fail
18 my $conn = makeconn($host, undef, undef, 1011);
20 # Connect as a user, but with incorrect password -- expect failure
22 $conn = makeconn($host, "user", "badpw", 1011);
24 # Connect as a non-privileged user with correct password
26 $conn = makeconn($host, "user", "frog", 0);
28 # Non-privileged user can't create database
29 makedb($conn, $dbname, 223);
31 # Connect as a privileged user with correct password, check DB is absent
33 $conn = makeconn($host, "admin", "fish", 0);
34 $conn->option(databaseName => $dbname);
35 count_hits($conn, "the", 109);
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);
41 # Trying to create the same database again will fail EEXIST
42 makedb($conn, $dbname, 224);
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);
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);
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);
56 # Now drop the newly-created database
57 dropdb($conn, $dbname, 0);
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);
65 my($host, $user, $password, $expected_error) = @_;
67 my $options = new ZOOM::Options();
68 $options->option(user => $user)
70 $options->option(password => $password)
74 eval { $conn = create ZOOM::Connection($options) };
75 ok(!$@, "unconnected connection object created");
77 eval { $conn->connect($host, 0) };
78 my($errcode, $errmsg, $addinfo) = maybe_error($@);
80 ok($errcode == $expected_error,
81 "connection to '$host'" . ($errcode ? " refused ($errcode)" : ""));
88 my($conn, $dbname, $expected_error) = @_;
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");
94 $p->option(databaseName => $dbname);
95 my $val = $p->option("databaseName");
96 ok($val eq $dbname, "package option retrieved as expected");
98 eval { $p->send("create") };
99 my($errcode, $errmsg, $addinfo) = maybe_error($@);
100 ok($errcode == $expected_error, "database creation '$dbname'" .
101 ($errcode ? " refused ($errcode)" : ""));
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.
112 ok(1, "destroyed createdb package");
117 my($conn, $dbname, $expected_error) = @_;
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
124 my($errcode, $errmsg, $addinfo) = maybe_error($@);
125 ok($errcode == $expected_error,
126 "database drop '$dbname'" . ($errcode ? " refused $errcode" : ""));
129 ok(1, "destroyed dropdb package");
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.
138 my($conn, $id, $file, $expected_error) = @_;
140 my $p = $conn->package();
141 $p->option(action => "specialUpdate");
142 $p->option(recordIdOpaque => $id);
143 $p->option(record => $file);
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)" : ""));
151 ok(1, "destroyed update package");
156 my($conn, $query, $expected_error, $expected_count) = @_;
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)"));
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)");
175 my $f = new IO::File("<$filename")
176 or die "can't open file '$filename': $!";
177 my $text = join("", <$f>);
184 # Return the elements of an exception as separate scalars
188 if ($x && $x->isa("ZOOM::Exception")) {
193 return (0, undef, undef);