Пример #1
0
def test_this_defaults(capsys):
    options = microcache.MicrocacheOptions()
    cache = microcache.Microcache(options_obj=options)

    @microcache.this(cache_obj=cache)
    def foo(bar='bar'):
        print('setting bar to: ' + bar)
        return bar

    assert foo() == 'bar'
    out, err = capsys.readouterr()
    assert out.strip() == 'setting bar to: bar'

    assert foo() == 'bar'
    out, err = capsys.readouterr()
    assert out.strip() == ''

    assert foo(bar='baz') == 'baz'
    out, err = capsys.readouterr()
    assert out.strip() == 'setting bar to: baz'

    assert foo() == 'bar'
    out, err = capsys.readouterr()
    assert out.strip() == ''

    assert foo(bar='baz') == 'baz'
    out, err = capsys.readouterr()
    assert out.strip() == ''
Пример #2
0
def test_Microcache_clear_specific():
    options = microcache.MicrocacheOptions()
    cache = microcache.Microcache(options_obj=options)
    assert cache.upsert('foo', 'bar') is True
    assert cache.has('foo') is True
    assert cache.clear('foo') is True
    assert cache.has('foo') is False
Пример #3
0
def test_Microcache_get_upsert():
    options = microcache.MicrocacheOptions()
    cache = microcache.Microcache(options_obj=options)
    assert cache.upsert('foo', 'baz', ttl=1) is True
    assert cache.get('foo') == 'baz'
    time.sleep(1)
    assert cache.get('foo') is microcache.CACHE_MISS
Пример #4
0
def test_Microcache_disable_enable_without_clear():
    options = microcache.MicrocacheOptions()
    cache = microcache.Microcache(options_obj=options)
    assert cache.upsert('foo', 'bar') is True
    cache.disable(clear_cache=False)
    assert cache.has('foo') is microcache.CACHE_DISABLED
    cache.enable()
    assert cache.has('foo') is True
Пример #5
0
def test_Microcache_temporary_enable_disable():
    options = microcache.MicrocacheOptions()
    cache = microcache.Microcache(options_obj=options)
    assert cache.upsert('foo', 'bar') is True
    with cache.temporarily_disabled():
        assert cache.has('foo') is microcache.CACHE_DISABLED
        with cache.temporarily_enabled():
            assert cache.has('foo') is True
        assert cache.has('foo') is microcache.CACHE_DISABLED
    assert cache.has('foo') is True
Пример #6
0
def test_Microcache_disable_enable_with_clear():
    options = microcache.MicrocacheOptions()
    cache = microcache.Microcache(options_obj=options)
    assert cache.upsert('foo', 'bar') is True
    cache.disable()
    assert cache.has('foo') is microcache.CACHE_DISABLED
    assert cache.get('foo') is microcache.CACHE_DISABLED
    assert cache.clear() is microcache.CACHE_DISABLED
    cache.enable()
    assert cache.has(
        'foo') is False  # the cache was cleared as part of disable()
Пример #7
0
def test_this_custom_key(capsys):
    options = microcache.MicrocacheOptions()
    cache = microcache.Microcache(options_obj=options)

    @microcache.this(cache_obj=cache, key='foo')
    def foo(bar='bar'):
        print('setting bar to: ' + bar)
        return bar

    assert foo() == 'bar'
    out, err = capsys.readouterr()
    assert out.strip() == 'setting bar to: bar'
    assert cache.has('foo') is True

    assert foo(bar='baz') == 'bar'
    out, err = capsys.readouterr()
    assert out.strip() == ''
    assert cache.get('foo') == 'bar'
Пример #8
0
def test_this_ttl(capsys):
    options = microcache.MicrocacheOptions()
    cache = microcache.Microcache(options_obj=options)

    @microcache.this(cache_obj=cache, ttl=1)
    def foo(bar='bar'):
        print('setting bar to: ' + bar)
        return bar

    assert foo() == 'bar'
    out, err = capsys.readouterr()
    assert out.strip() == 'setting bar to: bar'

    assert foo() == 'bar'
    out, err = capsys.readouterr()
    assert out.strip() == ''

    time.sleep(1)

    assert foo() == 'bar'
    out, err = capsys.readouterr()
    assert out.strip() == 'setting bar to: bar'
Пример #9
0
def test_Microcache_has_upsert():
    options = microcache.MicrocacheOptions()
    cache = microcache.Microcache(options_obj=options)
    assert cache.has('foo') is False
    assert cache.upsert('foo', 'bar') is True
    assert cache.has('foo') is True