1 # $Id: 26-packages.t,v 1.7 2006-04-12 12:29:49 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:2118";
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 dropdb($conn, $dbname, 10004);
64 my($host, $user, $password, $expected_error) = @_;
66 my $options = new ZOOM::Options();
67 $options->option(user => $user)
69 $options->option(password => $password)
73 eval { $conn = create ZOOM::Connection($options) };
74 ok(!$@, "unconnected connection object created");
76 eval { $conn->connect($host, 0) };
77 my($errcode, $errmsg, $addinfo) = maybe_error($@);
79 ok($errcode == $expected_error,
80 "connection to '$host'" . ($errcode ? " refused ($errcode)" : ""));
87 my($conn, $dbname, $expected_error) = @_;
89 my $p = $conn->package();
90 # Inspection of the ZOOM-C code shows that this can never fail, in fact.
91 ok(defined $p, "created package");
93 $p->option(databaseName => $dbname);
94 my $val = $p->option("databaseName");
95 ok($val eq $dbname, "package option retrieved as expected");
97 eval { $p->send("create") };
98 my($errcode, $errmsg, $addinfo) = maybe_error($@);
99 ok($errcode == $expected_error, "database creation '$dbname'" .
100 ($errcode ? " refused ($errcode)" : ""));
102 # Now we can inspect the package options to find out more about
103 # how the server dealt with the request. However, it seems that
104 # the "package database" described in the standard is not used,
105 # and that the only options we can inspect are the following:
106 $val = $p->option("targetReference");
107 $val = $p->option("xmlUpdateDoc");
108 # ... and we know nothing about expected or actual values.
111 ok(1, "destroyed createdb package");
116 my($conn, $dbname, $expected_error) = @_;
118 my $p = $conn->package();
119 # No need to keep ok()ing this, or checking the option-setting
120 $p->option(databaseName => $dbname);
121 eval { $p->send("drop") };
122 my($errcode, $errmsg, $addinfo) = maybe_error($@);
123 ok($errcode == $expected_error,
124 "database drop '$dbname'" . ($errcode ? " refused $errcode" : ""));
127 ok(1, "destroyed dropdb package");
131 # We always use "specialUpdate", which adds a record or replaces it if
132 # it's already there. By contrast, "insert" fails if the record
133 # already exists, and "replace" fails if it does not.
136 my($conn, $id, $file, $expected_error) = @_;
138 my $p = $conn->package();
139 $p->option(action => "specialUpdate");
140 $p->option(recordIdOpaque => $id);
141 $p->option(record => $file);
143 eval { $p->send("update") };
144 my($errcode, $errmsg, $addinfo) = maybe_error($@);
145 ok($errcode == $expected_error, "record update $id" .
146 ($errcode ? " failed $errcode '$errmsg' ($addinfo)" : ""));
149 ok(1, "destroyed update package");
154 my($conn, $query, $expected_error, $expected_count) = @_;
157 eval { $rs = $conn->search_pqf($query) };
158 my($errcode, $errmsg, $addinfo) = maybe_error($@);
159 ok($errcode == $expected_error, "database '$dbname' " .
160 ($errcode == 0 ? "can be searched" : "not searchable ($errcode)"));
162 return if $errcode != 0;
163 my $n = $rs->size($rs);
164 ok($n == $expected_count,
165 "database '$dbname' has $n records (expected $expected_count)");
173 my $f = new IO::File("<$filename")
174 or die "can't open file '$filename': $!";
175 my $text = join("", <$f>);
182 # Return the elements of an exception as separate scalars
186 if ($x && $x->isa("ZOOM::Exception")) {
191 return (0, undef, undef);