Files
scgenerator/tests/test_cache.py

89 lines
2.1 KiB
Python

import scgenerator.cache as scc
from scgenerator.cache import Cache
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()
def test_decorator():
cache = Cache("Test")
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)
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 call_count == 2
cache.delete()