mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
test_create_table_error_rows_twice_with_duplicates, refs #1927
This commit is contained in:
parent
9342b60f14
commit
dee18ed8ce
2 changed files with 38 additions and 1 deletions
|
|
@ -683,7 +683,11 @@ class TableCreateView(BaseView):
|
||||||
bad_pks = False
|
bad_pks = False
|
||||||
if len(actual_pks) == 1 and data.get("pk") and data["pk"] != actual_pks[0]:
|
if len(actual_pks) == 1 and data.get("pk") and data["pk"] != actual_pks[0]:
|
||||||
bad_pks = True
|
bad_pks = True
|
||||||
elif len(actual_pks) > 1 and data.get("pks") and set(data["pks"]) != set(actual_pks):
|
elif (
|
||||||
|
len(actual_pks) > 1
|
||||||
|
and data.get("pks")
|
||||||
|
and set(data["pks"]) != set(actual_pks)
|
||||||
|
):
|
||||||
bad_pks = True
|
bad_pks = True
|
||||||
if bad_pks:
|
if bad_pks:
|
||||||
return _error(["pk cannot be changed for existing table"])
|
return _error(["pk cannot be changed for existing table"])
|
||||||
|
|
|
||||||
|
|
@ -1236,6 +1236,39 @@ async def test_create_table_error_if_pk_changed(ds_write):
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_create_table_error_rows_twice_with_duplicates(ds_write):
|
||||||
|
# Error if you don't send ignore: True or replace: True
|
||||||
|
token = write_token(ds_write)
|
||||||
|
input = {
|
||||||
|
"rows": [{"id": 1, "name": "Row 1"}, {"id": 2, "name": "Row 2"}],
|
||||||
|
"table": "test_create_twice",
|
||||||
|
"pk": "id",
|
||||||
|
}
|
||||||
|
first_response = await ds_write.client.post(
|
||||||
|
"/data/-/create",
|
||||||
|
json=input,
|
||||||
|
headers={
|
||||||
|
"Authorization": "Bearer {}".format(token),
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert first_response.status_code == 201
|
||||||
|
second_response = await ds_write.client.post(
|
||||||
|
"/data/-/create",
|
||||||
|
json=input,
|
||||||
|
headers={
|
||||||
|
"Authorization": "Bearer {}".format(token),
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
assert second_response.status_code == 400
|
||||||
|
assert second_response.json() == {
|
||||||
|
"ok": False,
|
||||||
|
"errors": ["UNIQUE constraint failed: test_create_twice.id"],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"path",
|
"path",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue