source: trunk/test/plugins/mip/test_convert.py @ 1657

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

Removing coopr.opt-depricated, and
renaming coopr.core to coopr.opt.

Unfortunately, plugins are still not being properly loaded, so
some of the tests are failing.

File size: 9.5 KB
Line 
1#
2# Unit Tests for coopr.opt.base.convert
3#
4#
5
6import os
7import sys
8from os.path import abspath, dirname
9sys.path.insert(0, dirname(dirname(abspath(__file__)))+"/../..")
10cooprdir = dirname(abspath(__file__))+os.sep+".."+os.sep+".."+os.sep
11currdir = dirname(abspath(__file__))+os.sep
12
13import re
14import unittest
15from nose.tools import nottest
16import coopr.plugins
17from coopr.opt import ProblemFormat, ConverterError
18import coopr
19import pyutilib_th
20import pyutilib
21import xml
22import filecmp
23
24
25class MockArg(object):
26
27    def __init__(self):
28        pass
29
30    def valid_problem_types(self):
31        return [ProblemFormat.pyomo]
32
33    def write(self,filename="", format=None):
34        pass
35
36class MockArg2(MockArg):
37
38    def valid_problem_types(self):
39        return [ProblemFormat.nl]
40
41    def write(self,filename="", format=None):
42        OUTPUT=open(filename,"w")
43        INPUT=open(currdir+"test4.nl")
44        for line in INPUT:
45          print >>OUTPUT, line,
46        OUTPUT.close()
47        INPUT.close()
48
49class MockArg3(MockArg):
50
51    def valid_problem_types(self):
52        return [ProblemFormat.mod]
53
54    def write(self,filename="", format=None):
55        pass
56
57class MockArg4(MockArg):
58
59    def write(self,filename="", format=None):
60        OUTPUT=open(filename,"w")
61        INPUT=open(currdir+"test4.nl")
62        for line in INPUT:
63          print >>OUTPUT, line,
64        OUTPUT.close()
65        INPUT.close()
66
67
68class OptConvertDebug(pyutilib_th.TestCase):
69
70    def setUp(self):
71        pyutilib.TempfileManager.tempdir = currdir
72
73    def tearDown(self):
74        pyutilib.TempfileManager.clear_tempfiles()
75        #
76        # Reset all options
77        #
78        for ep in pyutilib.plugin.ExtensionPoint(pyutilib.plugin.IOption):
79            ep.reset()
80
81    def test_nl_nl1(self):
82        """ Convert from NL to NL """
83        ans = coopr.opt.convert_problem( ("test4.nl",), None, [ProblemFormat.nl])
84        self.failUnlessEqual(ans[0],("test4.nl",))
85
86    def test_nl_nl2(self):
87        """ Convert from NL to NL """
88        ans = coopr.opt.convert_problem( ("test4.nl","tmp.nl"), None, [ProblemFormat.nl])
89        self.failUnlessEqual(ans[0],("test4.nl","tmp.nl"))
90
91    def test_nl_lp1(self):
92        """ Convert from NL to LP """
93        try:
94           ans = coopr.opt.convert_problem( (currdir+"test4.nl",), None, [ProblemFormat.cpxlp])
95        except pyutilib.ApplicationError:
96           if not pyutilib.registered_executable("pico_convert") is None:
97              self.fail("Unexpected ApplicationError - pico_convert is enabled but not available")
98           return
99        except ConverterError:
100           if not pyutilib.registered_executable("pico_convert") is None:
101              self.fail("Unexpected ConverterError - pico_convert is enabled but not available")
102           return
103        self.failUnlessEqual(ans[0][0][-15:],"pico_convert.lp")
104        self.failUnlessFileEqualsBaseline(ans[0][0], currdir+"test1_convert.lp")
105
106    def test_mod_lp1(self):
107        """ Convert from MOD to LP """
108        try:
109           ans = coopr.opt.convert_problem( (currdir+"test3.mod",), None, [ProblemFormat.cpxlp])
110        except pyutilib.ApplicationError:
111           if not pyutilib.registered_executable("glpsol") is None:
112              self.fail("Unexpected ApplicationError - glpsol is enabled but not available")
113           return
114        except ConverterError:
115           if not pyutilib.registered_executable("glpsol") is None:
116              self.fail("Unexpected ConverterError - glpsol is enabled but not available")
117           return
118        self.failUnlessEqual(ans[0],("glpsol.lp",))
119        self.failUnlessFileEqualsBaseline(ans[0][0], currdir+"test2_convert.lp")
120
121    def test_mod_lp2(self):
122        """ Convert from MOD+DAT to LP """
123        try:
124           ans = coopr.opt.convert_problem( (currdir+"test5.mod",currdir+"test5.dat"), None, [ProblemFormat.cpxlp])
125        except pyutilib.ApplicationError:
126           if not pyutilib.registered_executable("glpsol") is None:
127              self.fail("Unexpected ApplicationError - glpsol is enabled but not available")
128           return
129        except ConverterError:
130           if not pyutilib.registered_executable("glpsol") is None:
131              self.fail("Unexpected ConverterError - glpsol is enabled but not available")
132           return
133        self.failUnlessEqual(ans[0],("glpsol.lp",))
134        self.failUnlessFileEqualsBaseline(ans[0][0], currdir+"test3_convert.lp")
135
136    def test_mock_lp1(self):
137        """ Convert from Pyomo to LP """
138        arg=MockArg()
139        ans = coopr.opt.convert_problem( (arg,ProblemFormat.cpxlp,arg), None, [ProblemFormat.cpxlp])
140        self.failIfEqual(re.match(".*tmp.*pyomo.lp$",ans[0][0]), None)
141
142    def test_mock_lp2(self):
143        """ Convert from NL to LP """
144        arg=MockArg2()
145        try:
146           ans = coopr.opt.convert_problem( (arg,), None, [ProblemFormat.cpxlp])
147        except ConverterError:
148           if pyutilib.registered_executable("pico_convert") is None:
149              return
150           else:
151              self.fail("Expected ApplicationError because pico_convert is not available")
152        self.failUnlessEqual(ans[0][0][-15:],"pico_convert.lp")
153        os.remove(ans[0][0])
154
155    def test_mock_mps1(self):
156        """ Convert from Pyomo to MPS """
157        arg=MockArg4()
158        try:
159           ans = coopr.opt.convert_problem( (arg,ProblemFormat.mps,arg), None, [ProblemFormat.mps])
160        except ConverterError:
161           if pyutilib.registered_executable("pico_convert") is None:
162                return
163           else:
164                self.fail("Expected ApplicationError because pico_convert is not available")
165        self.failUnlessEqual(ans[0][0][-16:],"pico_convert.mps")
166        os.remove(ans[0][0])
167
168    def test_mock_nl1(self):
169        """ Convert from Pyomo to NL """
170        arg=MockArg4()
171        ans = coopr.opt.convert_problem( (arg,ProblemFormat.nl,arg), None, [ProblemFormat.nl])
172        self.failIfEqual(re.match(".*tmp.*pyomo.nl$",ans[0][0]), None)
173        os.remove(ans[0][0])
174
175    def test_error1(self):
176        """ No valid problem types """
177        try:
178          coopr.opt.convert_problem( ("test4.nl","tmp.nl"), ProblemFormat.nl, [])
179          self.fail("Expected ConverterError exception")
180        except ConverterError:
181          pass
182
183    def test_error2(self):
184        """ Target problem type is not valid """
185        try:
186          coopr.opt.convert_problem( ("test4.nl","tmp.nl"), ProblemFormat.nl, [ProblemFormat.mps])
187          self.fail("Expected ConverterError exception")
188        except ConverterError:
189          pass
190
191    def test_error3(self):
192        """ Empty argument list """
193        try:
194          coopr.opt.convert_problem( (), None, [ProblemFormat.mps])
195          self.fail("Expected ConverterError exception")
196        except ConverterError:
197          pass
198
199    def test_error4(self):
200        """ Unknown source type """
201        try:
202          coopr.opt.convert_problem( ("prob.foo",), None, [ProblemFormat.mps])
203          self.fail("Expected ConverterError exception")
204        except ConverterError:
205          pass
206
207    def test_error5(self):
208        """ Unknown source type """
209        try:
210          coopr.opt.convert_problem( ("prob.lp",), ProblemFormat.nl, [ProblemFormat.nl])
211          self.fail("Expected ConverterError exception")
212        except ConverterError:
213          pass
214
215    def test_error6(self):
216        """ Cannot use pico_convert with more than one file """
217        try:
218          ans = coopr.opt.convert_problem( (currdir+"test4.nl","foo"), None, [ProblemFormat.cpxlp])
219          self.fail("Expected ConverterError exception")
220        except ConverterError:
221          pass
222
223    def test_error8(self):
224        """ Error when source file cannot be found """
225        try:
226          ans = coopr.opt.convert_problem( (currdir+"unknown.nl",), None, [ProblemFormat.cpxlp])
227          self.fail("Expected ConverterError exception")
228        except pyutilib.ApplicationError:
229           if pyutilib.registered_executable("pico_convert").enabled():
230              self.fail("Expected ApplicationError because pico_convert is not available")
231           return
232        except ConverterError:
233          pass
234
235    def test_error9(self):
236        """ The Opt configuration has not been initialized """
237        cmd = pyutilib.registered_executable("pico_convert")
238        if not cmd is None:
239            cmd.disable()
240        try:
241           ans = coopr.opt.convert_problem( (currdir+"test4.nl",), None, [ProblemFormat.cpxlp])
242           self.fail("This test didn't fail, but pico_convert should not be defined.")
243        except ConverterError:
244           pass
245        if not cmd is None:
246            cmd.enable()
247
248    def test_error10(self):
249        """ GLPSOL can only convert file data """
250        try:
251           arg = MockArg3()
252           ans = coopr.opt.convert_problem( (arg,ProblemFormat.cpxlp,arg), None, [ProblemFormat.cpxlp])
253           self.fail("This test didn't fail, but glpsol cannot handle objects.")
254        except ConverterError:
255           pass
256
257    def test_error11(self):
258        """ Cannot convert MOD that contains data """
259        try:
260          ans = coopr.opt.convert_problem( (currdir+"test3.mod",currdir+"test5.dat"), None, [ProblemFormat.cpxlp])
261          self.fail("Expected ConverterError exception because we provided a MOD file with a 'data;' declaration")
262        except pyutilib.ApplicationError:
263           if pyutilib.registered_executable("glpsol").enabled():
264              self.fail("Expected ApplicationError because glpsol is not available")
265           return
266        except ConverterError:
267          pass
268
269if __name__ == "__main__":
270   unittest.main()
Note: See TracBrowser for help on using the repository browser.