krz/crumb

A local alternative to your browser's history.

clone: git clone https://gitbay.org/krz/crumb.git

main: tests/test_search.py · raw

 1import io
 2import os
 3import tempfile
 4import unittest
 5from contextlib import redirect_stdout
 6from unittest.mock import patch
 7
 8import search
 9from search import search_log
10
11
12class TestSearchLog(unittest.TestCase):
13    """search_log prints matches; it does not modify the log. The previous tests
14    read the file back and asserted on its contents, which could only ever pass
15    for the match case and never for the no-match one."""
16
17    ENTRY = (
18        "* Example Entry\n"
19        ":PROPERTIES:\n"
20        ":URL:       http://example.com\n"
21        ":TIMESTAMP: 2023-10-27 10:00:00\n"
22        ":END:\n\n"
23    )
24
25    def setUp(self):
26        handle, self.log_path = tempfile.mkstemp(suffix=".org")
27        os.close(handle)
28        with open(self.log_path, "w") as f:
29            f.write(self.ENTRY)
30
31    def tearDown(self):
32        os.unlink(self.log_path)
33
34    def run_search(self, query):
35        out = io.StringIO()
36        with patch.object(search, "LOG_PATH", self.log_path), redirect_stdout(out):
37            search_log(query)
38        return out.getvalue()
39
40    def test_a_match_is_printed(self):
41        self.assertIn("Example Entry", self.run_search("example"))
42
43    def test_the_search_is_case_insensitive(self):
44        self.assertIn("Example Entry", self.run_search("EXAMPLE"))
45
46    def test_a_property_value_matches(self):
47        self.assertIn("Example Entry", self.run_search("example.com"))
48
49    def test_no_match_prints_no_entry(self):
50        self.assertNotIn("Example Entry", self.run_search("nonexistent"))
51
52    def test_a_missing_log_is_reported_not_raised(self):
53        out = io.StringIO()
54        with patch.object(search, "LOG_PATH", self.log_path + ".absent"), redirect_stdout(out):
55            search_log("anything")
56        self.assertIn("No history file found.", out.getvalue())