test: add tests
This commit is contained in:
@@ -0,0 +1,230 @@
|
||||
"""Tests for paperlib data models."""
|
||||
|
||||
import json
|
||||
import tempfile
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from paperlib.models import (
|
||||
ConversionStatus,
|
||||
PaperMetadata,
|
||||
PaperSummary,
|
||||
SourceType,
|
||||
SummaryStatus,
|
||||
)
|
||||
|
||||
|
||||
class TestPaperMetadata:
|
||||
"""Test PaperMetadata data model."""
|
||||
|
||||
def test_create_metadata(self):
|
||||
"""Test creating a PaperMetadata instance."""
|
||||
metadata = PaperMetadata(
|
||||
paper_id="test-paper-1",
|
||||
source_type=SourceType.LOCAL,
|
||||
title="Test Paper",
|
||||
authors=["Alice Smith", "Bob Jones"],
|
||||
categories=["cs.AI", "stat.ML"],
|
||||
tags=["machine-learning", "ai"],
|
||||
notes="Test notes",
|
||||
)
|
||||
|
||||
assert metadata.paper_id == "test-paper-1"
|
||||
assert metadata.source_type == SourceType.LOCAL
|
||||
assert metadata.title == "Test Paper"
|
||||
assert metadata.authors == ["Alice Smith", "Bob Jones"]
|
||||
assert metadata.categories == ["cs.AI", "stat.ML"]
|
||||
assert metadata.tags == ["machine-learning", "ai"]
|
||||
assert metadata.notes == "Test notes"
|
||||
assert metadata.conversion_status == ConversionStatus.PENDING
|
||||
assert metadata.summary_status == SummaryStatus.NOT_REQUESTED
|
||||
|
||||
def test_to_dict(self):
|
||||
"""Test converting metadata to dictionary."""
|
||||
metadata = PaperMetadata(
|
||||
paper_id="test-paper-1",
|
||||
source_type=SourceType.ARXIV,
|
||||
source_id="2212.06340",
|
||||
title="Test Paper",
|
||||
published_date=datetime(2022, 12, 13, 2, 46, 55),
|
||||
)
|
||||
|
||||
data = metadata.to_dict()
|
||||
|
||||
assert data["paper_id"] == "test-paper-1"
|
||||
assert data["source_type"] == "arxiv"
|
||||
assert data["source_id"] == "2212.06340"
|
||||
assert data["title"] == "Test Paper"
|
||||
assert data["published_date"] == "2022-12-13T02:46:55"
|
||||
|
||||
def test_from_dict(self):
|
||||
"""Test creating metadata from dictionary."""
|
||||
data = {
|
||||
"paper_id": "test-paper-1",
|
||||
"source_type": "local",
|
||||
"title": "Test Paper",
|
||||
"authors": ["Alice Smith"],
|
||||
"published_date": "2022-12-13T02:46:55",
|
||||
"categories": ["cs.AI"],
|
||||
"pdf_path": "papers/test.pdf",
|
||||
"imported_at": "2022-12-13T02:46:55",
|
||||
"conversion_status": "success",
|
||||
"summary_status": "pending",
|
||||
"tags": ["test"],
|
||||
"notes": "Test notes",
|
||||
}
|
||||
|
||||
metadata = PaperMetadata.from_dict(data)
|
||||
|
||||
assert metadata.paper_id == "test-paper-1"
|
||||
assert metadata.source_type == SourceType.LOCAL
|
||||
assert metadata.title == "Test Paper"
|
||||
assert metadata.authors == ["Alice Smith"]
|
||||
assert metadata.published_date == datetime(2022, 12, 13, 2, 46, 55)
|
||||
assert metadata.conversion_status == ConversionStatus.SUCCESS
|
||||
assert metadata.summary_status == SummaryStatus.PENDING
|
||||
|
||||
def test_save_and_load_file(self):
|
||||
"""Test saving and loading metadata from file."""
|
||||
metadata = PaperMetadata(
|
||||
paper_id="test-paper-1",
|
||||
source_type=SourceType.LOCAL,
|
||||
title="Test Paper",
|
||||
authors=["Alice Smith"],
|
||||
)
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as tmp:
|
||||
tmp_path = Path(tmp.name)
|
||||
|
||||
try:
|
||||
# Save to file
|
||||
metadata.save_to_file(tmp_path)
|
||||
|
||||
# Verify file exists and contains JSON
|
||||
assert tmp_path.exists()
|
||||
with tmp_path.open() as f:
|
||||
data = json.load(f)
|
||||
assert data["paper_id"] == "test-paper-1"
|
||||
|
||||
# Load from file
|
||||
loaded_metadata = PaperMetadata.load_from_file(tmp_path)
|
||||
assert loaded_metadata.paper_id == "test-paper-1"
|
||||
assert loaded_metadata.title == "Test Paper"
|
||||
assert loaded_metadata.source_type == SourceType.LOCAL
|
||||
|
||||
finally:
|
||||
if tmp_path.exists():
|
||||
tmp_path.unlink()
|
||||
|
||||
|
||||
class TestPaperSummary:
|
||||
"""Test PaperSummary data model."""
|
||||
|
||||
def test_create_summary(self):
|
||||
"""Test creating a PaperSummary instance."""
|
||||
summary = PaperSummary(
|
||||
one_sentence_summary="This paper introduces a new method.",
|
||||
problem_statement="Current methods are inefficient.",
|
||||
method_overview="We propose a novel approach.",
|
||||
main_results="Our method achieves 95% accuracy.",
|
||||
claimed_contributions=["Novel architecture", "Improved performance"],
|
||||
problem_tags=["classification", "optimization"],
|
||||
technique_tags=["neural-networks", "reinforcement-learning"],
|
||||
)
|
||||
|
||||
assert summary.schema_version == "1.0"
|
||||
assert summary.one_sentence_summary == "This paper introduces a new method."
|
||||
assert summary.problem_statement == "Current methods are inefficient."
|
||||
assert summary.claimed_contributions == [
|
||||
"Novel architecture",
|
||||
"Improved performance",
|
||||
]
|
||||
assert summary.problem_tags == ["classification", "optimization"]
|
||||
|
||||
def test_to_dict(self):
|
||||
"""Test converting summary to dictionary."""
|
||||
summary = PaperSummary(
|
||||
one_sentence_summary="Test summary",
|
||||
relevance_to_user=0.85,
|
||||
)
|
||||
|
||||
data = summary.to_dict()
|
||||
|
||||
assert data["schema_version"] == "1.0"
|
||||
assert data["one_sentence_summary"] == "Test summary"
|
||||
assert data["relevance_to_user"] == 0.85
|
||||
|
||||
def test_from_dict(self):
|
||||
"""Test creating summary from dictionary."""
|
||||
data = {
|
||||
"schema_version": "1.0",
|
||||
"one_sentence_summary": "Test summary",
|
||||
"problem_statement": "Test problem",
|
||||
"claimed_contributions": ["Test contribution"],
|
||||
"problem_tags": ["test"],
|
||||
"technique_tags": ["neural-networks"],
|
||||
"entities": ["Entity1", "Entity2"],
|
||||
}
|
||||
|
||||
summary = PaperSummary.from_dict(data)
|
||||
|
||||
assert summary.schema_version == "1.0"
|
||||
assert summary.one_sentence_summary == "Test summary"
|
||||
assert summary.problem_statement == "Test problem"
|
||||
assert summary.claimed_contributions == ["Test contribution"]
|
||||
assert summary.entities == ["Entity1", "Entity2"]
|
||||
|
||||
def test_save_and_load_file(self):
|
||||
"""Test saving and loading summary from file."""
|
||||
summary = PaperSummary(
|
||||
one_sentence_summary="Test summary",
|
||||
problem_tags=["tag1", "tag2"],
|
||||
)
|
||||
|
||||
with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as tmp:
|
||||
tmp_path = Path(tmp.name)
|
||||
|
||||
try:
|
||||
# Save to file
|
||||
summary.save_to_file(tmp_path)
|
||||
|
||||
# Verify file exists and contains JSON
|
||||
assert tmp_path.exists()
|
||||
with tmp_path.open() as f:
|
||||
data = json.load(f)
|
||||
assert data["one_sentence_summary"] == "Test summary"
|
||||
|
||||
# Load from file
|
||||
loaded_summary = PaperSummary.load_from_file(tmp_path)
|
||||
assert loaded_summary.one_sentence_summary == "Test summary"
|
||||
assert loaded_summary.problem_tags == ["tag1", "tag2"]
|
||||
|
||||
finally:
|
||||
if tmp_path.exists():
|
||||
tmp_path.unlink()
|
||||
|
||||
|
||||
class TestEnums:
|
||||
"""Test enum types."""
|
||||
|
||||
def test_source_type_values(self):
|
||||
"""Test SourceType enum values."""
|
||||
assert SourceType.LOCAL == "local"
|
||||
assert SourceType.ARXIV == "arxiv"
|
||||
|
||||
def test_conversion_status_values(self):
|
||||
"""Test ConversionStatus enum values."""
|
||||
assert ConversionStatus.PENDING == "pending"
|
||||
assert ConversionStatus.PROCESSING == "processing"
|
||||
assert ConversionStatus.SUCCESS == "success"
|
||||
assert ConversionStatus.FAILED == "failed"
|
||||
|
||||
def test_summary_status_values(self):
|
||||
"""Test SummaryStatus enum values."""
|
||||
assert SummaryStatus.PENDING == "pending"
|
||||
assert SummaryStatus.PROCESSING == "processing"
|
||||
assert SummaryStatus.SUCCESS == "success"
|
||||
assert SummaryStatus.FAILED == "failed"
|
||||
assert SummaryStatus.NOT_REQUESTED == "not_requested"
|
||||
Reference in New Issue
Block a user