]> git.jsancho.org Git - datasette-connectors.git/blob - tests/fixtures.py
Test HTTP redirection
[datasette-connectors.git] / tests / fixtures.py
1 from datasette_connectors import monkey; monkey.patch_datasette()
2 from datasette_connectors.connectors import ConnectorList
3 from .dummy import DummyConnector
4 ConnectorList.add_connector('dummy', DummyConnector())
5
6 from datasette.app import Datasette
7 from datasette.utils.testing import TestClient
8 import os
9 import pytest
10 import tempfile
11 import contextlib
12
13
14 def populate_file(filepath):
15     dummyfile = open(filepath, "w")
16     dummyfile.write("This is a dummy file. We need something to force a SQLite error")
17     dummyfile.close()
18
19
20 @contextlib.contextmanager
21 def make_app_client(
22         max_returned_rows=None,
23         config=None,
24         is_immutable=False,
25 ):
26     with tempfile.TemporaryDirectory() as tmpdir:
27         filepath = os.path.join(tmpdir, 'dummy_tables.db')
28         populate_file(filepath)
29         if is_immutable:
30             files = []
31             immutables = [filepath]
32         else:
33             files = [filepath]
34             immutables = []
35         config = config or {}
36         config.update({
37             'default_page_size': 50,
38             'max_returned_rows': max_returned_rows or 1000,
39         })
40         ds = Datasette(
41             files,
42             immutables=immutables,
43             config=config,
44         )
45         client = TestClient(ds.app())
46         client.ds = ds
47         yield client
48
49
50 @pytest.fixture(scope='session')
51 def app_client():
52     with make_app_client() as client:
53         yield client
54
55
56 @pytest.fixture(scope='session')
57 def app_client_with_hash():
58     with make_app_client(config={"hash_urls": True}, is_immutable=True) as client:
59         yield client