import tempfile import unittest from unittest.mock import patch from pathlib import Path from generator import resources from tests import scale_fixtures, rhythm_fixtures from tests import solo_fixtures as fixture class ResourceCache(unittest.TestCase): def test_compiled_and_xml_libraries_have_identical_contents(self): original=(scale_fixtures.library(),fixture.library(),rhythm_fixtures.library()) import json encoded=json.loads(json.dumps(resources.encode(original),default=str)) restored=resources.decode(encoded) self.assertEqual(original[0],restored[0]) self.assertEqual(original[1].paths,restored[1].paths) self.assertEqual(original[2].snippets,restored[2].snippets) def test_warm_cache_avoids_xml_import_and_corruption_recompiles(self): original=(scale_fixtures.library(),fixture.library(),rhythm_fixtures.library()) with tempfile.TemporaryDirectory() as root: with patch('generator.resources.scales.load_library',return_value=original[0]) as sc,patch('generator.resources.sequences.load_library',return_value=original[1]),patch('generator.resources.rhythm.load_library',return_value=original[2]): resources._load.cache_clear();resources.load(Path(root),{'fixture':'1'},root) self.assertEqual(sc.call_count,1) resources._load.cache_clear();resources.load(Path(root),{'fixture':'1'},root) self.assertEqual(sc.call_count,1) next((Path(root)/'_resources').glob('*.json')).write_text('{}') resources._load.cache_clear();resources.load(Path(root),{'fixture':'1'},root) self.assertEqual(sc.call_count,2)