def test_label_set_filters(self): """Test the filters for Labelset.""" filterable_thing = FilterableSubclass( iterable=LabelSet({"fred", "charlie"})) self.assertTrue( filterable_thing.satisfies(iterable__any_label_contains="a")) self.assertFalse( filterable_thing.satisfies(iterable__any_label_contains="z")) self.assertTrue( filterable_thing.satisfies(iterable__not_any_label_contains="z")) self.assertFalse( filterable_thing.satisfies(iterable__not_any_label_contains="a")) self.assertTrue( filterable_thing.satisfies(iterable__any_label_starts_with="f")) self.assertFalse( filterable_thing.satisfies(iterable__any_label_starts_with="e")) self.assertTrue( filterable_thing.satisfies(iterable__any_label_ends_with="e")) self.assertFalse( filterable_thing.satisfies(iterable__any_label_ends_with="i")) self.assertTrue( filterable_thing.satisfies( iterable__not_any_label_starts_with="e")) self.assertFalse( filterable_thing.satisfies( iterable__not_any_label_starts_with="f")) self.assertTrue( filterable_thing.satisfies(iterable__not_any_label_ends_with="i")) self.assertFalse( filterable_thing.satisfies(iterable__not_any_label_ends_with="e"))
def test_serialise_orders_labels(self): """Ensure that serialising a LabelSet results in a sorted list.""" label_set = LabelSet("z hello a c-no") self.assertEqual( json.dumps(label_set, cls=OctueJSONEncoder), json.dumps({ "_type": "set", "items": ["a", "c-no", "hello", "z"] }), )
def test_update(self): """Test that the update method adds valid labels but raises an error for invalid labels.""" label_set = LabelSet({"a", "b"}) label_set.update("c", "d") self.assertEqual(label_set, {"a", "b", "c", "d"}) with self.assertRaises(exceptions.InvalidLabelException): label_set.update("e", "f_")
def test_add(self): """Test that the add method adds a valid label but raises an error for an invalid label.""" label_set = LabelSet({"a", "b"}) label_set.add("c") self.assertEqual(label_set, {"a", "b", "c"}) with self.assertRaises(exceptions.InvalidLabelException): label_set.add("d_")
def __init__( self, path=None, files=None, recursive=False, hypothetical=False, id=None, name=None, tags=None, labels=None, ): super().__init__(name=name, id=id, tags=tags, labels=labels) self.path = path or os.getcwd() self.files = FilterSet() self._recursive = recursive self._hypothetical = hypothetical self._cloud_metadata = {} if files: if not any((isinstance(files, list), isinstance( files, set), isinstance(files, tuple))): raise InvalidInputException( "The `files` parameter of a `Dataset` must be an iterable of `Datafile` instances, strings, or " f"dictionaries. Received {files!r} instead.") self.files = self._instantiate_datafiles(files) return if storage.path.is_cloud_path(self.path): self._instantiate_from_cloud(path=self.path) else: self._instantiate_from_local_directory(path=self.path) if self._hypothetical: logger.debug("Ignored stored metadata for %r.", self) return if self.metadata(include_sdk_version=False) != { "name": name or self.name, "id": id or self.id, "tags": TagDict(tags), "labels": LabelSet(labels), }: logger.warning( "Overriding metadata given at instantiation with stored metadata for %r - set `hypothetical` to `True` " "at instantiation to avoid this.", self, )
def labels(self, labels): """Overwrite any existing label set and assign new labels.""" self._labels = LabelSet(labels)
def test_instantiation_from_filter_set_of_labels(self): """Test that a LabelSet can be instantiated from a FilterSet of labels.""" label_set = LabelSet( labels=FilterSet({Label( "a"), Label("b-c"), Label("d-e-f")})) self.assertEqual(label_set, self.LABEL_SET)
def test_instantiation_from_filter_set_of_strings(self): """Test that a LabelSet can be instantiated from a FilterSet of strings.""" label_set = LabelSet(labels=FilterSet({"a", "b-c", "d-e-f"})) self.assertEqual(label_set, self.LABEL_SET)
def test_instantiation_from_iterable_of_labels(self): """Test that a LabelSet can be instantiated from an iterable of labels.""" label_set = LabelSet(labels=[Label("a"), Label("b-c"), Label("d-e-f")]) self.assertEqual(label_set, self.LABEL_SET)
def test_instantiation_from_iterable_of_strings(self): """Test that a LabelSet can be instantiated from an iterable of strings.""" label_set = LabelSet(labels=["a", "b-c", "d-e-f"]) self.assertEqual(label_set, self.LABEL_SET)
def test_instantiation_from_space_delimited_string(self): """Test that a LabelSet can be instantiated from a space-delimited string of label names.""" label_set = LabelSet(labels="a b-c d-e-f") self.assertEqual(label_set, self.LABEL_SET)
class TestLabelSet(BaseTestCase): LABEL_SET = LabelSet(labels="a b-c d-e-f") def test_instantiation_from_space_delimited_string(self): """Test that a LabelSet can be instantiated from a space-delimited string of label names.""" label_set = LabelSet(labels="a b-c d-e-f") self.assertEqual(label_set, self.LABEL_SET) def test_instantiation_from_iterable_of_strings(self): """Test that a LabelSet can be instantiated from an iterable of strings.""" label_set = LabelSet(labels=["a", "b-c", "d-e-f"]) self.assertEqual(label_set, self.LABEL_SET) def test_instantiation_from_iterable_of_labels(self): """Test that a LabelSet can be instantiated from an iterable of labels.""" label_set = LabelSet(labels=[Label("a"), Label("b-c"), Label("d-e-f")]) self.assertEqual(label_set, self.LABEL_SET) def test_instantiation_from_filter_set_of_strings(self): """Test that a LabelSet can be instantiated from a FilterSet of strings.""" label_set = LabelSet(labels=FilterSet({"a", "b-c", "d-e-f"})) self.assertEqual(label_set, self.LABEL_SET) def test_instantiation_from_filter_set_of_labels(self): """Test that a LabelSet can be instantiated from a FilterSet of labels.""" label_set = LabelSet( labels=FilterSet({Label( "a"), Label("b-c"), Label("d-e-f")})) self.assertEqual(label_set, self.LABEL_SET) def test_instantiation_from_label_set(self): """Test that a LabelSet can be instantiated from another LabelSet.""" self.assertEqual(self.LABEL_SET, LabelSet(self.LABEL_SET)) def test_equality(self): """Ensure two LabelSets with the same labels compare equal.""" self.assertTrue(self.LABEL_SET == LabelSet(labels="a b-c d-e-f")) self.assertTrue(self.LABEL_SET == {"a", "b-c", "d-e-f"}) def test_inequality(self): """Ensure two LabelSets with different labels compare unequal.""" self.assertTrue(self.LABEL_SET != LabelSet(labels="a")) def test_non_label_sets_compare_unequal_to_label_sets(self): """Ensure a LabelSet and a non-LabelSet compare unequal.""" self.assertFalse(self.LABEL_SET == "a") self.assertTrue(self.LABEL_SET != "a") def test_iterating_over(self): """Ensure a LabelSet can be iterated over.""" self.assertEqual(set(self.LABEL_SET), {Label("a"), Label("b-c"), Label("d-e-f")}) def test_contains_with_string(self): """Ensure we can check that a LabelSet has a certain label using a string form.""" self.assertTrue("d-e-f" in self.LABEL_SET) self.assertFalse("hello" in self.LABEL_SET) def test_contains_with_label(self): """Ensure we can check that a LabelSet has a certain label.""" self.assertTrue(Label("d-e-f") in self.LABEL_SET) self.assertFalse(Label("hello") in self.LABEL_SET) def test_contains_only_matches_full_labels(self): """Test that the has_label method only matches full labels (i.e. that it doesn't match sublabels or parts of labels.""" for label in "a", "b-c", "d-e-f": self.assertTrue(label in self.LABEL_SET) for label in "b", "c", "d", "e", "f": self.assertFalse(label in self.LABEL_SET) def test_add(self): """Test that the add method adds a valid label but raises an error for an invalid label.""" label_set = LabelSet({"a", "b"}) label_set.add("c") self.assertEqual(label_set, {"a", "b", "c"}) with self.assertRaises(exceptions.InvalidLabelException): label_set.add("d_") def test_update(self): """Test that the update method adds valid labels but raises an error for invalid labels.""" label_set = LabelSet({"a", "b"}) label_set.update("c", "d") self.assertEqual(label_set, {"a", "b", "c", "d"}) with self.assertRaises(exceptions.InvalidLabelException): label_set.update("e", "f_") def test_any_label_starts_with(self): """Ensure starts_with only checks the starts of labels, and doesn't check the starts of sublabels.""" for label in "a", "b", "d": self.assertTrue(self.LABEL_SET.any_label_starts_with(label)) for label in "c", "e", "f": self.assertFalse(self.LABEL_SET.any_label_starts_with(label)) def test_any_label_ends_swith(self): """Ensure ends_with doesn't check ends of sublabels.""" for label in "a", "c", "f": self.assertTrue(self.LABEL_SET.any_label_ends_with(label)) for label in "b", "d", "e": self.assertFalse(self.LABEL_SET.any_label_ends_with(label)) def test_serialise(self): """Ensure that LabelSets serialise to a list.""" self.assertEqual( json.dumps(self.LABEL_SET, cls=OctueJSONEncoder), json.dumps({ "_type": "set", "items": ["a", "b-c", "d-e-f"] }), ) def test_serialise_orders_labels(self): """Ensure that serialising a LabelSet results in a sorted list.""" label_set = LabelSet("z hello a c-no") self.assertEqual( json.dumps(label_set, cls=OctueJSONEncoder), json.dumps({ "_type": "set", "items": ["a", "c-no", "hello", "z"] }), ) def test_deserialise(self): """Test that serialisation is reversible.""" serialised_label_set = json.dumps(self.LABEL_SET, cls=OctueJSONEncoder) deserialised_label_set = LabelSet( json.loads(serialised_label_set, cls=OctueJSONDecoder)) self.assertEqual(deserialised_label_set, self.LABEL_SET) def test_repr(self): """Test the representation of a LabelSet appears as expected.""" self.assertEqual(repr(self.LABEL_SET), f"LabelSet({set(self.LABEL_SET)})")
def test_deserialise(self): """Test that serialisation is reversible.""" serialised_label_set = json.dumps(self.LABEL_SET, cls=OctueJSONEncoder) deserialised_label_set = LabelSet( json.loads(serialised_label_set, cls=OctueJSONDecoder)) self.assertEqual(deserialised_label_set, self.LABEL_SET)
def test_inequality(self): """Ensure two LabelSets with different labels compare unequal.""" self.assertTrue(self.LABEL_SET != LabelSet(labels="a"))
def test_equality(self): """Ensure two LabelSets with the same labels compare equal.""" self.assertTrue(self.LABEL_SET == LabelSet(labels="a b-c d-e-f")) self.assertTrue(self.LABEL_SET == {"a", "b-c", "d-e-f"})
def test_instantiation_from_label_set(self): """Test that a LabelSet can be instantiated from another LabelSet.""" self.assertEqual(self.LABEL_SET, LabelSet(self.LABEL_SET))