Skip to content

Commit

Permalink
feat: reql command format
Browse files Browse the repository at this point in the history
Co-authored-by: lsabi <[email protected]>
Signed-off-by: Gabor Boros <[email protected]>
  • Loading branch information
gabor-boros and lsabi committed Jun 21, 2022
1 parent 4154f76 commit 54b6ad5
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ omit = *tests*

[report]
sort = cover
fail_under = 72
fail_under = 70
exclude_lines = pragma: no cover
if __name__ == .__main__.:
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
PACKAGE_NAME = rethinkdb

PROTO_FILE_NAME = ql2.proto
PROTO_FILE_URL = https://raw.githubusercontent.com/rethinkdb/rethinkdb/next/src/rdb_protocol/${PROTO_FILE_NAME}
PROTO_FILE_URL = https://raw.githubusercontent.com/rethinkdb/rethinkdb/70654faefe29bb0b4f6010fa82bd30a207d014d8/src/rdb_protocol/${PROTO_FILE_NAME}
TARGET_PROTO_FILE = ${PROTO_FILE_NAME}

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

.PHONY: test-integration
test-integration: ## run unit tests and generate coverage
coverage run -m pytest -m "integration" -vv
coverage run -m pytest -m "integration" -m "not v2_5" -vv
coverage report

.PHONY: test
test: ## run all tests and generate coverage
coverage run -m pytest -vv
coverage run -m pytest -m "not v2_5" -vv
coverage report
coverage xml

Expand Down
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ python_functions=test_*
markers =
unit: Select only unit tests
integration: Select only integration tests
v2_5: RethinkDB 2.5+ compatible tests
10 changes: 10 additions & 0 deletions rethinkdb/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,9 @@ def to_json_string(self, *args):
def match(self, *args):
return Match(self, *args)

def format(self, *args, **kwargs):
return Format(self, *args, **kwargs)

def split(self, *args):
return Split(self, *args)

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


class Format(ReqlMethodQuery):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.term_type = P_TERM.FORMAT
self.statement = "format"


class ToJsonString(ReqlMethodQuery):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
11 changes: 11 additions & 0 deletions rethinkdb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"error",
"february",
"floor",
"format",
"friday",
"ge",
"geojson",
Expand Down Expand Up @@ -787,6 +788,16 @@ def circle(*arguments, **kwargs):
return ast.Circle(*arguments, **kwargs)


def format(*arguments, **kwargs): # pylint: disable=redefined-builtin
"""
Format command takes a string as a template and formatting parameters as an
object. The parameters in the template string must exist as keys in the
object, otherwise, an error raised. The template must be a string literal
and cannot be the result of other commands.
"""
return ast.Format(*arguments, **kwargs)


row = ast.ImplicitVar()

# Time enum values
Expand Down
25 changes: 25 additions & 0 deletions tests/integration/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,31 @@ def test_floor(conn):
assert_test_table(query.floor, conn, scenarios)


@pytest.mark.integration
@pytest.mark.v2_5
def test_format(conn):
scenarios = [
Scenario(
name="text", args=["hello {name}", {"name": "bob"}], expected="hello bob"
),
Scenario(
name="numbers", args=["1..2..{count}", {"count": 3}], expected="1..2..3"
),
Scenario(
name="object",
args=["object: {obj}", {"obj": {"foo": "bar"}}],
expected='object: {"foo":"bar"}',
),
Scenario(
name="array",
args=["array: {arr}", {"arr": [1, 2, 3]}],
expected="array: [1,2,3]",
),
]

assert_test_table(query.format, conn, scenarios)


@pytest.mark.integration
def test_ge(conn):
scenarios = [
Expand Down

0 comments on commit 54b6ad5

Please sign in to comment.