source: trunk/test/core/base/test_os.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: 4.5 KB
Line 
1#
2# Unit Tests for coopr.opt.base.OS
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 unittest
14from nose.tools import nottest
15import coopr.opt
16import coopr
17import xml
18import pyutilib.th
19import pyutilib.services
20
21pyutilib.services.TempfileManager.tempdir = currdir
22
23class OptOSDebug(pyutilib.th.TestCase):
24
25    def setUp(self):
26        pyutilib.services.TempfileManager.tempdir = currdir
27        #
28        # Create OSrL object
29        #
30        self.osrl = coopr.opt.reader.OS.OSrL()
31        #
32        # Initialize header info
33        #
34        self.osrl.header.serviceURI=""
35        self.osrl.header.serviceName=""
36        self.osrl.header.instanceName=""
37        self.osrl.header.time=0.0
38        self.osrl.header.message=""
39        self.osrl.header.jobID=0
40
41    def tearDown(self):
42        pyutilib.services.TempfileManager.clear_tempfiles()
43
44    def test_write_solution(self):
45        #
46        # Initialize solutions
47        #
48        self.osrl.numVariables=4
49        self.osrl.numObjectives=2
50        self.osrl.numConstraints=4
51        self.soln = self.osrl.add_solution()
52        self.soln.status_description=""
53        self.soln.variable[0]=1.2
54        self.soln.variable[1]=1.2
55        self.soln.variable[3]=1.2
56        self.soln.objective[-1]=1.2
57        self.soln.objective[-2]=1.2
58        self.soln.dual[0]=1.2
59        self.soln.dual[1]=1.2
60        self.soln.dual[3]=1.2
61        self.osrl.write(currdir+"write_solution.OSrL.xml")
62        if not os.path.exists(currdir+"write_solution.OSrL.xml"):
63           self.fail("test_write_solution - failed to write OSrL.xml")
64        self.failUnlessFileEqualsBaseline(currdir+"write_solution.OSrL.xml",currdir+"test1.OSrL.xml")
65
66    def test_None_values(self):
67        self.osrl.read(currdir+"test1.OSrL.xml")
68        self.osrl.header.serviceName=None
69        self.osrl.header.serviceURI=None
70        self.osrl.header.instanceName=None
71        self.osrl.header.time=None
72        self.osrl.header.message=None
73        self.osrl.header.jobID=None
74        OUTPUT = open(currdir+"None_values.OSrL.xml","w")
75        self.osrl.write(OUTPUT)
76        OUTPUT.close()
77        if not os.path.exists(currdir+"None_values.OSrL.xml"):
78           self.fail("test_write_solution - failed to write OSrL.xml")
79        self.failUnlessFileEqualsBaseline(currdir+"None_values.OSrL.xml",currdir+"test7.OSrL.xml")
80
81    def test_read_solution(self):
82        self.osrl.read(currdir+"test1.OSrL.xml")
83        self.failUnlessEqual(len(self.osrl),1)
84       
85    def test_bad_status(self):
86        self.osrl.header.generalStatus=None
87        try:
88          self.osrl.validate()
89          self.fail("test_bad_status - Error validating 'None' status.")
90        except ValueError:
91          pass
92
93    def test_test2_error(self):
94        try:
95          self.osrl.read(currdir+"test2.OSrL.xml")
96          self.fail("test_test2_error - Failed to find error in test2.OSrL.xml")
97        except ValueError:
98          pass
99
100    def test_test3_error(self):
101        try:
102          self.osrl.read(currdir+"test3.OSrL.xml")
103          self.fail("test_test3_error - Failed to find error in test3.OSrL.xml")
104        except KeyError:
105          pass
106
107    def test_test4_error(self):
108        try:
109          self.osrl.read(currdir+"test4.OSrL.xml")
110          self.fail("test_test4_error - Failed to find error in test4.OSrL.xml")
111        except KeyError:
112          pass
113
114    def test_test5_error(self):
115        try:
116          self.osrl.read(currdir+"test5.OSrL.xml")
117          self.fail("test_test5_error - Failed to find error in test5.OSrL.xml")
118        except KeyError:
119          pass
120
121    def test_test6_error(self):
122        try:
123          self.osrl.read(currdir+"test6.OSrL.xml")
124          self.fail("test_test6_error - Failed to find error in test6.OSrL.xml")
125        except xml.parsers.expat.ExpatError:
126          pass
127        try:
128          self.osrl.read(currdir+"test6.OSrL")
129          self.fail("test_test6_error - Failed raise an error for a missing file")
130        except IOError:
131          pass
132
133    def test_factory(self):
134        reader = coopr.opt.ReaderFactory("osrl")
135        soln = reader(currdir+"test1.OSrL.xml")
136        soln.write(currdir+"test_os.txt")
137        #
138        # Compare with baseline
139        #
140        self.failUnlessFileEqualsBaseline(currdir+"test_os.txt", currdir+"test1.txt")
141       
142
143if __name__ == "__main__":
144   unittest.main()
Note: See TracBrowser for help on using the repository browser.