def test_path_alpha_index(): tests = [ ('alpha.txt', f'{cache_dir}/a/alpha.txt'), ('álpha.txt', f'{cache_dir}/_/álpha.txt'), ('0-zero.txt', f'{cache_dir}/_/0-zero.txt'), ('bands/bad-brains.html', f'{cache_dir}/bands/b/bad-brains.html') ] for t in tests: assert fscache.path(t[0], alpha_index='name', cache_dir=cache_dir, create_dirs=False).as_posix() == t[1]
#!/usr/bin/env python # -*- coding: utf-8 -*- from fscache import fscache cache_dir = '.fscache' cache_file = fscache.path('test_content.gif', cache_dir=cache_dir) # Smallest GIF http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever content = b'GIF89a\x01\x00\x01\x00\x00\xff\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x00;' # Create a cache file first. def test_save(): fscache.save(cache_file, content, mode='binary') assert cache_file.exists() def test_load(): assert fscache.load(cache_file, mode='binary') == content
#!/usr/bin/env python # -*- coding: utf-8 -*- import requests from fscache import fscache url = 'https://example.com/index.html' cache_file = fscache.path(url, cache_dir='.fscache') if fscache.valid(cache_file, lifetime=3600): content = fscache.load(cache_file) # Do something with content else: content = requests.get(url).text # Save content in .fscache/https/example.com/index.html fscache.save(cache_file, content)
def test_cache_dir_not_exists(): with raises(FileNotFoundError): fscache.path('file.txt', cache_dir='404', create_dirs=False)
def test_path_default_cache_dir(): assert fscache.path('file.txt', create_dirs=False).as_posix() == Path(Path.home(), '.fscache/file.txt').as_posix()
#!/usr/bin/env python # -*- coding: utf-8 -*- from pathlib import Path from pytest import raises from fscache import fscache cache_dir = '.fscache' cache_file = fscache.path('test_fscache.json', cache_dir=cache_dir) def test_slugify(): tests = [ ('äöüß', 'äöüß'), ('áéíóúñ', 'áéíóúñ'), ('a - b', 'a---b'), ('/path/to/data.json', 'path-to-data.json'), ('Gómez', 'Gómez'), ('https://ramiro.org/index.html', 'https-ramiro.org-index.html'), ('https/ramiro.org/index.html', 'https-ramiro.org-index.html'), ('-o2OEbUiPDs', '-o2OEbUiPDs'), ] for t in tests: assert fscache.slugify(t[0]) == t[1] def test_create_id(): tests = [ ('https://www.youtube.com/watch?v=HEOxdMWxIBM', 'https/www.youtube.com/watch-v-HEOxdMWxIBM'),
#!/usr/bin/env python # -*- coding: utf-8 -*- from fscache import fscache cache_dir = '.fscache' cache_file = fscache.path('test_valid.txt', cache_dir=cache_dir) cache_file_not_exists = fscache.path('test_valid_not_exists.txt') content = 'abc' # Create a cache file first. def test_save(): fscache.save(cache_file, content) assert cache_file.exists() def test_valid_not_exists(): assert fscache.valid(cache_file_not_exists) is False def test_valid_with_lifetime(): assert fscache.valid(cache_file, lifetime=3600) def test_valid_with_lifetime_zero(): assert fscache.valid(cache_file, lifetime=0) is False def test_valid_without_lifetime(): assert fscache.valid(cache_file)
#!/usr/bin/env python # -*- coding: utf-8 -*- import decimal import uuid from array import array from datetime import datetime from dateutil import parser from fscache import fscache cache_dir = '.fscache' cache_file = fscache.path('test_content.json', cache_dir=cache_dir) content = { 'array': array('I', [1, 2, 3]), # has 'tolist' attr 'bytes': b'abc', 'datetime': datetime.now(), 'decimal': decimal.Decimal(10), 'set': set('abc'), 'uuid': uuid.uuid1() } # Create a cache file first. def test_save(): fscache.save(cache_file, content, mode='json') assert cache_file.exists() def test_load():