test/unit/test_index.rb (1958B) - 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_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 assert_equal expected_qobj.description, parsed[:qobj].description
42
43 parsed = with_fake_time do
44 Redwood::Index.parse_query "before:yesterday"
45 end
46 expected_qobj = Xapian::Query.new(
47 Xapian::Query::OP_VALUE_RANGE,
48 Redwood::Index::DATE_VALUENO,
49 Xapian.sortable_serialise(0),
50 Xapian.sortable_serialise(Time.utc(2000, 1, 1).to_i),
51 )
52 assert_equal expected_qobj.description, parsed[:qobj].description
53
54 parsed = with_fake_time do
55 Redwood::Index.parse_query "during:yesterday"
56 end
57 expected_qobj = Xapian::Query.new(
58 Xapian::Query::OP_VALUE_RANGE,
59 Redwood::Index::DATE_VALUENO,
60 Xapian.sortable_serialise(Time.utc(1999, 12, 31).to_i),
61 Xapian.sortable_serialise(Time.utc(2000, 1, 1).to_i),
62 )
63 assert_equal expected_qobj.description, parsed[:qobj].description
64 end
65 end