Exemple #1
0
    def test_empty_write(self):
        bs = BlockStructure(self.f, block_size=16, initialize=True)
        io = BlockStructureOrderedDataIO(self.f, bs)
        io.write(b'')
        io.write(b'test.')

        self.reopen_file()

        io = BlockStructureOrderedDataIO(self.f, BlockStructure(self.f))
        assert b"tes" == io.read(3)
        assert b't.' == io.read(10)
Exemple #2
0
    def test_write_without_truncate(self):
        msg = "This is a not-so-basic test. I need to fill about one more block." +\
                " And I'm out of ideas."
        bs = BlockStructure(self.f, block_size=16, initialize=True)
        io = BlockStructureOrderedDataIO(self.f, bs, blocksize=16)

        io.write(string_to_bytes(msg))

        msg2 = "definitely a not-so-basic test."
        io.seek(21)
        io.write(string_to_bytes(msg2))
        self.f.flush()

        assert len(bs.blocks) == 6

        self.reopen_file()
        bs2 = BlockStructure(self.f)
        got = BlockStructureOrderedDataIO(self.f, bs2).read(pos=0)
        expected = (msg[:21] + msg2 + msg[21 + len(msg2):]).encode()
        assert expected == got
Exemple #3
0
    def test_write_with_truncate(self):
        msg = "This is a not-so-basic test. I need to fill about one more block." +\
                " And I'm out of ideas."
        bs = BlockStructure(self.f, block_size=16, initialize=True)
        io = BlockStructureOrderedDataIO(self.f, bs, blocksize=16)
        io.write(string_to_bytes(msg))
        self.f.flush()

        assert len(bs.blocks) == 6

        msg2 = "definitely a not-so-basic test."
        io.seek(8)
        io.write(string_to_bytes(msg2), truncate=True)
        self.f.flush()

        assert len(bs.blocks) == 3

        self.reopen_file()
        bs2 = BlockStructure(self.f)
        io2 = BlockStructureOrderedDataIO(self.f, bs2)
        got = b"".join(
            BlockStructureOrderedDataIO(self.f, bs2).iterdata(0, chunk_size=1))
        expected = "This is definitely a not-so-basic test.".encode()
        assert expected == got
Exemple #4
0
    def test_basic_data_io(self):
        msg = "Basic test."
        bs = BlockStructure(self.f, block_size=16, initialize=True)
        io = BlockStructureOrderedDataIO(self.f, bs)

        io.write(string_to_bytes(msg))
        self.f.flush()

        self.reopen_file()
        bs2 = BlockStructure(self.f)
        io2 = BlockStructureOrderedDataIO(self.f, bs2)
        got = b"".join(io2.iterdata(0, chunk_size=1))
        expected = msg.encode()
        assert expected == got
Exemple #5
0
    def test_auto_extension(self):
        msg = "This is an advanced test where multiple blocks will be dynamically added " +\
                "to the structure."
        bs = BlockStructure(self.f, block_size=16, initialize=True)
        io = BlockStructureOrderedDataIO(self.f, bs, blocksize=16)

        io.write(string_to_bytes(msg))
        self.f.flush()
        assert len(bs.blocks) == len(msg) // 16 + 1

        self.reopen_file()
        bs2 = BlockStructure(self.f)
        io2 = BlockStructureOrderedDataIO(self.f, bs2)
        got = b"".join(
            BlockStructureOrderedDataIO(self.f, bs2).iterdata(0, chunk_size=1))
        expected = msg.encode()
        assert expected == got
Exemple #6
0
 def reopen_file(self):
     super().reopen_file()
     bs = BlockStructure(self.f, block_size=128)
     self.io = BlockStructureOrderedDataIO(self.f, bs, blocksize=128)
Exemple #7
0
 def setup(self):
     super().setup()
     bs = BlockStructure(self.f, block_size=128, initialize=True)
     self.io = BlockStructureOrderedDataIO(self.f, bs, blocksize=128)
Exemple #8
0
    def test_size(self):
        msg = "A short string"
        bs = BlockStructure(self.f, block_size=16, initialize=True)
        io = BlockStructureOrderedDataIO(self.f, bs, blocksize=16)

        io.write(string_to_bytes(msg))

        assert io.size() == len(msg)

        msg2 = "Something random, really."
        io.seek(6)
        io.write(string_to_bytes(msg2))

        assert io.size() == len(msg[:6] + msg2)

        io.seek(10)
        io.write(string_to_bytes("small string"))

        assert io.size() == len(msg[:6] + msg2)

        msg3 = "tail."
        io.seek(io.size())
        io.write(string_to_bytes(msg3))

        assert io.size() == len(msg[:6] + msg2 + msg3)
Exemple #9
0
    def test_multiple_write(self):
        msg = "A very very very very long string for no reason."
        bs = BlockStructure(self.f, block_size=16, initialize=True)
        io = BlockStructureOrderedDataIO(self.f, bs, blocksize=16)

        io.write(string_to_bytes(msg[:5]))
        io.write(string_to_bytes(msg[5:12]))
        io.write(string_to_bytes(msg[12:14]))
        io.write(string_to_bytes(msg[14:134]))
        self.f.flush()

        self.reopen_file()
        bs2 = BlockStructure(self.f)
        io2 = BlockStructureOrderedDataIO(self.f, bs2)
        io2.seek(0)
        got = io2.read(3) + io2.read(6) + io2.read()
        expected = msg.encode()
        assert expected == got