diff --git a/pyproject.toml b/pyproject.toml index ba2618b..44395be 100755 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "rushdb" -version = "3.2.0" +version = "3.2.1" description = "RushDB SDK for Python: the client for RushDB, the database for AI (RushDB Cloud or self-hosted)" authors = [ {name = "RushDB Team", email = "hi@rushdb.com"} diff --git a/src/rushdb/api/records.py b/src/rushdb/api/records.py index 76d0781..99f3303 100755 --- a/src/rushdb/api/records.py +++ b/src/rushdb/api/records.py @@ -144,6 +144,7 @@ def update( data (Dict[str, Any]): The data to merge with the existing record data. transaction (Optional[Transaction], optional): Transaction context for the operation. If provided, the operation will be part of the transaction. Defaults to None. + label (Optional[str]): Optional label to re-assign to the record. Returns: Dict[str, str]: Response from the server containing operation status. @@ -154,8 +155,7 @@ def update( """ record_id = pick_record_id(target) headers = Transaction._build_transaction_header(transaction) - - payload = data.copy() + payload: Dict[str, Any] = {"data": data} if label is not None: payload["label"] = label return self.client._make_request( diff --git a/src/rushdb/direct/router.py b/src/rushdb/direct/router.py index 72a375a..ae35e46 100644 --- a/src/rushdb/direct/router.py +++ b/src/rushdb/direct/router.py @@ -122,20 +122,18 @@ def _sdk_records(records: Optional[List[Body]]) -> List[Body]: return [to_sdk_record(record) or {} for record in records or []] -_RECORD_KEYS = {"label", "options", "vectors", "data", "properties"} - - def _record_properties(body: Body) -> Body: - """A write's flat data: ``data``, property drafts as an object, or the flat body.""" + """A record write's data, as RushDB Cloud reads it: ``data`` (a flat object) + or ``properties`` (a list of property drafts). Anything else is refused.""" data = body.get("data") if isinstance(data, dict): return data properties = body.get("properties") if isinstance(properties, list): return {item.get("name"): item.get("value") for item in properties} - if isinstance(properties, dict): - return properties - return {key: value for key, value in body.items() if key not in _RECORD_KEYS} + raise _http_error( + 400, "A record write takes `data` (an object) or `properties` (a list)." + ) def _is_empty(value: Any) -> bool: diff --git a/tests/test_direct_mode.py b/tests/test_direct_mode.py index 922f57c..2540fff 100755 --- a/tests/test_direct_mode.py +++ b/tests/test_direct_mode.py @@ -483,6 +483,47 @@ def test_updates_replaces_deletes_and_links_are_staged_by_id(engine_calls): ] +def put_records(calls): + return [ + op["mutation"] + for op in operations(calls) + if op["kind"] == "mutate" and op["mutation"]["kind"] == "putRecord" + ] + + +def test_update_merges_the_data_object_with_the_label(engine_calls): + db = direct_db() + data = {"title": "Emma", "label": "classic", "options": "a,b"} + db.records.update(RECORD["id"], data, label="Book") + (put,) = put_records(engine_calls) + assert put["mode"] == "merge" and put["id"] == RECORD["id"] + assert put["properties"] == data + + +def test_a_flat_record_write_is_refused_as_cloud_refuses_it(engine_calls): + db = direct_db() + for method in ("PATCH", "PUT"): + with pytest.raises(RushDBError) as raised: + db._make_request( + method, f"/records/{RECORD['id']}", {"label": "Book", "title": "x"} + ) + assert raised.value.status == 400 + with pytest.raises(RushDBError): + db._make_request("POST", "/records", {"label": "Book", "title": "x"}) + assert not put_records(engine_calls) + + +def test_property_drafts_are_written_like_data(engine_calls): + db = direct_db() + db._make_request( + "PATCH", + f"/records/{RECORD['id']}", + {"label": "Book", "properties": [{"name": "title", "value": "Emma"}]}, + ) + (put,) = put_records(engine_calls) + assert put["properties"] == {"title": "Emma"} + + def test_imports_are_staged_into_a_transaction(engine_calls): db = direct_db() db.records.import_json([{"title": "Dune"}], label="Book", transaction="tx-1") diff --git a/tests/test_direct_mode_live.py b/tests/test_direct_mode_live.py index cde3864..5c3e3cc 100644 --- a/tests/test_direct_mode_live.py +++ b/tests/test_direct_mode_live.py @@ -50,6 +50,10 @@ def test_records_round_trip(db): assert db.records.find_by_id(created.id)["title"] == "Ubik" db.records.update(created.id, {"pages": 210}) assert db.records.find_by_id(created.id)["pages"] == 210 + db.records.update(created.id, {"pages": 211, "label": "worn"}, label=BOOK) + updated = db.records.find_by_id(created.id) + assert updated["pages"] == 211 and updated["label"] == "worn" + assert updated["title"] == "Ubik" and updated.label == BOOK db.records.set(created.id, {"title": "Ubik!"}, label=BOOK) replaced = db.records.find_by_id(created.id) assert replaced["title"] == "Ubik!" and replaced.get("pages") is None diff --git a/tests/test_records_write_body.py b/tests/test_records_write_body.py new file mode 100644 index 0000000..e01a782 --- /dev/null +++ b/tests/test_records_write_body.py @@ -0,0 +1,51 @@ +"""The request bodies record writes send to RushDB Cloud: the same +``{label, data, options}`` shape the JavaScript SDK sends.""" + +from unittest import mock + +from rushdb import RushDB + +RECORD_ID = "0190a7e2-0000-7000-8000-000000000001" + + +def sent(call): + db = RushDB("API-KEY", base_url="http://cloud.test/api/v1") + with mock.patch.object(db, "_make_request", return_value={"data": {}}) as request: + call(db) + (method, path, body, headers), _ = request.call_args + return method, path, body + + +def test_update_sends_the_fields_under_data_with_the_label(): + method, path, body = sent( + lambda db: db.records.update( + RECORD_ID, {"expires_at": "2026-12-01T00:00:00Z"}, label="Memory" + ) + ) + assert (method, path) == ("PATCH", f"/records/{RECORD_ID}") + assert body == {"label": "Memory", "data": {"expires_at": "2026-12-01T00:00:00Z"}} + + +def test_update_without_a_label_still_sends_data(): + _, _, body = sent(lambda db: db.records.update(RECORD_ID, {"status": "done"})) + assert body == {"data": {"status": "done"}} + + +def test_update_keeps_fields_named_like_body_keys_inside_data(): + data = {"label": "urgent", "options": "a,b", "data": "raw"} + _, _, body = sent(lambda db: db.records.update(RECORD_ID, data, label="Task")) + assert body == {"label": "Task", "data": data} + + +def test_update_does_not_mutate_the_callers_dict(): + data = {"status": "done"} + sent(lambda db: db.records.update(RECORD_ID, data, label="Task")) + assert data == {"status": "done"} + + +def test_set_sends_the_same_shape_over_put(): + method, path, body = sent( + lambda db: db.records.set(RECORD_ID, {"status": "done"}, label="Task") + ) + assert (method, path) == ("PUT", f"/records/{RECORD_ID}") + assert body == {"data": {"status": "done"}, "label": "Task"} diff --git a/uv.lock b/uv.lock index 5e42c1e..7e4ce67 100755 --- a/uv.lock +++ b/uv.lock @@ -710,7 +710,7 @@ wheels = [ [[package]] name = "rushdb" -version = "3.2.0" +version = "3.2.1" source = { editable = "." } [package.dev-dependencies]