mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-11 02:54:23 +02:00
.insert(hash_id_columns=) parameter, closes #343
This commit is contained in:
parent
b6c9dfce0b
commit
931b1e1513
7 changed files with 158 additions and 15 deletions
|
|
@ -9,7 +9,7 @@ from sqlite_utils.db import (
|
|||
Table,
|
||||
View,
|
||||
)
|
||||
from sqlite_utils.utils import sqlite3
|
||||
from sqlite_utils.utils import hash_record, sqlite3
|
||||
import collections
|
||||
import datetime
|
||||
import decimal
|
||||
|
|
@ -888,6 +888,32 @@ def test_insert_hash_id(fresh_db):
|
|||
assert 1 == dogs.count
|
||||
|
||||
|
||||
@pytest.mark.parametrize("use_table_factory", [True, False])
|
||||
def test_insert_hash_id_columns(fresh_db, use_table_factory):
|
||||
if use_table_factory:
|
||||
dogs = fresh_db.table("dogs", hash_id_columns=("name", "twitter"))
|
||||
insert_kwargs = {}
|
||||
else:
|
||||
dogs = fresh_db["dogs"]
|
||||
insert_kwargs = dict(hash_id_columns=("name", "twitter"))
|
||||
|
||||
id = dogs.insert(
|
||||
{"name": "Cleo", "twitter": "cleopaws", "age": 5},
|
||||
**insert_kwargs,
|
||||
).last_pk
|
||||
expected_hash = hash_record({"name": "Cleo", "twitter": "cleopaws"})
|
||||
assert id == expected_hash
|
||||
assert dogs.count == 1
|
||||
# Insert replacing a second time should not create a new row
|
||||
id2 = dogs.insert(
|
||||
{"name": "Cleo", "twitter": "cleopaws", "age": 6},
|
||||
**insert_kwargs,
|
||||
replace=True,
|
||||
).last_pk
|
||||
assert id2 == expected_hash
|
||||
assert dogs.count == 1
|
||||
|
||||
|
||||
def test_vacuum(fresh_db):
|
||||
fresh_db["data"].insert({"foo": "foo", "bar": "bar"})
|
||||
fresh_db.vacuum()
|
||||
|
|
|
|||
|
|
@ -45,6 +45,30 @@ def test_upsert_with_hash_id(fresh_db):
|
|||
assert "a5e744d0164540d33b1d7ea616c28f2fa97e754a" == table.last_pk
|
||||
|
||||
|
||||
@pytest.mark.parametrize("hash_id", (None, "custom_id"))
|
||||
def test_upsert_with_hash_id_columns(fresh_db, hash_id):
|
||||
table = fresh_db["table"]
|
||||
table.upsert({"a": 1, "b": 2, "c": 3}, hash_id=hash_id, hash_id_columns=("a", "b"))
|
||||
assert list(table.rows) == [
|
||||
{
|
||||
hash_id or "id": "4acc71e0547112eb432f0a36fb1924c4a738cb49",
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
"c": 3,
|
||||
}
|
||||
]
|
||||
assert table.last_pk == "4acc71e0547112eb432f0a36fb1924c4a738cb49"
|
||||
table.upsert({"a": 1, "b": 2, "c": 4}, hash_id=hash_id, hash_id_columns=("a", "b"))
|
||||
assert list(table.rows) == [
|
||||
{
|
||||
hash_id or "id": "4acc71e0547112eb432f0a36fb1924c4a738cb49",
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
"c": 4,
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def test_upsert_compound_primary_key(fresh_db):
|
||||
table = fresh_db["table"]
|
||||
table.upsert_all(
|
||||
|
|
|
|||
|
|
@ -35,3 +35,17 @@ def test_chunks(size, expected):
|
|||
input = ["a", "b", "c", "d"]
|
||||
chunks = list(map(list, utils.chunks(input, size)))
|
||||
assert chunks == expected
|
||||
|
||||
|
||||
def test_hash_record():
|
||||
expected = "d383e7c0ba88f5ffcdd09be660de164b3847401a"
|
||||
assert utils.hash_record({"name": "Cleo", "twitter": "CleoPaws"}) == expected
|
||||
assert (
|
||||
utils.hash_record(
|
||||
{"name": "Cleo", "twitter": "CleoPaws", "age": 7}, keys=("name", "twitter")
|
||||
)
|
||||
== expected
|
||||
)
|
||||
assert (
|
||||
utils.hash_record({"name": "Cleo", "twitter": "CleoPaws", "age": 7}) != expected
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue