EricGao888 commented on code in PR #11831: URL: https://github.com/apache/dolphinscheduler/pull/11831#discussion_r967657919
########## dolphinscheduler-python/pydolphinscheduler/tests/resources_plugin/test_github.py: ########## @@ -0,0 +1,228 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Test github resource plugin.""" +from unittest.mock import PropertyMock, patch + +import pytest + +from pydolphinscheduler.resources_plugin import GitHub +from pydolphinscheduler.resources_plugin.base.git import GitFileInfo + + [email protected]( + "attr, expected", + [ + ( + { + "user": "apache", + "repo_name": "dolphinscheduler", + "file_path": "script/install.sh", + "api": "https://api.github.com/repos/{user}/{repo_name}/contents/{file_path}", + }, + "https://api.github.com/repos/apache/dolphinscheduler/contents/script/install.sh", + ), + ], +) +def test_github_build_req_api(attr, expected): + """Test the build_req_api function of the github resource plug-in.""" + github = GitHub(prefix="prefix") + assert expected == github.build_req_api(**attr) + + [email protected]( + "attr, expected", + [ + ( + "https://github.com/apache/dolphinscheduler/blob/dev/script/install.sh", + { + "_user": "apache", + "_repo_name": "dolphinscheduler", + "_branch": "dev", + "_file_path": "script/install.sh", + }, + ), + ( + "https://github.com/apache/dolphinscheduler/blob/master/pom.xml", + { + "_user": "apache", + "_repo_name": "dolphinscheduler", + "_branch": "master", + "_file_path": "pom.xml", + }, + ), + ( + "https://github.com/apache/dolphinscheduler/blob/1.3.9-release/docker/build/startup.sh", + { + "_user": "apache", + "_repo_name": "dolphinscheduler", + "_branch": "1.3.9-release", + "_file_path": "docker/build/startup.sh", + }, + ), + ], +) +def test_github_get_git_file_info(attr, expected): + """Test the get_git_file_info function of the github resource plug-in.""" + github = GitHub(prefix="prefix") + github.get_git_file_info(attr) + assert expected == github._git_file_info.__dict__ + + [email protected]( + "attr, expected", + [ + ( + ( + { + "user": "apache", + "repo_name": "dolphinscheduler", + "file_path": "docker/build/startup.sh", + } + ), + "https://api.github.com/repos/apache/dolphinscheduler/contents/docker/build/startup.sh", + ), + ( + ( + { + "user": "apache", + "repo_name": "dolphinscheduler", + "file_path": "pom.xml", + } + ), + "https://api.github.com/repos/apache/dolphinscheduler/contents/pom.xml", + ), + ( + ( + { + "user": "apache", + "repo_name": "dolphinscheduler", + "file_path": "script/create-dolphinscheduler.sh", + } + ), + "https://api.github.com/repos/apache/dolphinscheduler/contents/script/create-dolphinscheduler.sh", + ), + ], +) +@patch( + "pydolphinscheduler.resources_plugin.github.GitHub._git_file_info", + new_callable=PropertyMock, +) +def test_github_get_req_url(m_git_file_info, attr, expected): + """Test the get_req_url function of the github resource plug-in.""" + github = GitHub(prefix="prefix") + m_git_file_info.return_value = GitFileInfo(**attr) + assert expected == github.get_req_url() + + [email protected](reason="Lack of test environment, need stable repository") Review Comment: For unit tests, we usually decouple the external system. You may mock the interactions instead of calling the real `GitHub` APIs and focus on testing the logic within `github.read_file(attr)` function. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
