def test_poll_cache(self, _poll): # only gets called once in interval j = Job('http://halob:8080/job/foo/', 'foo', self.J, poll_cache_timeout=1) for i in range(2): j.poll() self.assertEquals(_poll.call_count, 1) j.poll(True) # test force poll self.assertEquals(_poll.call_count, 2) # ensure it gets called again after cache timeout _poll.reset_mock() time.sleep(1.1) j.poll() self.assertTrue(_poll.called) # ensure it is disabled by default _poll.reset_mock() for i in range(2): self.j.poll() self.assertEquals(_poll.call_count, 2) self.assertIsNone(self.j.poll_cache_expires)
class TestJob(unittest.TestCase): JOB_DATA = { "actions": [], "description": "test job", "displayName": "foo", "displayNameOrNull": None, "name": "foo", "url": "http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetData) def setUp(self): self.J = mock.MagicMock() # Jenkins object self.j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetData) def test_wrong_field__build_id_for_type(self): with self.assertRaises(AssertionError): self.j._buildid_for_type('wrong') @mock.patch.object(JenkinsBase, 'get_data', fakeGetData) def test_get_last_good_buildnumber(self): ret = self.j.get_last_good_buildnumber() self.assertTrue(ret, 3) @mock.patch.object(JenkinsBase, 'get_data', fakeGetData) def test_get_last_failed_buildnumber(self): with self.assertRaises(NoBuildData): self.j.get_last_failed_buildnumber() @mock.patch.object(JenkinsBase, 'get_data', fakeGetData) def test_get_last_buildnumber(self): ret = self.j.get_last_buildnumber() self.assertEquals(ret, 4) @mock.patch.object(JenkinsBase, 'get_data', fakeGetData) def test_get_last_completed_buildnumber(self): ret = self.j.get_last_completed_buildnumber() self.assertEquals(ret, 3) def test_get_build_dict(self): ret = self.j.get_build_dict() self.assertTrue(isinstance(ret, dict)) self.assertEquals(len(ret), 4) @mock.patch.object(Job, '_poll') def test_nobuilds_get_build_dict(self, _poll): # Bare minimum build dict, we only testing dissapearance of 'builds' _poll.return_value = {"name": "foo"} j = Job('http://*****:*****@mock.patch.object(Job, '_poll') def test_nobuilds_get_revision_dict(self, _poll): # Bare minimum build dict, we only testing dissapearance of 'builds' _poll.return_value = {"name": "foo"} j = Job('http://*****:*****@mock.patch.object(Job, '_poll') def test_nobuilds_get_last_build(self, _poll): # Bare minimum build dict, we only testing dissapearance of 'builds' _poll.return_value = {"name": "foo"} j = Job('http://*****:*****@mock.patch.object(Job, '_poll') def test_poll_cache(self, _poll): # only gets called once in interval j = Job('http://halob:8080/job/foo/', 'foo', self.J, poll_cache_timeout=1) for i in range(2): j.poll() self.assertEquals(_poll.call_count, 1) j.poll(True) # test force poll self.assertEquals(_poll.call_count, 2) # ensure it gets called again after cache timeout _poll.reset_mock() time.sleep(1.1) j.poll() self.assertTrue(_poll.called) # ensure it is disabled by default _poll.reset_mock() for i in range(2): self.j.poll() self.assertEquals(_poll.call_count, 2) self.assertIsNone(self.j.poll_cache_expires)