Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.
| File tgtest.patch,
11.0 KB
(added by kskuhlman, 4 years ago) |
|
Almost complete..
|
-
|
|
|
|
| 155 | 155 | |
| 156 | 156 | |
| 157 | 157 | |
| 158 | | class TestIdentity(testutil.TGWebTest): |
| | 158 | class TestIdentity(testutil.TGTest): |
| 159 | 159 | |
| 160 | 160 | def setUp(self): |
| 161 | 161 | # identity requires visit and a failure_url |
| … |
… |
|
| 169 | 169 | self._original_config = original_config |
| 170 | 170 | config.configure_loggers(test_config) |
| 171 | 171 | config.update(test_config['global']) |
| 172 | | testutil.mount(IdentityRoot()) |
| 173 | | testutil.TGWebTest.setUp(self) |
| | 172 | self.root = IdentityRoot |
| | 173 | testutil.TGTest.setUp(self) |
| 174 | 174 | self.init_model() |
| 175 | 175 | |
| 176 | 176 | def tearDown(self): |
| 177 | | testutil.TGWebTest.tearDown(self) |
| | 177 | testutil.TGTest.tearDown(self) |
| 178 | 178 | config.update(self._original_config) |
| 179 | 179 | |
| 180 | 180 | def init_model(self): |
-
|
|
|
|
| 242 | 242 | return "redirected OK" |
| 243 | 243 | |
| 244 | 244 | |
| 245 | | class TestRoot(testutil.TGWebTest): |
| | 245 | class TestRoot(testutil.TGTest): |
| 246 | 246 | |
| 247 | 247 | def setUp(self): |
| 248 | 248 | testutil.mount(MyRoot(), '/') |
| 249 | 249 | testutil.mount(SubApp(), '/subthing') |
| | 250 | self.app = testutil.make_app() |
| 250 | 251 | super(TestRoot, self).setUp() |
| 251 | 252 | |
| 252 | 253 | |
| … |
… |
|
| 565 | 566 | assert 'handling_key' in response |
| 566 | 567 | |
| 567 | 568 | |
| 568 | | class TestURLs(testutil.TGWebTest): |
| | 569 | class TestURLs(testutil.TGTest): |
| 569 | 570 | |
| 570 | 571 | def setUp(self): |
| 571 | 572 | testutil.mount(MyRoot(), '/') |
| 572 | 573 | testutil.mount(SubApp(), '/subthing') |
| 573 | 574 | testutil.mount(SubApp(), '/subthing/subsubthing') |
| | 575 | self.app = testutil.make_app() |
| 574 | 576 | super(TestURLs, self).setUp() |
| 575 | 577 | |
| 576 | 578 | def tearDown(self): |
-
|
|
|
|
| 3 | 3 | import cherrypy |
| 4 | 4 | import unittest |
| 5 | 5 | |
| 6 | | class TestView(testutil.TGWebTest): |
| | 6 | class TestView(unittest.TestCase): |
| 7 | 7 | |
| | 8 | def setUp(self): |
| | 9 | # The server needs to be started so that the template engines get loaded. |
| | 10 | testutil.start_server() |
| | 11 | |
| 8 | 12 | def test_cycle(self): |
| 9 | 13 | oe = view.base.cycle(('odd','even')) |
| 10 | 14 | assert str(oe) == str(None) |
-
|
|
|
|
| 186 | 186 | return result |
| 187 | 187 | |
| 188 | 188 | |
| 189 | | class TestSpy(testutil.TGWebTest): |
| | 189 | class TestPagination(testutil.TGTest): |
| | 190 | """Base class for all Paginate TestCases""" |
| | 191 | |
| | 192 | def setUp(self): |
| | 193 | self.root = self.MyRoot |
| | 194 | super(TestPagination, self).setUp() |
| | 195 | |
| | 196 | def request(self, url, status=200): |
| | 197 | response = self.app.get(url, status=status) |
| | 198 | self.body = response.body |
| | 199 | if "fail: " in self.body: |
| | 200 | print self.body |
| | 201 | assert False, "Spy alert! Check body output for details..." |
| | 202 | |
| | 203 | |
| | 204 | class TestSpy(TestPagination): |
| 190 | 205 | """Never trust a spy""" |
| 191 | 206 | |
| 192 | 207 | class MyRoot(RootController): |
| … |
… |
|
| 219 | 234 | spy = Spy(foobar=10) |
| 220 | 235 | return dict(data=data, spy=spy) |
| 221 | 236 | |
| 222 | | |
| 223 | | def setUp(self): |
| 224 | | super(TestSpy, self).setUp(self.MyRoot) |
| 225 | | |
| 226 | | |
| 227 | 237 | def test_spy(self): |
| 228 | 238 | response = self.app.get('/spy') |
| 229 | 239 | Spy.assert_ok(response.body, 'current_page', 1) |
| … |
… |
|
| 251 | 261 | Spy.assert_ok(response.body, 'var_name', "'data'", raw=True) |
| 252 | 262 | |
| 253 | 263 | |
| 254 | | class TestPagination(testutil.TGWebTest): |
| 255 | | """Base class for all Paginate TestCases""" |
| 256 | | |
| 257 | | def setUp(self): |
| 258 | | super(TestPagination, self).setUp(self.MyRoot) |
| 259 | | |
| 260 | | def request(self, url, status=200): |
| 261 | | response = self.app.get(url, status=status) |
| 262 | | self.body = response.body |
| 263 | | if "fail: " in self.body: |
| 264 | | print self.body |
| 265 | | assert False, "Spy alert! Check body output for details..." |
| 266 | | |
| 267 | | |
| 268 | 264 | class TestBasicPagination(TestPagination): |
| 269 | 265 | |
| 270 | 266 | class MyRoot(RootController): |
-
|
|
|
|
| 17 | 17 | from ${package}.model import YourModelClass, User, ... |
| 18 | 18 | """ |
| 19 | 19 | |
| 20 | | from turbogears import testutil, database |
| | 20 | from turbogears.testutil import DBTest |
| 21 | 21 | |
| 22 | 22 | # from ${package}.model import YourModelClass, User |
| 23 | 23 | |
| 24 | | # class TestUser(testutil.DBTest): |
| 25 | | # def get_model(self): |
| 26 | | # return User |
| | 24 | # class TestUser(DBTest): |
| | 25 | # model = User |
| 27 | 26 | # |
| 28 | 27 | # def test_creation(self): |
| 29 | 28 | # """Object creation should set the name.""" |
-
|
|
|
|
| 4 | 4 | from ${package}.controllers import Root |
| 5 | 5 | import cherrypy |
| 6 | 6 | |
| 7 | | testutil.mount(root = Root()) |
| | 7 | class TestPages(testutil.TGTest): |
| 8 | 8 | |
| 9 | | class TestPages(unittest.TestCase): |
| | 9 | root = Root |
| 10 | 10 | |
| 11 | | def setUp(self): |
| 12 | | testutil.start_server() |
| 13 | | |
| 14 | | def tearDown(self): |
| 15 | | """Tests for apps using identity need to stop CP/TG after each test to |
| 16 | | stop the VisitManager thread. |
| 17 | | See http://trac.turbogears.org/turbogears/ticket/1217 for details. |
| 18 | | """ |
| 19 | | testutil.stop_server() |
| 20 | | |
| 21 | 11 | def test_method(self): |
| 22 | 12 | "the index method should return a string called now" |
| 23 | 13 | import types |
| 24 | | response = testutil.go("/") |
| | 14 | response = self.app.get("/") |
| 25 | 15 | assert type(response.raw["now"]) == types.StringType |
| 26 | 16 | |
| 27 | 17 | def test_indextitle(self): |
| 28 | 18 | "The indexpage should have the right title" |
| 29 | | response = testutil.go("/") |
| | 19 | response = self.app.get("/") |
| 30 | 20 | assert "<title>welcome to turbogears</title>" in response.body.lower() |
| 31 | 21 | |
| 32 | 22 | #if $identity != "none" |
| 33 | 23 | def test_logintitle(self): |
| 34 | 24 | "login page should have the right title" |
| 35 | | response = testutil.go("/login") |
| 36 | | assert "<title>login</title>" in response.body.lower() |
| | 25 | response = self.app.get("/login") |
| | 26 | assert "<title>Login</title>" in response, response |
| 37 | 27 | #end if |
-
|
|
|
|
| 27 | 27 | |
| 28 | 28 | install_requires=[ |
| 29 | 29 | "TurboGears >= ${turbogearsversion}", |
| | 30 | "WebTest", |
| 30 | 31 | #if $sqlobject == 'True' |
| 31 | 32 | "$sqlobjectversion" |
| 32 | 33 | #elif $sqlalchemy == 'True' |
-
|
|
|
|
| 157 | 157 | return TestApp(wsgiapp) |
| 158 | 158 | |
| 159 | 159 | |
| 160 | | class TGWebTest(unittest.TestCase): |
| | 160 | class TGTest(unittest.TestCase): |
| 161 | 161 | """A WebTest enabled unit testing class. |
| 162 | 162 | |
| 163 | | This allows testers to subclass us and use self.app to make WebTest calls. |
| | 163 | To use, subclass & set root to your controller object, or set app to a |
| | 164 | webtest.TestApp instance. |
| 164 | 165 | |
| | 166 | In your tests, use self.app to make WebTest calls. |
| 165 | 167 | """ |
| 166 | 168 | |
| 167 | | def setUp(self, controller=None): |
| 168 | | """Set up the WebTest by starting the server." |
| | 169 | root = None |
| | 170 | app = None |
| 169 | 171 | |
| | 172 | def setUp(self): |
| | 173 | """Set up the WebTest by starting the server. |
| | 174 | |
| 170 | 175 | You should override this and make sure you have properly |
| 171 | 176 | mounted a root for your server before calling super, |
| 172 | 177 | or simply pass a root controller to super. |
| 173 | 178 | Otherwise the Cherrypy filters for TurboGears will not be used. |
| 174 | | |
| 175 | 179 | """ |
| 176 | | self.app = make_app(controller) |
| | 180 | assert self.root or self.app, "Either self.root or self.app must be set" |
| | 181 | if not self.app: |
| | 182 | self.app = make_app(self.root) |
| 177 | 183 | start_server() |
| 178 | 184 | |
| 179 | 185 | def tearDown(self): |
| 180 | 186 | """Tear down the WebTest by stopping the server.""" |
| 181 | 187 | stop_server(tg_only = True) |
| 182 | | del self.app |
| 183 | 188 | |
| 184 | 189 | def login_user(self, user): |
| 185 | 190 | """Log a specified user object into the system.""" |
| … |
… |
|
| 281 | 286 | return output, response |
| 282 | 287 | |
| 283 | 288 | |
| 284 | | class DBTest(unittest.TestCase): |
| | 289 | class AbstractDBTest(unittest.TestCase): |
| | 290 | """A database enabled unit testing class. |
| 285 | 291 | |
| | 292 | Creates and destroys your database before and after each unit test. You must set the |
| | 293 | model attribute in order for this class to function correctly. |
| | 294 | """ |
| 286 | 295 | model = None |
| 287 | 296 | |
| | 297 | def setUp(self): |
| | 298 | raise NotImplementedError() |
| | 299 | |
| | 300 | def tearDown(self): |
| | 301 | raise NotImplementedError() |
| | 302 | |
| | 303 | class DBTestSO(AbstractDBTest): |
| 288 | 304 | def _get_soClasses(self): |
| 289 | 305 | try: |
| 290 | 306 | return [self.model.__dict__[x] for x in self.model.soClasses] |
| … |
… |
|
| 295 | 311 | if not self.model: |
| 296 | 312 | self.model = get_model() |
| 297 | 313 | if not self.model: |
| 298 | | raise "Unable to run database tests without a model" |
| 299 | | |
| | 314 | raise Exception("Unable to run database tests without a model") |
| 300 | 315 | for item in self._get_soClasses(): |
| 301 | 316 | if isinstance(item, types.TypeType) and issubclass(item, |
| 302 | 317 | sqlobject.SQLObject) and item != sqlobject.SQLObject \ |
| … |
… |
|
| 311 | 326 | and item != InheritableSQLObject: |
| 312 | 327 | item.dropTable(ifExists=True, cascade=True) |
| 313 | 328 | |
| | 329 | class DBTestSA(AbstractDBTest): |
| | 330 | def setUp(self): |
| | 331 | database.get_engine() |
| | 332 | database.metadata.create_all() |
| | 333 | |
| | 334 | def tearDown(self): |
| | 335 | database.metadata.drop_all() |
| | 336 | |
| | 337 | |
| | 338 | #Determine which class to use for "DBTest". Setup & teardown should behave |
| | 339 | #simularly regardless of which ORM you choose. |
| | 340 | if config.get("sqlobject.dburi"): |
| | 341 | DBTest = DBTestSO |
| | 342 | elif config.get("sqlalchemy.dburi"): |
| | 343 | DBTest = DBTestSA |
| | 344 | else: |
| | 345 | raise Exception("Unable to find sqlalchemy or sqlobject dburi") |
| | 346 | |
| | 347 | |
| 314 | 348 | def unmount(): |
| 315 | 349 | """Remove an application from the object traversal tree.""" |
| 316 | 350 | # There's no clean way to remove a subtree under CP2, so the only use case |
-
|
|
|
|
| 240 | 240 | sqlalchemyversion = str(get_requirement('sqlalchemy')) |
| 241 | 241 | cmd_args.append("sqlalchemyversion=%s" % sqlalchemyversion) |
| 242 | 242 | if self.elixir: |
| 243 | | elixirversion = str(get_requirement('future', 'elixir')) |
| | 243 | elixirversion = str(get_requirement('sqlalchemy')) |
| 244 | 244 | cmd_args.append("elixirversion=%s" % elixirversion) |
| 245 | 245 | |
| 246 | 246 | command.run(cmd_args) |
Download in other formats: