File size: 2,635 Bytes
2c3c408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import numpy as np
import pytest

import tiledb

from .common import DiskTestCase, assert_captured


class FilestoreTest(DiskTestCase):
    @pytest.fixture
    def text_fname(self):
        path = self.path("text_fname")
        vfs = tiledb.VFS()
        vfs.touch(path)
        with vfs.open(path, "wb") as fio:
            fio.write(b"Simple text file.\n")
            fio.write(b"With two lines.")
        return path

    def test_buffer(self, capfd):
        path = self.path("test_buffer")
        data = b"buffer"

        fs = tiledb.Filestore(path)

        with self.assertRaises(tiledb.TileDBError):
            fs.write(data)

        schema = tiledb.ArraySchema.from_file()
        tiledb.Array.create(path, schema)

        assert schema.attr(0).name == "contents"
        assert schema.attr(0).dtype == np.bytes_

        schema.attr(0).dump()
        assert_captured(capfd, "Type: BLOB")

        fs = tiledb.Filestore(path)
        fs.write(data)
        assert bytes(data) == fs.read()

    def test_small_buffer(self, capfd):
        path = self.path("test_small_buffer")
        # create a 4 byte array
        data = b"abcd"

        fs = tiledb.Filestore(path)

        with self.assertRaises(tiledb.TileDBError):
            fs.write(data)

        schema = tiledb.ArraySchema.from_file()
        tiledb.Array.create(path, schema)

        assert schema.attr(0).name == "contents"
        assert schema.attr(0).dtype == np.bytes_

        schema.attr(0).dump()
        assert_captured(capfd, "Type: BLOB")

        fs = tiledb.Filestore(path)
        fs.write(data)
        assert data[3:4] == fs.read(offset=3, size=1)

    def test_uri(self, text_fname):
        path = self.path("test_uri")
        schema = tiledb.ArraySchema.from_file(text_fname)
        tiledb.Array.create(path, schema)

        fs = tiledb.Filestore(path)
        tiledb.Filestore.copy_from(path, text_fname)
        with open(text_fname, "rb") as text:
            data = text.read()
            assert data == fs.read(0, len(data))
            assert len(fs) == len(data)

    def test_multiple_writes(self):
        path = self.path("test_buffer")
        schema = tiledb.ArraySchema.from_file()
        tiledb.Array.create(path, schema)

        fs = tiledb.Filestore(path)
        for i in range(1, 4):
            fs.write(("x" * i).encode())

        assert fs.read() == ("x" * i).encode()

        timestamps = [t[0] for t in tiledb.array_fragments(path).timestamp_range]
        for i, ts in enumerate(timestamps, start=1):
            with tiledb.open(path, timestamp=ts) as A:
                assert A.meta["file_size"] == i