source: coopr.pyomo/trunk/coopr/pyomo/tests/examples/test_convert.py @ 3681

Last change on this file since 3681 was 3681, checked in by wehart, 9 years ago

Migrating the profiling logic into scripting/util.py.
This allows it to be applied uniformly in the conversion scripts:
pyomo2nl and pyomo2lp.

File size: 2.6 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
12import coopr.pyomo
13import pyutilib.services
14import pyutilib.subprocess
15import pyutilib.th as unittest
16import coopr.pyomo.scripting.convert as main
17import StringIO 
18from pyutilib.misc import setup_redirect, reset_redirect
19import re
20
21if os.path.exists(sys.exec_prefix+os.sep+'bin'+os.sep+'coverage'):
22    executable=sys.exec_prefix+os.sep+'bin'+os.sep+'coverage -x '
23else:
24    executable=sys.executable
25
26def filter_fn(line):
27    return line.startswith('Disjunct')
28
29
30class Test(unittest.TestCase):
31
32    def convert(self, cmd, type, **kwds):
33        args=re.split('[ ]+',cmd)
34        if 'file' in kwds:
35            OUTPUT=kwds['file']
36        else:
37            OUTPUT=StringIO.StringIO()
38        setup_redirect(OUTPUT)
39        os.chdir(currdir)
40        if type == 'lp':
41            output = main.pyomo2lp(list(args))
42        else:
43            output = main.pyomo2nl(list(args))
44        reset_redirect()
45        if not 'file' in kwds:
46            return OUTPUT.getvalue()
47        return output
48
49    def run_convert2nl(self, cmd, file=None):
50        return pyutilib.subprocess.run('pyomo2nl '+cmd, outfile=file)
51       
52    def run_convert2lp(self, cmd, file=None):
53        return pyutilib.subprocess.run('pyomo2lp '+cmd, outfile=file)
54       
55    def test1a(self):
56        """Simple execution of 'convert2nl'"""
57        self.convert('pmedian.py pmedian.dat', type='nl', file=currdir+'test1a.out')
58        self.failUnlessFileEqualsBaseline(currdir+'unknown.nl', currdir+'_pmedian.nl')
59        os.remove(currdir+'test1a.out')
60
61    def test1b(self):
62        """Simple execution of 'pyomo2nl'"""
63        self.run_convert2nl('pmedian.py pmedian.dat', file=currdir+'test1b.out')
64        self.failUnlessFileEqualsBaseline(currdir+'unknown.nl', currdir+'_pmedian.nl')
65        os.remove(currdir+'test1b.out')
66
67    def test2a(self):
68        """Simple execution of 'convert2lp'"""
69        self.convert('pmedian.py pmedian.dat', type='lp', file=currdir+'test2a.out')
70        self.failUnlessFileEqualsBaseline(currdir+'unknown.lp', currdir+'_pmedian.lp')
71        os.remove(currdir+'test2a.out')
72
73    def test2b(self):
74        """Simple execution of 'pyomo2lp'"""
75        self.run_convert2lp('pmedian.py pmedian.dat', file=currdir+'test2b.out')
76        self.failUnlessFileEqualsBaseline(currdir+'unknown.lp', currdir+'_pmedian.lp')
77        os.remove(currdir+'test2b.out')
78
79
80if __name__ == "__main__":
81   unittest.main()
Note: See TracBrowser for help on using the repository browser.