test/unit/test_index.rb (2528B) - raw
1 require "minitest/mock"
2 require "test_helper"
3
4 require "sup"
5
6 class TestIndex < Minitest::Test
7 def setup
8 @path = Dir.mktmpdir
9 Redwood::start
10 Redwood::Logger.remove_sink $stderr
11 Redwood::Index.init @path
12 Redwood::Index.load
13 end
14
15 def teardown
16 Redwood::Index.save_index
17 ObjectSpace.each_object(Class).select {|a| a < Redwood::Singleton}.each do |klass|
18 klass.deinstantiate! unless klass == Redwood::Logger
19 end
20 FileUtils.rm_r @path
21 end
22
23 def with_fake_time &block
24 Time.stub :now, Time.utc(2000) do
25 ## Also stub Time.local to behave like Time.utc so that Chronic.parse
26 ## doesn't pick up the timezone of the test runner.
27 Time.stub :local, ->(*args) { Time.utc(*args) }, &block
28 end
29 end
30
31 def test_date_query_parsing
32 parsed = with_fake_time do
33 Redwood::Index.parse_query "after:yesterday"
34 end
35 expected_date_qobj = Xapian::Query.new(
36 Xapian::Query::OP_VALUE_RANGE,
37 Redwood::Index::DATE_VALUENO,
38 Xapian.sortable_serialise(Time.utc(2000, 1, 1).to_i),
39 Xapian.sortable_serialise(2**32),
40 )
41 expected_qobj = Xapian::Query.new(
42 0.0,
43 Xapian::Query.new(
44 Xapian::Query::OP_AND,
45 expected_date_qobj,
46 Xapian::Query.new("Kmail"),
47 ),
48 )
49 assert_equal expected_qobj.description, parsed[:qobj].description
50
51 parsed = with_fake_time do
52 Redwood::Index.parse_query "before:yesterday"
53 end
54 expected_date_qobj = Xapian::Query.new(
55 Xapian::Query::OP_VALUE_RANGE,
56 Redwood::Index::DATE_VALUENO,
57 Xapian.sortable_serialise(0),
58 Xapian.sortable_serialise(Time.utc(2000, 1, 1).to_i),
59 )
60 expected_qobj = Xapian::Query.new(
61 0.0,
62 Xapian::Query.new(
63 Xapian::Query::OP_AND,
64 expected_date_qobj,
65 Xapian::Query.new("Kmail"),
66 ),
67 )
68 assert_equal expected_qobj.description, parsed[:qobj].description
69
70 parsed = with_fake_time do
71 Redwood::Index.parse_query "during:yesterday"
72 end
73 expected_date_qobj = Xapian::Query.new(
74 Xapian::Query::OP_VALUE_RANGE,
75 Redwood::Index::DATE_VALUENO,
76 Xapian.sortable_serialise(Time.utc(1999, 12, 31).to_i),
77 Xapian.sortable_serialise(Time.utc(2000, 1, 1).to_i),
78 )
79 expected_qobj = Xapian::Query.new(
80 0.0,
81 Xapian::Query.new(
82 Xapian::Query::OP_AND,
83 expected_date_qobj,
84 Xapian::Query.new("Kmail"),
85 ),
86 )
87 assert_equal expected_qobj.description, parsed[:qobj].description
88 end
89 end