]> git.jsancho.org Git - datasette-pytables.git/commitdiff
Tests for api homepage and api database page
authorJavier Sancho <jsf@jsancho.org>
Wed, 23 May 2018 09:50:01 +0000 (11:50 +0200)
committerJavier Sancho <jsf@jsancho.org>
Wed, 23 May 2018 09:50:01 +0000 (11:50 +0200)
setup.py
tests/__init__.py [new file with mode: 0644]
tests/fixtures.py [new file with mode: 0644]
tests/test_api.py [new file with mode: 0644]

index 466ce4cc5e2fac0b0bfb805d895fc636bcc54473..04f4253f30ee49e1c7fe15acddfa175e9c372368 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -26,6 +26,7 @@ setup(
             'pytables = datasette_pytables'
         ],
     },
-    install_requires=['datasette', 'tables', 'moz-sql-parser', 'mo-future']
+    install_requires=['datasette', 'tables', 'moz-sql-parser', 'mo-future'],
+    tests_require=['pytest']
 )
 
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/fixtures.py b/tests/fixtures.py
new file mode 100644 (file)
index 0000000..d5fdb05
--- /dev/null
@@ -0,0 +1,51 @@
+from datasette.app import Datasette
+import os
+from tables import *
+import tempfile
+
+def app_client(max_returned_rows=None):
+    with tempfile.TemporaryDirectory() as tmpdir:
+        filepath = os.path.join(tmpdir, 'test_tables.h5')
+        populate_file(filepath)
+        ds = Datasette(
+            [filepath],
+            config={
+                'default_page_size': 50,
+                'max_returned_rows': max_returned_rows or 100,
+            }
+        )
+        client = ds.app().test_client
+        client.ds = ds
+        yield client
+
+def populate_file(filepath):
+    class Particle(IsDescription):
+        identity = StringCol(itemsize=22, dflt=' ', pos=0)
+        idnumber = Int16Col(dflt=1, pos=1)
+        speed = Float32Col(dflt=1, pos=2)
+
+    h5file = open_file(filepath, mode='w')
+    root = h5file.root
+
+    group1 = h5file.create_group(root, 'group1')
+    group2 = h5file.create_group(root, 'group2')
+
+    array1 = h5file.create_array(root, 'array1', ['string', 'array'])
+
+    table1 = h5file.create_table(group1, 'table1', Particle)
+    table2 = h5file.create_table(group2, 'table2', Particle)
+
+    array2 = h5file.create_array(group1, 'array2', [x for x in range(10000)])
+
+    for table in (table1, table2):
+        row = table.row
+
+        for i in range(10000):
+            row['identity'] = 'This is particle: %2d' % (i)
+            row['idnumber'] = i
+            row['speed'] = i * 2.
+            row.append()
+
+        table.flush()
+
+    h5file.close()
diff --git a/tests/test_api.py b/tests/test_api.py
new file mode 100644 (file)
index 0000000..80c6409
--- /dev/null
@@ -0,0 +1,54 @@
+from .fixtures import app_client
+import pytest
+
+pytest.fixture(scope='module')(app_client)
+
+def test_homepage(app_client):
+    _, response = app_client.get('/.json')
+    assert response.status == 200
+    assert response.json.keys() == {'test_tables': 0}.keys()
+    d = response.json['test_tables']
+    assert d['name'] == 'test_tables'
+    assert d['tables_count'] == 4
+
+def test_database_page(app_client):
+    response = app_client.get('/test_tables.json', gather_request=False)
+    data = response.json
+    assert 'test_tables' == data['database']
+    assert [{
+        'name': '/array1',
+        'columns': [],
+        'primary_keys': [],
+        'count': 2,
+        'label_column': None,
+        'hidden': False,
+        'fts_table': None,
+        'foreign_keys': {'incoming': [], 'outgoing': []}
+    }, {
+        'name': '/group1/array2',
+        'columns': [],
+        'primary_keys': [],
+        'count': 10000,
+        'label_column': None,
+        'hidden': False,
+        'fts_table': None,
+        'foreign_keys': {'incoming': [], 'outgoing': []}
+    }, {
+        'name': '/group1/table1',
+        'columns': ['identity', 'idnumber', 'speed'],
+        'primary_keys': [],
+        'count': 10000,
+        'label_column': None,
+        'hidden': False,
+        'fts_table': None,
+        'foreign_keys': {'incoming': [], 'outgoing': []}
+    }, {
+        'name': '/group2/table2',
+        'columns': ['identity', 'idnumber', 'speed'],
+        'primary_keys': [],
+        'count': 10000,
+        'label_column': None,
+        'hidden': False,
+        'fts_table': None,
+        'foreign_keys': {'incoming': [], 'outgoing': []}
+    }] == data['tables']