def setUp(self): import tempfile self.root_dir = tempfile.mkdtemp() self.factory = JobFactory(root_dir=self.root_dir, submit_url='http://localhost:9998/job', ) # fill user db transaction.begin() engine = create_engine('sqlite:///:memory:') mu.DBSession.configure(bind=engine) mu.Base.metadata.create_all(engine) # @UndefinedVariable mu.DBSession().add(mu.User(u'bob', u'Bob', u'*****@*****.**')) jobid = uuid.UUID('3ad25048-26f6-11e1-851e-00012e260790') mu.DBSession().add(mu.JobMeta(jobid, owner=u'bob'))
def setUp(self): self.command = MagmaCommand() init_user_db() self.command.job_factory = JobFactory('/dev/null')
class JobFactoryTestCase(unittest.TestCase): def setUp(self): import tempfile self.root_dir = tempfile.mkdtemp() self.factory = JobFactory(root_dir=self.root_dir, submit_url='http://localhost:9998/job', ) # fill user db transaction.begin() engine = create_engine('sqlite:///:memory:') mu.DBSession.configure(bind=engine) mu.Base.metadata.create_all(engine) # @UndefinedVariable mu.DBSession().add(mu.User(u'bob', u'Bob', u'*****@*****.**')) jobid = uuid.UUID('3ad25048-26f6-11e1-851e-00012e260790') mu.DBSession().add(mu.JobMeta(jobid, owner=u'bob')) def tearDown(self): import shutil shutil.rmtree(self.root_dir) mu.DBSession.remove() def test_hasrootdir(self): self.assertEqual(self.factory.root_dir, self.root_dir) def test_id2url(self): jobid = uuid.UUID('3ad25048-26f6-11e1-851e-00012e260790') jobdbfn = 'sqlite:///' jobdbfn += os.path.join(self.root_dir, str(jobid), 'results.db') self.assertEqual(self.factory.id2url(jobid), jobdbfn) def test_id2jobdir(self): jobid = uuid.UUID('3ad25048-26f6-11e1-851e-00012e260790') jobdir = os.path.join(self.root_dir, str(jobid)) self.assertEqual(self.factory.id2jobdir(jobid), jobdir) def test_id2db(self): jobid = uuid.UUID('3ad25048-26f6-11e1-851e-00012e260790') jobdbfn = os.path.join(self.root_dir, str(jobid), 'results.db') self.assertEqual(self.factory.id2db(jobid), jobdbfn) def test_fromdb(self): # mock/stub private methods which do external calls self.factory._makeJobDir = Mock(return_value='/mydir') self.factory._copyFile = Mock() self.factory._makeJobSession = Mock(return_value=initTestingDB()) dbfile = os.tmpfile() job = self.factory.fromDb(dbfile, u'bob') self.assertIsInstance(job.id, uuid.UUID) self.assertEqual(job.dir, u'/mydir') self.assertEqual(job.meta.owner, u'bob') self.assertEqual(job.meta.description, u'My first description') self.assertEqual(job.meta.ms_filename, u'F123456.mzxml') self.assertEqual(job.meta.state, u'STOPPED') self.factory._makeJobDir.assert_called_with(job.id) self.factory._copyFile.assert_called_with(dbfile, job.id) o = mu.DBSession().query(mu.JobMeta.owner ).filter(mu.JobMeta.jobid == job.id).scalar() self.assertEqual(o, u'bob', 'job meta has been inserted') def test_fromid(self): jobid = uuid.UUID('3ad25048-26f6-11e1-851e-00012e260790') self.factory._makeJobSession = Mock(return_value=456) self.factory.id2jobdir = Mock(return_value=789) job = self.factory.fromId(jobid) self.assertEqual(job.owner, u'bob') self.assertEqual(job.db.session, 456) self.assertEqual(job.dir, 789) self.factory._makeJobSession.assert_called_with(jobid) self.factory.id2jobdir.assert_called_with(jobid) def test_fromid_notfoundindb(self): jobid = uuid.UUID('11111111-1111-1111-1111-111111111111') self.factory._makeJobSession = Mock() self.factory.id2jobdir = Mock() with self.assertRaises(JobNotFound) as exc: self.factory.fromId(jobid) self.assertEqual(exc.exception.jobid, jobid) self.assertEqual(exc.exception.message, "Job not found in database") def test_fromid_notfoundasdb(self): jobid = uuid.UUID('11111111-1111-1111-1111-111111111111') self.factory._getJobMeta = Mock(mu.JobMeta) with self.assertRaises(JobNotFound) as exc: self.factory.fromId(jobid) self.assertEqual(exc.exception.jobid, jobid) self.assertEqual(exc.exception.message, "Data of job not found") def test_submitQuery(self): self.factory.init_script = "# make magma available" job = self.factory.fromScratch(u'bob') self.factory.script_fn = 'script.sh' cmd = "magma add_structures -t smiles structures.dat results.db\n" jobquery = JobQuery(job.dir, cmd, ['structures.dat']) status_cb_url = 'http://example.com/status/{}.json'.format(job.id) jobquery.status_callback_url = status_cb_url launcher_url = 'http://*****:*****@patch('requests.post') def test_submitJob2Launcher(self, ua): from requests import Response create_url = 'http://*****:*****@patch('requests.delete') def test_cancel(self, ua): job = self.factory.fromScratch(u'ed') url = 'http://*****:*****@patch('os.stat') def test_dbSize(self, stat): class Mystat(object): st_size = 1234 mystat = Mystat() stat.return_value = mystat jobid = uuid.UUID('11111111-1111-1111-1111-111111111111') result = self.factory.dbSize(jobid) self.assertEqual(result, 1234) @patch('os.stat') def test_dbSize_noresultdbfile(self, stat): stat.side_effect = OSError() jobid = uuid.UUID('11111111-1111-1111-1111-111111111111') result = self.factory.dbSize(jobid) self.assertEqual(result, 0)