source: coopr.plugins/trunk/coopr/plugins/tests/mip/test_factory.py @ 1939

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

Update to solvers to use the new structure for managing the parsing of solution results.

File size: 4.9 KB
Line 
1#
2# Unit Tests for util/misc
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__))+"/../.."
11currdir = dirname(abspath(__file__))+os.sep
12
13import unittest
14from nose.tools import nottest
15import coopr.opt
16import coopr.plugins.mip
17import coopr
18import pyutilib.services
19import pyutilib.plugin.core
20
21
22class TestWriter(coopr.opt.AbstractProblemWriter):
23
24    def __init__(self, name=None):
25        coopr.opt.AbstractProblemWriter.__init__(self,name)
26   
27
28class TestReader(coopr.opt.AbstractResultsReader):
29
30    def __init__(self, name=None):
31        coopr.opt.AbstractResultsReader.__init__(self,name)
32   
33
34class TestSolver(coopr.opt.OptSolver):
35
36    def __init__(self, **kwds):
37        kwds['type'] = 'stest_type'
38        kwds['doc'] = 'TestSolver Documentation'
39        coopr.opt.OptSolver.__init__(self,**kwds)
40
41    def enabled(self):
42        return False
43
44
45class OptFactoryDebug(unittest.TestCase):
46
47    def run(self, result=None):
48        self.stest_plugin = coopr.opt.SolverRegistration("stest", TestSolver)
49        self.wtest_plugin = coopr.opt.WriterRegistration("wtest", TestWriter)
50        self.rtest_plugin = coopr.opt.ReaderRegistration("rtest", TestReader)
51        unittest.TestCase.run(self,result)
52        self.stest_plugin.deactivate()
53        self.rtest_plugin.deactivate()
54        self.wtest_plugin.deactivate()
55
56    def setUp(self):
57        pyutilib.services.TempfileManager.tempdir = currdir
58
59    def tearDown(self):
60        pyutilib.services.TempfileManager.clear_tempfiles()
61
62    def test_solver_factory(self):
63        """
64        Testing the coopr.opt solver factory with MIP solvers
65        """
66        ans = coopr.opt.SolverFactory()
67        ans.sort()
68        tmp = ['_mock_cbc', '_mock_cplex', '_mock_glpk', '_mock_pico', 'cbc', 'cplex', 'glpk', 'pico', 'stest']
69        tmp.sort()
70        self.failUnless(set(tmp) <= set(ans), msg="Set %s is not a subset of set %s" %(tmp,ans))
71
72    def test_solver_instance(self):
73        """
74        Testing that we get a specific solver instance
75        """
76        ans = coopr.opt.SolverFactory("none")
77        self.failUnlessEqual(ans, None)
78        ans = coopr.opt.SolverFactory("_mock_pico")
79        self.failUnlessEqual(type(ans), coopr.plugins.mip.MockPICO)
80        ans = coopr.opt.SolverFactory("_mock_pico", name="mymock")
81        self.failUnlessEqual(type(ans), coopr.plugins.mip.MockPICO)
82        self.failUnlessEqual(ans.name,  "mymock")
83
84    def test_solver_registration(self):
85        """
86        Testing methods in the solverwriter factory registration process
87        """
88        ep = pyutilib.plugin.core.ExtensionPoint(coopr.opt.ISolverRegistration)
89        service = ep.service("_mock_pico")
90        self.failUnlessEqual(service.type(), "_mock_pico")
91
92    def test_writer_factory(self):
93        """
94        Testing the coopr.opt writer factory with MIP writers
95        """
96        factory = coopr.opt.WriterFactory()
97        self.failUnless(set(['wtest']) <= set(factory))
98
99    def test_writer_instance(self):
100        """
101        Testing that we get a specific writer instance
102
103        Note: this simply provides code coverage right now, but
104        later it should be adapted to generate a specific writer.
105        """
106        ans = coopr.opt.WriterFactory("none")
107        self.failUnlessEqual(ans, None)
108        ans = coopr.opt.WriterFactory("wtest")
109        self.failIfEqual(ans, None)
110        ans = coopr.opt.WriterFactory("wtest", "mywriter")
111        self.failIfEqual(ans, None)
112        self.failIfEqual(ans.name, "mywriter")
113
114    def test_writer_registration(self):
115        """
116        Testing methods in the writer factory registration process
117        """
118        ep = pyutilib.plugin.core.ExtensionPoint(coopr.opt.IWriterRegistration)
119        service = ep.service("wtest")
120        self.failUnlessEqual(service.type(), "wtest")
121
122
123    def test_reader_factory(self):
124        """
125        Testing the coopr.opt reader factory
126        """
127        ans = coopr.opt.ReaderFactory()
128        #self.failUnlessEqual(len(ans),4)
129        self.failUnlessEqual(set(ans), set(["osrl","rtest", "sol","yaml"]))
130
131    def test_reader_instance(self):
132        """
133        Testing that we get a specific reader instance
134        """
135        ans = coopr.opt.ReaderFactory("none")
136        self.failUnlessEqual(ans, None)
137        ans = coopr.opt.ReaderFactory("osrl")
138        self.failUnlessEqual(type(ans), coopr.opt.reader.OS.ResultsReader_osrl)
139        ans = coopr.opt.ReaderFactory("osrl", "myreader")
140        self.failUnlessEqual(type(ans), coopr.opt.reader.OS.ResultsReader_osrl)
141        self.failUnlessEqual(ans.name, "myreader")
142
143    def test_reader_registration(self):
144        """
145        Testing methods in the reader factory registration process
146        """
147        ep = pyutilib.plugin.core.ExtensionPoint(coopr.opt.IReaderRegistration)
148        service = ep.service("rtest")
149        self.failUnlessEqual(service.type(), "rtest")
150
151if __name__ == "__main__":
152   unittest.main()
153
Note: See TracBrowser for help on using the repository browser.