source: coopr.opt/trunk/coopr/opt/tests/base/test_factory.py @ 1938

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

Changes to clarify what solver outputs entail. Now, logfiles
are processed whenever available, and other data is processed with
an appropriate reader. The exception is 'soln' files, which
generically refers to solver-specific solution results. These are
parsed with a solver method.

File size: 4.7 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
17import pyutilib.services
18import pyutilib.plugin.core
19
20
21class TestWriter(coopr.opt.AbstractProblemWriter):
22
23    def __init__(self, name=None):
24        coopr.opt.AbstractProblemWriter.__init__(self,name)
25   
26
27class TestReader(coopr.opt.AbstractResultsReader):
28
29    def __init__(self, name=None):
30        coopr.opt.AbstractResultsReader.__init__(self,name)
31   
32
33class TestSolver(coopr.opt.OptSolver):
34
35    def __init__(self, **kwds):
36        kwds['type'] = 'stest_type'
37        kwds['doc'] = 'TestSolver Documentation'
38        coopr.opt.OptSolver.__init__(self,**kwds)
39
40    def enabled(self):
41        return False
42
43
44class OptFactoryDebug(unittest.TestCase):
45
46    def run(self, result=None):
47        self.stest_plugin = coopr.opt.SolverRegistration("stest", TestSolver)
48        self.wtest_plugin = coopr.opt.WriterRegistration("wtest", TestWriter)
49        self.rtest_plugin = coopr.opt.ReaderRegistration("rtest", TestReader)
50        unittest.TestCase.run(self,result)
51        self.stest_plugin.deactivate()
52        self.wtest_plugin.deactivate()
53        self.rtest_plugin.deactivate()
54
55    def setUp(self):
56        pyutilib.services.TempfileManager.tempdir = currdir
57
58    def tearDown(self):
59        pyutilib.services.TempfileManager.clear_tempfiles()
60
61    def test_solver_factory(self):
62        """
63        Testing the coopr.opt solver factory
64        """
65        ans = coopr.opt.SolverFactory()
66        #self.failUnlessEqual(len(ans),8)
67        ans.sort()
68        tmp = ["stest"]
69        tmp.sort()
70        self.failUnless(set(tmp) <= set(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("stest")
79        self.failUnlessEqual(type(ans), TestSolver)
80        ans = coopr.opt.SolverFactory("stest", name="mymock")
81        self.failUnlessEqual(type(ans), TestSolver)
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("stest")
90        self.failUnlessEqual(service.type(), "stest")
91
92    def test_writer_factory(self):
93        """
94        Testing the coopr.opt writer factory
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.