4 # Before `make install' is performed this script should be runnable with
5 # `make test'. After `make install' it should work as `perl test.pl'
7 #########################
10 # Number of test cases to run - increase this if you add more testcases.
13 use Search
::Xapian
qw(:standard);
15 my $db_dir = 'testdb-writabledatabase';
17 # Delete contents of database dir, if it exists.
18 if (opendir( DB_DIR
, $db_dir )) {
19 while( defined( my $file = readdir( DB_DIR
) ) ) {
20 next if $file =~ /^\.+$/;
21 unlink( "$db_dir/$file" ) or die "Could not delete '$db_dir/$file': $!";
26 my $write = Search
::Xapian
::WritableDatabase
->new( $db_dir, Search
::Xapian
::DB_CREATE
);
28 # Let's try to index something.
31 for my $num (1..1000) {
32 my $doc = Search
::Xapian
::Document
->new();
34 $doc->set_data( "$term $num" );
36 $doc->add_posting( $term, 0 );
37 $doc->add_posting( $num, 1 );
39 $doc->add_value(0, $num);
40 $write->add_document( $doc );
43 for my $num (qw(three four five)) {
44 my $doc = Search
::Xapian
::Document
->new();
46 $doc->set_data( "$term $num" );
48 $doc->add_posting( $term, 0 );
49 $doc->add_posting( $num, 1 );
51 $doc->add_value(0, $num);
52 $write->add_document( $doc );
56 my $doccount = $write->get_doccount();
57 is
($doccount, 1003, "check number of documents in WritableDatabase");
59 # replace document by docid
60 my $repdoc = Search
::Xapian
::Document
->new();
64 $repdoc->set_data( "$term $num" );
65 $repdoc->add_posting( $term, 0 );
66 $repdoc->add_posting( $num, 1 );
67 $repdoc->add_value(0, $num);
69 ok
(!$write->term_exists($num), "check term exists");
70 is
($write->get_document($docid)->get_data(), "$term $docid", "check document data");
72 $write->replace_document($docid, $repdoc);
77 ok
($write->term_exists($num), "check term exists");
78 is
($write->get_document($docid)->get_data(), "$term $num", "check document data");
80 is
($write->get_collection_freq($term), 1003, "check term frequency");
81 is
($write->get_avlength(), 2, "check term frequency");
83 # replace document by term
84 $repdoc = Search
::Xapian
::Document
->new();
87 $repdoc->set_data( "$term $num" );
88 $repdoc->add_posting( $term, 0 );
89 $repdoc->add_posting( $num, 1 );
90 $repdoc->add_value(0, $num);
93 ok
(!$write->term_exists($num), "check term exists");
94 ok
($write->term_exists($repterm), "check term exists");
95 is
($write->get_termfreq($num), 0, "check term frequency");
96 is
($write->get_termfreq($repterm), 1, "check term frequency");
98 $write->replace_document_by_term($repterm, $repdoc);
101 ok
($write->term_exists($num), "check term exists");
102 ok
(!$write->term_exists($repterm), "check term exists");
103 is
($write->get_termfreq($num), 1, "check term frequency");
104 is
($write->get_termfreq($repterm), 0, "check term frequency");
106 # replace document by term, if term is new
107 $repdoc = Search
::Xapian
::Document
->new();
110 $repdoc->set_data( "$term $num" );
111 $repdoc->add_posting( $term, 0 );
112 $repdoc->add_posting( $num, 1 );
113 $repdoc->add_value(0, $num);
115 is
($write->get_termfreq($term), $doccount, "check term frequency");
116 is
($write->get_termfreq($num), 0, "check term frequency");
118 $write->replace_document_by_term($num, $repdoc);
121 $doccount = $write->get_doccount();
122 is
($doccount, 1004, "check doccount");
123 is
($write->get_termfreq($term), $doccount, "check term frequency");
124 is
($write->get_termfreq($num), 1, "check term frequency");
126 # replace document by term.
127 # all documents indexed with the term are replaced; the replacement uses the
128 # lowest docid if multiple documents are indexed by the term.
129 $repdoc = Search
::Xapian
::Document
->new();
132 $repdoc->set_data( "$term $num" );
133 $repdoc->add_posting( $term, 0 );
134 $repdoc->add_posting( $num, 1 );
135 $repdoc->add_value(0, $num);
137 $write->replace_document_by_term($term, $repdoc);
139 my $doc = $write->get_document(1);
141 is
($write->get_doccount(), 1, "check document count");
142 is
($doc->get_data(), "$term $num", "check document data");
144 # add documents for following tests
145 for my $num (qw(one two three four five)) {
146 my $doc = Search
::Xapian
::Document
->new();
148 $doc->set_data( "$term $num" );
150 $doc->add_posting( $term, 0 );
151 $doc->add_posting( $num, 1 );
153 $doc->add_value(0, $num);
154 $write->add_document( $doc );
158 $doccount = $write->get_doccount();
159 is
($doccount, 6, "check number of documents in WritableDatabase");
161 # delete document by docid
162 my $lastdocid = $write->get_lastdocid();
163 my $lastdocterm = $write->get_document($lastdocid)->get_value(0);
164 ok
($write->term_exists($lastdocterm), "check term exists");
166 $write->delete_document($lastdocid);
169 is
($write->get_doccount(), $doccount - 1, "check number of documents in WritableDatabase");
170 ok
(!$write->term_exists($lastdocterm), "check term exists");
172 # delete document by term
173 my $delterm = 'three';
174 ok
($write->term_exists($delterm), 'check term exists before deleting a document');
175 is
($write->get_termfreq($delterm), 1, 'check term frequency before deleting a document');
177 $write->delete_document_by_term($delterm);
180 is
($write->get_doccount(), $doccount - 2, 'check WritableDatabase after deleting a document');
181 ok
(!$write->term_exists($delterm), 'check term exists after deleting a document');
182 is
($write->get_termfreq($delterm), 0, 'check term frequency after deleting a document');
184 # delete documents by term
186 ok
($write->term_exists($delterm), 'check term exists of documents which has term "test"');
187 is
($write->get_termfreq($delterm), $doccount - 2, 'check term frequency of term "test"');
189 $write->delete_document_by_term($delterm);
192 is
($write->get_doccount(), 0, 'check WritableDatabase after deleting all documents');
193 ok
(!$write->term_exists($delterm), 'check term exists after deleting all documents');
194 is
($write->get_termfreq($delterm), 0, 'check term frequency after deleting all documents');
197 # Should fail because the database is already open for writing.
198 Search
::Xapian
::WritableDatabase
->new( $db_dir, Search
::Xapian
::DB_CREATE_OR_OPEN
);
204 # Should fail because the database has been closed.
205 $write->add_document(Search
::Xapian
::Document
->new());
210 ok
( Search
::Xapian
::WritableDatabase
->new( $db_dir, Search
::Xapian
::DB_CREATE_OR_OPEN
) );
212 # And reference counting should have closed it.
213 ok
( Search
::Xapian
::WritableDatabase
->new( $db_dir, Search
::Xapian
::DB_CREATE_OR_OPEN
) );
215 my $read = Search
::Xapian
::Database
->new( $db_dir );
219 # Should fail because the database has been closed.
220 $write->allterms_begin();