1 | # Simple test that we can load the xapian module and run a simple test
|
---|
2 | #
|
---|
3 | # Copyright (C) 2004,2006 Olly Betts
|
---|
4 | #
|
---|
5 | # This program is free software; you can redistribute it and/or
|
---|
6 | # modify it under the terms of the GNU General Public License as
|
---|
7 | # published by the Free Software Foundation; either version 2 of the
|
---|
8 | # License, or (at your option) any later version.
|
---|
9 | #
|
---|
10 | # This program is distributed in the hope that it will be useful,
|
---|
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | # GNU General Public License for more details.
|
---|
14 | #
|
---|
15 | # You should have received a copy of the GNU General Public License
|
---|
16 | # along with this program; if not, write to the Free Software
|
---|
17 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
|
---|
18 | # USA
|
---|
19 |
|
---|
20 | # We need at least Tcl version 8
|
---|
21 | package require Tcl 8
|
---|
22 |
|
---|
23 | load [file join ".libs" xapian.so]
|
---|
24 |
|
---|
25 | # Test the version number reporting functions give plausible results.
|
---|
26 | set v [format {%d.%d.%d} [xapian::major_version] \
|
---|
27 | [xapian::minor_version] \
|
---|
28 | [xapian::revision]]
|
---|
29 | set v2 [xapian::version_string]
|
---|
30 | if { $v != $v2 } {
|
---|
31 | puts stderr "Unexpected version output ($v != $v2)"
|
---|
32 | exit 1
|
---|
33 | }
|
---|
34 |
|
---|
35 | # Check if the xapian version matches the provided tcl package version
|
---|
36 | if {$v != [package provide xapian]} {
|
---|
37 | puts stderr "Unexpected package version output ($v != [package provide xapian])"
|
---|
38 | exit 1
|
---|
39 | }
|
---|
40 |
|
---|
41 | xapian::Stem stem "english"
|
---|
42 | if { [stem get_description] != "Xapian::Stem(english)" } {
|
---|
43 | puts stderr "Unexpected stem.get_description()"
|
---|
44 | exit 1
|
---|
45 | }
|
---|
46 |
|
---|
47 | xapian::Document doc
|
---|
48 | doc set_data "a\u0000b"
|
---|
49 | if { [doc get_data] == "a" } {
|
---|
50 | puts stderr "get_data+set_data truncates at a zero byte"
|
---|
51 | exit 1
|
---|
52 | }
|
---|
53 | if { [doc get_data] != "a\u0000b" } {
|
---|
54 | puts stderr "get_data+set_data doesn't transparently handle a zero byte"
|
---|
55 | exit 1
|
---|
56 | }
|
---|
57 | doc set_data "is there anybody out there?"
|
---|
58 | doc add_term "XYzzy"
|
---|
59 | doc add_posting [stem stem_word "is"] 1
|
---|
60 | doc add_posting [stem stem_word "there"] 2
|
---|
61 | doc add_posting [stem stem_word "anybody"] 3
|
---|
62 | doc add_posting [stem stem_word "out"] 4
|
---|
63 | doc add_posting [stem stem_word "there"] 5
|
---|
64 |
|
---|
65 | xapian::WritableDatabase db [xapian::inmemory_open]
|
---|
66 | db add_document doc
|
---|
67 | if { [db get_doccount] != 1 } {
|
---|
68 | puts stderr "Unexpected db.get_doccount()"
|
---|
69 | exit 1
|
---|
70 | }
|
---|
71 |
|
---|
72 | set terms [list "smoke" "test" "terms"]
|
---|
73 | xapian::Query query $xapian::Query_OP_OR $terms
|
---|
74 | if { [query get_description] != "Xapian::Query((smoke OR test OR terms))" } {
|
---|
75 | puts stderr "Unexpected query.get_description()"
|
---|
76 | exit 1
|
---|
77 | }
|
---|
78 | xapian::Query query1 $xapian::Query_OP_PHRASE [list "smoke" "test" "tuple"]
|
---|
79 | if { [query1 get_description] != "Xapian::Query((smoke PHRASE 3 test PHRASE 3 tuple))" } {
|
---|
80 | puts stderr "Unexpected query1.get_description()"
|
---|
81 | exit 1
|
---|
82 | }
|
---|
83 | xapian::Query smoke "smoke"
|
---|
84 | xapian::Query query2 $xapian::Query_OP_XOR [list smoke query1 "string" ]
|
---|
85 | if { [query2 get_description] != "Xapian::Query((smoke XOR (smoke PHRASE 3 test PHRASE 3 tuple) XOR string))" } {
|
---|
86 | puts stderr "Unexpected query2.get_description()"
|
---|
87 | exit 1
|
---|
88 | }
|
---|
89 | set subqs [list "a" "b"]
|
---|
90 | xapian::Query query3 $xapian::Query_OP_OR $subqs
|
---|
91 | if { [query3 get_description] != "Xapian::Query((a OR b))" } {
|
---|
92 | puts stderr "Unexpected query3.get_description()"
|
---|
93 | exit 1
|
---|
94 | }
|
---|
95 |
|
---|
96 | xapian::Enquire enq db
|
---|
97 | xapian::Query q $xapian::Query_OP_OR "there" "is"
|
---|
98 | enq set_query q
|
---|
99 | set mset [enq get_mset 0 10]
|
---|
100 | if { [$mset size] != 1 } {
|
---|
101 | puts stderr "Unexpected number of entries in mset ([$mset size] != 1)"
|
---|
102 | exit 1
|
---|
103 | }
|
---|
104 | set terms [join [enq get_matching_terms [$mset get_hit 0]] " "]
|
---|
105 | if { $terms != "is there" } {
|
---|
106 | puts stderr "Unexpected terms"
|
---|
107 | exit 1
|
---|
108 | }
|
---|
109 |
|
---|
110 | # Check exception handling for Xapian::DocNotFoundError
|
---|
111 | if [catch {
|
---|
112 | xapian::Document doc2 [db get_document 2]
|
---|
113 | puts stderr "Retrieved non-existent document"
|
---|
114 | exit 1
|
---|
115 | } e] {
|
---|
116 | # We expect DocNotFoundError
|
---|
117 | if { $errorCode != "XAPIAN DocNotFoundError" } {
|
---|
118 | puts stderr "Unexpected exception from accessing non-existent document:"
|
---|
119 | puts stderr "errorCode: $errorCode"
|
---|
120 | puts stderr "message: $e"
|
---|
121 | exit 1
|
---|
122 | }
|
---|
123 | }
|
---|