import unittest

class TestJobs(unittest.TestCase):
    def test_CustomJobPolymorphic(self):
        '''
        Tests that a Custom job can be created in the db and retrieved from
        there as a CustomJob that inherits from Job.
        '''
        from clientjob.model import Job
        import web
        class CustomJob(Job):
            pass

        c = CustomJob()
        web.ctx.orm.add(c)
        web.ctx.orm.commit()
        query = web.ctx.orm.query(Job).get(c.id)
        self.assertEqual(type(query.__class__), type(CustomJob))
        web.ctx.orm.delete(c)
        web.ctx.orm.commit()

    def test_NonImportedJobPolymorphic(self):
        '''
        Tests that a job object can be created in the db and retrieved from
        there even if it is not imported in current module.
        '''
        from clientjob.model import Job
        import web

        def first_func():
            class CustomJob(Job):
                def f(self):
                    return True
            c = CustomJob()
            web.ctx.orm.add(c)
            web.ctx.orm.commit()
            return c.id

        c_id = first_func()
        query = web.ctx.orm.query(Job).get(c_id)
        self.assertEqual(query.__class__.__name__, 'CustomJob')
        self.assertEqual(query.f(), True)
        web.ctx.orm.delete(query)
        web.ctx.orm.commit()

def suite():
   suite = unittest.TestSuite()
   suite.addTest(unittest.makeSuite(TestJobs))
   return suite
