Skip to content

Commit

Permalink
Add manifest tests (#80)
Browse files Browse the repository at this point in the history
Expanded the tests to reach 100% coverage for all `manifest.py` code
  • Loading branch information
GeorgesLorre authored May 8, 2023
1 parent 156279c commit 90db5dd
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 6 deletions.
10 changes: 5 additions & 5 deletions fondant/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def remove_field(self, name: str) -> None:
del self._specification["fields"][name]

def __repr__(self) -> str:
return f"{self.__class__.__name__}({self._specification!r}"
return f"{self.__class__.__name__}({self._specification!r})"


class Index(Subset):
Expand Down Expand Up @@ -88,7 +88,7 @@ def _validate_spec(self) -> None:
spec_data = pkgutil.get_data("fondant", "schemas/manifest.json")

if spec_data is None:
raise FileNotFoundError("component_spec.json not found in fondant schema")
raise FileNotFoundError("schemas/manifest.json not found")
else:
spec_str = spec_data.decode("utf-8")
spec_schema = json.loads(spec_str)
Expand Down Expand Up @@ -142,7 +142,7 @@ def copy(self) -> "Manifest":
def metadata(self) -> t.Dict[str, t.Any]:
return self._specification["metadata"]

def add_metadata(self, key: str, value: t.Any) -> None:
def update_metadata(self, key: str, value: t.Any) -> None:
self.metadata[key] = value

@property
Expand Down Expand Up @@ -197,7 +197,7 @@ def evolve(self, component_spec: FondantComponentSpec) -> "Manifest":

# Update `component_id` of the metadata
component_id = component_spec.name.lower().replace(" ", "_")
evolved_manifest.add_metadata(key="component_id", value=component_id)
evolved_manifest.update_metadata(key="component_id", value=component_id)

# Update index location as this is currently always rewritten
evolved_manifest.index._specification[
Expand Down Expand Up @@ -255,4 +255,4 @@ def evolve(self, component_spec: FondantComponentSpec) -> "Manifest":
return evolved_manifest

def __repr__(self) -> str:
return f"{self.__class__.__name__}({self._specification!r}"
return f"{self.__class__.__name__}({self._specification!r})"
95 changes: 94 additions & 1 deletion tests/test_manifest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import pkgutil
from pathlib import Path

import pytest
from fondant.exceptions import InvalidManifest
from fondant.manifest import Manifest, Type
from fondant.manifest import Subset, Manifest, Type


manifest_path = Path(__file__).parent / "example_specs/manifests"
Expand All @@ -28,6 +29,63 @@ def test_manifest_validation(valid_manifest, invalid_manifest):
Manifest(invalid_manifest)


def test_subset_init():
"""Test initializing a subset"""
subset_spec = {
"location": "/ABC/123/images",
"fields": {
"data": {
"type": "binary",
},
},
}
subset = Subset(specification=subset_spec, base_path="/tmp")
assert subset.location == "/tmp/ABC/123/images"
assert (
subset.__repr__()
== "Subset({'location': '/ABC/123/images', 'fields': {'data': {'type': 'binary'}}})"
)


def test_subset_fields():
"""Test manipulating subset fields"""
subset_spec = {
"location": "/ABC/123/images",
"fields": {
"data": {
"type": "binary",
},
},
}
subset = Subset(specification=subset_spec, base_path="/tmp")

# add a field
subset.add_field(name="data2", type_=Type.binary)
assert "data2" in subset.fields

# add a duplicate field
with pytest.raises(ValueError):
subset.add_field(name="data2", type_=Type.binary)

# add a duplicate field but overwrite
subset.add_field(name="data2", type_=Type.utf8, overwrite=True)
assert subset.fields["data2"].type == "utf8"

# remove a field
subset.remove_field(name="data2")
assert "data2" not in subset.fields


def test_set_base_path(valid_manifest):
"""Test altering the base path in the manifest"""
manifest = Manifest(valid_manifest)
tmp_path = "/tmp/base_path"
manifest.update_metadata(key="base_path", value=tmp_path)

assert manifest.base_path == tmp_path
assert manifest._specification["metadata"]["base_path"] == tmp_path


def test_from_to_file(valid_manifest):
"""Test reading from and writing to file"""
tmp_path = "/tmp/manifest.json"
Expand Down Expand Up @@ -94,10 +152,45 @@ def test_manifest_creation():
}


def test_manifest_repr():
manifest = Manifest.create(base_path="/", run_id="A", component_id="1")
assert (
manifest.__repr__()
== "Manifest({'metadata': {'base_path': '/', 'run_id': 'A', 'component_id': '1'}, 'index': {'location': '/A/1/index'}, 'subsets': {}})"
)


def test_manifest_alteration(valid_manifest):
"""Test alteration functionalities of a manifest via the Manifest class"""
manifest = Manifest(valid_manifest)

# test adding a subset
manifest.add_subset("images2", [("width", Type.int32), ("height", Type.int32)])
assert "images2" in manifest.subsets

# test adding a duplicate subset
with pytest.raises(ValueError):
manifest.add_subset("images2", [("width", Type.int32), ("height", Type.int32)])

# test removing a subset
manifest.remove_subset("images2")
assert "images2" not in manifest.subsets

# test removing a nonexistant subset
with pytest.raises(ValueError):
manifest.remove_subset("pictures")


def test_manifest_copy_and_adapt(valid_manifest):
"""Test that a manifest can be copied and adapted without changing the original."""
manifest = Manifest(valid_manifest)
new_manifest = manifest.copy()
new_manifest.remove_subset("images")
assert manifest._specification == valid_manifest
assert new_manifest._specification != valid_manifest


def test_no_validate_schema(monkeypatch, valid_manifest):
monkeypatch.setattr(pkgutil, "get_data", lambda package, resource: None)
with pytest.raises(FileNotFoundError):
manifest = Manifest(valid_manifest)

0 comments on commit 90db5dd

Please sign in to comment.