Coverage for src/jsoncrack_for_sphinx/utils/fixtures.py: 79%

33 statements  

« prev     ^ index     » next       coverage.py v7.10.0, created at 2025-07-24 22:26 +0000

1""" 

2Pytest fixtures for testing the Sphinx extension. 

3""" 

4 

5import json 

6import tempfile 

7from pathlib import Path 

8from typing import Any, Dict, Generator 

9 

10import pytest 

11 

12 

13@pytest.fixture 

14def temp_schema_dir() -> Generator[Path, None, None]: 

15 """Create a temporary directory for schema files.""" 

16 with tempfile.TemporaryDirectory() as temp_dir: 

17 yield Path(temp_dir) 

18 

19 

20@pytest.fixture 

21def sample_schema() -> Dict[str, Any]: 

22 """Provide a sample JSON schema for testing.""" 

23 return { 

24 "$schema": "http://json-schema.org/draft-07/schema#", 

25 "type": "object", 

26 "title": "User", 

27 "description": "A user object", 

28 "properties": { 

29 "name": {"type": "string", "description": "The user's name"}, 

30 "age": {"type": "integer", "minimum": 0, "description": "The user's age"}, 

31 "email": { 

32 "type": "string", 

33 "format": "email", 

34 "description": "The user's email address", 

35 }, 

36 }, 

37 "required": ["name", "email"], 

38 } 

39 

40 

41@pytest.fixture 

42def schema_file(temp_schema_dir: Path, sample_schema: Dict[str, Any]) -> Path: 

43 """Create a sample schema file for testing.""" 

44 schema_path = temp_schema_dir / "User.schema.json" 

45 with open(schema_path, "w", encoding="utf-8") as f: 

46 json.dump(sample_schema, f, indent=2) 

47 return schema_path 

48 

49 

50# Helper functions for creating test schemas 

51def create_method_schema( 

52 temp_dir: Path, class_name: str, method_name: str, schema_data: Dict[str, Any] 

53) -> Path: 

54 """Create a schema file for a method.""" 

55 schema_path = temp_dir / f"{class_name}.{method_name}.schema.json" 

56 with open(schema_path, "w", encoding="utf-8") as f: 

57 json.dump(schema_data, f, indent=2) 

58 return schema_path 

59 

60 

61def create_function_schema( 

62 temp_dir: Path, function_name: str, schema_data: Dict[str, Any] 

63) -> Path: 

64 """Create a schema file for a function.""" 

65 schema_path = temp_dir / f"{function_name}.schema.json" 

66 with open(schema_path, "w", encoding="utf-8") as f: 

67 json.dump(schema_data, f, indent=2) 

68 return schema_path 

69 

70 

71def create_option_schema( 

72 temp_dir: Path, base_name: str, option_name: str, schema_data: Dict[str, Any] 

73) -> Path: 

74 """Create a schema file for an option.""" 

75 schema_path = temp_dir / f"{base_name}.{option_name}.schema.json" 

76 with open(schema_path, "w", encoding="utf-8") as f: 

77 json.dump(schema_data, f, indent=2) 

78 return schema_path