Skip to content

Commit

Permalink
Python: rename modules transaction to batch (#3092)
Browse files Browse the repository at this point in the history
Signed-off-by: Shoham Elias <[email protected]>
  • Loading branch information
shohamazon authored Feb 6, 2025
1 parent 926be5e commit cfee415
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 38 deletions.
4 changes: 2 additions & 2 deletions python/python/glide/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
OnlyIfEqual,
UpdateOptions,
)
from glide.async_commands.server_modules import ft, glide_json, json_transaction
from glide.async_commands.server_modules import ft, glide_json, json_batch
from glide.async_commands.server_modules.ft_options.ft_aggregate_options import (
FtAggregateApply,
FtAggregateClause,
Expand Down Expand Up @@ -273,7 +273,7 @@
"PubSubMsg",
# Json
"glide_json",
"json_transaction",
"json_batch",
"JsonGetOptions",
"JsonArrIndexOptions",
"JsonArrPopOptions",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
Examples:
>>> import json
>>> from glide import json_transaction
>>> from glide import json_batch
>>> transaction = ClusterTransaction()
>>> value = {'a': 1.0, 'b': 2}
>>> json_str = json.dumps(value) # Convert Python dictionary to JSON string using json.dumps()
>>> json_transaction.set(transaction, "doc", "$", json_str)
>>> json_transaction.get(transaction, "doc", "$") # Returns the value at path '$' in the JSON document stored at `doc` as JSON string.
>>> json_batch.set(transaction, "doc", "$", json_str)
>>> json_batch.get(transaction, "doc", "$") # Returns the value at path '$' in the JSON document stored at `doc` as JSON string.
>>> result = await glide_client.exec(transaction)
>>> print result[0] # set result
'OK' # Indicates successful setting of the value at path '$' in the key stored at `doc`.
Expand Down
64 changes: 31 additions & 33 deletions python/python/tests/tests_server_modules/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest
from glide.async_commands.core import ConditionalChange, InfoSection
from glide.async_commands.server_modules import glide_json as json
from glide.async_commands.server_modules import json_transaction
from glide.async_commands.server_modules import json_batch
from glide.async_commands.server_modules.glide_json import (
JsonArrIndexOptions,
JsonArrPopOptions,
Expand Down Expand Up @@ -2107,30 +2107,28 @@ async def test_json_arrpop(self, glide_client: TGlideClient):

@pytest.mark.parametrize("cluster_mode", [True])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_json_transaction_array(self, glide_client: GlideClusterClient):
async def test_json_batch_array(self, glide_client: GlideClusterClient):
transaction = ClusterTransaction()

key = get_random_string(5)
json_value1 = {"a": 1.0, "b": 2}
json_value2 = {"a": 1.0, "b": [1, 2]}

# Test 'set', 'get', and 'clear' commands
json_transaction.set(transaction, key, "$", OuterJson.dumps(json_value1))
json_transaction.clear(transaction, key, "$")
json_transaction.set(transaction, key, "$", OuterJson.dumps(json_value1))
json_transaction.get(transaction, key, ".")
json_batch.set(transaction, key, "$", OuterJson.dumps(json_value1))
json_batch.clear(transaction, key, "$")
json_batch.set(transaction, key, "$", OuterJson.dumps(json_value1))
json_batch.get(transaction, key, ".")

# Test array related commands
json_transaction.set(transaction, key, "$", OuterJson.dumps(json_value2))
json_transaction.arrappend(transaction, key, "$.b", ["3", "4"])
json_transaction.arrindex(transaction, key, "$.b", "2")
json_transaction.arrinsert(transaction, key, "$.b", 2, ["5"])
json_transaction.arrlen(transaction, key, "$.b")
json_transaction.arrpop(
transaction, key, JsonArrPopOptions(path="$.b", index=2)
)
json_transaction.arrtrim(transaction, key, "$.b", 1, 2)
json_transaction.get(transaction, key, ".")
json_batch.set(transaction, key, "$", OuterJson.dumps(json_value2))
json_batch.arrappend(transaction, key, "$.b", ["3", "4"])
json_batch.arrindex(transaction, key, "$.b", "2")
json_batch.arrinsert(transaction, key, "$.b", 2, ["5"])
json_batch.arrlen(transaction, key, "$.b")
json_batch.arrpop(transaction, key, JsonArrPopOptions(path="$.b", index=2))
json_batch.arrtrim(transaction, key, "$.b", 1, 2)
json_batch.get(transaction, key, ".")

result = await glide_client.exec(transaction)
assert isinstance(result, list)
Expand All @@ -2153,51 +2151,51 @@ async def test_json_transaction_array(self, glide_client: GlideClusterClient):

@pytest.mark.parametrize("cluster_mode", [True])
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3])
async def test_json_transaction(self, glide_client: GlideClusterClient):
async def test_json_batch(self, glide_client: GlideClusterClient):
transaction = ClusterTransaction()

key = f"{{key}}-1{get_random_string(5)}"
key2 = f"{{key}}-2{get_random_string(5)}"
key3 = f"{{key}}-3{get_random_string(5)}"
json_value = {"a": [1, 2], "b": [3, 4], "c": "c", "d": True}

json_transaction.set(transaction, key, "$", OuterJson.dumps(json_value))
json_batch.set(transaction, key, "$", OuterJson.dumps(json_value))

# Test debug commands
json_transaction.debug_memory(transaction, key, "$.a")
json_transaction.debug_fields(transaction, key, "$.a")
json_batch.debug_memory(transaction, key, "$.a")
json_batch.debug_fields(transaction, key, "$.a")

# Test obj commands
json_transaction.objlen(transaction, key, ".")
json_transaction.objkeys(transaction, key, ".")
json_batch.objlen(transaction, key, ".")
json_batch.objkeys(transaction, key, ".")

# Test num commands
json_transaction.numincrby(transaction, key, "$.a[*]", 10.0)
json_transaction.nummultby(transaction, key, "$.a[*]", 10.0)
json_batch.numincrby(transaction, key, "$.a[*]", 10.0)
json_batch.nummultby(transaction, key, "$.a[*]", 10.0)

# Test str commands
json_transaction.strappend(transaction, key, '"-test"', "$.c")
json_transaction.strlen(transaction, key, "$.c")
json_batch.strappend(transaction, key, '"-test"', "$.c")
json_batch.strlen(transaction, key, "$.c")

# Test type command
json_transaction.type(transaction, key, "$.a")
json_batch.type(transaction, key, "$.a")

# Test mget command
json_value2 = {"b": [3, 4], "c": "c", "d": True}
json_transaction.set(transaction, key2, "$", OuterJson.dumps(json_value2))
json_transaction.mget(transaction, [key, key2, key3], "$.a")
json_batch.set(transaction, key2, "$", OuterJson.dumps(json_value2))
json_batch.mget(transaction, [key, key2, key3], "$.a")

# Test toggle command
json_transaction.toggle(transaction, key, "$.d")
json_batch.toggle(transaction, key, "$.d")

# Test resp command
json_transaction.resp(transaction, key, "$")
json_batch.resp(transaction, key, "$")

# Test del command
json_transaction.delete(transaction, key, "$.d")
json_batch.delete(transaction, key, "$.d")

# Test forget command
json_transaction.forget(transaction, key, "$.c")
json_batch.forget(transaction, key, "$.c")

result = await glide_client.exec(transaction)
assert isinstance(result, list)
Expand Down

0 comments on commit cfee415

Please sign in to comment.