Skip to content

Commit

Permalink
Update tests including mocking head request #116
Browse files Browse the repository at this point in the history
Reference: #116
Signed-off-by: John M. Horan <[email protected]>
  • Loading branch information
johnmhoran committed Jun 20, 2024
1 parent 82f2a1e commit 1eb1bc3
Show file tree
Hide file tree
Showing 2 changed files with 298 additions and 3 deletions.
236 changes: 236 additions & 0 deletions tests/data/cocoapods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
[
{
"type": "cocoapods",
"namespace": null,
"name": "ASNetworking",
"version": "0.1.5",
"qualifiers": {},
"subpath": null,
"repository_homepage_url": "https://cocoapods.org/pods/ASNetworking",
"repository_download_url": null,
"api_data_url": null,
"primary_language": "Swift",
"description": null,
"release_date": null,
"parties": [],
"keywords": [],
"homepage_url": "https://github.com/Appspia/ASNetworking",
"download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.5.tar.gz",
"api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.5/ASNetworking.podspec.json",
"size": null,
"sha1": null,
"md5": null,
"sha256": null,
"sha512": null,
"bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues",
"code_view_url": "https://github.com/Appspia/ASNetworking",
"vcs_url": "https://github.com/Appspia/ASNetworking.git",
"copyright": null,
"license_expression": null,
"declared_license": {
"type": "MIT",
"file": "LICENSE"
},
"notice_text": null,
"root_path": null,
"dependencies": [],
"contains_source_code": null,
"source_packages": [],
"purl": "pkg:cocoapods/[email protected]"
},
{
"type": "cocoapods",
"namespace": null,
"name": "ASNetworking",
"version": "0.1.4",
"qualifiers": {},
"subpath": null,
"repository_homepage_url": "https://cocoapods.org/pods/ASNetworking",
"repository_download_url": null,
"api_data_url": null,
"primary_language": "Swift",
"description": null,
"release_date": null,
"parties": [],
"keywords": [],
"homepage_url": "https://github.com/Appspia/ASNetworking",
"download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.4.tar.gz",
"api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.4/ASNetworking.podspec.json",
"size": null,
"sha1": null,
"md5": null,
"sha256": null,
"sha512": null,
"bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues",
"code_view_url": "https://github.com/Appspia/ASNetworking",
"vcs_url": "https://github.com/Appspia/ASNetworking.git",
"copyright": null,
"license_expression": null,
"declared_license": {
"type": "MIT",
"file": "LICENSE"
},
"notice_text": null,
"root_path": null,
"dependencies": [],
"contains_source_code": null,
"source_packages": [],
"purl": "pkg:cocoapods/[email protected]"
},
{
"type": "cocoapods",
"namespace": null,
"name": "ASNetworking",
"version": "0.1.3",
"qualifiers": {},
"subpath": null,
"repository_homepage_url": "https://cocoapods.org/pods/ASNetworking",
"repository_download_url": null,
"api_data_url": null,
"primary_language": "Swift",
"description": null,
"release_date": null,
"parties": [],
"keywords": [],
"homepage_url": "https://github.com/Appspia/ASNetworking",
"download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.3.tar.gz",
"api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.3/ASNetworking.podspec.json",
"size": null,
"sha1": null,
"md5": null,
"sha256": null,
"sha512": null,
"bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues",
"code_view_url": "https://github.com/Appspia/ASNetworking",
"vcs_url": "https://github.com/Appspia/ASNetworking.git",
"copyright": null,
"license_expression": null,
"declared_license": {
"type": "MIT",
"file": "LICENSE"
},
"notice_text": null,
"root_path": null,
"dependencies": [],
"contains_source_code": null,
"source_packages": [],
"purl": "pkg:cocoapods/[email protected]"
},
{
"type": "cocoapods",
"namespace": null,
"name": "ASNetworking",
"version": "0.1.2",
"qualifiers": {},
"subpath": null,
"repository_homepage_url": "https://cocoapods.org/pods/ASNetworking",
"repository_download_url": null,
"api_data_url": null,
"primary_language": "Swift",
"description": null,
"release_date": null,
"parties": [],
"keywords": [],
"homepage_url": "https://github.com/Appspia/ASNetworking",
"download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.2.tar.gz",
"api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.2/ASNetworking.podspec.json",
"size": null,
"sha1": null,
"md5": null,
"sha256": null,
"sha512": null,
"bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues",
"code_view_url": "https://github.com/Appspia/ASNetworking",
"vcs_url": "https://github.com/Appspia/ASNetworking.git",
"copyright": null,
"license_expression": null,
"declared_license": {
"type": "MIT",
"file": "LICENSE"
},
"notice_text": null,
"root_path": null,
"dependencies": [],
"contains_source_code": null,
"source_packages": [],
"purl": "pkg:cocoapods/[email protected]"
},
{
"type": "cocoapods",
"namespace": null,
"name": "ASNetworking",
"version": "0.1.1",
"qualifiers": {},
"subpath": null,
"repository_homepage_url": "https://cocoapods.org/pods/ASNetworking",
"repository_download_url": null,
"api_data_url": null,
"primary_language": "Swift",
"description": null,
"release_date": null,
"parties": [],
"keywords": [],
"homepage_url": "https://github.com/Appspia/ASNetworking",
"download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.1.tar.gz",
"api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.1/ASNetworking.podspec.json",
"size": null,
"sha1": null,
"md5": null,
"sha256": null,
"sha512": null,
"bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues",
"code_view_url": "https://github.com/Appspia/ASNetworking",
"vcs_url": "https://github.com/Appspia/ASNetworking.git",
"copyright": null,
"license_expression": null,
"declared_license": {
"type": "MIT",
"file": "LICENSE"
},
"notice_text": null,
"root_path": null,
"dependencies": [],
"contains_source_code": null,
"source_packages": [],
"purl": "pkg:cocoapods/[email protected]"
},
{
"type": "cocoapods",
"namespace": null,
"name": "ASNetworking",
"version": "0.1.0",
"qualifiers": {},
"subpath": null,
"repository_homepage_url": "https://cocoapods.org/pods/ASNetworking",
"repository_download_url": null,
"api_data_url": null,
"primary_language": "Swift",
"description": null,
"release_date": null,
"parties": [],
"keywords": [],
"homepage_url": "https://github.com/Appspia/ASNetworking",
"download_url": "https://github.com/Appspia/ASNetworking/archive/refs/tags/0.1.0.tar.gz",
"api_url": "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/5/5/b/ASNetworking/0.1.0/ASNetworking.podspec.json",
"size": null,
"sha1": null,
"md5": null,
"sha256": null,
"sha512": null,
"bug_tracking_url": "https://github.com/Appspia/ASNetworking/issues",
"code_view_url": "https://github.com/Appspia/ASNetworking",
"vcs_url": "https://github.com/Appspia/ASNetworking.git",
"copyright": null,
"license_expression": null,
"declared_license": {
"type": "MIT",
"file": "LICENSE"
},
"notice_text": null,
"root_path": null,
"dependencies": [],
"contains_source_code": null,
"source_packages": [],
"purl": "pkg:cocoapods/[email protected]"
}
]
65 changes: 62 additions & 3 deletions tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,62 @@ def test_tuby_package_with_invalid_url(mock_get):
assert "Failed to fetch: https://rubygems.org/api/v1/gems/file.json" == e_info


