예제 #1
0
    def test_register_non_decorator(self):
        ayy_lmao_set = set()

        def ayy_lmao():
            return ayy_lmao_set

        self.register('ayy-lmao', ayy_lmao)

        expected_metrics_sets = mappingproxy({'ayy-lmao': ayy_lmao})
        assert_equal(self.metrics_sets, expected_metrics_sets)
        assert_is(self.load('ayy-lmao'), ayy_lmao_set)

        def other():  # pragma: no cover
            raise AssertionError('dead')

        msg = "metrics set 'ayy-lmao' is already registered"
        with assert_raises_str(ValueError, msg):
            self.register('ayy-lmao', other)

        # ensure that the failed registration didn't break the previously
        # registered set
        assert_equal(self.metrics_sets, expected_metrics_sets)
        assert_is(self.load('ayy-lmao'), ayy_lmao_set)

        self.unregister('ayy-lmao')
        assert_equal(self.metrics_sets, mappingproxy({}))

        msg = "no metrics set registered as 'ayy-lmao', options are: []"
        with assert_raises_str(ValueError, msg):
            self.load('ayy-lmao')

        msg = "metrics set 'ayy-lmao' was not already registered"
        with assert_raises_str(ValueError, msg):
            self.unregister('ayy-lmao')
    def test_empty_extra_dims(self):
        msg = (
            "DataSetFamily must be defined with non-empty extra_dims,"
            " or with `_abstract = True`"
        )
        with assert_raises_str(ValueError, msg):
            class NoExtraDims(DataSetFamily):
                pass

        with assert_raises_str(ValueError, msg):
            class EmptyExtraDims(DataSetFamily):
                extra_dims = []

        class AbstractParent(DataSetFamily):
            _abstract = True

        with assert_raises_str(ValueError, msg):
            class NoExtraDimsChild(AbstractParent):
                pass

        with assert_raises_str(ValueError, msg):
            class EmptyExtraDimsChild(AbstractParent):
                extra_dims = []

        class AbstractChild(AbstractParent):
            _abstract = True

        class Child(AbstractParent):
            extra_dims = [
                ('dim_0', {'a', 'b', 'c'}),
                ('dim_1', {'d', 'e', 'f'}),
            ]
예제 #3
0
    def test_load_not_registered(self):
        msg = "no metrics set registered as 'ayy-lmao', options are: []"
        with assert_raises_str(ValueError, msg):
            self.load('ayy-lmao')

        # register in reverse order to test the sorting of the options
        self.register('c', set)
        self.register('b', set)
        self.register('a', set)

        msg = (
            "no metrics set registered as 'ayy-lmao', options are: "
            "['a', 'b', 'c']"
        )
        with assert_raises_str(ValueError, msg):
            self.load('ayy-lmao')
