Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Renrut5 committed Aug 15, 2024
1 parent 3a69f5c commit b78c7b6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pynautobot/core/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,11 +713,13 @@ def run_and_wait(self, *args, api_version=None, interval=5, max_rechecks=50, **k
Examples:
To run a job for verifying hostnames:
>>> nb.extras.jobs.run(
>>> nb.extras.jobs.run_and_wait(
class_path="local/data_quality/VerifyHostnames",
data={"hostname_regex": ".*"},
commit=True,
interval=5,
max_rechecks=10,
)
"""
if max_rechecks <= 0:
raise ValueError("Attribute `max_rechecks` must be a postive integer to prevent recursive loops.")
Expand Down Expand Up @@ -749,7 +751,10 @@ def run_and_wait(self, *args, api_version=None, interval=5, max_rechecks=50, **k
api_version=api_version,
).get()

status = req.get("status", {}).get("value")
result = req.get("job_result", {})
status = result.get("status", {}).get("value")

if status not in active_job_statuses:
return req
return response_loader(req, self.return_obj, self)

raise ValueError("Did not receieve completed job result for job.")
36 changes: 36 additions & 0 deletions tests/unit/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,39 @@ def test_run_greater_v1_3(self):
test_obj = JobsEndpoint(api, app, "test")
test = test_obj.run(job_id="test")
self.assertEqual(len(test), 1)

# Run and Wait Tests
# =================================================

@patch("pynautobot.core.query.Request.get", return_value=Mock())
@patch("pynautobot.core.query.Request.post", return_value=Mock())
def test_run_and_wait_less_v1_3(self, mock_post, mock_get):
api = Mock(base_url="http://localhost:8000/api", api_version="1.2")
app = Mock(name="test")
test_obj = JobsEndpoint(api, app, "test")
mock_post.return_value = {"schedule": {"id": 123}, "job_result": {"id": 123, "status": {"value": "PENDING"}}}
mock_get.return_value = {"schedule": {"id": 123}, "job_result": {"id": 123, "status": {"value": "SUCCESS"}}}
test = test_obj.run_and_wait(class_path="test", interval=1, max_rechecks=5)
self.assertEqual(test.job_result.status.value, "SUCCESS")

@patch("pynautobot.core.query.Request.get", return_value=Mock())
@patch("pynautobot.core.query.Request.post", return_value=Mock())
def test_run_and_wait_greater_v1_3(self, mock_post, mock_get):
api = Mock(base_url="http://localhost:8000/api", api_version="1.3")
app = Mock(name="test")
test_obj = JobsEndpoint(api, app, "test")
mock_post.return_value = {"schedule": {"id": 123}, "job_result": {"id": 123, "status": {"value": "PENDING"}}}
mock_get.return_value = {"schedule": {"id": 123}, "job_result": {"id": 123, "status": {"value": "SUCCESS"}}}
test = test_obj.run_and_wait(job_id="test", interval=1, max_rechecks=5)
self.assertEqual(test.job_result.status.value, "SUCCESS")

@patch("pynautobot.core.query.Request.get", return_value=Mock())
@patch("pynautobot.core.query.Request.post", return_value=Mock())
def test_run_and_wait_no_complete(self, mock_post, mock_get):
api = Mock(base_url="http://localhost:8000/api", api_version="1.3")
app = Mock(name="test")
test_obj = JobsEndpoint(api, app, "test")
mock_post.return_value = {"schedule": {"id": 123}, "job_result": {"id": 123, "status": {"value": "PENDING"}}}
mock_get.return_value = {"schedule": {"id": 123}, "job_result": {"id": 123, "status": {"value": "PENDING"}}}
with self.assertRaises(ValueError):
test = test_obj.run_and_wait(job_id="test", interval=1, max_rechecks=2)

0 comments on commit b78c7b6

Please sign in to comment.