Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ users may find useful:
to you, if you need to pin your usage to a particular, supported
WherobotsDB version. Defaults to the latest, most-optimized version
of WherobotsDB available to your subscription.
* `environment_preset_id`: the ID of one of your organization's
environment presets to start the SQL session with. Defaults to your
organization's default preset. When the preset pins a WherobotsDB
version and `version` is omitted, the session uses the preset's
version; an explicit `version` takes precedence.
* `environment_override_preset_id`: the ID of one of your organization's
override presets to layer on the environment preset. Defaults to your
organization's default override preset.
* `session_type`: `"single"` or `"multi"`; if set to `"single"`, then
each call to `connect()` establishes an exclusive connection to a
distinct and dedicated Wherobots runtime; if set to "multi", then
Expand Down
64 changes: 64 additions & 0 deletions tests/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,70 @@ def _run_connect_full(
return post_kwargs, cd_kwargs


class TestConnectEnvironmentPresets:
"""Presets reach the API only when asked for."""

@patch("wherobots.db.driver.requests.get")
@patch("wherobots.db.driver.requests.post")
def test_presets_are_sent_in_the_body(self, mock_post, mock_get):
kwargs = _run_connect(
mock_post,
mock_get,
environment_preset_id="preset-1",
environment_override_preset_id="override-1",
)
assert kwargs["json"]["environmentPresetId"] == "preset-1"
assert kwargs["json"]["environmentOverridePresetId"] == "override-1"

@patch("wherobots.db.driver.requests.get")
@patch("wherobots.db.driver.requests.post")
def test_omitted_presets_leave_the_body_unchanged(self, mock_post, mock_get):
kwargs = _run_connect(mock_post, mock_get)
assert kwargs["json"] == {
"runtimeId": None,
"shutdownAfterInactiveSeconds": None,
"version": None,
"sessionType": "multi",
}

@pytest.mark.parametrize(
("param", "field"),
[
("environment_preset_id", "environmentPresetId"),
("environment_override_preset_id", "environmentOverridePresetId"),
],
)
@pytest.mark.parametrize("value", [None, ""])
@patch("wherobots.db.driver.requests.get")
@patch("wherobots.db.driver.requests.post")
def test_each_unset_preset_is_omitted(
self, mock_post, mock_get, param, field, value
):
"""An empty ID means the default, so it must not change session reuse."""
kwargs = _run_connect(mock_post, mock_get, **{param: value})
assert field not in kwargs["json"]

@pytest.mark.parametrize(
("param", "field"),
[
("environment_preset_id", "environmentPresetId"),
("environment_override_preset_id", "environmentOverridePresetId"),
],
)
@patch("wherobots.db.driver.requests.get")
@patch("wherobots.db.driver.requests.post")
def test_each_preset_is_sent_alone(self, mock_post, mock_get, param, field):
kwargs = _run_connect(mock_post, mock_get, **{param: "p-1"})
assert kwargs["json"][field] == "p-1"
assert (
len(
{"environmentPresetId", "environmentOverridePresetId"}
& kwargs["json"].keys()
)
== 1
)


class TestConnectSessionKey:
"""session_key reaches the API, and the API's fallback reaches the caller."""

Expand Down
12 changes: 12 additions & 0 deletions wherobots/db/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ def connect(
reconnect_timeout_seconds: Union[float, None] = DEFAULT_RECONNECT_TIMEOUT_SECONDS,
session_key_exclusive: bool = False,
require_execution_ownership: bool = False,
environment_preset_id: Union[str, None] = None,
environment_override_preset_id: Union[str, None] = None,
) -> Connection:
"""Create a connection to a Wherobots SQL session.

Expand Down Expand Up @@ -185,6 +187,11 @@ def connect(
another client. Requires ``session_key``. The connection's
``session_key_exclusive`` says whether the API did.
:param require_execution_ownership: See :func:`connect_direct`.
:param environment_preset_id: The environment preset to start the SQL
session with. When omitted, your organization's default preset is used.
:param environment_override_preset_id: The override preset to layer on the
environment preset. When omitted, your organization's default override
is used.
"""
if not token and not api_key:
raise ValueError("At least one of `token` or `api_key` is required")
Expand Down Expand Up @@ -241,6 +248,11 @@ def connect(
body["sessionKey"] = session_key
if session_key_exclusive:
body["sessionKeyExclusive"] = True
# An empty ID means the default, and must not split session reuse from it.
if environment_preset_id:
body["environmentPresetId"] = environment_preset_id
if environment_override_preset_id:
body["environmentOverridePresetId"] = environment_override_preset_id

try:
resp = requests.post(
Expand Down
Loading