]> git.jsancho.org Git - datasette-pytables.git/blob - tests/fixtures.py
Use the datasette-connectors module for monkey patching datasette
[datasette-pytables.git] / tests / fixtures.py
1 from datasette_connectors import monkey; monkey.patch_datasette()
2 from datasette_connectors import connectors; connectors.load()
3
4 from datasette.app import Datasette
5 import numpy as np
6 import os
7 import pytest
8 from tables import *
9 import tempfile
10
11 @pytest.fixture(scope='session')
12 def app_client(max_returned_rows=None):
13     with tempfile.TemporaryDirectory() as tmpdir:
14         filepath = os.path.join(tmpdir, 'test_tables.h5')
15         populate_file(filepath)
16         ds = Datasette(
17             [filepath],
18             config={
19                 'default_page_size': 50,
20                 'max_returned_rows': max_returned_rows or 1000,
21             }
22         )
23         client = ds.app().test_client
24         client.ds = ds
25         yield client
26
27 def populate_file(filepath):
28     class Particle(IsDescription):
29         identity = StringCol(itemsize=22, dflt=' ', pos=0)
30         idnumber = Int16Col(dflt=1, pos=1)
31         speed = Float32Col(dflt=1, pos=2)
32
33     h5file = open_file(filepath, mode='w')
34     root = h5file.root
35
36     group1 = h5file.create_group(root, 'group1')
37     group2 = h5file.create_group(root, 'group2')
38
39     array1 = h5file.create_array(root, 'array1', ['string', 'array'])
40
41     table1 = h5file.create_table(group1, 'table1', Particle)
42     table2 = h5file.create_table(group2, 'table2', Particle)
43
44     array2 = h5file.create_array(group1, 'array2', [x for x in range(10000)])
45
46     multiarray = h5file.create_array(group2, 'multi', np.arange(1000).reshape(10, 50, 2))
47
48     for table in (table1, table2):
49         row = table.row
50
51         for i in range(10000):
52             row['identity'] = 'This is particle: %2d' % (i)
53             row['idnumber'] = i
54             row['speed'] = i * 2.
55             row.append()
56
57         table.flush()
58
59     h5file.close()