Skip to content

Commit 595949c

Browse files
chinyeungliJonoYang
authored andcommitted
Fixed #594 - Add tests for on-demand package data collection for cargo
Signed-off-by: Chin Yeung Li <[email protected]>
1 parent 2b0b155 commit 595949c

File tree

5 files changed

+641
-0
lines changed

5 files changed

+641
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# purldb is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/purldb for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
import json
11+
import os
12+
13+
from django.test import TestCase as DjangoTestCase
14+
15+
from packageurl import PackageURL
16+
17+
import packagedb
18+
from minecode.collectors import cargo
19+
from minecode.utils_test import JsonBasedTesting
20+
21+
22+
class CargoPriorityQueueTests(JsonBasedTesting, DjangoTestCase):
23+
test_data_dir = os.path.join(
24+
os.path.dirname(os.path.dirname(__file__)), "testfiles"
25+
)
26+
27+
def setUp(self):
28+
super().setUp()
29+
self.expected_json_loc = self.get_test_loc("cargo/sam.json")
30+
with open(self.expected_json_loc) as f:
31+
self.expected_json_contents = json.load(f)
32+
33+
def test_get_package_json(self):
34+
# As certain fields, such as "downloads," "recent_downloads," and
35+
# "num_versions," may vary over time when executing
36+
# "cargo.get_package_json(name="sam")", we cannot rely on
37+
# "assertEqual" for comparison. Instead, we will verify that the
38+
# response includes four primary components: crate, version,
39+
# keywords, and categories, and the the "id" under crate is "sam"
40+
expected_list = ["crate", "versions", "keywords", "categories"]
41+
json_contents = cargo.get_package_json(name="sam")
42+
keys = json_contents.keys()
43+
self.assertListEqual(list(keys), expected_list)
44+
self.assertEqual(json_contents["crate"]["id"], "sam")
45+
46+
def test_map_npm_package(self):
47+
package_count = packagedb.models.Package.objects.all().count()
48+
self.assertEqual(0, package_count)
49+
package_url = PackageURL.from_string("pkg:cargo/[email protected]")
50+
cargo.map_cargo_package(package_url, ("test_pipeline"))
51+
package_count = packagedb.models.Package.objects.all().count()
52+
self.assertEqual(1, package_count)
53+
package = packagedb.models.Package.objects.all().first()
54+
expected_purl_str = "pkg:cargo/[email protected]"
55+
expected_download_url = "https://static.crates.io/crates/sam/sam-0.3.1.crate"
56+
self.assertEqual(expected_purl_str, package.purl)
57+
self.assertEqual(expected_download_url, package.download_url)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# purldb is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/aboutcode-org/purldb for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
11+
import json
12+
import os
13+
14+
from django.test import TestCase as DjangoTestCase
15+
16+
from minecode import miners
17+
from minecode.tests import FIXTURES_REGEN
18+
from minecode.utils_test import JsonBasedTesting
19+
from packageurl import PackageURL
20+
21+
22+
class TestCargoMap(JsonBasedTesting, DjangoTestCase):
23+
test_data_dir = os.path.join(
24+
os.path.dirname(os.path.dirname(__file__)), "testfiles"
25+
)
26+
27+
def test_build_packages_with_no_version(self):
28+
with open(self.get_test_loc("cargo/sam.json")) as cargo_meta:
29+
metadata = json.load(cargo_meta)
30+
package_url = PackageURL.from_string("pkg:cargo/sam")
31+
packages = miners.cargo.build_packages(metadata, package_url)
32+
packages = [p.to_dict() for p in packages]
33+
expected_loc = self.get_test_loc("cargo/expected-sam.json")
34+
self.check_expected_results(packages, expected_loc, regen=FIXTURES_REGEN)
35+
36+
def test_build_packages_with_version(self):
37+
with open(self.get_test_loc("cargo/sam.json")) as cargo_meta:
38+
metadata = json.load(cargo_meta)
39+
package_url = PackageURL.from_string("pkg:cargo/[email protected]")
40+
packages = miners.cargo.build_packages(metadata, package_url)
41+
packages = [p.to_dict() for p in packages]
42+
expected_loc = self.get_test_loc("cargo/expected-sam-0.3.1.json")
43+
self.check_expected_results(packages, expected_loc, regen=FIXTURES_REGEN)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[
2+
{
3+
"type": "cargo",
4+
"namespace": null,
5+
"name": "sam",
6+
"version": "0.3.1",
7+
"qualifiers": {},
8+
"subpath": null,
9+
"primary_language": null,
10+
"description": "A compile time instruction assembler.",
11+
"release_date": null,
12+
"parties": [],
13+
"keywords": [],
14+
"homepage_url": null,
15+
"download_url": "https://static.crates.io/crates/sam/sam-0.3.1.crate",
16+
"size": 3769,
17+
"sha1": null,
18+
"md5": null,
19+
"sha256": "5fe15167489125b7403c9730d2c1a4c163466a327f7672444643259e8ad22178",
20+
"sha512": null,
21+
"bug_tracking_url": null,
22+
"code_view_url": null,
23+
"vcs_url": null,
24+
"copyright": null,
25+
"holder": null,
26+
"declared_license_expression": "mit",
27+
"declared_license_expression_spdx": "MIT",
28+
"license_detections": [
29+
{
30+
"license_expression": "mit",
31+
"license_expression_spdx": "MIT",
32+
"matches": [
33+
{
34+
"license_expression": "mit",
35+
"license_expression_spdx": "MIT",
36+
"from_file": null,
37+
"start_line": 1,
38+
"end_line": 1,
39+
"matcher": "1-spdx-id",
40+
"score": 100,
41+
"matched_length": 1,
42+
"match_coverage": 100,
43+
"rule_relevance": 100,
44+
"rule_identifier": "spdx-license-identifier-mit-5da48780aba670b0860c46d899ed42a0f243ff06",
45+
"rule_url": null,
46+
"matched_text": "MIT"
47+
}
48+
],
49+
"identifier": "mit-a822f434-d61f-f2b1-c792-8b8cb9e7b9bf"
50+
}
51+
],
52+
"other_license_expression": null,
53+
"other_license_expression_spdx": null,
54+
"other_license_detections": [],
55+
"extracted_license_statement": "- MIT\n",
56+
"notice_text": null,
57+
"source_packages": [],
58+
"file_references": [],
59+
"is_private": false,
60+
"is_virtual": false,
61+
"extra_data": {},
62+
"dependencies": [],
63+
"repository_homepage_url": "https://github.com/ioncodes/sam",
64+
"repository_download_url": null,
65+
"api_data_url": null,
66+
"datasource_id": "cargo_api_metadata",
67+
"purl": "pkg:cargo/[email protected]"
68+
}
69+
]

0 commit comments

Comments
 (0)