|
1 # -*- coding: utf-8 -*- |
|
2 ''' |
|
3 Created on Aug 1, 2012 |
|
4 |
|
5 @author: ymh |
|
6 ''' |
|
7 from django.test import SimpleTestCase |
|
8 from ldt.indexation.query_parser import QueryParser |
|
9 from haystack.query import SQ |
|
10 import unittest |
|
11 |
|
12 class QueryParserTest(SimpleTestCase): |
|
13 |
|
14 def test_simple_term(self): |
|
15 |
|
16 qp = QueryParser("text") |
|
17 res = qp.parse("hello") |
|
18 |
|
19 self.assertEqual(str(res), str(SQ(text="hello"))) |
|
20 |
|
21 def test_multiple_terms(self): |
|
22 |
|
23 qp = QueryParser("text") |
|
24 res = qp.parse("hello title:world") |
|
25 |
|
26 self.assertEquals(str(res), str(SQ(text="hello")|SQ(title="world"))) |
|
27 |
|
28 |
|
29 def test_operator(self): |
|
30 |
|
31 qp = QueryParser("text") |
|
32 res = qp.parse("title:hello AND world") |
|
33 |
|
34 self.assertEquals(str(res), str(SQ(title="hello")&SQ(text="world"))) |
|
35 |
|
36 def test_complex(self): |
|
37 |
|
38 qp = QueryParser("text") |
|
39 res = qp.parse("hello AND world foo") |
|
40 |
|
41 self.assertEquals(str(res), str(SQ(text="hello")&SQ(text="world")|SQ(text="foo"))) |
|
42 |
|
43 def test_minus(self): |
|
44 qp = QueryParser("text") |
|
45 res = qp.parse("hello -world") |
|
46 |
|
47 self.assertEquals(str(res), str(SQ(text="hello")&~SQ(text="world"))) |
|
48 |
|
49 def test_not(self): |
|
50 qp = QueryParser("text") |
|
51 res = qp.parse("hello NOT world") |
|
52 |
|
53 self.assertEquals(str(res), str(SQ(text="hello")|~SQ(text="world"))) |
|
54 |
|
55 def test_exact(self): |
|
56 qp = QueryParser("text") |
|
57 res = qp.parse('title:"hello world"') |
|
58 |
|
59 self.assertEquals(str(res), str(SQ(title__exact="hello world"))) |
|
60 |
|
61 def test_single_quote(self): |
|
62 qp = QueryParser("text") |
|
63 res = qp.parse("title:'hello world'") |
|
64 |
|
65 self.assertEquals(str(res), str(SQ(title="hello world"))) |
|
66 |
|
67 def test_group(self): |
|
68 qp = QueryParser("text") |
|
69 |
|
70 res = qp.parse("(hello world) AND (foo bar)") |
|
71 |
|
72 self.assertEquals(str(res), str(SQ(text="hello")&SQ(text="world")&SQ(text="foo")&SQ(text="bar"))) |
|
73 |
|
74 def test_group_or(self): |
|
75 qp = QueryParser("text") |
|
76 |
|
77 res = qp.parse("(hello world) OR (foo bar)") |
|
78 |
|
79 self.assertEquals(str(res), str((SQ(text="hello")&SQ(text="world"))|(SQ(text="foo")&SQ(text="bar")) )) |
|
80 |
|
81 def test_prefix(self): |
|
82 qp = QueryParser("text") |
|
83 res = qp.parse("title:foo*") |
|
84 self.assertEquals(str(res), str(SQ(title__startswith='foo'))) |
|
85 |
|
86 def test_plus(self): |
|
87 qp = QueryParser("text") |
|
88 res = qp.parse("title:foo +bar") |
|
89 self.assertEquals(str(res), str(SQ(text='bar')&SQ(title='foo'))) |
|
90 |
|
91 def test_plus_multiple(self): |
|
92 qp = QueryParser("text") |
|
93 |
|
94 res = qp.parse("title:foo +bar +fighter") |
|
95 self.assertEquals(str(res), str(SQ(text='bar')&SQ(text="fighter")&SQ(title='foo'))) |
|
96 |
|
97 res = qp.parse("+title:foo +bar +fighter") |
|
98 self.assertEquals(str(res), str(SQ(title='foo')&SQ(text='bar')&SQ(text="fighter"))) |
|
99 |
|
100 def test_ltgt(self): |
|
101 qp = QueryParser("text") |
|
102 res = qp.parse("count:<10") |
|
103 self.assertEquals(str(res), str(SQ(count__lt=10))) |
|
104 res = qp.parse("count:>10") |
|
105 self.assertEquals(str(res), str(SQ(count__gt=10))) |
|
106 res = qp.parse("count:<=10") |
|
107 self.assertEquals(str(res), str(SQ(count__lte=10))) |
|
108 res = qp.parse("count:>=10") |
|
109 self.assertEquals(str(res), str(SQ(count__gte=10))) |
|
110 |
|
111 def test_ltgt_float(self): |
|
112 qp = QueryParser("text") |
|
113 res = qp.parse("count:<3.14") |
|
114 self.assertEquals(str(res), str(SQ(count__lt=3.14))) |
|
115 res = qp.parse("count:>3.14") |
|
116 self.assertEquals(str(res), str(SQ(count__gt=3.14))) |
|
117 res = qp.parse("count:<=3.14") |
|
118 self.assertEquals(str(res), str(SQ(count__lte=3.14))) |
|
119 res = qp.parse("count:>=3.14") |
|
120 self.assertEquals(str(res), str(SQ(count__gte=3.14))) |
|
121 |
|
122 def test_ltgt_str(self): |
|
123 qp = QueryParser("text") |
|
124 res = qp.parse("count:<foo") |
|
125 self.assertEquals(str(res), str(SQ(count__lt='foo'))) |
|
126 res = qp.parse("count:>foo") |
|
127 self.assertEquals(str(res), str(SQ(count__gt='foo'))) |
|
128 res = qp.parse("count:<=foo") |
|
129 self.assertEquals(str(res), str(SQ(count__lte='foo'))) |
|
130 res = qp.parse("count:>=foo") |
|
131 self.assertEquals(str(res), str(SQ(count__gte='foo'))) |
|
132 |
|
133 |
|
134 def test_range(self): |
|
135 qp = QueryParser("text") |
|
136 res = qp.parse("count:[foo to bar]") |
|
137 self.assertEquals(str(res), str(SQ(count__range=['foo','bar']))) |
|
138 |
|
139 def test_range_nb(self): |
|
140 qp = QueryParser("text") |
|
141 res = qp.parse("count:[3 to 5]") |
|
142 self.assertEquals(str(res), str(SQ(count__range=[3,5]))) |
|
143 |
|
144 if __name__ == '__main__': |
|
145 unittest.main() |