Post 2.0.1 release actions done
[datasette-pytables.git] / tests / fixtures.py
1 from datasette_connectors import monkey; monkey.patch_datasette()
2 from datasette_connectors.connectors import ConnectorList
3 from datasette_pytables import PyTablesConnector
4 ConnectorList.add_connector('pytables', PyTablesConnector)
5
6 from datasette.app import Datasette
7 from datasette.utils.testing import TestClient
8 import numpy as np
9 import os
10 import pytest
11 from tables import *
12 import tempfile
13 import contextlib
14
15
16 def populate_file(filepath):
17     class Particle(IsDescription):
18         identity = StringCol(itemsize=22, dflt=' ', pos=0)
19         idnumber = Int16Col(dflt=1, pos=1)
20         speed = Float32Col(dflt=1, pos=2)
21
22     h5file = open_file(filepath, mode='w')
23     root = h5file.root
24
25     group1 = h5file.create_group(root, 'group1')
26     group2 = h5file.create_group(root, 'group2')
27
28     array1 = h5file.create_array(root, 'array1', ['string', 'array'])
29
30     table1 = h5file.create_table(group1, 'table1', Particle)
31     table2 = h5file.create_table(group2, 'table2', Particle)
32
33     array2 = h5file.create_array(group1, 'array2', [x for x in range(10000)])
34
35     multiarray = h5file.create_array(group2, 'multi', np.arange(1000).reshape(10, 50, 2))
36
37     for table in (table1, table2):
38         row = table.row
39
40         for i in range(10000):
41             row['identity'] = 'This is particle: %2d' % (i)
42             row['idnumber'] = i
43             row['speed'] = i * 2.
44             row.append()
45
46         table.flush()
47
48     h5file.close()
49
50
51 @contextlib.contextmanager
52 def make_app_client(
53         max_returned_rows=None,
54         config=None,
55         is_immutable=False,
56 ):
57     with tempfile.TemporaryDirectory() as tmpdir:
58         filepath = os.path.join(tmpdir, 'test_tables.h5')
59         populate_file(filepath)
60         if is_immutable:
61             files = []
62             immutables = [filepath]
63         else:
64             files = [filepath]
65             immutables = []
66         config = config or {}
67         config.update({
68             'default_page_size': 50,
69             'max_returned_rows': max_returned_rows or 1000,
70         })
71         ds = Datasette(
72             files,
73             immutables=immutables,
74             config=config,
75         )
76         client = TestClient(ds.app())
77         client.ds = ds
78         yield client
79
80
81 @pytest.fixture(scope='session')
82 def app_client():
83     with make_app_client() as client:
84         yield client
85
86
87 @pytest.fixture(scope='session')
88 def app_client_with_hash():
89     with make_app_client(config={"hash_urls": True}, is_immutable=True) as client:
90         yield client