|
1 | 1 | from click.testing import CliRunner |
2 | | -from apple_notes_to_sqlite.cli import cli |
| 2 | +from apple_notes_to_sqlite.cli import cli, COUNT_SCRIPT |
| 3 | +import sqlite_utils |
| 4 | +import json |
| 5 | +import os |
| 6 | +from unittest.mock import patch |
3 | 7 |
|
| 8 | +FAKE_OUTPUT = b""" |
| 9 | +abcdefg-id: note-1 |
| 10 | +abcdefg-created: 2023-03-08T16:36:41 |
| 11 | +abcdefg-updated: 2023-03-08T15:36:41 |
| 12 | +abcdefg-title: Title 1 |
4 | 13 |
|
5 | | -def test_version(): |
| 14 | +This is the content of note 1 |
| 15 | +abcdefgabcdefg |
| 16 | +abcdefg-id: note-2 |
| 17 | +abcdefg-created: 2023-03-08T16:36:41 |
| 18 | +abcdefg-updated: 2023-03-08T15:36:41 |
| 19 | +abcdefg-title: Title 2 |
| 20 | +
|
| 21 | +This is the content of note 2 |
| 22 | +abcdefgabcdefg |
| 23 | +""".strip() |
| 24 | + |
| 25 | +EXPECTED_NOTES = [ |
| 26 | + { |
| 27 | + "id": "note-1", |
| 28 | + "created": "2023-03-08T16:36:41", |
| 29 | + "updated": "2023-03-08T15:36:41", |
| 30 | + "title": "Title 1", |
| 31 | + "body": "This is the content of note 1", |
| 32 | + }, |
| 33 | + { |
| 34 | + "id": "note-2", |
| 35 | + "created": "2023-03-08T16:36:41", |
| 36 | + "updated": "2023-03-08T15:36:41", |
| 37 | + "title": "Title 2", |
| 38 | + "body": "This is the content of note 2", |
| 39 | + }, |
| 40 | +] |
| 41 | + |
| 42 | + |
| 43 | +@patch("secrets.token_hex") |
| 44 | +def test_apple_notes_to_sqlite(mock_token_hex, fp): |
| 45 | + fp.register_subprocess(["osascript", "-e", COUNT_SCRIPT], stdout=b"2") |
| 46 | + fp.register_subprocess(["osascript", "-e", fp.any()], stdout=FAKE_OUTPUT) |
| 47 | + mock_token_hex.return_value = "abcdefg" |
| 48 | + runner = CliRunner() |
| 49 | + with runner.isolated_filesystem(): |
| 50 | + assert not os.path.exists("notes.db") |
| 51 | + # Run the CLI |
| 52 | + result = runner.invoke(cli, ["notes.db"]) |
| 53 | + assert result.exit_code == 0 |
| 54 | + # Check that the database was created |
| 55 | + assert os.path.exists("notes.db") |
| 56 | + db = sqlite_utils.Database("notes.db") |
| 57 | + # Check that the notes table was created |
| 58 | + assert db.table_names() == ["notes"] |
| 59 | + # Check that the notes were inserted |
| 60 | + assert list(db["notes"].rows) == EXPECTED_NOTES |
| 61 | + |
| 62 | + |
| 63 | +@patch("secrets.token_hex") |
| 64 | +def test_apple_notes_to_sqlite_dump(mock_token_hex, fp): |
| 65 | + fp.register_subprocess(["osascript", "-e", COUNT_SCRIPT], stdout=b"2") |
| 66 | + fp.register_subprocess(["osascript", "-e", fp.any()], stdout=FAKE_OUTPUT) |
| 67 | + mock_token_hex.return_value = "abcdefg" |
6 | 68 | runner = CliRunner() |
7 | 69 | with runner.isolated_filesystem(): |
8 | | - result = runner.invoke(cli, ["--version"]) |
| 70 | + assert not os.path.exists("notes.db") |
| 71 | + result = runner.invoke(cli, ["--dump"]) |
| 72 | + # Check the output |
9 | 73 | assert result.exit_code == 0 |
10 | | - assert result.output.startswith("cli, version ") |
| 74 | + # Should still be no database |
| 75 | + assert not os.path.exists("notes.db") |
| 76 | + # Output should be newline-delimited JSON |
| 77 | + notes = [] |
| 78 | + for line in result.output.splitlines(): |
| 79 | + notes.append(json.loads(line)) |
| 80 | + assert notes == EXPECTED_NOTES |
0 commit comments