@mock.patch("fetchcode.package_util.utils.make_head_request")
@mock.patch("fetchcode.package_util.utils.get_github_rest")
@mock.patch("fetchcode.package_util.utils.get_response")
@mock.patch("fetchcode.package.get_cocoapod_tags")
@mock.patch("fetchcode.package.get_hashed_path")
def test_cocoapods_packages(
mock_get_hashed_path,
mock_get_cocoapod_tags,
mock_get_response,
mock_get_github_rest,
mock_make_head_request,
):
mock_get_hashed_path.return_value = "5/5/b"

mock_get_cocoapod_tags.return_value = [
'0.1.5',
'0.1.4',
'0.1.3',
'0.1.2',
'0.1.1',
'0.1.0',
]

mock_get_response.side_effect = file_json("tests/data/cocoapods/mock_get_response_side_effect.json")

mock_get_github_rest.return_value = load_json("tests/data/cocoapods/mock_get_github_rest_return_value.json")

mock_response = mock.Mock()
mock_response.status_code = 200
mock_make_head_request.return_value = mock_response

purl = "pkg:cocoapods/ASNetworking"
expected_data = "tests/data/cocoapods.json"
packages = list(info(purl))
check_packages(packages, expected_data)


@mock.patch("fetchcode.package_util.utils.make_head_request")
@mock.patch("fetchcode.package_util.utils.get_github_rest")
@mock.patch("fetchcode.package_util.utils.get_response")
@mock.patch("fetchcode.package.get_cocoapod_tags")
@mock.patch("fetchcode.package.get_hashed_path")
def test_get_cocoapods_data_from_purl(
mock_get_hashed_path,
mock_get_cocoapod_tags,
mock_get_github_rest,
mock_get_response,
mock_get_github_rest,
mock_make_head_request,
):
"""
This already-existing test is structurally identical to the new
test_cocoapods_packages() except this test checks the
get_cocoapods_data_from_purl() function rather than list(info(purl)).
"""
mock_get_hashed_path.return_value = "5/5/b"

mock_get_cocoapod_tags.return_value = [
'0.1.5',
'0.1.4',
Expand All @@ -128,8 +173,15 @@ def test_get_cocoapods_data_from_purl(
'0.1.1',
'0.1.0',
]
mock_get_github_rest.return_value = load_json("tests/data/cocoapods/mock_get_github_rest_return_value.json")

mock_get_response.side_effect = file_json("tests/data/cocoapods/mock_get_response_side_effect.json")

mock_get_github_rest.return_value = load_json("tests/data/cocoapods/mock_get_github_rest_return_value.json")

mock_response = mock.Mock()
mock_response.status_code = 200
mock_make_head_request.return_value = mock_response

expected_result_to_dict = file_json("tests/data/cocoapods/expected_result_to_dict.json")
purl = "pkg:cocoapods/ASNetworking"
actual_result = get_cocoapods_data_from_purl(purl)
Expand Down Expand Up @@ -157,10 +209,17 @@ def test_get_cocoapod_tags(mock_get):


@mock.patch("fetchcode.package_util.utils.get_response")
@mock.patch("fetchcode.package_util.utils.make_head_request")
@mock.patch("fetchcode.package_util.utils.get_github_rest")
def test_construct_cocoapods_package(mock_get_github_rest, mock_get_response):
def test_construct_cocoapods_package(mock_get_github_rest, mock_make_head_request, mock_get_response):
mock_get_github_rest.return_value = "Failed to fetch: https://api.github.com/repos/KevalPatel94/KVLLibraries"

mock_response = mock.Mock()
mock_response.status_code = 404
mock_make_head_request.return_value = mock_response

mock_get_response.return_value = load_json("tests/data/cocoapods/get_response_kvllibraries.json")

expected_construct_cocoapods_package = load_json("tests/data/cocoapods/expected_construct_cocoapods_package.json")

purl = PackageURL.from_string("pkg:cocoapods/KVLLibraries")
Expand Down

0 comments on commit 1eb1bc3

Please sign in to comment.