def setUp(self):
     self.fp = open('page_test.pskv', 'w+')
     self.fp.write(" " * PAGE_SIZE)
     self.mmf = mmap.mmap(self.fp.fileno(), 0)
     self.page = Page(self.mmf)
class PageTest(TestCase):

    def setUp(self):
        self.fp = open('page_test.pskv', 'w+')
        self.fp.write(" " * PAGE_SIZE)
        self.mmf = mmap.mmap(self.fp.fileno(), 0)
        self.page = Page(self.mmf)

    def tearDown(self):
        self.fp.close()


    def test_set_response(self):
        result = self.page.put("test", "blah")
        assert result == 0

        result = self.page.put("test", "blah")
        assert result == 1

    def test_set_and_get(self):
        result = self.page.put("test", "blah")
        assert result == 0

        result = self.page.get("test")
        result.value.should.be("blah")

    def test_write(self):
        self.page.put("test", "blah")
        self.page.write()
        tmp = self.page.mmf
        assert tmp != ""

    def test_load(self):
        self.page.put("test", "blah")
        self.page.write()

        tmp = self.page.mmf

        self.page.load()

    def test_many_writes(self):
        for x in range(500):
            k = str(x)
            self.page.put(k, k)
            tmp = self.page.get(k)
            assert tmp.value == k
            assert tmp.key == k

    def test_iteration(self):
        self.page.put("test1", "test1")
        self.page.put("test2", "test2")

        c = 0
        for x in self.page:
            c += 1
            assert isinstance(x, Record)

        assert c == 2