Skip to content

Commit

Permalink
Fixed Resource.descriptor edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
roll committed Jul 21, 2023
1 parent f9c19b7 commit 18f42b8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion frictionless/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ def from_descriptor(
# TODO: remove in next version
# Transform with a base class in case the type is not available
cls.metadata_transform(descriptor)
Class = cls.metadata_select_class(descriptor.get("type"))
type = descriptor.get("type")
class_type = vars(cls).get("type")
if isinstance(class_type, str):
type = class_type
Class = cls.metadata_select_class(type)
Error = Class.metadata_Error or platform.frictionless_errors.MetadataError
Class.metadata_transform(descriptor)
errors = list(Class.metadata_validate(descriptor))
Expand Down
34 changes: 34 additions & 0 deletions tests/resource/test_datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,37 @@ def test_resource_datatype_package_with_packagify(source):
resource = Resource(source, packagify=True)
assert resource.datatype == "package"
assert isinstance(resource, resources.PackageResource)


# Bugs

DESCRIPTOR = {
"type": "json",
"name": "data",
"path": "data.json",
"scheme": "file",
"format": "json",
"mediatype": "text/json",
"encoding": "utf-8",
}


def test_resource_constructor_with_forced_datatype():
resource = Resource(path="data.json", datatype="table")
assert resource.type == "table"
assert resource.datatype == "table"
assert isinstance(resource, resources.TableResource)


def test_resource_from_descriptor_with_forced_datatype():
resource = Resource.from_descriptor(DESCRIPTOR, datatype="table")
assert resource.type == "table"
assert resource.datatype == "table"
assert isinstance(resource, resources.TableResource)


def test_resource_from_descriptor_with_class_datatype():
resource = resources.TableResource.from_descriptor(DESCRIPTOR)
assert resource.type == "table"
assert resource.datatype == "table"
assert isinstance(resource, resources.TableResource)

0 comments on commit 18f42b8

Please sign in to comment.