mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-10-25 02:37:07 +02:00
Allow table.get() to look up rows by column values
table.get() now accepts column=value keyword arguments and returns the first matching row, so a row can be fetched by a unique column rather than only by its primary key. Passing both a primary key and keyword arguments raises ValueError, and calling get() with neither raises TypeError. Refs #588
This commit is contained in:
parent
6bc1d33d58
commit
65b3fc8789
3 changed files with 106 additions and 1 deletions
|
|
@ -30,3 +30,71 @@ def test_get_not_found(argument, expected_msg, fresh_db):
|
|||
fresh_db.table("dogs").get(argument)
|
||||
if expected_msg is not None:
|
||||
assert expected_msg == excinfo.value.args[0]
|
||||
|
||||
|
||||
def test_get_by_column(fresh_db):
|
||||
dogs = fresh_db.table("dogs")
|
||||
dogs.insert_all(
|
||||
[
|
||||
{"id": 1, "name": "Cleo", "age": 4},
|
||||
{"id": 2, "name": "Pancakes", "age": 2},
|
||||
],
|
||||
pk="id",
|
||||
)
|
||||
assert dogs.get(name="Pancakes") == {"id": 2, "name": "Pancakes", "age": 2}
|
||||
|
||||
|
||||
def test_get_by_multiple_columns(fresh_db):
|
||||
dogs = fresh_db.table("dogs")
|
||||
dogs.insert_all(
|
||||
[
|
||||
{"id": 1, "name": "Cleo", "age": 4},
|
||||
{"id": 2, "name": "Cleo", "age": 2},
|
||||
],
|
||||
pk="id",
|
||||
)
|
||||
assert dogs.get(name="Cleo", age=2)["id"] == 2
|
||||
|
||||
|
||||
def test_get_by_column_sets_last_pk(fresh_db):
|
||||
dogs = fresh_db.table("dogs")
|
||||
dogs.insert({"id": 5, "name": "Cleo", "age": 4}, pk="id")
|
||||
dogs.get(name="Cleo")
|
||||
assert dogs.last_pk == 5
|
||||
|
||||
|
||||
def test_get_by_column_compound_pk_sets_last_pk(fresh_db):
|
||||
table = fresh_db.table("records")
|
||||
table.insert_all(
|
||||
[{"a": 1, "b": 2, "note": "x"}, {"a": 1, "b": 3, "note": "y"}],
|
||||
pk=("a", "b"),
|
||||
)
|
||||
assert table.get(note="y")["b"] == 3
|
||||
assert table.last_pk == (1, 3)
|
||||
|
||||
|
||||
def test_get_by_column_on_rowid_table(fresh_db):
|
||||
dogs = fresh_db.table("dogs")
|
||||
dogs.insert({"name": "Cleo", "age": 4})
|
||||
assert dogs.get(name="Cleo")["name"] == "Cleo"
|
||||
|
||||
|
||||
def test_get_by_column_not_found(fresh_db):
|
||||
dogs = fresh_db.table("dogs")
|
||||
dogs.insert({"id": 1, "name": "Cleo", "age": 4}, pk="id")
|
||||
with pytest.raises(NotFoundError):
|
||||
dogs.get(name="Pancakes")
|
||||
|
||||
|
||||
def test_get_rejects_pk_and_column(fresh_db):
|
||||
dogs = fresh_db.table("dogs")
|
||||
dogs.insert({"id": 1, "name": "Cleo", "age": 4}, pk="id")
|
||||
with pytest.raises(ValueError):
|
||||
dogs.get(1, name="Cleo")
|
||||
|
||||
|
||||
def test_get_requires_argument(fresh_db):
|
||||
dogs = fresh_db.table("dogs")
|
||||
dogs.insert({"id": 1, "name": "Cleo", "age": 4}, pk="id")
|
||||
with pytest.raises(TypeError):
|
||||
dogs.get()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue