initial
This commit is contained in:
commit
3d122b5022
8 files changed
+1065
No files matched your search
@@ -0,0 +1,198 @@
|
||||
import unittest
|
||||
import os
|
||||
import yaml
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from src.obsidian_cli import OnyxCLI
|
||||
from unittest.mock import patch, MagicMock
|
||||
import click
|
||||
|
||||
class TestOnyxCLI(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Create temporary test directory and files
|
||||
self.test_dir = Path('test_vault')
|
||||
self.test_dir.mkdir(exist_ok=True)
|
||||
|
||||
# Create a temporary config directory
|
||||
self.temp_config_dir = Path('temp_config')
|
||||
self.temp_config_dir.mkdir(exist_ok=True)
|
||||
|
||||
# Mock home directory for config
|
||||
self.patcher = patch('pathlib.Path.home')
|
||||
self.mock_home = self.patcher.start()
|
||||
self.mock_home.return_value = Path('temp_config')
|
||||
|
||||
self.cli = OnyxCLI()
|
||||
|
||||
# Set up test vault
|
||||
self.cli.set_default_vault('test_vault', str(self.test_dir.resolve()))
|
||||
|
||||
def tearDown(self):
|
||||
# Clean up test directories
|
||||
self.patcher.stop()
|
||||
shutil.rmtree(self.test_dir, ignore_errors=True)
|
||||
shutil.rmtree(self.temp_config_dir, ignore_errors=True)
|
||||
|
||||
def test_set_default_vault(self):
|
||||
"""Test setting default vault"""
|
||||
self.cli.set_default_vault('new_vault', str(self.test_dir))
|
||||
self.assertEqual(self.cli.default_vault, 'new_vault')
|
||||
|
||||
config = yaml.safe_load(self.cli.config_file.read_text())
|
||||
self.assertEqual(config['default_vault'], 'new_vault')
|
||||
self.assertEqual(Path(config['vaults']['new_vault']), self.test_dir.resolve())
|
||||
|
||||
def test_create_note(self):
|
||||
"""Test note creation"""
|
||||
# Test basic note creation
|
||||
self.cli.create_note('test.md', 'Test content')
|
||||
note_path = self.test_dir / 'test.md'
|
||||
self.assertTrue(note_path.exists())
|
||||
self.assertEqual(note_path.read_text(), 'Test content')
|
||||
|
||||
# Test note creation with auto-extension
|
||||
self.cli.create_note('test2', 'Test content 2')
|
||||
self.assertTrue((self.test_dir / 'test2.md').exists())
|
||||
|
||||
# Test overwrite
|
||||
with self.assertRaises(click.UsageError):
|
||||
self.cli.create_note('test.md', 'New content')
|
||||
self.cli.create_note('test.md', 'New content', overwrite=True)
|
||||
self.assertEqual((self.test_dir / 'test.md').read_text(), 'New content')
|
||||
|
||||
# Test append
|
||||
self.cli.create_note('test.md', 'Additional content', append=True)
|
||||
self.assertEqual(
|
||||
(self.test_dir / 'test.md').read_text(),
|
||||
'New content\nAdditional content'
|
||||
)
|
||||
|
||||
def test_frontmatter_operations(self):
|
||||
"""Test frontmatter operations"""
|
||||
# Create a test note with frontmatter
|
||||
initial_content = """---
|
||||
title: Test Note
|
||||
tags: [test]
|
||||
status: draft
|
||||
---
|
||||
Test content"""
|
||||
test_note = 'frontmatter_test.md'
|
||||
self.cli.create_note(test_note, initial_content)
|
||||
|
||||
# Mock click.UsageError for testing
|
||||
click_error = click.UsageError
|
||||
|
||||
# Test that frontmatter operations work correctly
|
||||
|
||||
# Test frontmatter --edit
|
||||
self.cli.frontmatter_edit(test_note, 'tags', 'test,new')
|
||||
self.cli.frontmatter_edit(test_note, 'status', 'published')
|
||||
|
||||
content = (self.test_dir / test_note).read_text()
|
||||
frontmatter = yaml.safe_load(content.split('---')[1])
|
||||
self.assertEqual(frontmatter['tags'], ['test', 'new'])
|
||||
self.assertEqual(frontmatter['status'], 'published')
|
||||
|
||||
# Test frontmatter --view
|
||||
with patch('click.echo') as mock_echo:
|
||||
self.cli.frontmatter_view(test_note, 'tags')
|
||||
mock_echo.assert_called_with(yaml.dump({'tags': ['test', 'new']}, default_flow_style=False).strip())
|
||||
|
||||
# Test frontmatter --view with value check
|
||||
with patch('click.echo') as mock_echo:
|
||||
self.cli.frontmatter_view(test_note, 'tags', 'test')
|
||||
mock_echo.assert_called_with('true')
|
||||
|
||||
self.cli.frontmatter_view(test_note, 'tags', 'nonexistent')
|
||||
mock_echo.assert_called_with('false')
|
||||
|
||||
# Test frontmatter --clear
|
||||
self.cli.frontmatter_clear(test_note, 'tags')
|
||||
content = (self.test_dir / test_note).read_text()
|
||||
frontmatter = yaml.safe_load(content.split('---')[1])
|
||||
self.assertEqual(frontmatter['tags'], [])
|
||||
|
||||
# Test frontmatter --clear with multiple keys
|
||||
self.cli.frontmatter_clear(test_note, 'status,title')
|
||||
content = (self.test_dir / test_note).read_text()
|
||||
frontmatter = yaml.safe_load(content.split('---')[1])
|
||||
self.assertEqual(frontmatter['status'], '')
|
||||
self.assertEqual(frontmatter['title'], '')
|
||||
|
||||
# Test frontmatter --delete
|
||||
self.cli.frontmatter_delete(test_note, 'tags')
|
||||
content = (self.test_dir / test_note).read_text()
|
||||
frontmatter = yaml.safe_load(content.split('---')[1])
|
||||
self.assertNotIn('tags', frontmatter)
|
||||
|
||||
# Test frontmatter --delete with multiple keys
|
||||
self.cli.frontmatter_delete(test_note, 'status,title')
|
||||
content = (self.test_dir / test_note).read_text()
|
||||
# After deleting all keys, there should be no frontmatter section
|
||||
self.assertNotIn('---', content)
|
||||
|
||||
def test_move_note(self):
|
||||
"""Test moving/renaming notes"""
|
||||
# Create a test note and another note that links to it
|
||||
self.cli.create_note('source.md', 'Test content')
|
||||
self.cli.create_note('link.md', 'Link to [[source]]')
|
||||
|
||||
# Move the note
|
||||
self.cli.move_note('source.md', 'folder/dest.md')
|
||||
|
||||
# Check that the note was moved
|
||||
self.assertFalse((self.test_dir / 'source.md').exists())
|
||||
self.assertTrue((self.test_dir / 'folder' / 'dest.md').exists())
|
||||
|
||||
# Check that links were updated
|
||||
link_content = (self.test_dir / 'link.md').read_text()
|
||||
self.assertIn('[[dest]]', link_content)
|
||||
|
||||
def test_daily_note(self):
|
||||
"""Test daily note creation"""
|
||||
# Create daily note (default settings don't use templates)
|
||||
with patch('datetime.datetime') as mock_date:
|
||||
mock_date.now.return_value.strftime.return_value = '2025-10-24'
|
||||
self.cli.daily_note()
|
||||
|
||||
daily_note = self.test_dir / '2025-10-24.md'
|
||||
self.assertTrue(daily_note.exists())
|
||||
# Daily note should be empty since no template is configured
|
||||
self.assertEqual(daily_note.read_text(), '')
|
||||
|
||||
@patch('subprocess.run')
|
||||
@patch('os.startfile')
|
||||
def test_open_note(self, mock_startfile, mock_run):
|
||||
"""Test opening notes"""
|
||||
# Create a test note
|
||||
self.cli.create_note('test.md', 'Test content')
|
||||
|
||||
# Test opening on different platforms
|
||||
with patch('sys.platform', 'win32'):
|
||||
self.cli.open_note('test.md')
|
||||
mock_startfile.assert_called_with('obsidian://open?vault=test_vault&file=test')
|
||||
|
||||
with patch('sys.platform', 'darwin'):
|
||||
self.cli.open_note('test.md')
|
||||
mock_run.assert_called_with(['open', 'obsidian://open?vault=test_vault&file=test'])
|
||||
|
||||
with patch('sys.platform', 'linux'):
|
||||
self.cli.open_note('test.md')
|
||||
mock_run.assert_called_with(['xdg-open', 'obsidian://open?vault=test_vault&file=test'])
|
||||
|
||||
def test_delete_note(self):
|
||||
"""Test note deletion"""
|
||||
# Create a test note
|
||||
self.cli.create_note('test.md', 'Test content')
|
||||
self.assertTrue((self.test_dir / 'test.md').exists())
|
||||
|
||||
# Delete the note
|
||||
self.cli.delete_note('test.md')
|
||||
self.assertFalse((self.test_dir / 'test.md').exists())
|
||||
|
||||
# Test deleting non-existent note
|
||||
with self.assertRaises(click.UsageError):
|
||||
self.cli.delete_note('nonexistent.md')
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user