Skip to content
Open
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
5 changes: 5 additions & 0 deletions dagshub/data_engine/model/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ def all(self, load_documents=True, load_annotations=True) -> "QueryResult":
ds = self.limit(None)
return ds.fetch(load_documents=load_documents, load_annotations=load_annotations)

def get_datapoint(self, path: str) -> Datapoint:
"""Get a datapoint by its path."""
result = (self[self["path"] == path]).head(1)
return result[path]

def select(self, *selected: Union[str, Field]) -> "Datasource":
"""
Select which fields should appear in the query result.
Expand Down
46 changes: 46 additions & 0 deletions tests/data_engine/test_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,49 @@ def test_annotation_in_dataframe(ds):

def test_annotation_in_dataframe_to_new_datasource(ds, other_ds):
_test_dataframe_annotation_addition(ds, other_ds)


def test_get_datapoint_by_path(ds):
datapoint_path = "folder/file.jpg"
expected = Datapoint(
datasource=ds,
path=datapoint_path,
datapoint_id=123,
metadata={},
)
query_result = QueryResult(
_entries=[expected],
datasource=ds,
fields=[],
)
ds.source.client.head.return_value = query_result

actual = ds.get_datapoint(datapoint_path)

assert actual is expected

queried_ds, size = ds.source.client.head.call_args.args
assert size == 1
assert queried_ds.get_query().filter.tree_to_dict() == {
"eq": {
"data": {
"field": "path",
"value": datapoint_path,
}
}
}


def test_get_datapoint_by_path_not_found(ds):
datapoint_path = "missing/file.jpg"
query_result = QueryResult(
_entries=[],
datasource=ds,
fields=[],
)
ds.source.client.head.return_value = query_result

with pytest.raises(KeyError) as exc_info:
ds.get_datapoint(datapoint_path)

assert exc_info.value.args == (datapoint_path,)