from ..lrucache import LRUCache
from . import BaseTestCase
from tempfile import TemporaryFile


class LRUCacheTestCase(BaseTestCase):

    def test(self):
        c = LRUCache(2, dispose=lambda _: None)
        self.assert_equal(len(c), 0)
        for i, x in enumerate('abc'):
            c[x] = i
        self.assert_equal(len(c), 2)
        self.assert_equal(c.items(), set([('b', 1), ('c', 2)]))
        self.assert_equal(False, 'a' in c)
        self.assert_equal(True, 'b' in c)
        self.assert_raises(KeyError, lambda: c['a'])
        self.assert_equal(c['b'], 1)
        self.assert_equal(c['c'], 2)
        c['d'] = 3
        self.assert_equal(len(c), 2)
        self.assert_equal(c['c'], 2)
        self.assert_equal(c['d'], 3)
        del c['c']
        self.assert_equal(len(c), 1)
        self.assert_raises(KeyError, lambda: c['c'])
        self.assert_equal(c['d'], 3)
        c.clear()
        self.assert_equal(c.items(), set())

    def test_dispose(self):
        c = LRUCache(2, dispose=lambda f: f.close())
        f1 = TemporaryFile()
        f2 = TemporaryFile()
        f3 = TemporaryFile()
        c[1] = f1
        c[2] = f2
        self.assert_equal(False, f2.closed)
        c[3] = f3
        self.assert_equal(False, 1 in c)
        self.assert_equal(True, f1.closed)
        self.assert_equal(True, 2 in c)
        self.assert_equal(False, f2.closed)
        del c[2]
        self.assert_equal(False, 2 in c)
        self.assert_equal(True, f2.closed)
        c.clear()
        self.assert_equal(c.items(), set())
        self.assert_equal(True, f3.closed)

from ..lrucache import LRUCache
import pytest
from tempfile import TemporaryFile


class TestLRUCache:

    def test_lrucache(self):
        c = LRUCache(2, dispose=lambda _: None)
        assert len(c) == 0
        assert c.items() == set()
        for i, x in enumerate('abc'):
            c[x] = i
        assert len(c) == 2
        assert c.items() == set([('b', 1), ('c', 2)])
        assert 'a' not in c
        assert 'b' in c
        with pytest.raises(KeyError):
            c['a']
        assert c['b'] == 1
        assert c['c'] == 2
        c['d'] = 3
        assert len(c) == 2
        assert c['c'] == 2
        assert c['d'] == 3
        del c['c']
        assert len(c) == 1
        with pytest.raises(KeyError):
            c['c']
        assert c['d'] == 3
        c.clear()
        assert c.items() == set()

    def test_dispose(self):
        c = LRUCache(2, dispose=lambda f: f.close())
        f1 = TemporaryFile()
        f2 = TemporaryFile()
        f3 = TemporaryFile()
        c[1] = f1
        c[2] = f2
        assert not f2.closed
        c[3] = f3
        assert 1 not in c
        assert f1.closed
        assert 2 in c
        assert not f2.closed
        del c[2]
        assert 2 not in c
        assert f2.closed
        c.clear()
        assert not c.items()
        assert f3.closed

change test case to py.test