Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ def search(self, context: ProviderContext):

def single_value(self, context: ProviderContext) -> Any:
try:
return next(self.search(context), None)
result = list(self.search(context))
if not result:
return None
if len(result) == 1:
return result[0]
return list(result)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conversation with Jon, the code for single value and search should be merged to have direct identification of whether the data is coming in as a list and using that knowledge to determine how to send the data. (directly return the raw_search). Develop something akin of a !pluck provider that takes the first entry when encountering a list as a single value to replace the prior functionality.

except Exception as e:
raise ValueProviderException(str(context.document), self) from e

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
{"first_name": "Zach", "last_name": "Probst"},
{"first_name": "Chad", "last_name": "Cloes"},
],
"project": {"tags": ["graphdb", "python"]},
"project": {"tags": ["graphdb", "python"], "name": "project_name"},
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
import pytest
from hamcrest import assert_that, equal_to, has_length, none

from nodestream.pipeline.value_providers import JmespathValueProvider
from nodestream.pipeline.value_providers.value_provider import ValueProviderException


def test_single_value_present(blank_context_with_document):
subject = JmespathValueProvider.from_string_expression("team.name")
assert_that(
subject.single_value(blank_context_with_document), equal_to("nodestream")
)
assert subject.single_value(blank_context_with_document) == "nodestream"


def test_single_value_present_complicated(blank_context_with_document):
subject = JmespathValueProvider.from_string_expression("*.name")
assert subject.single_value(blank_context_with_document) == [
"nodestream",
"project_name",
]


def test_single_value_missing(blank_context_with_document):
subject = JmespathValueProvider.from_string_expression("team.description")
assert_that(subject.single_value(blank_context_with_document), none())
assert subject.single_value(blank_context_with_document) is None


def test_single_value_is_list(blank_context_with_document):
subject = JmespathValueProvider.from_string_expression("project.tags")
result = subject.single_value(blank_context_with_document)
assert_that(result, equal_to("graphdb"))
assert result == ["graphdb", "python"]


def test_multiple_values_missing(blank_context_with_document):
subject = JmespathValueProvider.from_string_expression("team.description")
assert_that(list(subject.many_values(blank_context_with_document)), has_length(0))
result = list(subject.many_values(blank_context_with_document))
assert result == []


def test_multiple_values_returns_one_value(blank_context_with_document):
subject = JmespathValueProvider.from_string_expression("team.name")
result = list(subject.many_values(blank_context_with_document))
assert_that(result, has_length(1))
assert_that(result[0], equal_to("nodestream"))
assert result == ["nodestream"]


def test_multiple_values_hit(blank_context_with_document):
subject = JmespathValueProvider.from_string_expression("project.tags")
result = subject.many_values(blank_context_with_document)
assert_that(list(result), equal_to(["graphdb", "python"]))
result = list(subject.many_values(blank_context_with_document))
assert result == ["graphdb", "python"]


def test_single_value_error(blank_context_with_document):
Expand All @@ -60,9 +65,9 @@ def test_multiple_values_error(blank_context_with_document):
expression_with_error = "join('/', [team.name || '', team2.name])"
subject = JmespathValueProvider.from_string_expression(expression_with_error)

with pytest.raises(Exception) as e_info:
generator = subject.many_values(blank_context_with_document)
list(generator)
with pytest.raises(ValueProviderException) as e_info:
list(subject.many_values(blank_context_with_document))

error_message = str(e_info.value)

assert expression_with_error in error_message