2 # Before 'make install' is performed this script should be runnable with
3 # 'make test'. After 'make install' it should work as 'perl test.pl'
5 #########################
9 BEGIN {$SIG{__WARN__} = sub { die "Terminating test due to warning: $_[0]" } };
12 # Number of test cases to run - increase this if you add more testcases.
15 use Xapian qw(:standard);
17 my $db_dir = 'testdb-writabledatabase';
19 # Delete contents of database dir, if it exists.
20 if (opendir( DB_DIR, $db_dir )) {
21 while( defined( my $file = readdir( DB_DIR ) ) ) {
22 next if $file =~ /^\.+$/;
23 unlink( "$db_dir/$file" ) or die "Could not delete '$db_dir/$file': $!";
28 my $write = Xapian::WritableDatabase->new( $db_dir, Xapian::DB_CREATE );
30 # Let's try to index something.
33 for my $num (1..1000) {
34 my $doc = Xapian::Document->new();
36 $doc->set_data( "$term $num" );
38 $doc->add_posting( $term, 0 );
39 $doc->add_posting( "$num", 1 );
41 $doc->add_value(0, "$num");
42 $write->add_document( $doc );
45 for my $num (qw(three four five)) {
46 my $doc = Xapian::Document->new();
48 $doc->set_data( "$term $num" );
50 $doc->add_posting( $term, 0 );
51 $doc->add_posting( $num, 1 );
53 $doc->add_value(0, $num);
54 $write->add_document( $doc );
58 my $doccount = $write->get_doccount();
59 is($doccount, 1003, "check number of documents in WritableDatabase");
61 # replace document by docid
62 my $repdoc = Xapian::Document->new();
66 $repdoc->set_data( "$term $num" );
67 $repdoc->add_posting( $term, 0 );
68 $repdoc->add_posting( $num, 1 );
69 $repdoc->add_value(0, $num);
71 ok(!$write->term_exists($num), "check term exists");
72 is($write->get_document($docid)->get_data(), "$term $docid", "check document data");
74 $write->replace_document($docid, $repdoc);
79 ok($write->term_exists($num), "check term exists");
80 is($write->get_document($docid)->get_data(), "$term $num", "check document data");
82 is($write->get_collection_freq($term), 1003, "check term frequency");
83 is($write->get_avlength(), 2, "check term frequency");
85 # replace document by term
86 $repdoc = Xapian::Document->new();
89 $repdoc->set_data( "$term $num" );
90 $repdoc->add_posting( $term, 0 );
91 $repdoc->add_posting( $num, 1 );
92 $repdoc->add_value(0, $num);
95 ok(!$write->term_exists($num), "check term exists");
96 ok($write->term_exists($repterm), "check term exists");
97 is($write->get_termfreq($num), 0, "check term frequency");
98 is($write->get_termfreq($repterm), 1, "check term frequency");
100 $write->replace_document_by_term($repterm, $repdoc);
103 ok($write->term_exists($num), "check term exists");
104 ok(!$write->term_exists($repterm), "check term exists");
105 is($write->get_termfreq($num), 1, "check term frequency");
106 is($write->get_termfreq($repterm), 0, "check term frequency");
108 # replace document by term, if term is new
109 $repdoc = Xapian::Document->new();
112 $repdoc->set_data( "$term $num" );
113 $repdoc->add_posting( $term, 0 );
114 $repdoc->add_posting( $num, 1 );
115 $repdoc->add_value(0, $num);
117 is($write->get_termfreq($term), $doccount, "check term frequency");
118 is($write->get_termfreq($num), 0, "check term frequency");
120 $write->replace_document_by_term($num, $repdoc);
123 $doccount = $write->get_doccount();
124 is($doccount, 1004, "check doccount");
125 is($write->get_termfreq($term), $doccount, "check term frequency");
126 is($write->get_termfreq($num), 1, "check term frequency");
128 # replace document by term.
129 # all documents indexed with the term are replaced; the replacement uses the
130 # lowest docid if multiple documents are indexed by the term.
131 $repdoc = Xapian::Document->new();
134 $repdoc->set_data( "$term $num" );
135 $repdoc->add_posting( $term, 0 );
136 $repdoc->add_posting( $num, 1 );
137 $repdoc->add_value(0, $num);
139 $write->replace_document_by_term($term, $repdoc);
141 my $doc = $write->get_document(1);
143 is($write->get_doccount(), 1, "check document count");
144 is($doc->get_data(), "$term $num", "check document data");
146 # add documents for following tests
147 for my $num (qw(one two three four five)) {
148 my $doc = Xapian::Document->new();
150 $doc->set_data( "$term $num" );
152 $doc->add_posting( $term, 0 );
153 $doc->add_posting( $num, 1 );
155 $doc->add_value(0, $num);
156 $write->add_document( $doc );
160 $doccount = $write->get_doccount();
161 is($doccount, 6, "check number of documents in WritableDatabase");
163 # delete document by docid
164 my $lastdocid = $write->get_lastdocid();
165 my $lastdocterm = $write->get_document($lastdocid)->get_value(0);
166 ok($write->term_exists($lastdocterm), "check term exists");
168 $write->delete_document($lastdocid);
171 is($write->get_doccount(), $doccount - 1, "check number of documents in WritableDatabase");
172 ok(!$write->term_exists($lastdocterm), "check term exists");
174 # delete document by term
175 my $delterm = 'three';
176 ok($write->term_exists($delterm), 'check term exists before deleting a document');
177 is($write->get_termfreq($delterm), 1, 'check term frequency before deleting a document');
179 $write->delete_document_by_term($delterm);
182 is($write->get_doccount(), $doccount - 2, 'check WritableDatabase after deleting a document');
183 ok(!$write->term_exists($delterm), 'check term exists after deleting a document');
184 is($write->get_termfreq($delterm), 0, 'check term frequency after deleting a document');
186 # delete documents by term
188 ok($write->term_exists($delterm), 'check term exists of documents which has term "test"');
189 is($write->get_termfreq($delterm), $doccount - 2, 'check term frequency of term "test"');
191 $write->delete_document_by_term($delterm);
194 is($write->get_doccount(), 0, 'check WritableDatabase after deleting all documents');
195 ok(!$write->term_exists($delterm), 'check term exists after deleting all documents');
196 is($write->get_termfreq($delterm), 0, 'check term frequency after deleting all documents');
199 # Should fail because the database is already open for writing.
200 Xapian::WritableDatabase->new( $db_dir, Xapian::DB_CREATE_OR_OPEN );
206 # Should fail because the database has been closed.
207 $write->add_document(Xapian::Document->new());
212 ok( Xapian::WritableDatabase->new( $db_dir, Xapian::DB_CREATE_OR_OPEN ) );
214 # And reference counting should have closed it.
215 ok( Xapian::WritableDatabase->new( $db_dir, Xapian::DB_CREATE_OR_OPEN ) );
217 my $read = Xapian::Database->new( $db_dir );
221 # Should fail because the database has been closed.
222 $write->allterms_begin();