source: trunk/test/core/base/test_convert.py @ 1768

Last change on this file since 1768 was 1768, checked in by wehart, 10 years ago

Rework of Coopr to use the new PyUtilib? package decomposition.

NOTE: to use Coopr with this update, we need to work with a new version of coopr_install.

File size: 5.8 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.opt
17from coopr.opt import ProblemFormat, ConverterError
18import coopr
19import pyutilib.th
20import pyutilib.services
21import pyutilib.plugin.config
22import pyutilib.plugin.core
23import pyutilib.common
24import xml
25import filecmp
26
27
28class MockArg(object):
29
30    def __init__(self):
31        pass
32
33    def valid_problem_types(self):
34        return [ProblemFormat.pyomo]
35
36    def write(self,filename="", format=None):
37        pass
38
39class MockArg2(MockArg):
40
41    def valid_problem_types(self):
42        return [ProblemFormat.nl]
43
44    def write(self,filename="", format=None):
45        OUTPUT=open(filename,"w")
46        INPUT=open(currdir+"test4.nl")
47        for line in INPUT:
48          print >>OUTPUT, line,
49        OUTPUT.close()
50        INPUT.close()
51
52class MockArg3(MockArg):
53
54    def valid_problem_types(self):
55        return [ProblemFormat.mod]
56
57    def write(self,filename="", format=None):
58        pass
59
60class MockArg4(MockArg):
61
62    def write(self,filename="", format=None):
63        OUTPUT=open(filename,"w")
64        INPUT=open(currdir+"test4.nl")
65        for line in INPUT:
66          print >>OUTPUT, line,
67        OUTPUT.close()
68        INPUT.close()
69
70
71class OptConvertDebug(pyutilib.th.TestCase):
72
73    def setUp(self):
74        pyutilib.services.TempfileManager.tempdir = currdir
75
76    def tearDown(self):
77        pyutilib.services.TempfileManager.clear_tempfiles()
78        #
79        # Reset all options
80        #
81        for ep in pyutilib.plugin.core.ExtensionPoint(pyutilib.plugin.config.IOption):
82            ep.reset()
83
84    def test_nl_nl1(self):
85        """ Convert from NL to NL """
86        ans = coopr.opt.convert_problem( ("test4.nl",), None, [ProblemFormat.nl])
87        self.failUnlessEqual(ans[0],("test4.nl",))
88
89    def test_nl_nl2(self):
90        """ Convert from NL to NL """
91        ans = coopr.opt.convert_problem( ("test4.nl","tmp.nl"), None, [ProblemFormat.nl])
92        self.failUnlessEqual(ans[0],("test4.nl","tmp.nl"))
93
94    def test_error1(self):
95        """ No valid problem types """
96        try:
97          coopr.opt.convert_problem( ("test4.nl","tmp.nl"), ProblemFormat.nl, [])
98          self.fail("Expected ConverterError exception")
99        except ConverterError:
100          pass
101
102    def test_error2(self):
103        """ Target problem type is not valid """
104        try:
105          coopr.opt.convert_problem( ("test4.nl","tmp.nl"), ProblemFormat.nl, [ProblemFormat.mps])
106          self.fail("Expected ConverterError exception")
107        except ConverterError:
108          pass
109
110    def test_error3(self):
111        """ Empty argument list """
112        try:
113          coopr.opt.convert_problem( (), None, [ProblemFormat.mps])
114          self.fail("Expected ConverterError exception")
115        except ConverterError:
116          pass
117
118    def test_error4(self):
119        """ Unknown source type """
120        try:
121          coopr.opt.convert_problem( ("prob.foo",), None, [ProblemFormat.mps])
122          self.fail("Expected ConverterError exception")
123        except ConverterError:
124          pass
125
126    def test_error5(self):
127        """ Unknown source type """
128        try:
129          coopr.opt.convert_problem( ("prob.lp",), ProblemFormat.nl, [ProblemFormat.nl])
130          self.fail("Expected ConverterError exception")
131        except ConverterError:
132          pass
133
134    def test_error6(self):
135        """ Cannot use pico_convert with more than one file """
136        try:
137          ans = coopr.opt.convert_problem( (currdir+"test4.nl","foo"), None, [ProblemFormat.cpxlp])
138          self.fail("Expected ConverterError exception")
139        except ConverterError:
140          pass
141
142    def test_error8(self):
143        """ Error when source file cannot be found """
144        try:
145          ans = coopr.opt.convert_problem( (currdir+"unknown.nl",), None, [ProblemFormat.cpxlp])
146          self.fail("Expected ConverterError exception")
147        except pyutilib.common.ApplicationError:
148           if pyutilib.services.registered_executable("pico_convert").enabled():
149              self.fail("Expected ApplicationError because pico_convert is not available")
150           return
151        except ConverterError:
152          pass
153
154    def test_error9(self):
155        """ The Opt configuration has not been initialized """
156        cmd = pyutilib.services.registered_executable("pico_convert")
157        if not cmd is None:
158            cmd.disable()
159        try:
160           ans = coopr.opt.convert_problem( (currdir+"test4.nl",), None, [ProblemFormat.cpxlp])
161           self.fail("This test didn't fail, but pico_convert should not be defined.")
162        except ConverterError:
163           pass
164        if not cmd is None:
165            cmd.enable()
166
167    def test_error10(self):
168        """ GLPSOL can only convert file data """
169        try:
170           arg = MockArg3()
171           ans = coopr.opt.convert_problem( (arg,ProblemFormat.cpxlp,arg), None, [ProblemFormat.cpxlp])
172           self.fail("This test didn't fail, but glpsol cannot handle objects.")
173        except ConverterError:
174           pass
175
176    def test_error11(self):
177        """ Cannot convert MOD that contains data """
178        try:
179          ans = coopr.opt.convert_problem( (currdir+"test3.mod",currdir+"test5.dat"), None, [ProblemFormat.cpxlp])
180          self.fail("Expected ConverterError exception because we provided a MOD file with a 'data;' declaration")
181        except pyutilib.common.ApplicationError:
182           if pyutilib.registered_executable("glpsol").enabled():
183              self.fail("Expected ApplicationError because glpsol is not available")
184           return
185        except ConverterError:
186          pass
187
188if __name__ == "__main__":
189   unittest.main()
Note: See TracBrowser for help on using the repository browser.