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