예제 #4
0
    def test_parse_namespaces(self):
        n = Namespace()

        create_args(
            [
                "first.second.a=blah1",
                "first.second.b=blah2",
                "first.third=blah3",
                "second.a=blah4",
                "second.b=blah5",
            ],
            n
        )

        assert_equal(n.first.second.a, 'blah1')
        assert_equal(n.first.second.b, 'blah2')
        assert_equal(n.first.third, 'blah3')
        assert_equal(n.second.a, 'blah4')
        assert_equal(n.second.b, 'blah5')

        n = Namespace()

        msg = "Conflicting assignments at namespace level 'second'"
        with assert_raises_str(ValueError, msg):
            create_args(
                [
                    "first.second.a=blah1",
                    "first.second.b=blah2",
                    "first.second=blah3",
                ],
                n
            )
    def test_register_non_decorator(self):
        rm = Registry(FakeInterface)

        class ProperDummyInterface(FakeInterface):
            pass

        rm.register('ayy-lmao', ProperDummyInterface)

        def check_registered():
            assert_true(
                rm.is_registered('ayy-lmao'),
                "Class ProperDummyInterface wasn't properly registered under"
                "name 'ayy-lmao'"
            )
            self.assertIsInstance(rm.load('ayy-lmao'), ProperDummyInterface)

        # Check that we successfully registered.
        check_registered()

        class Fake(object):
            pass

        # Try and fail to register with the same key again.
        m = "FakeInterface factory with name 'ayy-lmao' is already registered"
        with assert_raises_str(ValueError, m):
            rm.register('ayy-lmao', Fake)

        # check that the failed registration didn't break the previous
        # registration
        check_registered()

        rm.unregister('ayy-lmao')

        msg = (
            "no FakeInterface factory registered under name 'ayy-lmao', "
            "options are: []"
        )
        with assert_raises_str(ValueError, msg):
            rm.load('ayy-lmao')

        msg = "FakeInterface factory 'ayy-lmao' was not already registered"
        with assert_raises_str(ValueError, msg):
            rm.unregister('ayy-lmao')
    def test_load_not_registered(self):
        rm = Registry(FakeInterface)

        msg = (
            "no FakeInterface factory registered under name 'ayy-lmao', "
            "options are: []"
        )
        with assert_raises_str(ValueError, msg):
            rm.load('ayy-lmao')

        # register in reverse order to test the sorting of the options
        rm.register('c', FakeInterface)
        rm.register('b', FakeInterface)
        rm.register('a', FakeInterface)

        msg = (
            "no FakeInterface factory registered under name 'ayy-lmao', "
            "options are: ['a', 'b', 'c']"
        )
        with assert_raises_str(ValueError, msg):
            rm.load('ayy-lmao')
    def test_column_access_without_slice(self):
        class Parent(DataSetFamily):
            extra_dims = [
                ('dim_0', {'a', 'b', 'c'}),
                ('dim_1', {'d', 'e', 'f'}),
            ]

            column_0 = Column('f8')
            column_1 = Column('?')

        class Child(Parent):
            column_2 = Column('O')
            column_3 = Column('i8', -1)

        def make_expected_msg(ds, attr):
            return dedent(
                """\
                Attempted to access column {c} from DataSetFamily {d}:

                To work with dataset families, you must first select a
                slice using the ``slice`` method:

                    {d}.slice(...).{c}
                """
                .format(c=attr, d=ds),  # noqa
            )

        expected_msg = make_expected_msg('Parent', 'column_0')
        with assert_raises_str(AttributeError, expected_msg):
            Parent.column_0

        expected_msg = make_expected_msg('Parent', 'column_1')
        with assert_raises_str(AttributeError, expected_msg):
            Parent.column_1

        expected_msg = make_expected_msg('Child', 'column_0')
        with assert_raises_str(AttributeError, expected_msg):
            Child.column_0

        expected_msg = make_expected_msg('Child', 'column_1')
        with assert_raises_str(AttributeError, expected_msg):
            Child.column_1

        expected_msg = make_expected_msg('Child', 'column_2')
        with assert_raises_str(AttributeError, expected_msg):
            Child.column_2

        expected_msg = make_expected_msg('Child', 'column_3')
        with assert_raises_str(AttributeError, expected_msg):
            Child.column_3
        def expect_slice_fails(*args, **kwargs):
            expected_msg = kwargs.pop('expected_msg')

            with assert_raises_str(ValueError, expected_msg):
                MD.slice(*args, **kwargs)
        def expect_slice_fails(*args, **kwargs):
            expected_msg = kwargs.pop('expected_msg')

            with assert_raises_str(TypeError, expected_msg):
                MD.slice(*args, **kwargs)
예제 #10
0
        def expect_slice_fails(*args, **kwargs):
            expected_msg = kwargs.pop("expected_msg")

            with assert_raises_str(ValueError, expected_msg):
                MD.slice(*args, **kwargs)
예제 #11
0
    def test_parse_args(self):
        n = Namespace()

        arg_dict = {}

        arg_list = [
            'key=value',
            'arg1=test1',
            'arg2=test2',
            'arg_3=test3',
            '_arg_4_=test4',
        ]
        for arg in arg_list:
            parse_extension_arg(arg, arg_dict)
        assert_equal(
            arg_dict,
            {
                '_arg_4_': 'test4',
                'arg_3': 'test3',
                'arg2': 'test2',
                'arg1': 'test1',
                'key': 'value',
            }
        )
        create_args(arg_list, n)
        assert_equal(n.key, 'value')
        assert_equal(n.arg1, 'test1')
        assert_equal(n.arg2, 'test2')
        assert_equal(n.arg_3, 'test3')
        assert_equal(n._arg_4_, 'test4')

        msg = (
            "invalid extension argument '1=test3', "
            "must be in key=value form"
        )
        with assert_raises_str(ValueError, msg):
            parse_extension_arg('1=test3', {})
        msg = (
            "invalid extension argument 'arg4 test4', "
            "must be in key=value form"
        )
        with assert_raises_str(ValueError, msg):
            parse_extension_arg('arg4 test4', {})
        msg = (
            "invalid extension argument 'arg5.1=test5', "
            "must be in key=value form"
        )
        with assert_raises_str(ValueError, msg):
            parse_extension_arg('arg5.1=test5', {})
        msg = (
            "invalid extension argument 'arg6.6arg=test6', "
            "must be in key=value form"
        )
        with assert_raises_str(ValueError, msg):
            parse_extension_arg('arg6.6arg=test6', {})
        msg = (
            "invalid extension argument 'arg7.-arg7=test7', "
            "must be in key=value form"
        )
        with assert_raises_str(ValueError, msg):
            parse_extension_arg('arg7.-arg7=test7', {})