File size: 2,245 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
import os

import pytest

import tiledb
import tiledb.cc as lt


@pytest.fixture
def text_fname(tmp_path):
    path = os.path.join(tmp_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_lt_schema_create(text_fname):
    ctx = lt.Context()
    schema = lt.Filestore._schema_create(ctx, text_fname)
    assert type(schema) == lt.ArraySchema


def test_libtiledb_schema_create_buffer(tmp_path, text_fname):
    ctx = lt.Context()
    path = os.path.join(tmp_path, "test_libtiledb_schema_create_buffer")
    schema = tiledb.ArraySchema.from_file(text_fname)
    tiledb.Array.create(path, schema)

    data = b"buffer"
    lt.Filestore._buffer_import(ctx, path, data, lt.MIMEType.AUTODETECT)
    assert bytes(data) == lt.Filestore._buffer_export(ctx, path, 0, len(data))
    assert lt.Filestore._size(ctx, path) == len(data)

    output_file = os.path.join(tmp_path, "output_file")
    vfs = tiledb.VFS()
    vfs.touch(output_file)
    lt.Filestore._uri_export(ctx, path, output_file)
    with vfs.open(output_file, "rb") as fio:
        assert fio.read() == data


def test_libtiledb_schema_create_uri(tmp_path, text_fname):
    ctx = lt.Context()
    path = os.path.join(tmp_path, "test_libtiledb_schema_create_uri")
    schema = tiledb.ArraySchema.from_file(text_fname)
    tiledb.Array.create(path, schema)

    lt.Filestore._uri_import(ctx, path, text_fname, lt.MIMEType.AUTODETECT)
    with open(text_fname, "rb") as text:
        data = text.read()
        assert data == lt.Filestore._buffer_export(ctx, path, 0, len(data))
        assert lt.Filestore._size(ctx, path) == len(data)


def test_mime_type():
    to_str = {
        lt.MIMEType.AUTODETECT: "AUTODETECT",
        lt.MIMEType.TIFF: "image/tiff",
        lt.MIMEType.PDF: "application/pdf",
    }

    for k in to_str:
        assert lt.Filestore._mime_type_to_str(k) == to_str[k]

    from_str = {
        "AUTODETECT": lt.MIMEType.AUTODETECT,
        "image/tiff": lt.MIMEType.TIFF,
        "application/pdf": lt.MIMEType.PDF,
    }

    for k in from_str:
        assert lt.Filestore._mime_type_from_str(k) == from_str[k]