Skip to content

Commit f0451f5

Browse files
SG-37544 Include the "in" and "not_in" operators for payload optimization (#362)
* Include the "in" and "not_in" for payload optimization * Use a decorator to mock environmental variable
1 parent 31df878 commit f0451f5

2 files changed

Lines changed: 40 additions & 9 deletions

File tree

shotgun_api3/shotgun.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4482,16 +4482,11 @@ def _translate_filters_simple(sg_filter):
44824482
if (
44834483
SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION
44844484
and condition["path"] != "id"
4485-
and condition["relation"] in ["is", "is_not"]
4485+
and condition["relation"] in ["is", "is_not", "in", "not_in"]
44864486
and isinstance(values[0], dict)
44874487
):
44884488
try:
4489-
values = [
4490-
{
4491-
"type": values[0]["type"],
4492-
"id": values[0]["id"],
4493-
}
4494-
]
4489+
values = [{"type": v["type"], "id": v["id"]} for v in values]
44954490
except KeyError:
44964491
pass
44974492

tests/test_unit.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import os
1414
import unittest
15+
from unittest import mock
1516
from .mock import patch
1617
import shotgun_api3 as api
1718
from shotgun_api3.shotgun import _is_mimetypes_broken
@@ -434,7 +435,8 @@ def test_related_object(self):
434435
result = api.shotgun._translate_filters(filters, "all")
435436
self.assertEqual(result, expected)
436437

437-
def test_related_object_entity_optimization(self):
438+
@mock.patch.dict(os.environ, {"SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION": "1"})
439+
def test_related_object_entity_optimization_is(self):
438440
filters = [
439441
[
440442
"project",
@@ -457,7 +459,41 @@ def test_related_object_entity_optimization(self):
457459
}
458460
],
459461
}
460-
os.environ["SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION"] = "1"
462+
api.Shotgun("http://server_path", "script_name", "api_key", connect=False)
463+
result = api.shotgun._translate_filters(filters, "all")
464+
self.assertEqual(result, expected)
465+
466+
@mock.patch.dict(os.environ, {"SHOTGUN_API_ENABLE_ENTITY_OPTIMIZATION": "1"})
467+
def test_related_object_entity_optimization_in(self):
468+
filters = [
469+
[
470+
"project",
471+
"in",
472+
[
473+
{"foo1": "foo1", "bar1": "bar1", "id": 999, "baz1": "baz1", "type": "Anything"},
474+
{"foo2": "foo2", "bar2": "bar2", "id": 998, "baz2": "baz2", "type": "Anything"}
475+
],
476+
],
477+
]
478+
expected = {
479+
"logical_operator": "and",
480+
"conditions": [
481+
{
482+
"path": "project",
483+
"relation": "in",
484+
"values": [
485+
{
486+
"id": 999,
487+
"type": "Anything",
488+
},
489+
{
490+
"id": 998,
491+
"type": "Anything",
492+
}
493+
],
494+
}
495+
],
496+
}
461497
api.Shotgun("http://server_path", "script_name", "api_key", connect=False)
462498
result = api.shotgun._translate_filters(filters, "all")
463499
self.assertEqual(result, expected)

0 commit comments

Comments
 (0)