2020-12-08 18:49:42 +01:00
|
|
|
import collections
|
|
|
|
|
import json
|
|
|
|
|
|
2019-07-28 15:30:28 +03:00
|
|
|
import pytest
|
|
|
|
|
|
2020-12-08 18:49:42 +01:00
|
|
|
from sqlite_utils.db import NotFoundError
|
|
|
|
|
|
2019-07-28 15:30:28 +03:00
|
|
|
|
|
|
|
|
def test_update_rowid_table(fresh_db):
|
|
|
|
|
table = fresh_db["table"]
|
|
|
|
|
rowid = table.insert({"foo": "bar"}).last_pk
|
|
|
|
|
table.update(rowid, {"foo": "baz"})
|
|
|
|
|
assert [{"foo": "baz"}] == list(table.rows)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_pk_table(fresh_db):
|
|
|
|
|
table = fresh_db["table"]
|
|
|
|
|
pk = table.insert({"foo": "bar", "id": 5}, pk="id").last_pk
|
|
|
|
|
assert 5 == pk
|
|
|
|
|
table.update(pk, {"foo": "baz"})
|
|
|
|
|
assert [{"id": 5, "foo": "baz"}] == list(table.rows)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_update_compound_pk_table(fresh_db):
|
|
|
|
|
table = fresh_db["table"]
|
|
|
|
|
pk = table.insert({"id1": 5, "id2": 3, "v": 1}, pk=("id1", "id2")).last_pk
|
|
|
|
|
assert (5, 3) == pk
|
|
|
|
|
table.update(pk, {"v": 2})
|
|
|
|
|
assert [{"id1": 5, "id2": 3, "v": 2}] == list(table.rows)
|
2019-07-28 17:46:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"pk,update_pk",
|
|
|
|
|
(
|
|
|
|
|
(None, 2),
|
|
|
|
|
(None, None),
|
|
|
|
|
("id1", None),
|
|
|
|
|
("id1", 4),
|
|
|
|
|
(("id1", "id2"), None),
|
|
|
|
|
(("id1", "id2"), 4),
|
|
|
|
|
(("id1", "id2"), (4, 5)),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
def test_update_invalid_pk(fresh_db, pk, update_pk):
|
|
|
|
|
table = fresh_db["table"]
|
|
|
|
|
table.insert({"id1": 5, "id2": 3, "v": 1}, pk=pk).last_pk
|
|
|
|
|
with pytest.raises(NotFoundError):
|
|
|
|
|
table.update(update_pk, {"v": 2})
|
|
|
|
|
|
|
|
|
|
|
2019-07-28 17:51:49 +03:00
|
|
|
def test_update_alter(fresh_db):
|
|
|
|
|
table = fresh_db["table"]
|
|
|
|
|
rowid = table.insert({"foo": "bar"}).last_pk
|
|
|
|
|
table.update(rowid, {"new_col": 1.2}, alter=True)
|
|
|
|
|
assert [{"foo": "bar", "new_col": 1.2}] == list(table.rows)
|
|
|
|
|
# Let's try adding three cols at once
|
|
|
|
|
table.update(
|
|
|
|
|
rowid,
|
|
|
|
|
{"str_col": "str", "bytes_col": b"\xa0 has bytes", "int_col": -10},
|
|
|
|
|
alter=True,
|
|
|
|
|
)
|
|
|
|
|
assert [
|
|
|
|
|
{
|
|
|
|
|
"foo": "bar",
|
|
|
|
|
"new_col": 1.2,
|
|
|
|
|
"str_col": "str",
|
|
|
|
|
"bytes_col": b"\xa0 has bytes",
|
|
|
|
|
"int_col": -10,
|
|
|
|
|
}
|
|
|
|
|
] == list(table.rows)
|
2019-07-28 17:59:52 +03:00
|
|
|
|
|
|
|
|
|
2020-02-26 20:55:17 -08:00
|
|
|
def test_update_alter_with_invalid_column_characters(fresh_db):
|
|
|
|
|
table = fresh_db["table"]
|
|
|
|
|
rowid = table.insert({"foo": "bar"}).last_pk
|
|
|
|
|
with pytest.raises(AssertionError):
|
|
|
|
|
table.update(rowid, {"new_col[abc]": 1.2}, alter=True)
|
|
|
|
|
|
|
|
|
|
|
2019-07-28 17:59:52 +03:00
|
|
|
def test_update_with_no_values_sets_last_pk(fresh_db):
|
|
|
|
|
table = fresh_db.table("dogs", pk="id")
|
2019-07-28 18:37:27 +03:00
|
|
|
table.insert_all([{"id": 1, "name": "Cleo"}, {"id": 2, "name": "Pancakes"}])
|
2019-07-28 17:59:52 +03:00
|
|
|
table.update(1)
|
|
|
|
|
assert 1 == table.last_pk
|
|
|
|
|
table.update(2)
|
|
|
|
|
assert 2 == table.last_pk
|
|
|
|
|
with pytest.raises(NotFoundError):
|
|
|
|
|
table.update(3)
|
2020-12-08 18:49:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"data_structure",
|
|
|
|
|
(
|
|
|
|
|
["list with one item"],
|
|
|
|
|
["list with", "two items"],
|
|
|
|
|
{"dictionary": "simple"},
|
|
|
|
|
{"dictionary": {"nested": "complex"}},
|
|
|
|
|
collections.OrderedDict(
|
|
|
|
|
[
|
|
|
|
|
("key1", {"nested": "complex"}),
|
|
|
|
|
("key2", "foo"),
|
|
|
|
|
]
|
|
|
|
|
),
|
|
|
|
|
[{"list": "of"}, {"two": "dicts"}],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
def test_update_dictionaries_and_lists_as_json(fresh_db, data_structure):
|
|
|
|
|
fresh_db["test"].insert({"id": 1, "data": ""}, pk="id")
|
|
|
|
|
fresh_db["test"].update(1, {"data": data_structure})
|
|
|
|
|
row = fresh_db.execute("select id, data from test").fetchone()
|
|
|
|
|
assert row[0] == 1
|
|
|
|
|
assert data_structure == json.loads(row[1])
|