1 # $Id: 26-packages.t,v 1.2 2005-12-09 10:33:48 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, but does not do so -- I think that
62 # "drop" is an always-"successful" no-op. Yuck.
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);
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);