source: coopr.pyomo/stable/2.3/coopr/pyomo/tests/examples/test_pyomo.py @ 2433

Last change on this file since 2433 was 2433, checked in by wehart, 11 years ago

Merged revisions 2388-2432 via svnmerge from
https://software.sandia.gov/svn/public/coopr/coopr.pyomo/trunk

........

r2399 | wehart | 2010-02-24 11:36:56 -0700 (Wed, 24 Feb 2010) | 2 lines


Adding a work-around when pstats cannot be imported.

........

r2400 | wehart | 2010-02-24 14:51:08 -0700 (Wed, 24 Feb 2010) | 2 lines


Misc documentation updates.

........

r2408 | wehart | 2010-02-28 05:41:04 -0700 (Sun, 28 Feb 2010) | 3 lines


Allow an earlier version of Python. I'm still not sure this is a good idea,
but it's necessary for Jython.

........

r2409 | wehart | 2010-02-28 05:42:33 -0700 (Sun, 28 Feb 2010) | 7 lines


Rework of profiling imports.


Refinement of Pyomo command-line parsing, which is more
specific now.


Adding automatic import of pyomo to package.

........

r2415 | jwatson | 2010-03-08 20:53:13 -0700 (Mon, 08 Mar 2010) | 3 lines


Significantly improved performance of PyomoModel? _clear_attribute method, mainly by eliminate unnecessary calls to it through _setattr_exec_.

........

r2416 | jwatson | 2010-03-09 16:45:22 -0700 (Tue, 09 Mar 2010) | 5 lines


Modified the AMPL dat file parser to instantiate the lexer and yaccer objects only once, for the lifetime of the module.


They were being inadvertently created at each invocation, which pyomo users wouldn't notice. But PySP users, who can be creating 1K or more instances, do!

........

r2422 | wehart | 2010-03-11 16:01:09 -0700 (Thu, 11 Mar 2010) | 3 lines


Rework of unit tests to (a) import pyutilib.th as 'unittest' and
(b) employ test skipping.

........

r2430 | wehart | 2010-03-11 23:38:22 -0700 (Thu, 11 Mar 2010) | 2 lines


Reworking class decorators.

........

File size: 4.3 KB
Line 
1#
2# Test the Pyomo command-line interface
3#
4
5import unittest
6import os
7import sys
8from os.path import abspath, dirname
9currdir = dirname(abspath(__file__))+os.sep
10scriptdir = dirname(dirname(dirname(dirname(dirname(abspath(__file__))))))+os.sep
11
12from coopr.pyomo import *
13import pyutilib.services
14import pyutilib.subprocess
15import pyutilib.th as unittest
16import coopr.pyomo.scripting.pyomo as main
17import StringIO 
18from pyutilib.misc import setup_redirect, reset_redirect
19
20if os.path.exists(sys.exec_prefix+os.sep+'bin'+os.sep+'coverage'):
21    executable=sys.exec_prefix+os.sep+'bin'+os.sep+'coverage -x '
22else:
23    executable=sys.executable
24
25
26class Test(unittest.TestCase):
27
28    def pyomo(self, cmd, **kwds):
29        args=re.split('[ ]+',cmd)
30        if 'file' in kwds:
31            OUTPUT=kwds['file']
32        else:
33            OUTPUT=StringIO.StringIO()
34        setup_redirect(OUTPUT)
35        os.chdir(currdir)
36        output = main.run(list(args))
37        reset_redirect()
38        if not 'file' in kwds:
39            return OUTPUT.getvalue()
40        return output
41       
42    def test1(self):
43        """Simple execution of 'pyomo'"""
44        self.pyomo('pmedian.py pmedian.dat', file=currdir+'test1.out')
45        self.failUnlessFileEqualsBaseline(currdir+"test1.out", currdir+"test1.txt")
46
47    def test2(self):
48        """Run pyomo with bad --model-name option value"""
49        output=self.pyomo('--model-name=dummy pmedian.py pmedian.dat')
50        self.failUnlessEqual(output.strip(),"Exiting pyomo: Neither 'dummy' nor 'create_model' are available in module pmedian.py")
51
52    def test3(self):
53        """Run pyomo with model that does not define model object"""
54        output=self.pyomo('pmedian1.py pmedian.dat')
55        self.failUnlessEqual(output.strip(),"Exiting pyomo: Neither 'model' nor 'create_model' are available in module pmedian1.py")
56
57    def test4(self):
58        """Run pyomo with good --model-name option value"""
59        self.pyomo('--model-name=MODEL pmedian1.py pmedian.dat', file=currdir+'test4.out')
60        self.failUnlessFileEqualsBaseline(currdir+"test4.out", currdir+"test1.txt")
61
62    def test5(self):
63        """Run pyomo with create_model function"""
64        self.pyomo('pmedian2.py pmedian.dat', file=currdir+'test5.out')
65        self.failUnlessFileEqualsBaseline(currdir+"test5.out", currdir+"test1.txt")
66
67    def test6(self):
68        """Run pyomo with help-components option"""
69        self.pyomo('--help-components', file=currdir+'test6.out')
70        self.failUnlessFileEqualsBaseline(currdir+"test6.out", currdir+"test6.txt")
71
72    def Xtest7(self):
73        """Run pyomo with help option"""
74        self.pyomo('--help', file=currdir+'test7.out')
75        self.failUnlessFileEqualsBaseline(currdir+"test7.out", currdir+"test7.txt")
76
77    def test8(self):
78        """Run pyomo with --instance-only option"""
79        output = self.pyomo('--instance-only pmedian.py pmedian.dat', file=currdir+'test8.out')
80        self.failUnlessEqual(type(output.instance), Model)
81        os.remove(currdir+'test8.out')
82
83    def test9(self):
84        """Run pyomo with --disable-gc option"""
85        output = self.pyomo('--disable-gc pmedian.py pmedian.dat', file=currdir+'test9.out')
86        self.failUnlessEqual(type(output.instance), Model)
87        os.remove(currdir+'test9.out')
88
89    def test10(self):
90        """Run pyomo with --verbose option"""
91        def filter(line):
92            #print "HERE",line
93            return line.startswith("Writing")
94        self.pyomo('-v pmedian.py pmedian.dat', file=currdir+'test10.out')
95        self.failUnlessFileEqualsBaseline(currdir+"test10.out", currdir+"test10.txt", filter)
96
97    def test11(self):
98        """Run pyomo with --debug=generate option"""
99        self.pyomo('--debug=generate pmedian.py pmedian.dat', file=currdir+'test11.out')
100        self.failUnlessFileEqualsBaseline(currdir+"test11.out", currdir+"test11.txt")
101
102    def test12(self):
103        """Run pyomo with --logfile option"""
104        self.pyomo('--debug=errors --logfile=%s pmedian.py pmedian.dat' % (currdir+'test12.log'), file=currdir+'test12.out')
105        self.failUnlessFileEqualsBaseline(currdir+"test12.log", currdir+"test12.txt")
106        os.remove(currdir+'test12.out')
107
108Test = unittest.skipIf(pyutilib.services.registered_executable("glpsol") is None, "The 'glpsol' executable is not available")(Test)
109
110if __name__ == "__main__":
111   unittest.main()
Note: See TracBrowser for help on using the repository browser.