|
| 1 | +import unittest |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +from codeocean.capsule import Capsules, GitSyncResults |
| 5 | +from codeocean.pipeline import Pipelines |
| 6 | + |
| 7 | + |
| 8 | +class TestGitSync(unittest.TestCase): |
| 9 | + """Test cases for capsule and pipeline Git sync.""" |
| 10 | + |
| 11 | + def _mock_session(self, body): |
| 12 | + """Build a mock session whose post() returns a response with the given JSON body.""" |
| 13 | + session = MagicMock() |
| 14 | + response = MagicMock() |
| 15 | + response.json.return_value = body |
| 16 | + session.post.return_value = response |
| 17 | + return session |
| 18 | + |
| 19 | + def test_sync_capsule_returns_results(self): |
| 20 | + """sync_capsule posts to the capsule sync route and parses the results.""" |
| 21 | + session = self._mock_session({"pushed": 2, "pulled": 1, "new_branch": True}) |
| 22 | + capsules = Capsules(client=session) |
| 23 | + |
| 24 | + result = capsules.sync_capsule("cap-123") |
| 25 | + |
| 26 | + session.post.assert_called_once_with("capsules/cap-123/sync") |
| 27 | + self.assertEqual(result, GitSyncResults(pushed=2, pulled=1, new_branch=True)) |
| 28 | + |
| 29 | + def test_sync_capsule_empty_body_defaults(self): |
| 30 | + """An empty body (all fields omitempty on the server) deserializes to zero defaults.""" |
| 31 | + session = self._mock_session({}) |
| 32 | + capsules = Capsules(client=session) |
| 33 | + |
| 34 | + result = capsules.sync_capsule("cap-123") |
| 35 | + |
| 36 | + self.assertEqual(result, GitSyncResults(pushed=0, pulled=0, new_branch=False)) |
| 37 | + |
| 38 | + def test_sync_pipeline_returns_results(self): |
| 39 | + """sync_pipeline posts to the pipeline sync route and parses the results.""" |
| 40 | + session = self._mock_session({"pushed": 0, "pulled": 3, "new_branch": False}) |
| 41 | + pipelines = Pipelines(client=session) |
| 42 | + |
| 43 | + result = pipelines.sync_pipeline("pipe-456") |
| 44 | + |
| 45 | + session.post.assert_called_once_with("pipelines/pipe-456/sync") |
| 46 | + self.assertEqual(result, GitSyncResults(pushed=0, pulled=3, new_branch=False)) |
0 commit comments