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