Skip to content

Commit 82d126b

Browse files
gabor-borosGabor Boros
and
Gabor Boros
committed
feat: reql command format
Co-authored-by: Gabor Boros <[email protected]> Signed-off-by: Gabor Boros <[email protected]>
1 parent fcc4191 commit 82d126b

File tree

6 files changed

+51
-4
lines changed

6 files changed

+51
-4
lines changed

.coveragerc

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ omit = *tests*
77

88
[report]
99
sort = cover
10-
fail_under = 72
10+
fail_under = 70
1111
exclude_lines = pragma: no cover
1212
if __name__ == .__main__.:

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
PACKAGE_NAME = rethinkdb
1818

1919
PROTO_FILE_NAME = ql2.proto
20-
PROTO_FILE_URL = https://raw.githubusercontent.com/rethinkdb/rethinkdb/next/src/rdb_protocol/${PROTO_FILE_NAME}
20+
PROTO_FILE_URL = https://raw.githubusercontent.com/rethinkdb/rethinkdb/70654faefe29bb0b4f6010fa82bd30a207d014d8/src/rdb_protocol/${PROTO_FILE_NAME}
2121
TARGET_PROTO_FILE = ${PROTO_FILE_NAME}
2222

2323
FILE_CONVERTER_NAME = ./scripts/convert_protofile.py
@@ -118,12 +118,12 @@ test-unit: ## run unit tests and generate coverage
118118

119119
.PHONY: test-integration
120120
test-integration: ## run unit tests and generate coverage
121-
coverage run -m pytest -m "integration" -vv
121+
coverage run -m pytest -m "integration" -m "not v2_5" -vv
122122
coverage report
123123

124124
.PHONY: test
125125
test: ## run all tests and generate coverage
126-
coverage run -m pytest -vv
126+
coverage run -m pytest -m "not v2_5" -vv
127127
coverage report
128128
coverage xml
129129

pytest.ini

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ python_functions=test_*
55
markers =
66
unit: Select only unit tests
77
integration: Select only integration tests
8+
v2_5: RethinkDB 2.5+ compatible tests

rethinkdb/ast.py

+10
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,9 @@ def to_json_string(self, *args):
545545
def match(self, *args):
546546
return Match(self, *args)
547547

548+
def format(self, *args, **kwargs):
549+
return Format(self, *args, **kwargs)
550+
548551
def split(self, *args):
549552
return Split(self, *args)
550553

@@ -1624,6 +1627,13 @@ def __init__(self, *args, **kwargs):
16241627
self.statement = "match"
16251628

16261629

1630+
class Format(ReqlMethodQuery):
1631+
def __init__(self, *args, **kwargs):
1632+
super().__init__(*args, **kwargs)
1633+
self.term_type = P_TERM.FORMAT
1634+
self.statement = "format"
1635+
1636+
16271637
class ToJsonString(ReqlMethodQuery):
16281638
def __init__(self, *args, **kwargs):
16291639
super().__init__(*args, **kwargs)

rethinkdb/query.py

+11
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"error",
5151
"february",
5252
"floor",
53+
"format",
5354
"friday",
5455
"ge",
5556
"geojson",
@@ -787,6 +788,16 @@ def circle(*arguments, **kwargs):
787788
return ast.Circle(*arguments, **kwargs)
788789

789790

791+
def format(*arguments, **kwargs): # pylint: disable=redefined-builtin
792+
"""
793+
Format command takes a string as a template and formatting parameters as an
794+
object. The parameters in the template string must exist as keys in the
795+
object, otherwise, an error raised. The template must be a string literal
796+
and cannot be the result of other commands.
797+
"""
798+
return ast.Format(*arguments, **kwargs)
799+
800+
790801
row = ast.ImplicitVar()
791802

792803
# Time enum values

tests/integration/test_queries.py

+25
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,31 @@ def test_floor(conn):
389389
assert_test_table(query.floor, conn, scenarios)
390390

391391

392+
@pytest.mark.integration
393+
@pytest.mark.v2_5
394+
def test_format(conn):
395+
scenarios = [
396+
Scenario(
397+
name="text", args=["hello {name}", {"name": "bob"}], expected="hello bob"
398+
),
399+
Scenario(
400+
name="numbers", args=["1..2..{count}", {"count": 3}], expected="1..2..3"
401+
),
402+
Scenario(
403+
name="object",
404+
args=["object: {obj}", {"obj": {"foo": "bar"}}],
405+
expected='object: {"foo":"bar"}',
406+
),
407+
Scenario(
408+
name="array",
409+
args=["array: {arr}", {"arr": [1, 2, 3]}],
410+
expected="array: [1,2,3]",
411+
),
412+
]
413+
414+
assert_test_table(query.format, conn, scenarios)
415+
416+
392417
@pytest.mark.integration
393418
def test_ge(conn):
394419
scenarios = [

0 commit comments

Comments
 (0)