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
6 changes: 5 additions & 1 deletion tableauserverclient/server/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ def __init__(self, field, operator, value):
def __str__(self):
value_string = str(self._value)
if isinstance(self._value, list):
value_string = value_string.replace(" ", "").replace("'", "")
# this should turn the string representation of the list
# from ['<string1>', '<string2>', ...]
# to [<string1>,<string2>]
# so effectively, remove any spaces between "," and "'" and then remove all "'"
value_string = value_string.replace(", '", ",'").replace("'", "")
return "{0}:{1}:{2}".format(self.field, self.operator, value_string)

@property
Expand Down
12 changes: 12 additions & 0 deletions test/assets/request_option_filter_name_in.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version='1.0' encoding='UTF-8'?>
<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">
<pagination pageNumber="1" pageSize="100" totalAvailable="2"/>
<projects>
<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">
<owner id="19aadc25-d3d8-4467-96cc-09db69687b28"/>
</project>
<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">
<owner id="bc38ea93-af2b-4004-9ef8-2144a57d3777"/>
</project>
</projects>
</tsResponse>
22 changes: 22 additions & 0 deletions test/test_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
import unittest

import tableauserverclient as TSC


class FilterTests(unittest.TestCase):
def setUp(self):
pass

def test_filter_equal(self):
filter = TSC.Filter(TSC.RequestOptions.Field.Name, TSC.RequestOptions.Operator.Equals, "Superstore")

self.assertEqual(str(filter), "name:eq:Superstore")

def test_filter_in(self):
# create a IN filter condition with project names that
# contain spaces and "special" characters
projects_to_find = ["default", "Salesforce Sales Projeśt"]
filter = TSC.Filter(TSC.RequestOptions.Field.Name, TSC.RequestOptions.Operator.In, projects_to_find)

self.assertEqual(str(filter), "name:in:[default,Salesforce Sales Projeśt]")
25 changes: 25 additions & 0 deletions test/test_request_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
PAGE_NUMBER_XML = os.path.join(TEST_ASSET_DIR, "request_option_page_number.xml")
PAGE_SIZE_XML = os.path.join(TEST_ASSET_DIR, "request_option_page_size.xml")
FILTER_EQUALS = os.path.join(TEST_ASSET_DIR, "request_option_filter_equals.xml")
FILTER_NAME_IN = os.path.join(TEST_ASSET_DIR, "request_option_filter_name_in.xml")
FILTER_TAGS_IN = os.path.join(TEST_ASSET_DIR, "request_option_filter_tags_in.xml")
FILTER_MULTIPLE = os.path.join(TEST_ASSET_DIR, "request_option_filter_tags_in.xml")
SLICING_QUERYSET = os.path.join(TEST_ASSET_DIR, "request_option_slicing_queryset.xml")
Expand Down Expand Up @@ -114,6 +115,30 @@ def test_filter_tags_in(self) -> None:
self.assertEqual(set(["safari"]), matching_workbooks[1].tags)
self.assertEqual(set(["sample"]), matching_workbooks[2].tags)

# check if filtered projects with spaces & special characters
# get correctly returned
def test_filter_name_in(self) -> None:
with open(FILTER_NAME_IN, "rb") as f:
response_xml = f.read().decode("utf-8")
with requests_mock.mock() as m:
m.get(
self.baseurl + "/projects?filter=name%3Ain%3A%5Bdefault%2CSalesforce+Sales+Proje%C5%9Bt%5D",
text=response_xml,
)
req_option = TSC.RequestOptions()
req_option.filter.add(
TSC.Filter(
TSC.RequestOptions.Field.Name,
TSC.RequestOptions.Operator.In,
["default", "Salesforce Sales Projeśt"],
)
)
matching_projects, pagination_item = self.server.projects.get(req_option)

self.assertEqual(2, pagination_item.total_available)
self.assertEqual("default", matching_projects[0].name)
self.assertEqual("Salesforce Sales Projeśt", matching_projects[1].name)

def test_filter_tags_in_shorthand(self) -> None:
with open(FILTER_TAGS_IN, "rb") as f:
response_xml = f.read().decode("utf-8")
Expand Down