source: trunk/test/core/base/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: 5.7 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
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_error1(self):
92        """ No valid problem types """
93        try:
94          coopr.opt.convert_problem( ("test4.nl","tmp.nl"), ProblemFormat.nl, [])
95          self.fail("Expected ConverterError exception")
96        except ConverterError:
97          pass
98
99    def test_error2(self):
100        """ Target problem type is not valid """
101        try:
102          coopr.opt.convert_problem( ("test4.nl","tmp.nl"), ProblemFormat.nl, [ProblemFormat.mps])
103          self.fail("Expected ConverterError exception")
104        except ConverterError:
105          pass
106
107    def test_error3(self):
108        """ Empty argument list """
109        try:
110          coopr.opt.convert_problem( (), None, [ProblemFormat.mps])
111          self.fail("Expected ConverterError exception")
112        except ConverterError:
113          pass
114
115    def test_error4(self):
116        """ Unknown source type """
117        try:
118          coopr.opt.convert_problem( ("prob.foo",), None, [ProblemFormat.mps])
119          self.fail("Expected ConverterError exception")
120        except ConverterError:
121          pass
122
123    def test_error5(self):
124        """ Unknown source type """
125        try:
126          coopr.opt.convert_problem( ("prob.lp",), ProblemFormat.nl, [ProblemFormat.nl])
127          self.fail("Expected ConverterError exception")
128        except ConverterError:
129          pass
130
131    def test_error6(self):
132        """ Cannot use pico_convert with more than one file """
133        try:
134          ans = coopr.opt.convert_problem( (currdir+"test4.nl","foo"), None, [ProblemFormat.cpxlp])
135          self.fail("Expected ConverterError exception")
136        except ConverterError:
137          pass
138
139    def test_error8(self):
140        """ Error when source file cannot be found """
141        try:
142          ans = coopr.opt.convert_problem( (currdir+"unknown.nl",), None, [ProblemFormat.cpxlp])
143          self.fail("Expected ConverterError exception")
144        except pyutilib.ApplicationError:
145           if pyutilib.registered_executable("pico_convert").enabled():
146              self.fail("Expected ApplicationError because pico_convert is not available")
147           return
148        except ConverterError:
149          pass
150
151    def test_error9(self):
152        """ The Opt configuration has not been initialized """
153        cmd = pyutilib.registered_executable("pico_convert")
154        if not cmd is None:
155            cmd.disable()
156        try:
157           ans = coopr.opt.convert_problem( (currdir+"test4.nl",), None, [ProblemFormat.cpxlp])
158           self.fail("This test didn't fail, but pico_convert should not be defined.")
159        except ConverterError:
160           pass
161        if not cmd is None:
162            cmd.enable()
163
164    def test_error10(self):
165        """ GLPSOL can only convert file data """
166        try:
167           arg = MockArg3()
168           ans = coopr.opt.convert_problem( (arg,ProblemFormat.cpxlp,arg), None, [ProblemFormat.cpxlp])
169           self.fail("This test didn't fail, but glpsol cannot handle objects.")
170        except ConverterError:
171           pass
172
173    def test_error11(self):
174        """ Cannot convert MOD that contains data """
175        try:
176          ans = coopr.opt.convert_problem( (currdir+"test3.mod",currdir+"test5.dat"), None, [ProblemFormat.cpxlp])
177          self.fail("Expected ConverterError exception because we provided a MOD file with a 'data;' declaration")
178        except pyutilib.ApplicationError:
179           if pyutilib.registered_executable("glpsol").enabled():
180              self.fail("Expected ApplicationError because glpsol is not available")
181           return
182        except ConverterError:
183          pass
184
185if __name__ == "__main__":
186   unittest.main()
Note: See TracBrowser for help on using the repository browser.