Esempio n. 1
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)
Esempio n. 2
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
Esempio n. 3
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
Esempio n. 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
Esempio n. 5
0
    def encode_metadata(self, io, pos=0):
        io.seek(pos)
        io.write(self.MAGIC_BYTES)

        io.write(int_to_bytes(len(self.class_name)))
        io.write(string_to_bytes(self.class_name))

        io.write(int_to_bytes(len(self.column_names)))

        for x in self.column_names:
            io.write(int_to_bytes(len(x)))
            io.write(string_to_bytes(x))

        io.write(int_to_bytes(len(self.primary_key)))
        io.write(string_to_bytes(self.primary_key))

        io.write(int_to_bytes(len(self.unique_keys)))

        for x in self.unique_keys:
            io.write(int_to_bytes(len(x)))
            io.write(string_to_bytes(x))

        io.write(int_to_bytes(self.row_count))
Esempio n. 6
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
Esempio n. 7
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
Esempio n. 8
0
def test_string_to_bytes():
    assert string_to_bytes("testing ...") == b'testing ...'