def test_filter_metadata(self):
        class Person(HasTraits):
            n_jobs = Int(status="public")
            n_children = Int()  # no metadata

        observer = FilteredTraitObserver(
            filter=MetadataFilter(metadata_name="status", ),
            notify=True,
        )
        person = Person()
        handler = mock.Mock()
        call_add_or_remove_notifiers(
            object=person,
            graph=create_graph(observer),
            handler=handler,
        )

        # when
        person.n_jobs += 1

        # then
        self.assertEqual(handler.call_count, 1)
        handler.reset_mock()

        # when
        person.n_children += 1

        # then
        self.assertEqual(handler.call_count, 0)
 def test_metadata_defined_as_none_is_same_as_undefined(self):
     # If a metadata has a None value, it is equivalent to the metadata
     # not being defined.
     metadata_filter = MetadataFilter(metadata_name="name", )
     self.assertFalse(metadata_filter("name",
                                      Int(name=None).as_ctrait()),
                      "Expected the filter to return false")
Esempio n. 3
0
def metadata(metadata_name, notify=True):
    """ Return a new expression for observing traits where the given metadata
    is not None.

    Events emitted (if any) will be instances of
    :class:`~traits.observation.events.TraitChangeEvent`.

    e.g. ``metadata("age")`` matches traits whose 'age' attribute has a
    non-None value.

    Parameters
    ----------
    metadata_name : str
        Name of the metadata to filter traits with.
    notify : bool, optional
        Whether to notify for changes. Default is to notify.

    Returns
    -------
    new_expression : ObserverExpression
    """
    return match(
        filter=MetadataFilter(metadata_name=metadata_name),
        notify=notify,
    )
Esempio n. 4
0
 def test_metadata_method_notify_false(self):
     # Test the instance method calls the top-level function correctly.
     expr = expression.metadata("bee").metadata("ant", notify=False)
     expected = [
         create_graph(
             FilteredTraitObserver(
                 filter=MetadataFilter(metadata_name="bee"),
                 notify=True,
             ),
             FilteredTraitObserver(
                 filter=MetadataFilter(metadata_name="ant"),
                 notify=False,
             ),
         ),
     ]
     actual = expr._as_graphs()
     self.assertEqual(actual, expected)
 def test_metadata_defined_vs_undefined(self):
     metadata_filter = MetadataFilter(metadata_name="name", )
     self.assertTrue(metadata_filter("name",
                                     Int(name=True).as_ctrait()),
                     "Expected the filter to return true")
     self.assertFalse(metadata_filter("name",
                                      Int().as_ctrait()),
                      "Expected the filter to return false")
Esempio n. 6
0
 def test_metadata_notify_false(self):
     # Test the top-level function
     expr = expression.metadata("butterfly", notify=False)
     expected = [
         create_graph(
             FilteredTraitObserver(
                 filter=MetadataFilter(metadata_name="butterfly"),
                 notify=False,
             ), ),
     ]
     actual = expr._as_graphs()
     self.assertEqual(actual, expected)
 def test_repr_value(self):
     metadata_filter = MetadataFilter(metadata_name="name", )
     actual = repr(metadata_filter)
     self.assertEqual(actual, "MetadataFilter(metadata_name='name')")
 def test_filter_not_equal_different_type(self):
     filter1 = MetadataFilter(metadata_name="name", )
     imposter = mock.Mock()
     imposter.metadata_name = "name"
     self.assertNotEqual(imposter, filter1)
 def test_filter_not_equal_name_different(self):
     filter1 = MetadataFilter(metadata_name="number", )
     filter2 = MetadataFilter(metadata_name="name", )
     self.assertNotEqual(filter1, filter2)
Esempio n. 10
0
 def test_filter_equality(self):
     filter1 = MetadataFilter(metadata_name="name", )
     filter2 = MetadataFilter(metadata_name="name", )
     self.assertEqual(filter1, filter2)
     self.assertEqual(hash(filter1), hash(filter2))