"""
Tests for the markdown task parser
"""
import pytest
from src.importers.markdown_parser import MarkdownTask, MarkdownTaskParser
class TestMarkdownTask:
"""Test the MarkdownTask class"""
def test_task_initialization(self):
"""Test creating a new task"""
task = MarkdownTask("Test Task")
assert task.title == "Test Task"
assert task.tags == []
assert task.project is None
assert task.content == []
assert task.completed is False
def test_task_to_dict(self):
"""Test converting task to dictionary"""
task = MarkdownTask("Test Task")
task.tags = ["work", "urgent"]
task.project = "Work Tasks"
task.content = ["Line 1", "Line 2"]
task.completed = False
result = task.to_dict()
assert result['title'] == "Test Task"
assert result['tags'] == ["work", "urgent"]
assert result['project'] == "Work Tasks"
assert result['content'] == "Line 1\nLine 2"
assert result['completed'] is False
def test_task_to_dict_empty_content(self):
"""Test converting task with no content"""
task = MarkdownTask("Simple Task")
result = task.to_dict()
assert result['content'] == ''
def test_task_repr(self):
"""Test task string representation"""
task = MarkdownTask("Test Task")
task.tags = ["test"]
task.project = "Test Project"
repr_str = repr(task)
assert "Test Task" in repr_str
assert "['test']" in repr_str
assert "Test Project" in repr_str
@pytest.mark.unit
class TestMarkdownTaskParser:
"""Test the MarkdownTaskParser class"""
def test_parser_initialization(self):
"""Test parser initialization"""
parser = MarkdownTaskParser()
assert parser.tasks == []
def test_parse_simple_task(self):
"""Test parsing a simple task without metadata"""
parser = MarkdownTaskParser()
content = "- [ ] Simple task"
tasks = parser.parse_content(content)
assert len(tasks) == 1
assert tasks[0].title == "Simple task"
assert tasks[0].tags == []
assert tasks[0].project is None
assert tasks[0].completed is False
def test_parse_completed_task(self):
"""Test parsing a completed task"""
parser = MarkdownTaskParser()
content = "- [x] Completed task"
tasks = parser.parse_content(content)
assert len(tasks) == 1
assert tasks[0].completed is True
def test_parse_completed_task_uppercase(self):
"""Test parsing completed task with uppercase X"""
parser = MarkdownTaskParser()
content = "- [X] Completed task"
tasks = parser.parse_content(content)
assert len(tasks) == 1
assert tasks[0].completed is True
def test_parse_task_with_tags(self):
"""Test parsing task with tags"""
parser = MarkdownTaskParser()
content = """- [ ] Task with tags
Tags: work, urgent, important"""
tasks = parser.parse_content(content)
assert len(tasks) == 1
assert tasks[0].tags == ["work", "urgent", "important"]
def test_parse_task_with_project(self):
"""Test parsing task with project"""
parser = MarkdownTaskParser()
content = """- [ ] Task with project
Project: Work Tasks"""
tasks = parser.parse_content(content)
assert len(tasks) == 1
assert tasks[0].project == "Work Tasks"
def test_parse_task_with_tags_and_project(self):
"""Test parsing task with both tags and project"""
parser = MarkdownTaskParser()
content = """- [ ] Complete task
Tags: work, priority
Project: Important Projects"""
tasks = parser.parse_content(content)
assert len(tasks) == 1
assert tasks[0].tags == ["work", "priority"]
assert tasks[0].project == "Important Projects"
def test_parse_task_with_description(self):
"""Test parsing task with description"""
parser = MarkdownTaskParser()
content = """- [ ] Task with description
Description: This is a description"""
tasks = parser.parse_content(content)
assert len(tasks) == 1
assert tasks[0].content == ["This is a description"]
def test_parse_task_with_multiline_content(self):
"""Test parsing task with multi-line content"""
parser = MarkdownTaskParser()
content = """- [ ] Task with content
Tags: test
Line 1 of content
Line 2 of content"""
tasks = parser.parse_content(content)
assert len(tasks) == 1
assert "Line 1 of content" in tasks[0].content
assert "Line 2 of content" in tasks[0].content
def test_parse_multiple_tasks(self):
"""Test parsing multiple tasks"""
parser = MarkdownTaskParser()
content = """- [ ] Task 1
Tags: tag1
- [ ] Task 2
Tags: tag2
- [ ] Task 3"""
tasks = parser.parse_content(content)
assert len(tasks) == 3
assert tasks[0].title == "Task 1"
assert tasks[1].title == "Task 2"
assert tasks[2].title == "Task 3"
def test_parse_tasks_separated_by_empty_lines(self):
"""Test parsing tasks separated by empty lines"""
parser = MarkdownTaskParser()
content = """- [ ] Task 1
- [ ] Task 2
- [ ] Task 3"""
tasks = parser.parse_content(content)
assert len(tasks) == 3
def test_parse_with_markdown_headers(self):
"""Test parsing with markdown headers (should be ignored)"""
parser = MarkdownTaskParser()
content = """# My Tasks
- [ ] Task 1
## Section 2
- [ ] Task 2"""
tasks = parser.parse_content(content)
assert len(tasks) == 2
assert tasks[0].title == "Task 1"
assert tasks[1].title == "Task 2"
def test_parse_asterisk_bullet_style(self):
"""Test parsing with asterisk bullet style"""
parser = MarkdownTaskParser()
content = "* [ ] Task with asterisk"
tasks = parser.parse_content(content)
assert len(tasks) == 1
assert tasks[0].title == "Task with asterisk"
def test_parse_tags_with_extra_whitespace(self):
"""Test parsing tags with extra whitespace"""
parser = MarkdownTaskParser()
content = """- [ ] Task
Tags: tag1 , tag2 , tag3 """
tasks = parser.parse_content(content)
assert len(tasks) == 1
assert tasks[0].tags == ["tag1", "tag2", "tag3"]
def test_parse_empty_content(self):
"""Test parsing empty content"""
parser = MarkdownTaskParser()
content = ""
tasks = parser.parse_content(content)
assert len(tasks) == 0
def test_parse_from_file(self, tmp_path):
"""Test parsing from a file"""
parser = MarkdownTaskParser()
# Create temporary markdown file
md_file = tmp_path / "test_tasks.md"
md_file.write_text("""- [ ] Task from file
Tags: file-test""")
tasks = parser.parse_file(str(md_file))
assert len(tasks) == 1
assert tasks[0].title == "Task from file"
assert tasks[0].tags == ["file-test"]
def test_get_summary(self, sample_markdown_content):
"""Test getting parser summary statistics"""
parser = MarkdownTaskParser()
tasks = parser.parse_content(sample_markdown_content)
summary = parser.get_summary()
assert summary['total_tasks'] == len(tasks)
assert summary['completed_tasks'] >= 0
assert summary['tasks_with_tags'] >= 0
assert summary['tasks_with_project'] >= 0
assert 'unique_tags' in summary
assert 'unique_projects' in summary
def test_parse_complex_markdown(self, sample_markdown_content):
"""Test parsing complex markdown with various formats"""
parser = MarkdownTaskParser()
tasks = parser.parse_content(sample_markdown_content)
# Should parse multiple tasks
assert len(tasks) > 0
# Check that completed tasks are marked
completed = [t for t in tasks if t.completed]
assert len(completed) >= 1
# Check that tags are parsed
tasks_with_tags = [t for t in tasks if t.tags]
assert len(tasks_with_tags) >= 1
# Check that projects are parsed
tasks_with_project = [t for t in tasks if t.project]
assert len(tasks_with_project) >= 1