Skip to content

Commit 376c05c

Browse files
committed
add string-in option
1 parent 94255d2 commit 376c05c

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

planet/cli/data.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,16 @@ def _func(obj):
221221
return [_func(v) for v in values]
222222

223223

224+
def string_in_to_filter(ctx, param, values) -> Union[dict, None]:
225+
226+
def _func(obj):
227+
field, values = obj
228+
return data_filter.string_in_filter(field_name=field, values=values)
229+
230+
if values:
231+
return [_func(v) for v in values]
232+
233+
224234
@data.command()
225235
@click.pass_context
226236
@translate_exceptions
@@ -231,7 +241,7 @@ def _func(obj):
231241
callback=assets_to_filter,
232242
help="""Filter to items with one or more of specified assets.
233243
VALUE is a comma-separated list of entries.
234-
When multiple entries are specified an implicit 'or' logic is applied.""")
244+
When multiple entries are specified, an implicit 'or' logic is applied.""")
235245
@click.option('--date-range',
236246
type=click.Tuple([FieldType(), ComparisonType(),
237247
DateTimeType()]),
@@ -253,7 +263,7 @@ def _func(obj):
253263
help="""Filter field by numeric in.
254264
FIELD is the name of the field to filter on.
255265
VALUE is a comma-separated list of entries.
256-
When multiple entries are specified an implicit 'or' logic is applied.""")
266+
When multiple entries are specified, an implicit 'or' logic is applied.""")
257267
@click.option('--range',
258268
'nrange',
259269
type=click.Tuple([FieldType(), ComparisonType(), float]),
@@ -263,10 +273,14 @@ def _func(obj):
263273
FIELD is the name of the field to filter on.
264274
COMP can be lt, lte, gt, or gte.
265275
DATETIME can be an RFC3339 or ISO 8601 string.""")
266-
# @click.option('--string-in',
267-
# type=RangeFilter(),
268-
# multiple=True,
269-
# help=RangeFilter.help)
276+
@click.option('--string-in',
277+
type=click.Tuple([FieldType(), CommaSeparatedString()]),
278+
multiple=True,
279+
callback=string_in_to_filter,
280+
help="""Filter field by numeric in.
281+
FIELD is the name of the field to filter on.
282+
VALUE is a comma-separated list of entries.
283+
When multiple entries are specified, an implicit 'or' logic is applied.""")
270284
@click.option(
271285
'--update',
272286
type=click.Tuple([FieldType(), GTComparisonType(), DateTimeType()]),
@@ -287,6 +301,7 @@ def filter(ctx,
287301
geom,
288302
number_in,
289303
nrange,
304+
string_in,
290305
update,
291306
permission,
292307
pretty):
@@ -305,6 +320,7 @@ def filter(ctx,
305320
geom,
306321
number_in,
307322
nrange,
323+
string_in,
308324
update,
309325
permission)
310326

tests/integration/test_data_cli.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,9 @@ def test_data_filter_geom(geom_fixture,
172172
assert_and_filters_equal(json.loads(result.output), expected_filt)
173173

174174

175-
# @pytest.mark.skip
176175
@respx.mock
177176
@pytest.mark.asyncio
178-
def test_data_filter_number_in(
179-
invoke,
180-
assert_and_filters_equal):
177+
def test_data_filter_number_in(invoke, assert_and_filters_equal):
181178
runner = CliRunner()
182179

183180
result = invoke(["filter"] + '--number-in field 1'.split() +
@@ -189,10 +186,8 @@ def test_data_filter_number_in(
189186
"type": "NumberInFilter", "field_name": "field", "config": [1.0]
190187
}
191188
number_in_filter2 = {
192-
"type": "NumberInFilter",
193-
"field_name": "field2",
194-
"config": [2.0, 3.5]
195-
}
189+
"type": "NumberInFilter", "field_name": "field2", "config": [2.0, 3.5]
190+
}
196191

197192
expected_filt = {
198193
"type": "AndFilter",
@@ -235,6 +230,33 @@ def test_data_filter_range(invoke, assert_and_filters_equal):
235230
assert_and_filters_equal(json.loads(result.output), expected_filt)
236231

237232

233+
@respx.mock
234+
@pytest.mark.asyncio
235+
def test_data_filter_string_in(invoke, assert_and_filters_equal):
236+
runner = CliRunner()
237+
238+
result = invoke(["filter"] + '--string-in field foo'.split() +
239+
'--string-in field2 foo,bar'.split(),
240+
runner=runner)
241+
assert result.exit_code == 0
242+
243+
string_in_filter1 = {
244+
"type": "StringInFilter", "field_name": "field", "config": ["foo"]
245+
}
246+
string_in_filter2 = {
247+
"type": "StringInFilter",
248+
"field_name": "field2",
249+
"config": ["foo", "bar"]
250+
}
251+
252+
expected_filt = {
253+
"type": "AndFilter",
254+
"config": [permission_filter, string_in_filter1, string_in_filter2]
255+
}
256+
257+
assert_and_filters_equal(json.loads(result.output), expected_filt)
258+
259+
238260
@respx.mock
239261
@pytest.mark.asyncio
240262
def test_data_filter_update(invoke, assert_and_filters_equal):

0 commit comments

Comments
 (0)