ready for try
This commit is contained in:
151
tests/models/test_base.py
Normal file
151
tests/models/test_base.py
Normal file
@@ -0,0 +1,151 @@
|
||||
"""Tests for base model functionality."""
|
||||
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from wikijs.models.base import BaseModel, TimestampedModel
|
||||
|
||||
|
||||
class TestModelForTesting(BaseModel):
|
||||
"""Test model for testing base functionality."""
|
||||
|
||||
name: str
|
||||
value: int = 42
|
||||
optional_field: str = None
|
||||
|
||||
|
||||
class TestTimestampedModelForTesting(TimestampedModel):
|
||||
"""Test model with timestamps."""
|
||||
|
||||
title: str
|
||||
|
||||
|
||||
class TestBaseModel:
|
||||
"""Test base model functionality."""
|
||||
|
||||
def test_basic_model_creation(self):
|
||||
"""Test basic model creation."""
|
||||
model = TestModelForTesting(name="test", value=100)
|
||||
assert model.name == "test"
|
||||
assert model.value == 100
|
||||
assert model.optional_field is None
|
||||
|
||||
def test_to_dict_basic(self):
|
||||
"""Test to_dict method."""
|
||||
model = TestModelForTesting(name="test", value=100)
|
||||
result = model.to_dict()
|
||||
|
||||
expected = {"name": "test", "value": 100}
|
||||
assert result == expected
|
||||
|
||||
def test_to_dict_with_none_values(self):
|
||||
"""Test to_dict with None values."""
|
||||
model = TestModelForTesting(name="test", value=100)
|
||||
|
||||
# Test excluding None values (default)
|
||||
result_exclude = model.to_dict(exclude_none=True)
|
||||
expected_exclude = {"name": "test", "value": 100}
|
||||
assert result_exclude == expected_exclude
|
||||
|
||||
# Test including None values
|
||||
result_include = model.to_dict(exclude_none=False)
|
||||
expected_include = {"name": "test", "value": 100, "optional_field": None}
|
||||
assert result_include == expected_include
|
||||
|
||||
def test_to_json_basic(self):
|
||||
"""Test to_json method."""
|
||||
model = TestModelForTesting(name="test", value=100)
|
||||
result = model.to_json()
|
||||
|
||||
# Parse the JSON to verify structure
|
||||
parsed = json.loads(result)
|
||||
expected = {"name": "test", "value": 100}
|
||||
assert parsed == expected
|
||||
|
||||
def test_to_json_with_none_values(self):
|
||||
"""Test to_json with None values."""
|
||||
model = TestModelForTesting(name="test", value=100)
|
||||
|
||||
# Test excluding None values (default)
|
||||
result_exclude = model.to_json(exclude_none=True)
|
||||
parsed_exclude = json.loads(result_exclude)
|
||||
expected_exclude = {"name": "test", "value": 100}
|
||||
assert parsed_exclude == expected_exclude
|
||||
|
||||
# Test including None values
|
||||
result_include = model.to_json(exclude_none=False)
|
||||
parsed_include = json.loads(result_include)
|
||||
expected_include = {"name": "test", "value": 100, "optional_field": None}
|
||||
assert parsed_include == expected_include
|
||||
|
||||
def test_from_dict(self):
|
||||
"""Test from_dict class method."""
|
||||
data = {"name": "test", "value": 200}
|
||||
model = TestModelForTesting.from_dict(data)
|
||||
|
||||
assert isinstance(model, TestModelForTesting)
|
||||
assert model.name == "test"
|
||||
assert model.value == 200
|
||||
|
||||
def test_from_json(self):
|
||||
"""Test from_json class method."""
|
||||
json_str = '{"name": "test", "value": 300}'
|
||||
model = TestModelForTesting.from_json(json_str)
|
||||
|
||||
assert isinstance(model, TestModelForTesting)
|
||||
assert model.name == "test"
|
||||
assert model.value == 300
|
||||
|
||||
|
||||
class TestTimestampedModel:
|
||||
"""Test timestamped model functionality."""
|
||||
|
||||
def test_timestamped_model_creation(self):
|
||||
"""Test timestamped model creation."""
|
||||
model = TestTimestampedModelForTesting(title="Test Title")
|
||||
assert model.title == "Test Title"
|
||||
assert model.created_at is None
|
||||
assert model.updated_at is None
|
||||
|
||||
def test_timestamped_model_with_timestamps(self):
|
||||
"""Test timestamped model with timestamps."""
|
||||
now = datetime.now()
|
||||
model = TestTimestampedModelForTesting(
|
||||
title="Test Title",
|
||||
created_at=now,
|
||||
updated_at=now
|
||||
)
|
||||
assert model.title == "Test Title"
|
||||
assert model.created_at == now
|
||||
assert model.updated_at == now
|
||||
|
||||
def test_is_new_property_true(self):
|
||||
"""Test is_new property returns True for new models."""
|
||||
model = TestTimestampedModelForTesting(title="Test Title")
|
||||
assert model.is_new is True
|
||||
|
||||
def test_is_new_property_false(self):
|
||||
"""Test is_new property returns False for existing models."""
|
||||
now = datetime.now()
|
||||
model = TestTimestampedModelForTesting(
|
||||
title="Test Title",
|
||||
created_at=now
|
||||
)
|
||||
assert model.is_new is False
|
||||
|
||||
def test_datetime_serialization(self):
|
||||
"""Test datetime serialization in JSON."""
|
||||
now = datetime(2023, 1, 1, 12, 0, 0)
|
||||
model = TestTimestampedModelForTesting(
|
||||
title="Test Title",
|
||||
created_at=now,
|
||||
updated_at=now
|
||||
)
|
||||
|
||||
json_str = model.to_json()
|
||||
parsed = json.loads(json_str)
|
||||
|
||||
assert parsed["created_at"] == "2023-01-01T12:00:00"
|
||||
assert parsed["updated_at"] == "2023-01-01T12:00:00"
|
||||
372
tests/models/test_page.py
Normal file
372
tests/models/test_page.py
Normal file
@@ -0,0 +1,372 @@
|
||||
"""Tests for Page models."""
|
||||
|
||||
import pytest
|
||||
from datetime import datetime
|
||||
|
||||
from wikijs.models.page import Page, PageCreate, PageUpdate
|
||||
|
||||
|
||||
class TestPageModel:
|
||||
"""Test Page model functionality."""
|
||||
|
||||
@pytest.fixture
|
||||
def valid_page_data(self):
|
||||
"""Valid page data for testing."""
|
||||
return {
|
||||
"id": 123,
|
||||
"title": "Test Page",
|
||||
"path": "test-page",
|
||||
"content": "# Test Page\n\nThis is test content with **bold** and *italic* text.",
|
||||
"description": "A test page",
|
||||
"is_published": True,
|
||||
"is_private": False,
|
||||
"tags": ["test", "example"],
|
||||
"locale": "en",
|
||||
"author_id": 1,
|
||||
"author_name": "Test User",
|
||||
"author_email": "test@example.com",
|
||||
"editor": "markdown",
|
||||
"created_at": "2023-01-01T00:00:00Z",
|
||||
"updated_at": "2023-01-02T00:00:00Z"
|
||||
}
|
||||
|
||||
def test_page_creation_valid(self, valid_page_data):
|
||||
"""Test creating a valid page."""
|
||||
page = Page(**valid_page_data)
|
||||
|
||||
assert page.id == 123
|
||||
assert page.title == "Test Page"
|
||||
assert page.path == "test-page"
|
||||
assert page.content == "# Test Page\n\nThis is test content with **bold** and *italic* text."
|
||||
assert page.is_published is True
|
||||
assert page.tags == ["test", "example"]
|
||||
|
||||
def test_page_creation_minimal(self):
|
||||
"""Test creating page with minimal required fields."""
|
||||
page = Page(
|
||||
id=1,
|
||||
title="Minimal Page",
|
||||
path="minimal",
|
||||
content="Content",
|
||||
created_at="2023-01-01T00:00:00Z",
|
||||
updated_at="2023-01-01T00:00:00Z"
|
||||
)
|
||||
|
||||
assert page.id == 1
|
||||
assert page.title == "Minimal Page"
|
||||
assert page.is_published is True # Default value
|
||||
assert page.tags == [] # Default value
|
||||
|
||||
def test_page_path_validation_valid(self):
|
||||
"""Test valid path validation."""
|
||||
valid_paths = [
|
||||
"simple-path",
|
||||
"path/with/slashes",
|
||||
"path_with_underscores",
|
||||
"path123",
|
||||
"category/subcategory/page-name"
|
||||
]
|
||||
|
||||
for path in valid_paths:
|
||||
page = Page(
|
||||
id=1,
|
||||
title="Test",
|
||||
path=path,
|
||||
content="Content",
|
||||
created_at="2023-01-01T00:00:00Z",
|
||||
updated_at="2023-01-01T00:00:00Z"
|
||||
)
|
||||
assert page.path == path
|
||||
|
||||
def test_page_path_validation_invalid(self):
|
||||
"""Test invalid path validation."""
|
||||
invalid_paths = [
|
||||
"", # Empty
|
||||
"path with spaces", # Spaces
|
||||
"path@with@symbols", # Special characters
|
||||
"path.with.dots", # Dots
|
||||
]
|
||||
|
||||
for path in invalid_paths:
|
||||
with pytest.raises(ValueError):
|
||||
Page(
|
||||
id=1,
|
||||
title="Test",
|
||||
path=path,
|
||||
content="Content",
|
||||
created_at="2023-01-01T00:00:00Z",
|
||||
updated_at="2023-01-01T00:00:00Z"
|
||||
)
|
||||
|
||||
def test_page_path_normalization(self):
|
||||
"""Test path normalization."""
|
||||
# Leading/trailing slashes should be removed
|
||||
page = Page(
|
||||
id=1,
|
||||
title="Test",
|
||||
path="/path/to/page/",
|
||||
content="Content",
|
||||
created_at="2023-01-01T00:00:00Z",
|
||||
updated_at="2023-01-01T00:00:00Z"
|
||||
)
|
||||
assert page.path == "path/to/page"
|
||||
|
||||
def test_page_title_validation_valid(self):
|
||||
"""Test valid title validation."""
|
||||
page = Page(
|
||||
id=1,
|
||||
title=" Valid Title with Spaces ", # Should be trimmed
|
||||
path="test",
|
||||
content="Content",
|
||||
created_at="2023-01-01T00:00:00Z",
|
||||
updated_at="2023-01-01T00:00:00Z"
|
||||
)
|
||||
assert page.title == "Valid Title with Spaces"
|
||||
|
||||
def test_page_title_validation_invalid(self):
|
||||
"""Test invalid title validation."""
|
||||
invalid_titles = [
|
||||
"", # Empty
|
||||
" ", # Only whitespace
|
||||
"x" * 256, # Too long
|
||||
]
|
||||
|
||||
for title in invalid_titles:
|
||||
with pytest.raises(ValueError):
|
||||
Page(
|
||||
id=1,
|
||||
title=title,
|
||||
path="test",
|
||||
content="Content",
|
||||
created_at="2023-01-01T00:00:00Z",
|
||||
updated_at="2023-01-01T00:00:00Z"
|
||||
)
|
||||
|
||||
def test_page_word_count(self, valid_page_data):
|
||||
"""Test word count calculation."""
|
||||
page = Page(**valid_page_data)
|
||||
# "# Test Page\n\nThis is test content with **bold** and *italic* text."
|
||||
# Words: Test, Page, This, is, test, content, with, bold, and, italic, text
|
||||
assert page.word_count == 12
|
||||
|
||||
def test_page_word_count_empty_content(self):
|
||||
"""Test word count with empty content."""
|
||||
page = Page(
|
||||
id=1,
|
||||
title="Test",
|
||||
path="test",
|
||||
content="",
|
||||
created_at="2023-01-01T00:00:00Z",
|
||||
updated_at="2023-01-01T00:00:00Z"
|
||||
)
|
||||
assert page.word_count == 0
|
||||
|
||||
def test_page_reading_time(self, valid_page_data):
|
||||
"""Test reading time calculation."""
|
||||
page = Page(**valid_page_data)
|
||||
# 11 words, assuming 200 words per minute, should be 1 minute (minimum)
|
||||
assert page.reading_time == 1
|
||||
|
||||
def test_page_reading_time_long_content(self):
|
||||
"""Test reading time with long content."""
|
||||
long_content = " ".join(["word"] * 500) # 500 words
|
||||
page = Page(
|
||||
id=1,
|
||||
title="Test",
|
||||
path="test",
|
||||
content=long_content,
|
||||
created_at="2023-01-01T00:00:00Z",
|
||||
updated_at="2023-01-01T00:00:00Z"
|
||||
)
|
||||
# 500 words / 200 words per minute = 2.5, rounded down to 2
|
||||
assert page.reading_time == 2
|
||||
|
||||
def test_page_url_path(self, valid_page_data):
|
||||
"""Test URL path generation."""
|
||||
page = Page(**valid_page_data)
|
||||
assert page.url_path == "/test-page"
|
||||
|
||||
def test_page_extract_headings(self):
|
||||
"""Test heading extraction from markdown content."""
|
||||
content = """# Main Title
|
||||
|
||||
Some content here.
|
||||
|
||||
## Secondary Heading
|
||||
|
||||
More content.
|
||||
|
||||
### Third Level
|
||||
|
||||
And more content.
|
||||
|
||||
## Another Secondary
|
||||
|
||||
Final content."""
|
||||
|
||||
page = Page(
|
||||
id=1,
|
||||
title="Test",
|
||||
path="test",
|
||||
content=content,
|
||||
created_at="2023-01-01T00:00:00Z",
|
||||
updated_at="2023-01-01T00:00:00Z"
|
||||
)
|
||||
|
||||
headings = page.extract_headings()
|
||||
expected = ["Main Title", "Secondary Heading", "Third Level", "Another Secondary"]
|
||||
assert headings == expected
|
||||
|
||||
def test_page_extract_headings_empty_content(self):
|
||||
"""Test heading extraction with no content."""
|
||||
page = Page(
|
||||
id=1,
|
||||
title="Test",
|
||||
path="test",
|
||||
content="",
|
||||
created_at="2023-01-01T00:00:00Z",
|
||||
updated_at="2023-01-01T00:00:00Z"
|
||||
)
|
||||
assert page.extract_headings() == []
|
||||
|
||||
def test_page_has_tag(self, valid_page_data):
|
||||
"""Test tag checking."""
|
||||
page = Page(**valid_page_data)
|
||||
|
||||
assert page.has_tag("test") is True
|
||||
assert page.has_tag("example") is True
|
||||
assert page.has_tag("TEST") is True # Case insensitive
|
||||
assert page.has_tag("nonexistent") is False
|
||||
|
||||
def test_page_has_tag_no_tags(self):
|
||||
"""Test tag checking with no tags."""
|
||||
page = Page(
|
||||
id=1,
|
||||
title="Test",
|
||||
path="test",
|
||||
content="Content",
|
||||
created_at="2023-01-01T00:00:00Z",
|
||||
updated_at="2023-01-01T00:00:00Z"
|
||||
)
|
||||
assert page.has_tag("any") is False
|
||||
|
||||
|
||||
class TestPageCreateModel:
|
||||
"""Test PageCreate model functionality."""
|
||||
|
||||
def test_page_create_valid(self):
|
||||
"""Test creating valid PageCreate."""
|
||||
page_create = PageCreate(
|
||||
title="New Page",
|
||||
path="new-page",
|
||||
content="# New Page\n\nContent here."
|
||||
)
|
||||
|
||||
assert page_create.title == "New Page"
|
||||
assert page_create.path == "new-page"
|
||||
assert page_create.content == "# New Page\n\nContent here."
|
||||
assert page_create.is_published is True # Default
|
||||
assert page_create.editor == "markdown" # Default
|
||||
|
||||
def test_page_create_with_optional_fields(self):
|
||||
"""Test PageCreate with optional fields."""
|
||||
page_create = PageCreate(
|
||||
title="New Page",
|
||||
path="new-page",
|
||||
content="Content",
|
||||
description="A new page",
|
||||
is_published=False,
|
||||
is_private=True,
|
||||
tags=["new", "test"],
|
||||
locale="fr",
|
||||
editor="html"
|
||||
)
|
||||
|
||||
assert page_create.description == "A new page"
|
||||
assert page_create.is_published is False
|
||||
assert page_create.is_private is True
|
||||
assert page_create.tags == ["new", "test"]
|
||||
assert page_create.locale == "fr"
|
||||
assert page_create.editor == "html"
|
||||
|
||||
def test_page_create_path_validation(self):
|
||||
"""Test path validation in PageCreate."""
|
||||
# Valid path
|
||||
PageCreate(title="Test", path="valid-path", content="Content")
|
||||
|
||||
# Invalid paths should raise errors
|
||||
with pytest.raises(ValueError):
|
||||
PageCreate(title="Test", path="", content="Content")
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
PageCreate(title="Test", path="invalid path", content="Content")
|
||||
|
||||
def test_page_create_title_validation(self):
|
||||
"""Test title validation in PageCreate."""
|
||||
# Valid title
|
||||
PageCreate(title="Valid Title", path="test", content="Content")
|
||||
|
||||
# Invalid titles should raise errors
|
||||
with pytest.raises(ValueError):
|
||||
PageCreate(title="", path="test", content="Content")
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
PageCreate(title="x" * 256, path="test", content="Content")
|
||||
|
||||
|
||||
class TestPageUpdateModel:
|
||||
"""Test PageUpdate model functionality."""
|
||||
|
||||
def test_page_update_empty(self):
|
||||
"""Test creating empty PageUpdate."""
|
||||
page_update = PageUpdate()
|
||||
|
||||
assert page_update.title is None
|
||||
assert page_update.content is None
|
||||
assert page_update.description is None
|
||||
assert page_update.is_published is None
|
||||
assert page_update.tags is None
|
||||
|
||||
def test_page_update_partial(self):
|
||||
"""Test partial PageUpdate."""
|
||||
page_update = PageUpdate(
|
||||
title="Updated Title",
|
||||
content="Updated content"
|
||||
)
|
||||
|
||||
assert page_update.title == "Updated Title"
|
||||
assert page_update.content == "Updated content"
|
||||
assert page_update.description is None # Not updated
|
||||
|
||||
def test_page_update_full(self):
|
||||
"""Test full PageUpdate."""
|
||||
page_update = PageUpdate(
|
||||
title="Updated Title",
|
||||
content="Updated content",
|
||||
description="Updated description",
|
||||
is_published=False,
|
||||
is_private=True,
|
||||
tags=["updated", "test"]
|
||||
)
|
||||
|
||||
assert page_update.title == "Updated Title"
|
||||
assert page_update.content == "Updated content"
|
||||
assert page_update.description == "Updated description"
|
||||
assert page_update.is_published is False
|
||||
assert page_update.is_private is True
|
||||
assert page_update.tags == ["updated", "test"]
|
||||
|
||||
def test_page_update_title_validation(self):
|
||||
"""Test title validation in PageUpdate."""
|
||||
# Valid title
|
||||
PageUpdate(title="Valid Title")
|
||||
|
||||
# None should be allowed (no update)
|
||||
PageUpdate(title=None)
|
||||
|
||||
# Invalid titles should raise errors
|
||||
with pytest.raises(ValueError):
|
||||
PageUpdate(title="")
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
PageUpdate(title="x" * 256)
|
||||
Reference in New Issue
Block a user