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