Coverage for tests\test_reconcile_schema.py: 100%
68 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-12-20 00:39 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-12-20 00:39 +0000
1import json
2import logging
4import httpx
5import jsonschema
6import pytest
7from datasette.app import Datasette
9from tests.conftest import get_schema, plugin_metadata
11logger = logging.getLogger(__name__)
14@pytest.mark.asyncio
15@pytest.mark.parametrize("schema_version, schema", get_schema("manifest.json").items())
16async def test_schema_manifest(schema_version, schema, db_path):
17 app = Datasette([db_path], metadata=plugin_metadata({"name_field": "name"})).app()
18 async with httpx.AsyncClient(app=app) as client:
19 response = await client.get("http://localhost/test/dogs/-/reconcile")
20 data = response.json()
21 logging.info(f"Schema version: {schema_version}")
22 jsonschema.validate(
23 instance=data,
24 schema=schema,
25 cls=jsonschema.Draft7Validator,
26 )
29@pytest.mark.asyncio
30@pytest.mark.parametrize("schema_version, schema", get_schema("manifest.json").items())
31async def test_schema_manifest_extend(schema_version, schema, db_path):
32 app = Datasette(
33 [db_path],
34 metadata=plugin_metadata(
35 {"name_field": "name", "properties": [{"name": "status", "label": "Status", "type": "text"}]}
36 ),
37 ).app()
38 async with httpx.AsyncClient(app=app) as client:
39 response = await client.get("http://localhost/test/dogs/-/reconcile")
40 data = response.json()
41 logging.info(f"Schema version: {schema_version}")
42 jsonschema.validate(
43 instance=data,
44 schema=schema,
45 cls=jsonschema.Draft7Validator,
46 )
49@pytest.mark.asyncio
50@pytest.mark.parametrize("schema_version, schema", get_schema("reconciliation-result-batch.json").items())
51async def test_response_queries_schema_post(schema_version, schema, db_path):
52 app = Datasette([db_path], metadata=plugin_metadata({"name_field": "name"})).app()
53 async with httpx.AsyncClient(app=app) as client:
54 response = await client.post(
55 "http://localhost/test/dogs/-/reconcile",
56 data={"queries": json.dumps({"q0": {"query": "fido"}})},
57 )
58 assert 200 == response.status_code
59 data = response.json()
60 logging.info(f"Schema version: {schema_version}")
61 jsonschema.validate(
62 instance=data,
63 schema=schema,
64 cls=jsonschema.Draft7Validator,
65 )
68@pytest.mark.asyncio
69@pytest.mark.parametrize("schema_version, schema", get_schema("reconciliation-result-batch.json").items())
70async def test_response_queries_schema_get(schema_version, schema, db_path):
71 app = Datasette([db_path], metadata=plugin_metadata({"name_field": "name"})).app()
72 async with httpx.AsyncClient(app=app) as client:
73 queries = json.dumps({"q0": {"query": "fido"}})
74 response = await client.get(f"http://localhost/test/dogs/-/reconcile?queries={queries}")
75 assert 200 == response.status_code
76 data = response.json()
77 logging.info(f"Schema version: {schema_version}")
78 jsonschema.validate(
79 instance=data,
80 schema=schema,
81 cls=jsonschema.Draft7Validator,
82 )
85@pytest.mark.asyncio
86@pytest.mark.parametrize("schema_version, schema", get_schema("reconciliation-result-batch.json").items())
87async def test_response_queries_no_results_schema_post(schema_version, schema, db_path):
88 app = Datasette([db_path], metadata=plugin_metadata({"name_field": "name"})).app()
89 async with httpx.AsyncClient(app=app) as client:
90 response = await client.post(
91 "http://localhost/test/dogs/-/reconcile",
92 data={"queries": json.dumps({"q0": {"query": "abcdef"}})},
93 )
94 assert 200 == response.status_code
95 data = response.json()
96 logging.info(f"Schema version: {schema_version}")
97 jsonschema.validate(
98 instance=data,
99 schema=schema,
100 cls=jsonschema.Draft7Validator,
101 )
104@pytest.mark.asyncio
105@pytest.mark.parametrize("schema_version, schema", get_schema("reconciliation-result-batch.json").items())
106async def test_response_queries_no_results_schema_get(schema_version, schema, db_path):
107 app = Datasette([db_path], metadata=plugin_metadata({"name_field": "name"})).app()
108 async with httpx.AsyncClient(app=app) as client:
109 queries = json.dumps({"q0": {"query": "abcdef"}})
110 response = await client.get(f"http://localhost/test/dogs/-/reconcile?queries={queries}")
111 assert 200 == response.status_code
112 data = response.json()
113 logging.info(f"Schema version: {schema_version}")
114 jsonschema.validate(
115 instance=data,
116 schema=schema,
117 cls=jsonschema.Draft7Validator,
118 )