8000 846 fix filter in operator spaces bug (#1259) · cbini/server-client-python@5650adc · GitHub
[go: up one dir, main page]

Skip to content

Commit 5650adc

Browse files
846 fix filter 8000 in operator spaces bug (tableau#1259)
* encode spaces in filter conditions as %20 * corrected string replacement for filter condition * removed trailing space from comment * added tests for filter with IN condition and spaces in names
1 parent beda2d8 commit 5650adc

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

tableauserverclient/server/filter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ def __init__(self, field, operator, value):
1111
def __str__(self):
1212
value_string = str(self._value)
1313
if isinstance(self._value, list):
14-
value_string = value_string.replace(" ", "").replace("'", "")
14+
# this should turn the string representation of the list
15+
# from ['<string1>', '<string2>', ...]
16+
# to [<string1>,<string2>]
17+
# so effectively, remove any spaces between "," and "'" and then remove all "'"
18+
value_string = value_string.replace(", '", ",'").replace("'", "")
1519
return "{0}:{1}:{2}".format(self.field, self.operator, value_string)
1620

1721
@property
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api https://help.tableau.com/samples/en-us/rest_api/ts-api_3_18.xsd">
3+
<pagination pageNumber="1" pageSize="100" totalAvailable="2"/>
4+
<projects>
5+
<project id="26558843-133c-45fc-9f67-697f30d25e62" name="default" description="The default project that was automatically created by Tableau." createdAt="2022-06-21T07:52:28Z" updatedAt="2022-06-21T07:52:28Z" contentPermissions="ManagedByOwner">
6+
<owner id="19aadc25-d3d8-4467-96cc-09db69687b28"/>
7+
</project>
8+
<project id="7dce3c52-f058-4484-932c-3fc264638618" name="Salesforce Sales Projeśt" description="" createdAt="2023-02-21T02:53:34Z" updatedAt="2023-07-10T04:09:29Z" contentPermissions="ManagedByOwner">
9+
<owner id="bc38ea93-af2b-4004-9ef8-2144a57d3777"/>
10+
</project>
11+
</projects>
12+
</tsResponse>

test/test_filter.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
import unittest
3+
4+
import tableauserverclient as TSC
< 8000 /code>5+
6+
7+
class FilterTests(unittest.TestCase):
8+
def setUp(self):
9+
pass
10+
11+
def test_filter_equal(self):
12+
filter = TSC.Filter(TSC.RequestOptions.Field.Name, TSC.RequestOptions.Operator.Equals, "Superstore")
13+
14+
self.assertEqual(str(filter), "name:eq:Superstore")
15+
16+
def test_filter_in(self):
17+
# create a IN filter condition with project names that
18+
# contain spaces and "special" characters
19+
projects_to_find = ["default", "Salesforce Sales Projeśt"]
20+
filter = TSC.Filter(TSC.RequestOptions.Field.Name, TSC.RequestOptions.Operator.In, projects_to_find)
21+
22+
self.assertEqual(str(filter), "name:in:[default,Salesforce Sales Projeśt]")

test/test_request_option.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
PAGE_NUMBER_XML = os.path.join(TEST_ASSET_DIR, "request_option_page_number.xml")
1414
PAGE_SIZE_XML = os.path.join(TEST_ASSET_DIR, "request_option_page_size.xml")
1515
FILTER_EQUALS = os.path.join(TEST_ASSET_DIR, "request_option_filter_equals.xml")
16+
FILTER_NAME_IN = os.path.join(TEST_ASSET_DIR, "request_option_filter_name_in.xml")
1617
FILTER_TAGS_IN = os.path.join(TEST_ASSET_DIR, "request_option_filter_tags_in.xml")
1718
FILTER_MULTIPLE = os.path.join(TEST_ASSET_DIR, "request_option_filter_tags_in.xml")
1819
SLICING_QUERYSET = os.path.join(TEST_ASSET_DIR, "request_option_slicing_queryset.xml")
@@ -114,6 +115,30 @@ def test_filter_tags_in(self) -> None:
114115
self.assertEqual(set(["safari"]), matching_workbooks[1].tags)
115116
self.assertEqual(set(["sample"]), matching_workbooks[2].tags)
116117

118+
# check if filtered projects with spaces & special characters
119+
# get correctly returned
120+
def test_filter_name_in(self) -> None:
121+
with open(FILTER_NAME_IN, "rb") as f:
122+
response_xml = f.read().decode("utf-8")
123+
with requests_mock.mock() as m:
124+
m.get(
125+
self.baseurl + "/projects?filter=name%3Ain%3A%5Bdefault%2CSalesforce+Sales+Proje%C5%9Bt%5D",
126+
text=response_xml,
127+
)
128+
req_option = TSC.RequestOptions()
129+
req_option.filter.add(
130+
TSC.Filter(
131+
TSC.RequestOptions.Field.Name,
132+
TSC.RequestOptions.Operator.In,
133+
["default", "Salesforce Sales Projeśt"],
134+
)
135+
)
136+
matching_projects, pagination_item = self.server.projects.get(req_option)
137+
138+
self.assertEqual(2, pagination_item.total_available)
139+
self.assertEqual("default", matching_projects[0].name)
140+
self.assertEqual("Salesforce Sales Projeśt", matching_projects[1].name)
141+
117142
def test_filter_tags_in_shorthand(self) -> None:
118143
with open(FILTER_TAGS_IN, "rb") as f:
119144
response_xml = f.read().decode("utf-8")

0 commit comments

Comments
 (0)
0