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