| 1 |
""" |
| 2 |
Tests for flexible style loading from citeproc-py-styles package |
| 3 |
""" |
| 4 |
|
| 5 |
import os |
| 6 |
import sys |
| 7 |
import tempfile |
| 8 |
import unittest |
| 9 |
from unittest.mock import patch, MagicMock |
| 10 |
|
| 11 |
from citeproc import CitationStylesStyle, STYLES_PATH |
| 12 |
|
| 13 |
class TestStyleLoading(unittest.TestCase): |
| 14 |
"""Test suite for flexible style loading""" |
| 15 |
|
| 16 |
def test_bundled_style_loads(self): |
| 17 |
"""Test that the bundled harvard-cite-them-right style loads correctly""" |
| 18 |
style = CitationStylesStyle('harvard-cite-them-right', validate=False) |
| 19 |
self.assertIsNotNone(style) |
| 20 |
|
| 21 |
def test_full_path_style_loads(self): |
| 22 |
"""Test that a style specified with full path loads correctly""" |
| 23 |
harvard_path = os.path.join(STYLES_PATH, 'harvard-cite-them-right.csl') |
| 24 |
style = CitationStylesStyle(harvard_path, validate=False) |
| 25 |
self.assertIsNotNone(style) |
| 26 |
|
| 27 |
def test_nonexistent_style_without_citeproc_py_styles(self): |
| 28 |
"""Test error message when style not found and citeproc-py-styles not installed""" |
| 29 |
with patch('citeproc.frontend.os.path.exists') as mock_exists: |
| 30 |
# Make sure the style doesn't exist as a file or bundled |
| 31 |
mock_exists.side_effect = lambda path: False |
| 32 |
|
| 33 |
# Mock the import to fail |
| 34 |
with patch.dict('sys.modules', {'citeproc_styles': None}): |
| 35 |
with self.assertRaises(ValueError) as context: |
| 36 |
CitationStylesStyle('nonexistent-style', validate=False) |
| 37 |
|
| 38 |
error_msg = str(context.exception) |
| 39 |
self.assertIn('nonexistent-style', error_msg) |
| 40 |
self.assertIn('not found in bundled styles', error_msg) |
| 41 |
self.assertIn('pip install citeproc-py-styles', error_msg) |
| 42 |
|
| 43 |
def test_style_from_citeproc_py_styles(self): |
| 44 |
"""Test that styles load from citeproc-py-styles when available""" |
| 45 |
# Create a mock module |
| 46 |
mock_module = MagicMock() |
| 47 |
mock_module.get_style_filepath = MagicMock(return_value='/mock/path/to/apa.csl') |
| 48 |
|
| 49 |
with patch('citeproc.frontend.os.path.exists') as mock_exists: |
| 50 |
# First call checks if 'apa' exists as path (False) |
| 51 |
# Second call checks bundled path (False) |
| 52 |
mock_exists.side_effect = [False, False] |
| 53 |
|
| 54 |
with patch.dict('sys.modules', {'citeproc_styles': mock_module}): |
| 55 |
# Mock the parent class __init__ to set root properly |
| 56 |
def mock_init(self, path, validate=False): |
| 57 |
# Create a mock root object |
| 58 |
self.root = MagicMock() |
| 59 |
self.root.get.return_value = 'en-US' |
| 60 |
self.root.set_locale_list = MagicMock() |
| 61 |
# Store the path for verification |
| 62 |
self._test_path = path |
| 63 |
|
| 64 |
with patch('citeproc.frontend.CitationStylesXML.__init__', mock_init): |
| 65 |
style = CitationStylesStyle('apa', validate=False) |
| 66 |
|
| 67 |
# Verify get_style_filepath was called with correct argument |
| 68 |
mock_module.get_style_filepath.assert_called_once_with('apa') |
| 69 |
# Verify parent init was called with the path from citeproc-py-styles |
| 70 |
self.assertEqual(style._test_path, '/mock/path/to/apa.csl') |
| 71 |
|
| 72 |
def test_style_not_in_citeproc_py_styles(self): |
| 73 |
"""Test error when style not found even in citeproc-py-styles""" |
| 74 |
# Create a mock module that raises KeyError |
| 75 |
mock_module = MagicMock() |
| 76 |
mock_module.get_style_filepath = MagicMock(side_effect=KeyError('Style not found')) |
| 77 |
|
| 78 |
with patch('citeproc.frontend.os.path.exists') as mock_exists: |
| 79 |
# Style doesn't exist as file or bundled |
| 80 |
mock_exists.return_value = False |
| 81 |
|
| 82 |
with patch.dict('sys.modules', {'citeproc_styles': mock_module}): |
| 83 |
with self.assertRaises(ValueError) as context: |
| 84 |
CitationStylesStyle('unknown-style', validate=False) |
| 85 |
|
| 86 |
error_msg = str(context.exception) |
| 87 |
self.assertIn('unknown-style', error_msg) |
| 88 |
self.assertIn('not found in bundled styles', error_msg) |
| 89 |
self.assertIn('or in citeproc-py-styles package', error_msg) |
| 90 |
|
| 91 |
def test_style_file_not_found_in_citeproc_py_styles(self): |
| 92 |
"""Test error when citeproc-py-styles returns path but file doesn't exist""" |
| 93 |
# Create a mock module that raises FileNotFoundError |
| 94 |
mock_module = MagicMock() |
| 95 |
mock_module.get_style_filepath = MagicMock(side_effect=FileNotFoundError('File not found')) |
| 96 |
|
| 97 |
with patch('citeproc.frontend.os.path.exists') as mock_exists: |
| 98 |
# Style doesn't exist as file or bundled |
| 99 |
mock_exists.return_value = False |
| 100 |
|
| 101 |
with patch.dict('sys.modules', {'citeproc_styles': mock_module}): |
| 102 |
with self.assertRaises(ValueError) as context: |
| 103 |
CitationStylesStyle('missing-style', validate=False) |
| 104 |
|
| 105 |
error_msg = str(context.exception) |
| 106 |
self.assertIn('missing-style', error_msg) |
| 107 |
self.assertIn('not found in bundled styles', error_msg) |
| 108 |
self.assertIn('or in citeproc-py-styles package', error_msg) |
| 109 |
|
| 110 |
def test_citeproc_py_styles_load(self): |
| 111 |
"""Test loading a real style from citeproc-py-styles if installed""" |
| 112 |
try: |
| 113 |
import citeproc_styles |
| 114 |
except ImportError: |
| 115 |
self.skipTest("citeproc-py-styles (provides citeproc_styles package) not installed") |
| 116 |
from citeproc_styles import get_style_filepath |
| 117 |
# Attempt to load a known style from citeproc-py-styles |
| 118 |
style_name = 'chicago-author-date' |
| 119 |
style_path = get_style_filepath(style_name) |
| 120 |
style = CitationStylesStyle(style_name, validate=False) |
| 121 |
self.assertIsNotNone(style) |
| 122 |
self.assertTrue(hasattr(style, 'root')) |
| 123 |
# And loading with the full path should also work |
| 124 |
style = CitationStylesStyle(style_path, validate=False) |
| 125 |
self.assertIsNotNone(style) |
| 126 |
self.assertTrue(hasattr(style, 'root')) |