|
1 | 1 | import json
|
2 |
| -import unittest |
3 |
| -from unittest.mock import MagicMock |
| 2 | + |
| 3 | +from pytest_mock import MockerFixture |
4 | 4 |
|
5 | 5 | from graphdatascience.arrow_client.v2.api_types import JobIdConfig, JobStatus
|
6 | 6 | from graphdatascience.arrow_client.v2.job_client import JobClient
|
7 | 7 | from graphdatascience.tests.unit.arrow_client.arrow_test_utils import ArrowTestResult
|
8 | 8 |
|
9 | 9 |
|
10 |
| -class TestJobClient(unittest.TestCase): |
11 |
| - def setUp(self) -> None: |
12 |
| - self.mock_client = MagicMock() |
| 10 | +def test_run_job(mocker: MockerFixture) -> None: |
| 11 | + mock_client = mocker.Mock() |
| 12 | + job_id = "test-job-123" |
| 13 | + endpoint = "v2/test.endpoint" |
| 14 | + config = {"param1": "value1", "param2": 42} |
| 15 | + |
| 16 | + mock_client.do_action_with_retry.return_value = iter([ArrowTestResult({"jobId": job_id})]) |
| 17 | + |
| 18 | + result = JobClient.run_job(mock_client, endpoint, config) |
| 19 | + |
| 20 | + expected_config = json.dumps(config).encode("utf-8") |
| 21 | + mock_client.do_action_with_retry.assert_called_once_with(endpoint, expected_config) |
| 22 | + assert result == job_id |
13 | 23 |
|
14 |
| - def test_run_job(self) -> None: |
15 |
| - job_id = "test-job-123" |
16 |
| - endpoint = "v2/test.endpoint" |
17 |
| - config = {"param1": "value1", "param2": 42} |
18 | 24 |
|
19 |
| - self.mock_client.do_action_with_retry.return_value = iter([ArrowTestResult({"jobId": job_id})]) |
| 25 | +def test_run_job_and_wait(mocker: MockerFixture) -> None: |
| 26 | + mock_client = mocker.Mock() |
| 27 | + job_id = "test-job-456" |
| 28 | + endpoint = "v2/test.endpoint" |
| 29 | + config = {"param": "value"} |
20 | 30 |
|
21 |
| - result = JobClient.run_job(self.mock_client, endpoint, config) |
| 31 | + job_id_config = JobIdConfig(jobId=job_id) |
22 | 32 |
|
23 |
| - expected_config = json.dumps(config).encode("utf-8") |
24 |
| - self.mock_client.do_action_with_retry.assert_called_once_with(endpoint, expected_config) |
25 |
| - self.assertEqual(result, job_id) |
| 33 | + status = JobStatus( |
| 34 | + jobId=job_id, |
| 35 | + progress=1.0, |
| 36 | + status="Done", |
| 37 | + ) |
26 | 38 |
|
27 |
| - def test_run_job_and_wait( |
28 |
| - self, |
29 |
| - ) -> None: |
30 |
| - job_id = "test-job-456" |
31 |
| - endpoint = "v2/test.endpoint" |
32 |
| - config = {"param": "value"} |
| 39 | + do_action_with_retry = mocker.Mock() |
| 40 | + do_action_with_retry.side_effect = [ |
| 41 | + iter([ArrowTestResult(job_id_config.dump_camel())]), |
| 42 | + iter([ArrowTestResult(status.dump_camel())]), |
| 43 | + ] |
33 | 44 |
|
34 |
| - job_id_config = JobIdConfig(jobId=job_id) |
| 45 | + mock_client.do_action_with_retry = do_action_with_retry |
35 | 46 |
|
36 |
| - status = JobStatus( |
37 |
| - jobId=job_id, |
38 |
| - progress=1.0, |
39 |
| - status="Done", |
40 |
| - ) |
| 47 | + result = JobClient.run_job_and_wait(mock_client, endpoint, config) |
41 | 48 |
|
42 |
| - do_action_with_retry = MagicMock() |
43 |
| - do_action_with_retry.side_effect = [ |
44 |
| - iter([ArrowTestResult(job_id_config.dump_camel())]), |
45 |
| - iter([ArrowTestResult(status.dump_camel())]), |
46 |
| - ] |
| 49 | + do_action_with_retry.assert_called_with("v2/jobs.status", job_id_config.dump_json().encode("utf-8")) |
| 50 | + assert result == job_id |
47 | 51 |
|
48 |
| - self.mock_client.do_action_with_retry = do_action_with_retry |
49 | 52 |
|
50 |
| - result = JobClient.run_job_and_wait(self.mock_client, endpoint, config) |
| 53 | +def test_wait_for_job_completes_immediately(mocker: MockerFixture) -> None: |
| 54 | + mock_client = mocker.Mock() |
| 55 | + job_id = "test-job-789" |
51 | 56 |
|
52 |
| - do_action_with_retry.assert_called_with("v2/jobs.status", job_id_config.dump_json().encode("utf-8")) |
53 |
| - self.assertEqual(result, job_id) |
| 57 | + status = JobStatus( |
| 58 | + jobId=job_id, |
| 59 | + progress=1.0, |
| 60 | + status="Done", |
| 61 | + ) |
54 | 62 |
|
55 |
| - def test_wait_for_job_completes_immediately(self) -> None: |
56 |
| - job_id = "test-job-789" |
| 63 | + mock_client.do_action_with_retry.return_value = iter([ArrowTestResult(status.dump_camel())]) |
57 | 64 |
|
58 |
| - status = JobStatus( |
59 |
| - jobId=job_id, |
60 |
| - progress=1.0, |
61 |
| - status="Done", |
62 |
| - ) |
| 65 | + JobClient.wait_for_job(mock_client, job_id) |
63 | 66 |
|
64 |
| - self.mock_client.do_action_with_retry.return_value = iter([ArrowTestResult(status.dump_camel())]) |
| 67 | + mock_client.do_action_with_retry.assert_called_once_with( |
| 68 | + "v2/jobs.status", JobIdConfig(jobId=job_id).dump_json().encode("utf-8") |
| 69 | + ) |
65 | 70 |
|
66 |
| - JobClient.wait_for_job(self.mock_client, job_id) |
67 | 71 |
|
68 |
| - self.mock_client.do_action_with_retry.assert_called_once_with( |
69 |
| - "v2/jobs.status", JobIdConfig(jobId=job_id).dump_json().encode("utf-8") |
70 |
| - ) |
| 72 | +def test_wait_for_job_waits_for_completion(mocker: MockerFixture) -> None: |
| 73 | + mock_client = mocker.Mock() |
| 74 | + job_id = "test-job-waiting" |
| 75 | + status_running = JobStatus( |
| 76 | + jobId=job_id, |
| 77 | + progress=0.5, |
| 78 | + status="RUNNING", |
| 79 | + ) |
| 80 | + status_done = JobStatus( |
| 81 | + jobId=job_id, |
| 82 | + progress=1.0, |
| 83 | + status="Done", |
| 84 | + ) |
71 | 85 |
|
72 |
| - def test_wait_for_job_waits_for_completion(self) -> None: |
73 |
| - job_id = "test-job-waiting" |
74 |
| - status_running = JobStatus( |
75 |
| - jobId=job_id, |
76 |
| - progress=0.5, |
77 |
| - status="RUNNING", |
78 |
| - ) |
79 |
| - status_done = JobStatus( |
80 |
| - jobId=job_id, |
81 |
| - progress=1.0, |
82 |
| - status="Done", |
83 |
| - ) |
| 86 | + do_action_with_retry = mocker.Mock() |
| 87 | + do_action_with_retry.side_effect = [ |
| 88 | + iter([ArrowTestResult(status_running.dump_camel())]), |
| 89 | + iter([ArrowTestResult(status_done.dump_camel())]), |
| 90 | + ] |
84 | 91 |
|
85 |
| - do_action_with_retry = MagicMock() |
86 |
| - do_action_with_retry.side_effect = [ |
87 |
| - iter([ArrowTestResult(status_running.dump_camel())]), |
88 |
| - iter([ArrowTestResult(status_done.dump_camel())]), |
89 |
| - ] |
| 92 | + mock_client.do_action_with_retry = do_action_with_retry |
90 | 93 |
|
91 |
| - self.mock_client.do_action_with_retry = do_action_with_retry |
| 94 | + JobClient.wait_for_job(mock_client, job_id) |
92 | 95 |
|
93 |
| - JobClient.wait_for_job(self.mock_client, job_id) |
| 96 | + assert mock_client.do_action_with_retry.call_count == 2 |
94 | 97 |
|
95 |
| - self.assertEqual(self.mock_client.do_action_with_retry.call_count, 2) |
96 | 98 |
|
97 |
| - def test_get_summary(self) -> None: |
98 |
| - # Setup |
99 |
| - job_id = "summary-job-123" |
100 |
| - expected_summary = {"nodeCount": 100, "relationshipCount": 200, "requiredMemory": "1GB"} |
| 99 | +def test_get_summary(mocker: MockerFixture) -> None: |
| 100 | + mock_client = mocker.Mock() |
| 101 | + # Setup |
| 102 | + job_id = "summary-job-123" |
| 103 | + expected_summary = {"nodeCount": 100, "relationshipCount": 200, "requiredMemory": "1GB"} |
101 | 104 |
|
102 |
| - self.mock_client.do_action_with_retry.return_value = iter([ArrowTestResult(expected_summary)]) |
| 105 | + mock_client.do_action_with_retry.return_value = iter([ArrowTestResult(expected_summary)]) |
103 | 106 |
|
104 |
| - result = JobClient.get_summary(self.mock_client, job_id) |
| 107 | + result = JobClient.get_summary(mock_client, job_id) |
105 | 108 |
|
106 |
| - self.mock_client.do_action_with_retry.assert_called_once_with( |
107 |
| - "v2/results.summary", JobIdConfig(jobId=job_id).dump_json().encode("utf-8") |
108 |
| - ) |
109 |
| - self.assertEqual(result, expected_summary) |
| 109 | + mock_client.do_action_with_retry.assert_called_once_with( |
| 110 | + "v2/results.summary", JobIdConfig(jobId=job_id).dump_json().encode("utf-8") |
| 111 | + ) |
| 112 | + assert result == expected_summary |
0 commit comments