commit b49dcfe7dd25100feb88ed8aefe629fd91423a26
parent e95a132c6adc9dcf2220c2e09920b0830ba789b7
Author: Dan Callaghan <djc@djc.id.au>
Date: Sun, 5 Apr 2026 20:07:53 +1000
tests: add coverage for date query parsing
Diffstat:
2 files changed, 64 insertions(+), 0 deletions(-)
diff --git a/Manifest.txt b/Manifest.txt
@@ -175,6 +175,7 @@ test/unit/service/test_label_service.rb
test/unit/test_contact.rb
test/unit/test_edit_message_mode.rb
test/unit/test_horizontal_selector.rb
+test/unit/test_index.rb
test/unit/test_line_cursor_mode.rb
test/unit/test_locale_fiddler.rb
test/unit/test_person.rb
diff --git a/test/unit/test_index.rb b/test/unit/test_index.rb
@@ -0,0 +1,63 @@
+require "minitest/mock"
+require "test_helper"
+
+require "sup"
+
+class TestIndex < Minitest::Test
+ def setup
+ @path = Dir.mktmpdir
+ Redwood::start
+ Redwood::Index.init @path
+ Redwood::Index.load
+ end
+
+ def teardown
+ ObjectSpace.each_object(Class).select {|a| a < Redwood::Singleton}.each do |klass|
+ klass.deinstantiate! unless klass == Redwood::Logger
+ end
+ FileUtils.rm_r @path
+ end
+
+ def with_fake_time &block
+ Time.stub :now, Time.utc(2000) do
+ ## Also stub Time.local to behave like Time.utc so that Chronic.parse
+ ## doesn't pick up the timezone of the test runner.
+ Time.stub :local, ->(*args) { Time.utc(*args) }, &block
+ end
+ end
+
+ def test_date_query_parsing
+ parsed = with_fake_time do
+ Redwood::Index.parse_query "after:yesterday"
+ end
+ expected_qobj = Xapian::Query.new(
+ Xapian::Query::OP_VALUE_RANGE,
+ Redwood::Index::DATE_VALUENO,
+ Xapian.sortable_serialise(Time.utc(2000, 1, 1).to_i),
+ Xapian.sortable_serialise(2**32),
+ )
+ assert_equal expected_qobj.description, parsed[:qobj].description
+
+ parsed = with_fake_time do
+ Redwood::Index.parse_query "before:yesterday"
+ end
+ expected_qobj = Xapian::Query.new(
+ Xapian::Query::OP_VALUE_RANGE,
+ Redwood::Index::DATE_VALUENO,
+ Xapian.sortable_serialise(0),
+ Xapian.sortable_serialise(Time.utc(2000, 1, 1).to_i),
+ )
+ assert_equal expected_qobj.description, parsed[:qobj].description
+
+ parsed = with_fake_time do
+ Redwood::Index.parse_query "during:yesterday"
+ end
+ expected_qobj = Xapian::Query.new(
+ Xapian::Query::OP_VALUE_RANGE,
+ Redwood::Index::DATE_VALUENO,
+ Xapian.sortable_serialise(Time.utc(1999, 12, 31).to_i),
+ Xapian.sortable_serialise(Time.utc(2000, 1, 1).to_i),
+ )
+ assert_equal expected_qobj.description, parsed[:qobj].description
+ end
+end