File: test_cache.py

package info (click to toggle)
mkdocs-include-markdown-plugin 7.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,316 kB
  • sloc: python: 5,360; makefile: 6
file content (60 lines) | stat: -rw-r--r-- 1,440 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import time

from mkdocs_include_markdown_plugin.cache import (
    Cache,
    get_cache_directory,
    initialize_cache,
    is_platformdirs_installed,
)


def test_cache_read_file(tmp_path):
    cache = Cache(tmp_path)
    assert cache.read_file('pyproject.toml').split('\n', 1)[0] == (
        'name = "mkdocs-include-markdown-plugin"'
    )


def test_cache_expiration_on_get(tmp_path):
    cache = Cache(tmp_path)
    cache.set_('foo', f'{time.time() - 600*10}\nbar')
    assert cache.get_('foo') is None


def test_cache_clean(tmp_path):
    now_ts = int(time.time())

    file1 = tmp_path / 'file1'
    file1.write_text(f'{now_ts}\n')
    file2 = tmp_path / 'file2'
    file2.write_text(f'{now_ts}\n')

    assert len(os.listdir(tmp_path)) == 2

    cache = Cache(tmp_path, 0)
    cache.clean()

    assert len(os.listdir(tmp_path)) == 0


def test_get_cache_directory_empty():
    if not is_platformdirs_installed():
        assert get_cache_directory('') is None
    else:
        assert isinstance(get_cache_directory(''), str)


def test_get_cache_directory_custom():
    assert get_cache_directory('foo') == 'foo'


def test_initialize_cache_not_cache_dir():
    if not is_platformdirs_installed():
        assert initialize_cache(300, '') is None
    else:
        assert isinstance(initialize_cache(300, ''), Cache)


def test_initialize_cache_cache_dir():
    assert isinstance(initialize_cache(300, 'foo'), Cache)