@@ -19,7 +19,10 @@ If you use the template described in :ref:`writing_plugins_cookiecutter` your pl
19
19
response = await datasette.client.get(" /-/plugins.json" )
20
20
assert response.status_code == 200
21
21
installed_plugins = {p[" name" ] for p in response.json()}
22
- assert " datasette-plugin-template-demo" in installed_plugins
22
+ assert (
23
+ " datasette-plugin-template-demo"
24
+ in installed_plugins
25
+ )
23
26
24
27
25
28
This test uses the :ref: `internals_datasette_client ` object to exercise a test instance of Datasette. ``datasette.client `` is a wrapper around the `HTTPX <https://www.python-httpx.org/ >`__ Python library which can imitate HTTP requests using ASGI. This is the recommended way to write tests against a Datasette instance.
@@ -37,9 +40,7 @@ If you are building an installable package you can add them as test dependencies
37
40
setup(
38
41
name = " datasette-my-plugin" ,
39
42
# ...
40
- extras_require = {
41
- " test" : [" pytest" , " pytest-asyncio" ]
42
- },
43
+ extras_require = {" test" : [" pytest" , " pytest-asyncio" ]},
43
44
tests_require = [" datasette-my-plugin[test]" ],
44
45
)
45
46
@@ -87,31 +88,34 @@ Here's an example that uses the `sqlite-utils library <https://sqlite-utils.data
87
88
import pytest
88
89
import sqlite_utils
89
90
91
+
90
92
@pytest.fixture (scope = " session" )
91
93
def datasette (tmp_path_factory ):
92
94
db_directory = tmp_path_factory.mktemp(" dbs" )
93
95
db_path = db_directory / " test.db"
94
96
db = sqlite_utils.Database(db_path)
95
- db[" dogs" ].insert_all([
96
- {" id" : 1 , " name" : " Cleo" , " age" : 5 },
97
- {" id" : 2 , " name" : " Pancakes" , " age" : 4 }
98
- ], pk = " id" )
97
+ db[" dogs" ].insert_all(
98
+ [
99
+ {" id" : 1 , " name" : " Cleo" , " age" : 5 },
100
+ {" id" : 2 , " name" : " Pancakes" , " age" : 4 },
101
+ ],
102
+ pk = " id" ,
103
+ )
99
104
datasette = Datasette(
100
105
[db_path],
101
106
metadata = {
102
107
" databases" : {
103
108
" test" : {
104
109
" tables" : {
105
- " dogs" : {
106
- " title" : " Some dogs"
107
- }
110
+ " dogs" : {" title" : " Some dogs" }
108
111
}
109
112
}
110
113
}
111
- }
114
+ },
112
115
)
113
116
return datasette
114
117
118
+
115
119
@pytest.mark.asyncio
116
120
async def test_example_table_json (datasette ):
117
121
response = await datasette.client.get(" /test/dogs.json?_shape=array" )
@@ -121,6 +125,7 @@ Here's an example that uses the `sqlite-utils library <https://sqlite-utils.data
121
125
{" id" : 2 , " name" : " Pancakes" , " age" : 4 },
122
126
]
123
127
128
+
124
129
@pytest.mark.asyncio
125
130
async def test_example_table_html (datasette ):
126
131
response = await datasette.client.get(" /test/dogs" )
@@ -137,6 +142,7 @@ If you want to create that test database repeatedly for every individual test fu
137
142
@pytest.fixture
138
143
def datasette (tmp_path_factory ):
139
144
# This fixture will be executed repeatedly for every test
145
+ ...
140
146
141
147
.. _testing_plugins_pytest_httpx :
142
148
@@ -197,14 +203,17 @@ Here's a test for that plugin that mocks the HTTPX outbound request:
197
203
198
204
async def test_outbound_http_call (httpx_mock ):
199
205
httpx_mock.add_response(
200
- url = ' https://www.example.com/' ,
201
- text = ' Hello world' ,
206
+ url = " https://www.example.com/" ,
207
+ text = " Hello world" ,
202
208
)
203
209
datasette = Datasette([], memory = True )
204
- response = await datasette.client.post(" /-/fetch-url" , data = {
205
- " url" : " https://www.example.com/"
206
- })
210
+ response = await datasette.client.post(
211
+ " /-/fetch-url" ,
212
+ data = {" url" : " https://www.example.com/" },
213
+ )
207
214
assert response.text == " Hello world"
208
215
209
216
outbound_request = httpx_mock.get_request()
210
- assert outbound_request.url == " https://www.example.com/"
217
+ assert (
218
+ outbound_request.url == " https://www.example.com/"
219
+ )
0 commit comments