示例#1
0
    def test_process_with_duplicate_existing_id(
        self,
        app: App,
        capsys: pytest.CaptureFixture[str],
    ) -> None:
        """slipbox.process must show a warning if a new note shares the ID of an
        existing note.

        The warning message must show the filenames of both notes.
        """
        file_a = app.root / "a.md"
        file_a.write_text("# 0 Existing note\n\nTest.\n")

        process_notes(app, [file_a])
        result = list(app.database.execute("SELECT id, title FROM Notes"))
        assert len(result) == 1
        assert result == [(0, "Existing note")]

        assert is_quiet(capsys)

        file_b = app.root / "b.md"
        file_b.write_text("# 0 Duplicate note\n\nTest.\n")

        process_notes(app, [file_b])
        result = list(app.database.execute("SELECT id, title FROM Notes"))
        assert len(result) == 1
        assert result == [(0, "Existing note")]

        stdout, stderr = capsys.readouterr()
        assert not stdout
        assert stderr
        assert str(file_a.relative_to(app.root)) in stderr
        assert str(file_b.relative_to(app.root)) in stderr
示例#2
0
    def test_process_rst(
        self,
        capsys: pytest.CaptureFixture[str],
        app: App,
    ) -> None:
        """Slipbox filters shouldn't raise metadata-related errors."""
        rst = app.root / "test.rst"
        rst.write_text("""
1 Foo
=====

Foo.

2 Bar
=====

Bar.""")

        process_notes(app, [rst])
        result = list(
            app.database.execute("SELECT id, title, filename FROM Notes"))

        assert result == [(1, "Foo", "test.rst"), (2, "Bar", "test.rst")]

        assert is_quiet(capsys)
示例#3
0
    def test_process_with_non_text_titles(
        self,
        capsys: pytest.CaptureFixture[str],
        app: App,
    ) -> None:
        """Notes with non-text titles in the header must still be recognized.
        """
        file_a = app.root / "a.md"
        file_b = app.root / "b.md"
        file_c = app.root / "c.md"
        file_a.write_text("# 0 $1 + 1 = 2$\n\nTest.\n")
        file_b.write_text("# 1 [Note 0](#0)\n\nTest.\n")
        file_c.write_text("# 2 `print('Title')`\n\nTest.\n")

        process_notes(app, [file_a, file_b, file_c])

        sql = "SELECT id, title FROM Notes ORDER BY id"
        result = list(app.database.execute(sql))

        assert result == [
            (0, "1 + 1 = 2"),
            (1, "Note 0"),
            (2, "print('Title')"),
        ]

        assert is_quiet(capsys)
示例#4
0
    def test_process_with_duplicate_ids_in_a_file(
        self,
        capsys: pytest.CaptureFixture[str],
        app: App,
    ) -> None:
        """If there are duplicate IDs in a file, only the first one must be saved.

        slipbox.process must show a warning when this happens.
        """
        markdown = app.root / "test.md"
        markdown.write_text("""# 0 First note

Foo.

# 0 Duplicate

Bar.
""")
        process_notes(app, [markdown])
        result = list(app.database.execute("SELECT id, title FROM Notes"))
        assert len(result) == 1
        assert result == [(0, "First note")]

        stdout, stderr = capsys.readouterr()
        assert not stdout
        assert stderr
示例#5
0
 def test_process_empty_file(self, app: App) -> None:
     """Empty files shouldn't have entries in the database."""
     empty = app.root / "empty.md"
     empty.touch()
     process_notes(app, [empty])
     assert not list(app.database.execute("SELECT * FROM Files"))
     assert not list(app.database.execute("SELECT * FROM Notes"))
     assert not list(app.database.execute("SELECT * FROM Tags"))
     assert not list(app.database.execute("SELECT * FROM Links"))
     assert not list(app.database.execute("SELECT * FROM Bibliography"))
     assert not list(app.database.execute("SELECT * FROM Citations"))
