[omega] Compute date spans in days
[xapian.git] / xapian-bindings / perl / t / writabledatabase.t
blob8bbbf3c2c0e6c47282939ba39b6ecc3f52d51998
1 use strict;
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 #########################
7 # Make warnings fatal
8 use warnings;
9 BEGIN {$SIG{__WARN__} = sub { die "Terminating test due to warning: $_[0]" } };
11 use Test::More;
12 # Number of test cases to run - increase this if you add more testcases.
13 plan tests => 42;
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': $!";
24   }
25   closedir( DB_DIR );
28 my $write = Xapian::WritableDatabase->new( $db_dir, Xapian::DB_CREATE );
30 # Let's try to index something.
31 my $term = 'test';
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 );
56 $write->commit();
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();
63 my $num = "six";
64 $term = "test";
65 my $docid = 500;
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);
75 $write->commit();
77 $write->keep_alive();
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();
87 $term = "test";
88 $num = "seven";
89 $repdoc->set_data( "$term $num" );
90 $repdoc->add_posting( $term, 0 );
91 $repdoc->add_posting( $num, 1 );
92 $repdoc->add_value(0, $num);
93 my $repterm = "five";
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);
101 $write->commit();
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();
110 $term = "test";
111 $num = "eight";
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);
121 $write->commit();
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();
132 $term = "test";
133 $num = "nine";
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);
140 $write->commit();
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 );
158 $write->commit();
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);
169 $write->commit();
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);
180 $write->commit();
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
187 $delterm = 'test';
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);
192 $write->commit();
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');
198 eval {
199   # Should fail because the database is already open for writing.
200   Xapian::WritableDatabase->new( $db_dir, Xapian::DB_CREATE_OR_OPEN );
202 ok( $@ );
204 $write->close();
205 eval {
206   # Should fail because the database has been closed.
207   $write->add_document(Xapian::Document->new());
209 ok( $@ );
211 # Should work now.
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 );
218 ok( $@ );
219 $read->close();
220 eval {
221   # Should fail because the database has been closed.
222   $write->allterms_begin();
224 ok( $@ );