|
| 1 | +# Databricks CLI |
| 2 | +# Copyright 2022 Databricks, Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"), except |
| 5 | +# that the use of services to which certain application programming |
| 6 | +# interfaces (each, an "API") connect requires that the user first obtain |
| 7 | +# a license for the use of the APIs from Databricks, Inc. ("Databricks"), |
| 8 | +# by creating an account at www.databricks.com and agreeing to either (a) |
| 9 | +# the Community Edition Terms of Service, (b) the Databricks Terms of |
| 10 | +# Service, or (c) another written agreement between Licensee and Databricks |
| 11 | +# for the use of the APIs. |
| 12 | +# |
| 13 | +# You may not use this file except in compliance with the License. |
| 14 | +# You may obtain a copy of the License at |
| 15 | +# |
| 16 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +# |
| 18 | +# Unless required by applicable law or agreed to in writing, software |
| 19 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 20 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | +# See the License for the specific language governing permissions and |
| 22 | +# limitations under the License. |
| 23 | +import pytest |
| 24 | +import mock |
| 25 | + |
| 26 | +from databricks_cli.sdk.service import ReposService |
| 27 | +from tests.utils import provide_conf |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture() |
| 31 | +def repos_service(): |
| 32 | + with mock.patch('databricks_cli.sdk.ApiClient') as api_client: |
| 33 | + yield ReposService(api_client) |
| 34 | + |
| 35 | + |
| 36 | +def test_provider_inference(): |
| 37 | + for url, provider in { |
| 38 | + "https://github.com/org/repo.git": "gitHub", |
| 39 | + "https://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo": "awsCodeCommit", |
| 40 | + "https://github1.com/org/repo.git": None, |
| 41 | + }.items(): |
| 42 | + assert provider == ReposService.detect_repo_provider(url) |
| 43 | + |
| 44 | + |
| 45 | +@provide_conf |
| 46 | +def test_list(repos_service): |
| 47 | + repos_service.list_repos() |
| 48 | + repos_service.client.perform_query.assert_called_with('GET', '/repos', data={}, headers=None) |
| 49 | + |
| 50 | + repos_service.list_repos(path_prefix='foo') |
| 51 | + repos_service.client.perform_query.assert_called_with('GET', '/repos', data={'path_prefix': 'foo'}, headers=None) |
| 52 | + |
| 53 | + repos_service.list_repos(next_page_token='token') |
| 54 | + repos_service.client.perform_query.assert_called_with('GET', '/repos', data={'next_page_token': 'token'}, headers=None) |
| 55 | + |
| 56 | + |
| 57 | +@provide_conf |
| 58 | +def test_create(repos_service): |
| 59 | + url = 'https://github.com/foo/bar.git' |
| 60 | + |
| 61 | + repos_service.create_repo(url, None) |
| 62 | + repos_service.client.perform_query.assert_called_with('POST', '/repos', data={'url': url, 'provider': 'gitHub'}, headers=None) |
| 63 | + |
| 64 | + repos_service.create_repo(url, '') |
| 65 | + repos_service.client.perform_query.assert_called_with('POST', '/repos', data={'url': url, 'provider': 'gitHub'}, headers=None) |
| 66 | + |
| 67 | + repos_service.create_repo(url, ' ') |
| 68 | + repos_service.client.perform_query.assert_called_with('POST', '/repos', data={'url': url, 'provider': 'gitHub'}, headers=None) |
| 69 | + |
| 70 | + repos_service.create_repo(url, 'gitHub') |
| 71 | + repos_service.client.perform_query.assert_called_with('POST', '/repos', data={'url': url, 'provider': 'gitHub'}, headers=None) |
| 72 | + |
| 73 | + repos_service.create_repo(url, None, path='/path') |
| 74 | + repos_service.client.perform_query.assert_called_with('POST', '/repos', data={'url': url, 'provider': 'gitHub', 'path': '/path'}, headers=None) |
| 75 | + |
| 76 | + |
| 77 | +@provide_conf |
| 78 | +def test_get(repos_service): |
| 79 | + repos_service.get_repo('id') |
| 80 | + repos_service.client.perform_query.assert_called_with('GET', '/repos/id', data={}, headers=None) |
| 81 | + |
| 82 | + |
| 83 | +@provide_conf |
| 84 | +def test_update(repos_service): |
| 85 | + repos_service.update_repo('id') |
| 86 | + repos_service.client.perform_query.assert_called_with('PATCH', '/repos/id', data={}, headers=None) |
| 87 | + |
| 88 | + repos_service.update_repo('id', branch='branch') |
| 89 | + repos_service.client.perform_query.assert_called_with('PATCH', '/repos/id', data={'branch': 'branch'}, headers=None) |
| 90 | + |
| 91 | + repos_service.update_repo('id', tag='tag') |
| 92 | + repos_service.client.perform_query.assert_called_with('PATCH', '/repos/id', data={'tag': 'tag'}, headers=None) |
| 93 | + |
| 94 | +@provide_conf |
| 95 | +def test_delete(repos_service): |
| 96 | + repos_service.delete_repo('id') |
| 97 | + repos_service.client.perform_query.assert_called_with('DELETE', '/repos/id', data={}, headers=None) |
0 commit comments