Пример #1
0
 def test_err_undeclared_list_attr(self):
     #"""Undeclared attribute"""
     data = PyomoAPIData()
     data.declare(['a'])
     with self.assertRaisesRegexp(AttributeError,
                                  "Undeclared attribute 'x'"):
         data.x
Пример #2
0
    def test6a_kwds_return_apidata(self):
        #"""Outputs specified in docstring: keyword arguments, non-data return values with data"""
        @pyomo_api
        def test6a(data, x=1, y=2):
            """
            Required:
                data: input data
                x: integer
            Optional:
                y: integer
            Return:
                data: output data
                z: integer
            """
            data.a = y
            data.b[0] = x
            return PyomoAPIData(data=data, z=x)

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test6a(options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test6a(data=options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        self.assertEqual(retval.z, 2)
Пример #3
0
    def test4_kwds_return_data(self):
        #"""Simple test: keyword arguments, simple return value"""
        @pyomo_api
        def test4(data, x=1, y=2):
            """
            Required:
                data: input data
                x: integer
            Optional:
                y: integer
            Return:
                data: output data
            """
            data.a = y
            data.b[0] = x
            return data

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test4(options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test4(data=options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
Пример #4
0
    def test5_kwds_return_apidata(self):
        #"""Simple test: keyword arguments, non-data return values"""
        @pyomo_api(outputs=('z'))
        def test5(data, x=1, y=2):
            """
            Required:
                data: input data
                x: integer
            Optional:
                y: integer
            Return:
                data: output data
                z: integer
            """
            data.a = y
            data.b[0] = x
            return PyomoAPIData(z=x)

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test5(options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test5(data=options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        self.assertEqual(retval.z, 2)
Пример #5
0
    def test_err8b_missing_nested_arg(self):
        #"""Nested value with None value"""
        @pyomo_api
        def err8b(data):
            """
            Required:
                data.x: integer
            """
            pass

        with self.assertRaisesRegexp(
                RuntimeError,
                "None value found for nested attribute 'data.x'"):
            err8b(PyomoAPIData(data=PyomoAPIData()))
Пример #6
0
    def test1b_data_kwds_no_return(self):
        #"""Simple test: data keyword argument, no return values"""
        @pyomo_api
        def test1b(data=None):
            data.a = 2
            data.b[0] = 2

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test1b(options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test1b(data=options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
Пример #7
0
 def err7c(data, x=1, y=2):
     """
     Required:
         x: integer
     Optional:
         y: integer
     """
     return PyomoAPIData(z=1)
Пример #8
0
 def test10(data=None, x=1):
     """
     Required:
         x:
     Optional:
         data:
     """
     return PyomoAPIData(z=2 * x)
Пример #9
0
    def test2_implicit_no_kwds_return_data(self):
        #"""Simple test: no keyword arguments, returning data"""
        @pyomo_api
        def test2(data):
            data.a = 2
            data.b[0] = 2
            return data

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test2(options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test2(data=options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
Пример #10
0
    def test_err5_unexpected_return(self):
        #"""Expect an error when an unspecified return value is given"""
        @pyomo_api
        def err5(data):
            return PyomoAPIData(z=None)

        with self.assertRaisesRegexp(
                RuntimeError,
                "Cannot return value 'z' that is not a predefined output "
                "of a Pyomo task"):
            err5(PyomoAPIData())
Пример #11
0
    def test_err3_invalid_return(self):
        #"""Expect an error when return value is not None or Options()"""
        @pyomo_api
        def err3(data):
            return 1

        with self.assertRaisesRegexp(
                RuntimeError,
                "A Pyomo task function must return either None, a "
                "PyomoAPIData object, or an instance of dict"):
            err3(PyomoAPIData())
Пример #12
0
    def test9_complex_documentation(self):
        #"""Simple test: no keyword arguments or return values"""
        @pyomo_api
        def test9(data):
            """
            This is the
            short
            documentation.

            This

            is

            the

            long documentation.
            Required:
                data: multiline
                      description of
                      input data object
                data.a: More data
            """
            data.a = 2
            data.b[0] = 2

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test9(options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test9(data=options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        #
        self.assertTrue('test1' in PyomoAPIFactory.services())
        self.assertEqual(test9.__short_doc__,
                         'This is the\nshort\ndocumentation.')
        self.assertEqual(test9.__long_doc__,
                         'This\n\nis\n\nthe\n\nlong documentation.')
Пример #13
0
    def test_print_PyomoAPIData_string(self):
        #"""Print PyomoAPIData string"""
        data = PyomoAPIData()
        data.a = 1
        data.b = [1, 2]
        data.c = PyomoAPIData()
        data.c.x = 1
        data.c.y = 2
        data['aa'] = 'here is more'
        data.clean()
        self.assertEqual(sorted(data.unused()), ['a', 'aa', 'b', 'c'])
        self.assertEqual(
            str(data), """a: 1
aa: here is more
b: [1, 2]
c:
    x: 1
    y: 2""")
        self.assertEqual(len(data._dirty_), 0)
Пример #14
0
 def test6a(data, x=1, y=2):
     """
     Required:
         data: input data
         x: integer
     Optional:
         y: integer
     Return:
         data: output data
         z: integer
     """
     data.a = y
     data.b[0] = x
     return PyomoAPIData(data=data, z=x)
Пример #15
0
    def test1_no_kwds_no_return(self):
        #"""Simple test: no keyword arguments or return values"""
        @pyomo_api
        def test1(data):
            """
            Required:
                data: input data
            """
            data.a = 2
            data.b[0] = 2

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test1(options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test1(data=options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        #
        self.assertTrue('test1' in PyomoAPIFactory.services())
Пример #16
0
    def test_err8c_missing_nested_dict_arg(self):
        #"""Nested value with None value"""
        @pyomo_api
        def err8c(data):
            """
            Required:
                data.x.y: integer
            """
            pass

        with self.assertRaisesRegexp(
                RuntimeError,
                "Failed to verify existence of nested attribute 'data.x.y'"):
            err8c(PyomoAPIData(x={}))
Пример #17
0
    def test1a_no_kwds_single_return(self):
        #"""Simple test: no keyword arguments or return values"""
        @pyomo_api
        def test1a(data):
            """
            Required:
                data: input data
            Return:
                data: output data
            """
            data.a = 2
            data.b[0] = 2

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test1a(options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test1a(data=options)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
Пример #18
0
    def test3a_kwds_no_return(self):
        #"""Simple test: keyword arguments, no return values"""
        @pyomo_api
        def test3a(x=1, y=2, data=None):
            """
            Required:
                data: input data
                x: integer
            Optional:
                y: integer
            """
            data.a = y
            data.b[0] = x

        #
        options = PyomoAPIData()
        options.a = 1
        options.b = [1, 2]
        retval = test3a(options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
        retval = test3a(data=options, x=2)
        self.assertEqual(retval.data.a, 2)
        self.assertEqual(retval.data.b, [2, 2])
Пример #19
0
 def test_print_PyomoAPIData_repr(self):
     #"""Print PyomoAPIData representation"""
     data = PyomoAPIData()
     data.a = 1
     data.b = [1, 2]
     data.c = PyomoAPIData()
     data.c.x = 1
     data.c.y = 2
     data['aa'] = 'here is more'
     data.clean()
     # Because the PyomoAPIData is a dict, the repr() is subject to
     # change between python versions.  Cast back to a dict and
     # verify.
     self.assertEqual(
         eval(repr(data)),
         eval("{'a': 1, 'aa': 'here is more', 'b': [1, 2], "
              "'c': {'y': 2, 'x': 1}}"))
     self.assertEqual(len(data._dirty_), 0)
Пример #20
0
    def test_err6_missing_input_data(self):
        #"""Expect an error when no data argument is specified"""

        buf = StringIO()
        with LoggingIntercept(buf, 'pyomo.common'):

            @pyomo_api
            def err6():
                pass

        self.assertIn("A Pyomo functor 'err6' must have a 'data argument",
                      buf.getvalue())

        # Note: the TypeError message changes in Python 3.6, so we need
        # a weaker regexp.
        with self.assertRaisesRegexp(TypeError,
                                     "err6\(\) takes .* arguments .* given"):
            err6(PyomoAPIData())
Пример #21
0
    def test8_nested_data(self):
        #"""Simple test with required nested data"""
        @pyomo_api
        def test8(data):
            """
            Required:
                data: input data
                data.foo.bar:
            """
            data.foo.foo = 3
            #data.a = 2
            #data.b[0] = 2

        #
        options = PyomoAPIData()
        options.foo = PyomoAPIData()
        options.foo.bar = 1
        options.a = 1
        options.b = [1, 2]
        retval = test8(options)
        self.assertEqual(retval.data.a, 1)
        self.assertEqual(retval.data.b, [1, 2])
        self.assertEqual(retval.data.foo.foo, 3)
Пример #22
0
    def test_err10_test_with_nested_data(self):
        #"""Simple test with required nested data"""
        @pyomo_api
        def err10(data):
            """
            Required:
                data: input data
                data.foo.bar:
            """
            data.foo.foo = 3
            with self.assertRaisesRegexp(AttributeError,
                                         "Undeclared attribute 'a'"):
                data.a = 2

        #
        options = PyomoAPIData()
        options.foo = PyomoAPIData()
        options.foo.bar = 1
        options.a = 1
        options.b = [1, 2]
        retval = err10(options)
        self.assertEqual(retval.data.a, 1)
        self.assertEqual(retval.data.b, [1, 2])
        self.assertEqual(retval.data.foo.foo, 3)
Пример #23
0
 def err5(data):
     return PyomoAPIData(z=None)
Пример #24
0
 def test_err_unknown_attr(self):
     #"""Unknown attribute"""
     data = PyomoAPIData()
     with self.assertRaisesRegexp(AttributeError, 'Unknown attribute _x'):
         data._x