Reset _metadata_local in a couple of tests

Refs https://github.com/simonw/datasette/pull/1960#issuecomment-1356476886
This commit is contained in:
Simon Willison 2022-12-17 13:40:27 -08:00
commit 89cffcf14c
2 changed files with 44 additions and 34 deletions

View file

@ -582,22 +582,28 @@ async def test_facet_size():
data5 = response5.json() data5 = response5.json()
assert len(data5["facet_results"]["city"]["results"]) == 20 assert len(data5["facet_results"]["city"]["results"]) == 20
# Now try messing with facet_size in the table metadata # Now try messing with facet_size in the table metadata
ds._metadata_local = { orig_metadata = ds._metadata_local
"databases": { try:
"test_facet_size": {"tables": {"neighbourhoods": {"facet_size": 6}}} ds._metadata_local = {
"databases": {
"test_facet_size": {"tables": {"neighbourhoods": {"facet_size": 6}}}
}
} }
} response6 = await ds.client.get(
response6 = await ds.client.get("/test_facet_size/neighbourhoods.json?_facet=city") "/test_facet_size/neighbourhoods.json?_facet=city"
data6 = response6.json() )
assert len(data6["facet_results"]["city"]["results"]) == 6 data6 = response6.json()
# Setting it to max bumps it up to 50 again assert len(data6["facet_results"]["city"]["results"]) == 6
ds._metadata_local["databases"]["test_facet_size"]["tables"]["neighbourhoods"][ # Setting it to max bumps it up to 50 again
"facet_size" ds._metadata_local["databases"]["test_facet_size"]["tables"]["neighbourhoods"][
] = "max" "facet_size"
data7 = ( ] = "max"
await ds.client.get("/test_facet_size/neighbourhoods.json?_facet=city") data7 = (
).json() await ds.client.get("/test_facet_size/neighbourhoods.json?_facet=city")
assert len(data7["facet_results"]["city"]["results"]) == 20 ).json()
assert len(data7["facet_results"]["city"]["results"]) == 20
finally:
ds._metadata_local = orig_metadata
def test_other_types_of_facet_in_metadata(): def test_other_types_of_facet_in_metadata():

View file

@ -990,28 +990,32 @@ def test_hook_skip_csrf(app_client):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_hook_get_metadata(ds_client): async def test_hook_get_metadata(ds_client):
ds_client.ds._metadata_local = { try:
"title": "Testing get_metadata hook!", orig_metadata = ds_client.ds._metadata_local
"databases": {"from-local": {"title": "Hello from local metadata"}}, ds_client.ds._metadata_local = {
} "title": "Testing get_metadata hook!",
og_pm_hook_get_metadata = pm.hook.get_metadata "databases": {"from-local": {"title": "Hello from local metadata"}},
}
og_pm_hook_get_metadata = pm.hook.get_metadata
def get_metadata_mock(*args, **kwargs): def get_metadata_mock(*args, **kwargs):
return [ return [
{ {
"databases": { "databases": {
"from-hook": {"title": "Hello from the plugin hook"}, "from-hook": {"title": "Hello from the plugin hook"},
"from-local": {"title": "This will be overwritten!"}, "from-local": {"title": "This will be overwritten!"},
}
} }
} ]
]
pm.hook.get_metadata = get_metadata_mock pm.hook.get_metadata = get_metadata_mock
meta = ds_client.ds.metadata() meta = ds_client.ds.metadata()
assert "Testing get_metadata hook!" == meta["title"] assert "Testing get_metadata hook!" == meta["title"]
assert "Hello from local metadata" == meta["databases"]["from-local"]["title"] assert "Hello from local metadata" == meta["databases"]["from-local"]["title"]
assert "Hello from the plugin hook" == meta["databases"]["from-hook"]["title"] assert "Hello from the plugin hook" == meta["databases"]["from-hook"]["title"]
pm.hook.get_metadata = og_pm_hook_get_metadata pm.hook.get_metadata = og_pm_hook_get_metadata
finally:
ds_client.ds._metadata_local = orig_metadata
def _extract_commands(output): def _extract_commands(output):