import scgenerator.cache as scc from scgenerator.cache import Cache, methodcache, normalize_path def test_io(): cache = Cache("Hello") assert not cache.dir.exists() cache.reset() assert cache.dir.exists() cache.save("a", "bonjour") assert cache.load("a") == "bonjour" assert "a" in cache cache.delete() assert not cache.dir.exists() def test_toml(): s1 = """ [config] plot_range = [750, 1350] rin_measurement = "./DualComb1GHz_updated_corrected_extrapolated_1GHz_noqn.csv" num_segments = 31 num_frequencies = 513 noise_seed_start = 3012 anticorrelated_width = true """ s2 = """ # some commment [config] anticorrelated_width = true plot_range = [750,1350] num_segments=31 num_frequencies = 513 noise_seed_start=3012 rin_measurement='./DualComb1GHz_updated_corrected_extrapolated_1GHz_noqn.csv' """ s3 = """ # some commment [config] anticorrelated_width = true plot_range = [750,1351] num_segments=31 num_frequencies = 513 noise_seed_start=3012 rin_measurement='./DualComb1GHz_updated_corrected_extrapolated_1GHz_noqn.csv' """ cache1 = Cache.from_toml(s1) cache3 = Cache.from_toml(s3, create=False) assert cache1.dir == Cache.from_toml(s2).dir assert cache1.dir != cache3.dir assert cache1.dir.exists() cache1.delete() assert not cache1.dir.exists() assert not cache3.dir.exists() class Stuff: call_count = 0 @classmethod def from_other(cls, stuff: str): cls.call_count += 1 return cls(stuff[:5]) def __init__(self, x): self.x = x def test_decorator(): cache = Cache("Test") cache.compile_class(Stuff) cache.delete() call_count = 0 @cache() def func(x: str) -> str: nonlocal call_count call_count += 1 return x + x @cache(lambda li: f"{li[0]}-{li[-1]} {len(li)}") def func2(some_list: list): nonlocal call_count call_count += 1 return sum(some_list) Stuff.from_other = cache(lambda _, *args: str(args))(Stuff.from_other) assert func("hello") == "hellohello" assert func2([0, 1, 2, 80]) == 83 assert func2([0, 1, 2, 80]) == 83 assert (scc.CACHE_DIR / "Test" / "test_decorator._locals_.func hello").exists() assert (scc.CACHE_DIR / "Test" / "test_decorator._locals_.func2 0-80 4").exists() assert Stuff.from_other("Bonjour").x == "Bonjo" assert Stuff.from_other("Bonjour").x == "Bonjo" assert Stuff.call_count == 1 assert call_count == 2 cache.delete() def test_normalize(): assert normalize_path("abd") == "abd" assert normalize_path("a/b") == "a_b" assert normalize_path("bonjour\nsdfk") == "bonjour sdfk" assert normalize_path("a\\b") == "a_b" assert ( normalize_path( """sdfl asdfk asd """ ) == "sdfl asdfk asd" )