示例#6
0
    def test_process(self, app: App) -> None:
        """Smoke test for slipbox.process."""
        input_file = app.root / "input.md"
        input_file.write_text("# 1 Test note\n\nHello, world!\n")
        assert not list(
            app.database.execute("SELECT * FROM Notes WHERE html IS NOT NULL"))

        process_notes(app, [input_file])
        result = list(
            app.database.execute("SELECT * FROM Notes WHERE html IS NOT NULL"))
        assert result
示例#7
0
    def test_process_filenames0(self, app: App) -> None:
        """Filenames must be scanned correctly."""
        markdown = app.root / "bar.md"
        skip = app.root / "foo.md"
        markdown.write_text("# 0 Note\n\nBody.\n")
        skip.write_text("# 0 Note\n\nBody.\n")
        process_notes(app, [markdown])

        sql = "SELECT filename FROM Notes WHERE id = 0"
        result = list(app.database.execute(sql))
        assert len(result) == 1
        assert markdown.samefile(app.root / result[0][0])
示例#8
0
    def test_process_with_id_in_scientific_form(
        self,
        capsys: pytest.CaptureFixture[str],
        app: App,
    ) -> None:
        """Headers with non-integer IDs should be ignored."""
        markdown = app.root / "test.md"
        markdown.write_text("# 1e1 Invalid note ID\n\nTest.\n")
        process_notes(app, [markdown])

        result = list(app.database.execute("SELECT * FROM Notes"))
        assert not result

        assert is_quiet(capsys)
示例#9
0
    def test_process_tags_with_trailing_punctuation(
        self,
        capsys: pytest.CaptureFixture[str],
        app: App,
    ) -> None:
        """If a hashtag has trailing invalid symbols, only the prefix must be
        saved.
        """
        markdown = app.root / "test.md"
        markdown.write_text("# 0 Test\n\n#tag.\n#tags.\n#0.\n")
        process_notes(app, [markdown])
        result = app.database.execute("SELECT tag FROM Tags ORDER BY tag")
        assert list(result) == [("#0", ), ("#tag", ), ("#tags", )]

        assert is_quiet(capsys)
示例#10
0
    def test_process_non_level1_headers(self, app: App) -> None:
        """Only level 1 headers must be considered as note headers."""
        markdown = app.root / "test.md"
        markdown.write_text("""# 0 Valid note header

Foo.

## 1 Invalid note header

Bar.
""")
        process_notes(app, [markdown])
        result = [nid for nid, in app.database.execute("SELECT id FROM Notes")]
        assert len(result) == 1
        assert result == [0]
示例#11
0
    def test_process_with_external_links(
        self,
        capsys: pytest.CaptureFixture[str],
        app: App,
    ) -> None:
        """External links shouldn't be saved in the db."""
        markdown = app.root / "test.md"
        markdown.write_text("# 0 Test\n\n[Example](https://example.com)")
        process_notes(app, [markdown])

        result = list(app.database.execute("SELECT id, title FROM Notes"))
        assert (0, "Test") in result

        result = list(app.database.execute("SELECT * FROM Links"))
        assert not result

        assert is_quiet(capsys)
示例#12
0
    def test_process_with_empty_link_target(
        self,
        capsys: pytest.CaptureFixture[str],
        app: App,
    ) -> None:
        """slipbox.process must show a warning if there is a link with an empty
        target.
        """
        markdown = app.root / "test.md"
        markdown.write_text("# 0 Foo\n\n[Empty]().\n")
        process_notes(app, [markdown])
        result = list(app.database.execute("SELECT * FROM Links"))
        assert not result

        stdout, stderr = capsys.readouterr()
        assert not stdout
        assert stderr
示例#13
0
    def test_process_clusters_from_context(
        self,
        app: App,
    ) -> None:
        """Check if clusters are stored in db."""
        markdown = app.root / "test.md"
        markdown.write_text("""# 0 Test

#test

[](#1).

# 1 Dest

Test.
""")
        process_notes(app, [markdown])
        result = sorted(app.database.execute("SELECT src, dest FROM Links"))
        assert result == sorted([(0, 1)])

        result = sorted(app.database.execute("SELECT tag, id FROM Tags"))
        assert result == sorted([("#test", 0)])