Linted and formatted everything new
This commit is contained in:
@@ -4,16 +4,17 @@ Test configuration and fixtures for HXBooks.
|
||||
Provides isolated test database, Flask app instances, and CLI testing utilities.
|
||||
"""
|
||||
|
||||
import tempfile
|
||||
from collections.abc import Generator
|
||||
from pathlib import Path
|
||||
from typing import Generator
|
||||
|
||||
import pytest
|
||||
from click.testing import CliRunner
|
||||
from flask import Flask
|
||||
from flask.testing import FlaskClient
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from hxbooks import cli, create_app
|
||||
from hxbooks import cli
|
||||
from hxbooks.app import create_app
|
||||
from hxbooks.db import db
|
||||
from hxbooks.models import User
|
||||
|
||||
@@ -64,7 +65,7 @@ def test_user(app: Flask) -> User:
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def db_session(app: Flask):
|
||||
def db_session(app: Flask) -> Generator[Session]:
|
||||
"""Create database session for direct database testing."""
|
||||
with app.app_context():
|
||||
yield db.session
|
||||
|
||||
@@ -6,22 +6,21 @@ Tests all CLI commands for correct behavior, database integration, and output fo
|
||||
|
||||
import json
|
||||
import re
|
||||
import tempfile
|
||||
from datetime import date
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from click.testing import CliRunner
|
||||
from flask import Flask
|
||||
|
||||
from hxbooks.cli import cli
|
||||
from hxbooks.db import db
|
||||
from hxbooks.models import Author, Book, Genre, Reading, User, Wishlist
|
||||
from hxbooks.models import Author, Book, Genre, Reading, User
|
||||
|
||||
|
||||
class TestBookAddCommand:
|
||||
"""Test the 'hxbooks book add' command."""
|
||||
|
||||
def test_book_add_basic(self, app, cli_runner):
|
||||
def test_book_add_basic(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test basic book addition with title and owner."""
|
||||
# Run the CLI command
|
||||
result = cli_runner.invoke(
|
||||
@@ -70,7 +69,8 @@ class TestBookAddCommand:
|
||||
|
||||
# Check book was created with correct fields
|
||||
books = (
|
||||
db.session.execute(db.select(Book).join(Book.authors).join(Book.genres))
|
||||
db.session
|
||||
.execute(db.select(Book).join(Book.authors).join(Book.genres))
|
||||
.unique()
|
||||
.scalars()
|
||||
.all()
|
||||
@@ -106,7 +106,7 @@ class TestBookAddCommand:
|
||||
assert book in genre.books
|
||||
assert genre in book.genres
|
||||
|
||||
def test_book_add_minimal_fields(self, app, cli_runner):
|
||||
def test_book_add_minimal_fields(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test book addition with only required fields."""
|
||||
result = cli_runner.invoke(
|
||||
cli, ["book", "add", "Minimal Book", "--owner", "alice"]
|
||||
@@ -124,7 +124,9 @@ class TestBookAddCommand:
|
||||
assert len(book.authors) == 0 # No authors provided
|
||||
assert len(book.genres) == 0 # No genres provided
|
||||
|
||||
def test_book_add_missing_owner_fails(self, app, cli_runner):
|
||||
def test_book_add_missing_owner_fails(
|
||||
self, app: Flask, cli_runner: CliRunner
|
||||
) -> None:
|
||||
"""Test that book addition fails when owner is not provided."""
|
||||
result = cli_runner.invoke(
|
||||
cli,
|
||||
@@ -144,14 +146,14 @@ class TestBookAddCommand:
|
||||
class TestBookListCommand:
|
||||
"""Test the 'hxbooks book list' command."""
|
||||
|
||||
def test_book_list_empty(self, app, cli_runner):
|
||||
def test_book_list_empty(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test listing books when database is empty."""
|
||||
result = cli_runner.invoke(cli, ["book", "list"])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert "No books found." in result.output
|
||||
|
||||
def test_book_list_with_books(self, app, cli_runner):
|
||||
def test_book_list_with_books(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test listing books in table format."""
|
||||
# Add test data
|
||||
cli_runner.invoke(
|
||||
@@ -172,7 +174,7 @@ class TestBookListCommand:
|
||||
assert "alice" in result.output
|
||||
assert "bob" in result.output
|
||||
|
||||
def test_book_list_json_format(self, app, cli_runner):
|
||||
def test_book_list_json_format(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test listing books in JSON format."""
|
||||
# Add test data
|
||||
cli_runner.invoke(
|
||||
@@ -201,7 +203,7 @@ class TestBookListCommand:
|
||||
assert book["owner"] == "alice"
|
||||
assert book["isbn"] == "1234567890"
|
||||
|
||||
def test_book_list_filter_by_owner(self, app, cli_runner):
|
||||
def test_book_list_filter_by_owner(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test filtering books by owner."""
|
||||
# Add books for different owners
|
||||
cli_runner.invoke(cli, ["book", "add", "Alice Book", "--owner", "alice"])
|
||||
@@ -213,7 +215,9 @@ class TestBookListCommand:
|
||||
assert "Alice Book" in result.output
|
||||
assert "Bob Book" not in result.output
|
||||
|
||||
def test_book_list_filter_by_location(self, app, cli_runner):
|
||||
def test_book_list_filter_by_location(
|
||||
self, app: Flask, cli_runner: CliRunner
|
||||
) -> None:
|
||||
"""Test filtering books by location."""
|
||||
# Add books in different locations
|
||||
cli_runner.invoke(
|
||||
@@ -271,7 +275,7 @@ class TestBookListCommand:
|
||||
class TestBookSearchCommand:
|
||||
"""Test the 'hxbooks book search' command."""
|
||||
|
||||
def test_book_search_basic(self, app, cli_runner):
|
||||
def test_book_search_basic(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test basic book search functionality."""
|
||||
# Add test books
|
||||
cli_runner.invoke(
|
||||
@@ -309,14 +313,14 @@ class TestBookSearchCommand:
|
||||
assert "The Hobbit" in result.output
|
||||
assert "Dune" not in result.output
|
||||
|
||||
def test_book_search_no_results(self, app, cli_runner):
|
||||
def test_book_search_no_results(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test search with no matching results."""
|
||||
result = cli_runner.invoke(cli, ["book", "search", "nonexistent"])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert "No books found." in result.output
|
||||
|
||||
def test_book_search_json_format(self, app, cli_runner):
|
||||
def test_book_search_json_format(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test book search with JSON output."""
|
||||
cli_runner.invoke(
|
||||
cli,
|
||||
@@ -378,8 +382,8 @@ class TestBookSearchCommand:
|
||||
],
|
||||
)
|
||||
def test_book_search_advanced_queries(
|
||||
self, app, cli_runner, query, expected_titles
|
||||
):
|
||||
self, app: Flask, cli_runner: CliRunner, query: str, expected_titles: list[str]
|
||||
) -> None:
|
||||
"""Test advanced search queries with various field filters."""
|
||||
# Set up comprehensive test data
|
||||
self._setup_search_test_data(app, cli_runner)
|
||||
@@ -400,7 +404,7 @@ class TestBookSearchCommand:
|
||||
f"Query '{query}' expected {expected_titles}, got {actual_titles}"
|
||||
)
|
||||
|
||||
def _setup_search_test_data(self, app, cli_runner):
|
||||
def _setup_search_test_data(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Set up comprehensive test data for advanced search testing."""
|
||||
# Book 1: The Hobbit - Fantasy, high rating, shelf 1, home
|
||||
cli_runner.invoke(
|
||||
@@ -517,7 +521,8 @@ class TestBookSearchCommand:
|
||||
with app.app_context():
|
||||
# Get reading session IDs
|
||||
readings = (
|
||||
db.session.execute(db.select(Reading).order_by(Reading.id))
|
||||
db.session
|
||||
.execute(db.select(Reading).order_by(Reading.id))
|
||||
.scalars()
|
||||
.all()
|
||||
)
|
||||
@@ -539,16 +544,20 @@ class TestBookSearchCommand:
|
||||
# Update one book with bought_date for date filter testing
|
||||
with app.app_context():
|
||||
prog_book = db.session.get(Book, prog_id)
|
||||
assert prog_book is not None
|
||||
prog_book.bought_date = date(2025, 12, 1) # Before 2026-01-01
|
||||
prog_book.first_published = 2000
|
||||
|
||||
hobbit_book = db.session.get(Book, hobbit_id)
|
||||
assert hobbit_book is not None
|
||||
hobbit_book.first_published = 1937
|
||||
|
||||
fellowship_book = db.session.get(Book, fellowship_id)
|
||||
assert fellowship_book is not None
|
||||
fellowship_book.first_published = 1954
|
||||
|
||||
dune_book = db.session.get(Book, dune_id)
|
||||
assert dune_book is not None
|
||||
dune_book.first_published = 1965
|
||||
|
||||
db.session.commit()
|
||||
@@ -557,7 +566,7 @@ class TestBookSearchCommand:
|
||||
class TestReadingCommands:
|
||||
"""Test reading-related CLI commands."""
|
||||
|
||||
def test_reading_start_basic(self, app, cli_runner):
|
||||
def test_reading_start_basic(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test starting a reading session."""
|
||||
# Add a book first
|
||||
result = cli_runner.invoke(
|
||||
@@ -566,7 +575,6 @@ class TestReadingCommands:
|
||||
assert result.exit_code == 0
|
||||
|
||||
# Extract book ID from output
|
||||
import re
|
||||
|
||||
book_id_match = re.search(r"ID: (\d+)", result.output)
|
||||
assert book_id_match
|
||||
@@ -578,10 +586,12 @@ class TestReadingCommands:
|
||||
)
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert f"Started reading session" in result.output
|
||||
assert "Started reading session" in result.output
|
||||
assert f"for book {book_id}" in result.output
|
||||
|
||||
def test_reading_finish_with_rating(self, app, cli_runner):
|
||||
def test_reading_finish_with_rating(
|
||||
self, app: Flask, cli_runner: CliRunner
|
||||
) -> None:
|
||||
"""Test finishing a reading session with rating."""
|
||||
# Add book and start reading
|
||||
cli_runner.invoke(cli, ["book", "add", "Test Book", "--owner", "alice"])
|
||||
@@ -597,7 +607,6 @@ class TestReadingCommands:
|
||||
assert result.exit_code == 0
|
||||
|
||||
# Extract reading session ID
|
||||
import re
|
||||
|
||||
reading_id_match = re.search(r"Started reading session (\d+)", result.output)
|
||||
assert reading_id_match
|
||||
@@ -621,7 +630,7 @@ class TestReadingCommands:
|
||||
assert "Finished reading: Test Book" in result.output
|
||||
assert "Rating: 4/5" in result.output
|
||||
|
||||
def test_reading_drop(self, app, cli_runner):
|
||||
def test_reading_drop(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test dropping a reading session."""
|
||||
# Add book and start reading
|
||||
cli_runner.invoke(cli, ["book", "add", "Boring Book", "--owner", "alice"])
|
||||
@@ -634,9 +643,8 @@ class TestReadingCommands:
|
||||
cli, ["reading", "start", str(book_id), "--owner", "alice"]
|
||||
)
|
||||
|
||||
import re
|
||||
|
||||
reading_id_match = re.search(r"Started reading session (\d+)", result.output)
|
||||
assert reading_id_match is not None
|
||||
reading_id = reading_id_match.group(1)
|
||||
|
||||
# Drop the reading
|
||||
@@ -647,7 +655,7 @@ class TestReadingCommands:
|
||||
assert result.exit_code == 0
|
||||
assert "Dropped reading: Boring Book" in result.output
|
||||
|
||||
def test_reading_list_current(self, app, cli_runner):
|
||||
def test_reading_list_current(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test listing current (unfinished) readings."""
|
||||
# Add book and start reading
|
||||
cli_runner.invoke(cli, ["book", "add", "Current Book", "--owner", "alice"])
|
||||
@@ -666,7 +674,7 @@ class TestReadingCommands:
|
||||
assert "Current Book" in result.output
|
||||
assert "Reading" in result.output
|
||||
|
||||
def test_reading_list_json_format(self, app, cli_runner):
|
||||
def test_reading_list_json_format(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test listing readings in JSON format."""
|
||||
# Add book and start reading
|
||||
cli_runner.invoke(cli, ["book", "add", "JSON Book", "--owner", "alice"])
|
||||
@@ -692,7 +700,7 @@ class TestReadingCommands:
|
||||
class TestWishlistCommands:
|
||||
"""Test wishlist-related CLI commands."""
|
||||
|
||||
def test_wishlist_add(self, app, cli_runner):
|
||||
def test_wishlist_add(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test adding a book to wishlist."""
|
||||
# Add a book first
|
||||
cli_runner.invoke(cli, ["book", "add", "Desired Book", "--owner", "alice"])
|
||||
@@ -708,7 +716,7 @@ class TestWishlistCommands:
|
||||
assert result.exit_code == 0
|
||||
assert "Added 'Desired Book' to wishlist" in result.output
|
||||
|
||||
def test_wishlist_remove(self, app, cli_runner):
|
||||
def test_wishlist_remove(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test removing a book from wishlist."""
|
||||
# Add book and add to wishlist
|
||||
cli_runner.invoke(cli, ["book", "add", "Unwanted Book", "--owner", "alice"])
|
||||
@@ -726,7 +734,9 @@ class TestWishlistCommands:
|
||||
assert result.exit_code == 0
|
||||
assert f"Removed book {book_id} from wishlist" in result.output
|
||||
|
||||
def test_wishlist_remove_not_in_list(self, app, cli_runner):
|
||||
def test_wishlist_remove_not_in_list(
|
||||
self, app: Flask, cli_runner: CliRunner
|
||||
) -> None:
|
||||
"""Test removing a book that's not in wishlist."""
|
||||
# Add book but don't add to wishlist
|
||||
cli_runner.invoke(cli, ["book", "add", "Not Wished Book", "--owner", "alice"])
|
||||
@@ -742,14 +752,14 @@ class TestWishlistCommands:
|
||||
assert result.exit_code == 0
|
||||
assert f"Book {book_id} was not in wishlist" in result.output
|
||||
|
||||
def test_wishlist_list_empty(self, app, cli_runner):
|
||||
def test_wishlist_list_empty(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test listing empty wishlist."""
|
||||
result = cli_runner.invoke(cli, ["wishlist", "list", "--owner", "alice"])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert "Wishlist is empty." in result.output
|
||||
|
||||
def test_wishlist_list_with_items(self, app, cli_runner):
|
||||
def test_wishlist_list_with_items(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test listing wishlist with items."""
|
||||
# Add books and add to wishlist
|
||||
cli_runner.invoke(
|
||||
@@ -793,7 +803,7 @@ class TestWishlistCommands:
|
||||
assert "Author One" in result.output
|
||||
assert "Author Two" in result.output
|
||||
|
||||
def test_wishlist_list_json_format(self, app, cli_runner):
|
||||
def test_wishlist_list_json_format(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test listing wishlist in JSON format."""
|
||||
cli_runner.invoke(
|
||||
cli,
|
||||
@@ -829,14 +839,14 @@ class TestWishlistCommands:
|
||||
class TestDatabaseCommands:
|
||||
"""Test database management CLI commands."""
|
||||
|
||||
def test_db_init(self, app, cli_runner):
|
||||
def test_db_init(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test database initialization."""
|
||||
result = cli_runner.invoke(cli, ["db", "init"])
|
||||
|
||||
assert result.exit_code == 0
|
||||
assert "Database initialized." in result.output
|
||||
|
||||
def test_db_seed(self, app, cli_runner):
|
||||
def test_db_seed(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test database seeding with sample data."""
|
||||
result = cli_runner.invoke(cli, ["db", "seed", "--owner", "test_owner"])
|
||||
|
||||
@@ -855,7 +865,7 @@ class TestDatabaseCommands:
|
||||
assert "Dune" in titles
|
||||
assert "The Pragmatic Programmer" in titles
|
||||
|
||||
def test_db_status_empty(self, app, cli_runner):
|
||||
def test_db_status_empty(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test database status with empty database."""
|
||||
result = cli_runner.invoke(cli, ["db", "status"])
|
||||
|
||||
@@ -868,7 +878,7 @@ class TestDatabaseCommands:
|
||||
assert "Reading sessions: 0" in result.output
|
||||
assert "Wishlist items: 0" in result.output
|
||||
|
||||
def test_db_status_with_data(self, app, cli_runner):
|
||||
def test_db_status_with_data(self, app: Flask, cli_runner: CliRunner) -> None:
|
||||
"""Test database status with sample data."""
|
||||
# Add some test data
|
||||
cli_runner.invoke(
|
||||
@@ -908,21 +918,27 @@ class TestDatabaseCommands:
|
||||
class TestErrorScenarios:
|
||||
"""Test error handling and edge cases."""
|
||||
|
||||
def test_reading_start_invalid_book_id(self, app, cli_runner):
|
||||
def test_reading_start_invalid_book_id(
|
||||
self, app: Flask, cli_runner: CliRunner
|
||||
) -> None:
|
||||
"""Test starting reading with non-existent book ID."""
|
||||
result = cli_runner.invoke(cli, ["reading", "start", "999", "--owner", "alice"])
|
||||
|
||||
assert result.exit_code == 1
|
||||
assert "Error starting reading:" in result.output
|
||||
|
||||
def test_wishlist_add_invalid_book_id(self, app, cli_runner):
|
||||
def test_wishlist_add_invalid_book_id(
|
||||
self, app: Flask, cli_runner: CliRunner
|
||||
) -> None:
|
||||
"""Test adding non-existent book to wishlist."""
|
||||
result = cli_runner.invoke(cli, ["wishlist", "add", "999", "--owner", "alice"])
|
||||
|
||||
assert result.exit_code == 1
|
||||
assert "Error adding to wishlist:" in result.output
|
||||
|
||||
def test_reading_finish_invalid_reading_id(self, app, cli_runner):
|
||||
def test_reading_finish_invalid_reading_id(
|
||||
self, app: Flask, cli_runner: CliRunner
|
||||
) -> None:
|
||||
"""Test finishing non-existent reading session."""
|
||||
result = cli_runner.invoke(cli, ["reading", "finish", "999"])
|
||||
|
||||
|
||||
@@ -6,16 +6,15 @@ field filters, and edge case handling.
|
||||
"""
|
||||
|
||||
from datetime import date
|
||||
from typing import List
|
||||
|
||||
import pytest
|
||||
|
||||
from hxbooks.search import (
|
||||
ComparisonOperator,
|
||||
Field,
|
||||
FieldFilter,
|
||||
QueryParser,
|
||||
SearchQuery,
|
||||
_convert_value, # noqa: PLC2701
|
||||
)
|
||||
|
||||
|
||||
@@ -28,31 +27,31 @@ def parser() -> QueryParser:
|
||||
class TestQueryParser:
|
||||
"""Test the QueryParser class functionality."""
|
||||
|
||||
def test_parse_empty_query(self, parser: QueryParser):
|
||||
def test_parse_empty_query(self, parser: QueryParser) -> None:
|
||||
"""Test parsing an empty query string."""
|
||||
result = parser.parse("")
|
||||
assert result.text_terms == []
|
||||
assert result.field_filters == []
|
||||
|
||||
def test_parse_whitespace_only(self, parser: QueryParser):
|
||||
def test_parse_whitespace_only(self, parser: QueryParser) -> None:
|
||||
"""Test parsing a query with only whitespace."""
|
||||
result = parser.parse(" \t\n ")
|
||||
assert result.text_terms == []
|
||||
assert result.field_filters == []
|
||||
|
||||
def test_parse_simple_text_terms(self, parser: QueryParser):
|
||||
def test_parse_simple_text_terms(self, parser: QueryParser) -> None:
|
||||
"""Test parsing simple text search terms."""
|
||||
result = parser.parse("hobbit tolkien")
|
||||
assert result.text_terms == ["hobbit", "tolkien"]
|
||||
assert result.field_filters == []
|
||||
|
||||
def test_parse_quoted_text_terms(self, parser: QueryParser):
|
||||
def test_parse_quoted_text_terms(self, parser: QueryParser) -> None:
|
||||
"""Test parsing quoted text search terms."""
|
||||
result = parser.parse('"the hobbit" tolkien')
|
||||
assert result.text_terms == ["the hobbit", "tolkien"]
|
||||
assert result.field_filters == []
|
||||
|
||||
def test_parse_quoted_text_with_spaces(self, parser: QueryParser):
|
||||
def test_parse_quoted_text_with_spaces(self, parser: QueryParser) -> None:
|
||||
"""Test parsing quoted text containing multiple spaces."""
|
||||
result = parser.parse('"lord of the rings"')
|
||||
assert result.text_terms == ["lord of the rings"]
|
||||
@@ -62,7 +61,7 @@ class TestQueryParser:
|
||||
class TestFieldFilters:
|
||||
"""Test field filter parsing."""
|
||||
|
||||
def test_parse_title_filter(self, parser: QueryParser):
|
||||
def test_parse_title_filter(self, parser: QueryParser) -> None:
|
||||
"""Test parsing title field filter."""
|
||||
result = parser.parse("title:hobbit")
|
||||
assert len(result.field_filters) == 1
|
||||
@@ -72,7 +71,7 @@ class TestFieldFilters:
|
||||
assert filter.value == "hobbit"
|
||||
assert filter.negated is False
|
||||
|
||||
def test_parse_quoted_title_filter(self, parser: QueryParser):
|
||||
def test_parse_quoted_title_filter(self, parser: QueryParser) -> None:
|
||||
"""Test parsing quoted title field filter."""
|
||||
result = parser.parse('title:"the hobbit"')
|
||||
assert len(result.field_filters) == 1
|
||||
@@ -80,7 +79,7 @@ class TestFieldFilters:
|
||||
assert filter.field == Field.TITLE
|
||||
assert filter.value == "the hobbit"
|
||||
|
||||
def test_parse_author_filter(self, parser: QueryParser):
|
||||
def test_parse_author_filter(self, parser: QueryParser) -> None:
|
||||
"""Test parsing author field filter."""
|
||||
result = parser.parse("author:tolkien")
|
||||
assert len(result.field_filters) == 1
|
||||
@@ -88,7 +87,7 @@ class TestFieldFilters:
|
||||
assert filter.field == Field.AUTHOR
|
||||
assert filter.value == "tolkien"
|
||||
|
||||
def test_parse_negated_filter(self, parser: QueryParser):
|
||||
def test_parse_negated_filter(self, parser: QueryParser) -> None:
|
||||
"""Test parsing negated field filter."""
|
||||
result = parser.parse("-genre:romance")
|
||||
assert len(result.field_filters) == 1
|
||||
@@ -97,7 +96,7 @@ class TestFieldFilters:
|
||||
assert filter.value == "romance"
|
||||
assert filter.negated is True
|
||||
|
||||
def test_parse_multiple_filters(self, parser: QueryParser):
|
||||
def test_parse_multiple_filters(self, parser: QueryParser) -> None:
|
||||
"""Test parsing multiple field filters."""
|
||||
result = parser.parse("author:tolkien genre:fantasy")
|
||||
assert len(result.field_filters) == 2
|
||||
@@ -108,7 +107,7 @@ class TestFieldFilters:
|
||||
genre_filter = next(f for f in result.field_filters if f.field == Field.GENRE)
|
||||
assert genre_filter.value == "fantasy"
|
||||
|
||||
def test_parse_mixed_filters_and_text(self, parser: QueryParser):
|
||||
def test_parse_mixed_filters_and_text(self, parser: QueryParser) -> None:
|
||||
"""Test parsing mix of field filters and text terms."""
|
||||
result = parser.parse('epic author:tolkien "middle earth"')
|
||||
assert "epic" in result.text_terms
|
||||
@@ -133,8 +132,11 @@ class TestComparisonOperators:
|
||||
],
|
||||
)
|
||||
def test_parse_comparison_operators(
|
||||
self, parser: QueryParser, operator_str, expected_operator
|
||||
):
|
||||
self,
|
||||
parser: QueryParser,
|
||||
operator_str: str,
|
||||
expected_operator: ComparisonOperator,
|
||||
) -> None:
|
||||
"""Test parsing all supported comparison operators."""
|
||||
query = f"rating{operator_str}4"
|
||||
result = parser.parse(query)
|
||||
@@ -145,7 +147,7 @@ class TestComparisonOperators:
|
||||
assert filter.operator == expected_operator
|
||||
assert filter.value == 4
|
||||
|
||||
def test_parse_date_comparison(self, parser: QueryParser):
|
||||
def test_parse_date_comparison(self, parser: QueryParser) -> None:
|
||||
"""Test parsing date comparison operators."""
|
||||
result = parser.parse("added>=2026-03-15")
|
||||
assert len(result.field_filters) == 1
|
||||
@@ -154,7 +156,7 @@ class TestComparisonOperators:
|
||||
assert filter.operator == ComparisonOperator.GREATER_EQUAL
|
||||
assert filter.value == date(2026, 3, 15)
|
||||
|
||||
def test_parse_numeric_comparison(self, parser: QueryParser):
|
||||
def test_parse_numeric_comparison(self, parser: QueryParser) -> None:
|
||||
"""Test parsing numeric comparison operators."""
|
||||
result = parser.parse("shelf>2")
|
||||
assert len(result.field_filters) == 1
|
||||
@@ -167,79 +169,77 @@ class TestComparisonOperators:
|
||||
class TestTypeConversion:
|
||||
"""Test the _convert_value method for different field types."""
|
||||
|
||||
def test_convert_date_field_valid(self, parser: QueryParser):
|
||||
def test_convert_date_field_valid(self, parser: QueryParser) -> None:
|
||||
"""Test converting valid date strings for date fields."""
|
||||
result = parser._convert_value(Field.BOUGHT_DATE, "2026-03-15")
|
||||
result = _convert_value(Field.BOUGHT_DATE, "2026-03-15")
|
||||
assert result == date(2026, 3, 15)
|
||||
|
||||
result = parser._convert_value(Field.READ_DATE, "2025-12-31")
|
||||
result = _convert_value(Field.READ_DATE, "2025-12-31")
|
||||
assert result == date(2025, 12, 31)
|
||||
|
||||
result = parser._convert_value(Field.ADDED_DATE, "2024-01-01")
|
||||
result = _convert_value(Field.ADDED_DATE, "2024-01-01")
|
||||
assert result == date(2024, 1, 1)
|
||||
|
||||
def test_convert_date_field_invalid(self, parser: QueryParser):
|
||||
def test_convert_date_field_invalid(self, parser: QueryParser) -> None:
|
||||
"""Test converting invalid date strings falls back to string."""
|
||||
result = parser._convert_value(Field.BOUGHT_DATE, "invalid-date")
|
||||
result = _convert_value(Field.BOUGHT_DATE, "invalid-date")
|
||||
assert result == "invalid-date"
|
||||
|
||||
result = parser._convert_value(
|
||||
Field.READ_DATE, "2026-13-45"
|
||||
) # Invalid month/day
|
||||
result = _convert_value(Field.READ_DATE, "2026-13-45") # Invalid month/day
|
||||
assert result == "2026-13-45"
|
||||
|
||||
result = parser._convert_value(Field.ADDED_DATE, "not-a-date")
|
||||
result = _convert_value(Field.ADDED_DATE, "not-a-date")
|
||||
assert result == "not-a-date"
|
||||
|
||||
def test_convert_numeric_field_integers(self, parser: QueryParser):
|
||||
def test_convert_numeric_field_integers(self, parser: QueryParser) -> None:
|
||||
"""Test converting integer strings for numeric fields."""
|
||||
result = parser._convert_value(Field.RATING, "5")
|
||||
result = _convert_value(Field.RATING, "5")
|
||||
assert result == 5
|
||||
assert isinstance(result, int)
|
||||
|
||||
result = parser._convert_value(Field.SHELF, "10")
|
||||
result = _convert_value(Field.SHELF, "10")
|
||||
assert result == 10
|
||||
|
||||
result = parser._convert_value(Field.YEAR, "2026")
|
||||
result = _convert_value(Field.YEAR, "2026")
|
||||
assert result == 2026
|
||||
|
||||
def test_convert_numeric_field_floats(self, parser: QueryParser):
|
||||
def test_convert_numeric_field_floats(self, parser: QueryParser) -> None:
|
||||
"""Test converting float strings for numeric fields."""
|
||||
result = parser._convert_value(Field.RATING, "4.5")
|
||||
assert result == 4.5
|
||||
result = _convert_value(Field.RATING, "4.5")
|
||||
assert result == pytest.approx(4.5)
|
||||
assert isinstance(result, float)
|
||||
|
||||
result = parser._convert_value(Field.SHELF, "2.0")
|
||||
assert result == 2.0
|
||||
result = _convert_value(Field.SHELF, "2.0")
|
||||
assert result == pytest.approx(2.0)
|
||||
|
||||
def test_convert_numeric_field_invalid(self, parser: QueryParser):
|
||||
def test_convert_numeric_field_invalid(self, parser: QueryParser) -> None:
|
||||
"""Test converting invalid numeric strings falls back to string."""
|
||||
result = parser._convert_value(Field.RATING, "not-a-number")
|
||||
result = _convert_value(Field.RATING, "not-a-number")
|
||||
assert result == "not-a-number"
|
||||
|
||||
result = parser._convert_value(Field.SHELF, "abc")
|
||||
result = _convert_value(Field.SHELF, "abc")
|
||||
assert result == "abc"
|
||||
|
||||
result = parser._convert_value(Field.YEAR, "twenty-twenty-six")
|
||||
result = _convert_value(Field.YEAR, "twenty-twenty-six")
|
||||
assert result == "twenty-twenty-six"
|
||||
|
||||
def test_convert_string_fields(self, parser: QueryParser):
|
||||
def test_convert_string_fields(self, parser: QueryParser) -> None:
|
||||
"""Test converting values for string fields returns as-is."""
|
||||
result = parser._convert_value(Field.TITLE, "The Hobbit")
|
||||
result = _convert_value(Field.TITLE, "The Hobbit")
|
||||
assert result == "The Hobbit"
|
||||
|
||||
result = parser._convert_value(Field.AUTHOR, "Tolkien")
|
||||
result = _convert_value(Field.AUTHOR, "Tolkien")
|
||||
assert result == "Tolkien"
|
||||
|
||||
result = parser._convert_value(Field.GENRE, "Fantasy")
|
||||
result = _convert_value(Field.GENRE, "Fantasy")
|
||||
assert result == "Fantasy"
|
||||
|
||||
# Even things that look like dates/numbers should stay as strings for string fields
|
||||
result = parser._convert_value(Field.TITLE, "2026-03-15")
|
||||
result = _convert_value(Field.TITLE, "2026-03-15")
|
||||
assert result == "2026-03-15"
|
||||
assert isinstance(result, str)
|
||||
|
||||
result = parser._convert_value(Field.AUTHOR, "123")
|
||||
result = _convert_value(Field.AUTHOR, "123")
|
||||
assert result == "123"
|
||||
assert isinstance(result, str)
|
||||
|
||||
@@ -247,13 +247,13 @@ class TestTypeConversion:
|
||||
class TestParsingEdgeCases:
|
||||
"""Test edge cases and error handling in query parsing."""
|
||||
|
||||
def test_parse_invalid_field_name(self, parser: QueryParser):
|
||||
def test_parse_invalid_field_name(self, parser: QueryParser) -> None:
|
||||
"""Test parsing with invalid field names falls back to text search."""
|
||||
result = parser.parse("invalid_field:value")
|
||||
# Should fall back to treating the whole thing as text
|
||||
assert len(result.text_terms) >= 1 or len(result.field_filters) == 0
|
||||
|
||||
def test_parse_mixed_quotes_and_operators(self, parser: QueryParser):
|
||||
def test_parse_mixed_quotes_and_operators(self, parser: QueryParser) -> None:
|
||||
"""Test parsing complex queries with quotes and operators."""
|
||||
result = parser.parse('title:"The Lord" author:tolkien rating>=4')
|
||||
|
||||
@@ -278,35 +278,36 @@ class TestParsingEdgeCases:
|
||||
assert rating_filter.value == 4
|
||||
assert rating_filter.operator == ComparisonOperator.GREATER_EQUAL
|
||||
|
||||
def test_parse_escaped_quotes(self, parser: QueryParser):
|
||||
def test_parse_escaped_quotes(self, parser: QueryParser) -> None:
|
||||
"""Test parsing strings with escaped quotes."""
|
||||
result = parser.parse(r'title:"She said \"hello\""')
|
||||
if result.field_filters:
|
||||
# If parsing succeeds, check the escaped quote handling
|
||||
filter = result.field_filters[0]
|
||||
assert isinstance(filter.value, str)
|
||||
assert "hello" in filter.value
|
||||
# If parsing fails, it should fall back gracefully
|
||||
|
||||
def test_parse_special_characters(self, parser: QueryParser):
|
||||
def test_parse_special_characters(self, parser: QueryParser) -> None:
|
||||
"""Test parsing queries with special characters."""
|
||||
result = parser.parse("title:C++ author:Stroustrup")
|
||||
# Should handle the + characters gracefully
|
||||
assert len(result.field_filters) >= 1 or len(result.text_terms) >= 1
|
||||
|
||||
def test_parse_very_long_query(self, parser: QueryParser):
|
||||
def test_parse_very_long_query(self, parser: QueryParser) -> None:
|
||||
"""Test parsing very long query strings."""
|
||||
long_value = "a" * 1000
|
||||
result = parser.parse(f"title:{long_value}")
|
||||
# Should handle long strings without crashing
|
||||
assert isinstance(result, SearchQuery)
|
||||
|
||||
def test_parse_unicode_characters(self, parser: QueryParser):
|
||||
def test_parse_unicode_characters(self, parser: QueryParser) -> None:
|
||||
"""Test parsing queries with unicode characters."""
|
||||
result = parser.parse("title:Café author:José")
|
||||
# Should handle unicode gracefully
|
||||
assert isinstance(result, SearchQuery)
|
||||
|
||||
def test_fallback_behavior_on_parse_error(self, parser: QueryParser):
|
||||
def test_fallback_behavior_on_parse_error(self, parser: QueryParser) -> None:
|
||||
"""Test that invalid syntax falls back to text search."""
|
||||
# Construct a query that should cause parse errors
|
||||
invalid_queries = [
|
||||
@@ -327,7 +328,7 @@ class TestParsingEdgeCases:
|
||||
class TestComplexQueries:
|
||||
"""Test parsing of complex, real-world query examples."""
|
||||
|
||||
def test_parse_realistic_book_search(self, parser: QueryParser):
|
||||
def test_parse_realistic_book_search(self, parser: QueryParser) -> None:
|
||||
"""Test parsing realistic book search queries."""
|
||||
result = parser.parse(
|
||||
'author:tolkien genre:fantasy -genre:romance rating>=4 "middle earth"'
|
||||
@@ -363,7 +364,7 @@ class TestComplexQueries:
|
||||
assert romance_filter.value == "romance"
|
||||
assert romance_filter.negated is True
|
||||
|
||||
def test_parse_location_and_date_filters(self, parser: QueryParser):
|
||||
def test_parse_location_and_date_filters(self, parser: QueryParser) -> None:
|
||||
"""Test parsing location and date-based queries."""
|
||||
result = parser.parse("place:home bookshelf:fantasy shelf>=2 added>=2026-01-01")
|
||||
|
||||
@@ -372,21 +373,24 @@ class TestComplexQueries:
|
||||
place_filter = next(
|
||||
(f for f in result.field_filters if f.field == Field.PLACE), None
|
||||
)
|
||||
assert place_filter is not None
|
||||
assert place_filter.value == "home"
|
||||
|
||||
shelf_filter = next(
|
||||
(f for f in result.field_filters if f.field == Field.SHELF), None
|
||||
)
|
||||
assert shelf_filter is not None
|
||||
assert shelf_filter.value == 2
|
||||
assert shelf_filter.operator == ComparisonOperator.GREATER_EQUAL
|
||||
|
||||
added_filter = next(
|
||||
(f for f in result.field_filters if f.field == Field.ADDED_DATE), None
|
||||
)
|
||||
assert added_filter is not None
|
||||
assert added_filter.value == date(2026, 1, 1)
|
||||
assert added_filter.operator == ComparisonOperator.GREATER_EQUAL
|
||||
|
||||
def test_parse_mixed_types_comprehensive(self, parser: QueryParser):
|
||||
def test_parse_mixed_types_comprehensive(self, parser: QueryParser) -> None:
|
||||
"""Test parsing query with all major field types."""
|
||||
query = 'title:"Complex Book" author:Author year=2020 rating>=4 bought<=2025-12-31 -genre:boring epic adventure'
|
||||
result = parser.parse(query)
|
||||
|
||||
Reference in New Issue
Block a user