Skip to content

Commit d8ae0b8

Browse files
authored
Improve test coverage. (#40)
Signed-off-by: Caroline Russell <[email protected]>
1 parent 411a807 commit d8ae0b8

File tree

3 files changed

+173
-18
lines changed

3 files changed

+173
-18
lines changed

test/test_bom_diff.py

+47-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33

44
import pytest
55

6-
from custom_json_diff.lib.custom_diff import compare_dicts, perform_bom_diff, unpack_misc_data
6+
from custom_json_diff.lib.custom_diff import (
7+
add_short_ref_for_report,
8+
calculate_pcts,
9+
compare_dicts,
10+
generate_counts,
11+
perform_bom_diff,
12+
unpack_misc_data
13+
)
714
from custom_json_diff.lib.custom_diff_classes import (
815
BomComponent, BomDependency, BomDicts, FlatDicts, Options, BomVdr, BomVdrAffects
916
)
@@ -318,10 +325,32 @@ def results():
318325
def test_bom_diff(results, options_1):
319326
result, j1, j2 = compare_dicts(options_1)
320327
_, result_summary = perform_bom_diff(j1, j2)
321-
x = unpack_misc_data(result_summary, j1.options)
322-
assert len(x.get("diff_summary", {}).get(j1.filename, {}).get("components", {}).get("frameworks", [])) == 13
323-
assert len(x.get("diff_summary", {}).get(j2.filename, {}).get("components", {}).get("frameworks", [])) == 1
324-
assert len(x.get("common_summary", {}).get("components", {}).get("frameworks", [])) == 5
328+
result_summary = unpack_misc_data(result_summary, j1.options)
329+
assert len(result_summary.get("diff_summary", {}).get(j1.filename, {}).get("components", {}).get("frameworks", [])) == 13
330+
assert len(result_summary.get("diff_summary", {}).get(j2.filename, {}).get("components", {}).get("frameworks", [])) == 1
331+
assert len(result_summary.get("common_summary", {}).get("components", {}).get("frameworks", [])) == 5
332+
assert calculate_pcts(result_summary, j1, j2) == {
333+
'breakdown': {'dependencies': ['13/17 (76.47%)', '1/5 (20.0%)'],
334+
'frameworks': ['13/18 (72.22%)', '1/6 (16.67%)'],
335+
'libraries': ['11/13 (84.62%)', '1/3 (33.33%)'],
336+
'services': ['8/14 (57.14%)', '0/6 (0.0%)']},
337+
'common_summary': [['Common libraries matched: ', '2 (25.0)%'],
338+
['Common frameworks matched: ', '5 (41.67)%'],
339+
['Common services matched: ', '6 (60.0)%'],
340+
['Common dependencies matched: ', '4 (36.36)%']]
341+
}
342+
assert generate_counts(result_summary.get("common_summary", {})) == {'applications': 0,
343+
'dependencies': 4, 'frameworks': 5, 'libraries': 2, 'other_components': 0, 'services': 6,
344+
'vulnerabilities': 0}
345+
assert generate_counts(result_summary.get("diff_summary", {}).get(j1.filename, {})) == {
346+
'applications': 0, 'dependencies': 13, 'frameworks': 13, 'libraries': 11,
347+
'other_components': 0, 'services': 8, 'vulnerabilities': 0}
348+
assert add_short_ref_for_report(result_summary, j1.options).get("diff_summary", {}).get(
349+
j2.filename, {}).get("dependencies") == [
350+
{'ref': 'pkg:maven/javax.activation/[email protected]?type=jar',
351+
'short_ref': 'javax.activation/[email protected]'}]
352+
353+
325354

326355
def test_bom_diff_component_options(results, bom_dicts_1, bom_dicts_2, bom_dicts_3, bom_dicts_4, bom_dicts_5, bom_dicts_6, bom_dicts_7, bom_dicts_8):
327356
# test --allow-new-data for components
@@ -642,3 +671,16 @@ def test_bom_dependencies(options_3):
642671
bom_1 = BomDependency({"dependsOn": ["pkg:npm/[email protected]"], "ref": "pkg:npm/[email protected]"},options_3)
643672
bom_1.options.allow_new_versions = True
644673
assert bom_1 == bom_2
674+
675+
676+
def test_unpack_misc_data(options_1):
677+
data = {
678+
"common_summary": {"misc_data": {"foo": "bar"}},
679+
"diff_summary": {
680+
options_1.file_1: {"misc_data": {"foo": "bar"}},
681+
options_1.file_2: {"misc_data": {"foo": "bar"}}}
682+
}
683+
assert unpack_misc_data(data, options_1) == {
684+
'common_summary': {'foo': 'bar'},
685+
'diff_summary': {'test/sbom-java.json': {'foo': 'bar'},
686+
'test/sbom-java2.json': {'foo': 'bar'}}}

test/test_custom_json_diff.py

-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
compare_dicts, get_bom_status, get_diff, json_to_class
77
)
88
from custom_json_diff.lib.custom_diff_classes import Options
9-
from custom_json_diff.lib.utils import sort_dict_lists
109

1110

1211
@pytest.fixture
@@ -79,18 +78,6 @@ def test_flat_dicts_class(java_1_flat, python_1_flat, java_2_flat, python_2_flat
7978
assert python_1_flat.to_dict(True) == results["result_11"]
8079

8180

82-
def test_sort_dict():
83-
x = {
84-
"a": 1, "b": 2, "c": [3, 2, 1],
85-
"d": [{"name": "test 3", "value": 1}, {"value": 3}, {"name": "test 2", "value": 2}]
86-
}
87-
88-
assert sort_dict_lists(x, ["url", "content", "ref", "name", "value"]) == {
89-
"a": 1, "b": 2, "c": [1, 2, 3],
90-
"d": [{"name": "test 3", "value": 1}, {"name": "test 2", "value": 2}, {"value": 3}]
91-
}
92-
93-
9481
def test_get_bom_status():
9582
diff_summary_1 = {}
9683
diff_summary_2 = {}

test/test_utils.py

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import os.path
2+
from datetime import datetime
3+
4+
import semver
5+
6+
from custom_json_diff.lib.utils import (
7+
compare_bom_refs,
8+
compare_date,
9+
compare_generic,
10+
compare_recommendations,
11+
compare_versions,
12+
filter_empty,
13+
import_config,
14+
json_dump,
15+
json_load,
16+
manual_version_compare,
17+
sort_dict,
18+
split_bom_ref
19+
)
20+
21+
22+
def test_compare_bom_refs():
23+
assert compare_bom_refs("", "pkg:pypi/[email protected]", "=") is False
24+
assert compare_bom_refs("pkg:maven/org.springframework.cloud/[email protected]", "pkg:maven/org.springframework.cloud/[email protected]?type=jar", "=") is True
25+
assert compare_bom_refs("pkg:pypi/[email protected]", "pkg:pypi/[email protected]", "<=") is False
26+
assert compare_bom_refs("pkg:pypi/[email protected]", "pkg:pypi/[email protected]", "<=") is True
27+
assert compare_bom_refs("pkg:pypi/[email protected]", "pkg:pypi/[email protected]", "<=") is True
28+
assert compare_bom_refs("", "", "=") is True
29+
assert compare_bom_refs("", "", "<") is False
30+
31+
32+
def test_compare_date():
33+
assert compare_date("", "", "=") is True
34+
assert compare_date("", "", "==") is True
35+
assert compare_date("", "", "<") is False
36+
assert compare_date("2024-06-05T03:01:41.936Z", "2024-06-05", ">=") is True
37+
assert compare_date("2024-06-05T03:01:41.936Z", "2024-06-06", "<=") is True
38+
assert compare_date("2024-06-05T03:01:41.936Z", "2024-06-06", ">") is False
39+
assert compare_date("", "2024-06-05", ">=") is False
40+
assert compare_date("55-55-55", "2024-06-05", ">=") is True
41+
42+
43+
def test_compare_generic():
44+
assert compare_generic("", "", "=") is True
45+
assert compare_generic("", "", "==") is True
46+
assert compare_generic("", "", "<") is False
47+
assert compare_generic("1.0", "0.9", ">=") is True
48+
assert compare_generic(semver.Version.parse("1.0.0"), semver.Version.parse("0.9.0"), ">=") is True
49+
50+
51+
def test_compare_recommendations():
52+
assert compare_recommendations("Update to 3.1.", "Update to 3.2.", "<=") is True
53+
assert compare_recommendations("Update to 3.9.", "Update to version 3.13.", "<=") is True
54+
assert compare_recommendations("Update to 3.9.", "Update to v3.13.", "<=") is True
55+
assert compare_recommendations("", "Update to version 3.13.", "<=") is True
56+
57+
58+
def test_compare_versions():
59+
assert compare_versions("", "", "=") is True
60+
assert compare_versions("", "", "==") is True
61+
assert compare_versions("", "", "<") is False
62+
assert compare_versions("", "0.9.0", "=") is False
63+
assert compare_versions("1.0.0", "0.9.0", ">=") is True
64+
65+
66+
def test_filter_empty():
67+
assert filter_empty(True, {"a": 1, "b": None}) == {"a": 1, "b": None}
68+
assert filter_empty(False, {"a": 1, "b": None}) == {"a": 1}
69+
assert filter_empty(False, {"a": 1, "b": {"c": 1, "d": []}}) == {"a": 1, "b": {"c": 1}}
70+
71+
72+
def test_import_config():
73+
assert import_config("test/config.toml") == {
74+
'preset_settings': {'allow_new_data': False, 'allow_new_versions': True,
75+
'components_only': False,
76+
'include_extra': ['licenses', 'properties', 'hashes', 'evidence'],
77+
'report_template': 'custom_json_diff/bom_diff_template.j2',
78+
'type': 'bom'}, 'settings': {'excluded_fields': [],
79+
'sort_keys': ['url', 'content', 'ref',
80+
'name', 'value']}}
81+
82+
83+
def test_json_dump():
84+
json_dump("testfile.json", {"a": {1, 2, 3}}, sort_keys=["a"])
85+
assert not os.path.exists("testfile.json")
86+
87+
88+
def test_json_load():
89+
assert list(json_load("test/test_data.json").keys()) == ['result_1', 'result_10', 'result_11',
90+
'result_12', 'result_2', 'result_3',
91+
'result_4', 'result_5', 'result_6',
92+
'result_7', 'result_8', 'result_9',
93+
'result_13', 'result_14']
94+
assert json_load("notafile.json") == {}
95+
96+
97+
def test_manual_version_compare():
98+
assert manual_version_compare("1.0.0", "0.9.0", ">=") is True
99+
assert manual_version_compare("1.0.0", "1.0.1", ">=") is False
100+
assert manual_version_compare("2024-10", "2024-09", ">=") is True
101+
assert manual_version_compare("1.0.0", "0.9.0", "<=") is False
102+
assert manual_version_compare("1.0.0", "1.0.1", "<=") is True
103+
assert manual_version_compare("2024-10", "2024-09", "<=") is False
104+
assert manual_version_compare("1.0.0", "0.9.0", ">") is True
105+
assert manual_version_compare("1.0.0", "1.0.1", ">") is False
106+
assert manual_version_compare("2024-10", "2024-09", ">") is True
107+
assert manual_version_compare("1.0.0", "0.9.0", ">=") is True
108+
assert manual_version_compare(".", ".", ".") is True
109+
110+
111+
def test_sort_dict():
112+
x = {
113+
"a": 1, "b": 2, "c": [3, 2, 1],
114+
"d": [{"name": "test 3", "value": 1}, {"value": 3}, {"name": "test 2", "value": 2}]
115+
}
116+
117+
assert sort_dict(x, ["url", "content", "ref", "name", "value"]) == {
118+
"a": 1, "b": 2, "c": [1, 2, 3],
119+
"d": [{"name": "test 3", "value": 1}, {"name": "test 2", "value": 2}, {"value": 3}]
120+
}
121+
122+
123+
def test_split_bom_ref():
124+
assert split_bom_ref("pkg:pypi/[email protected]") == ("pkg:pypi/werkzeug", "1.1.1")
125+
assert split_bom_ref("pkg:pypi/[email protected]?type=jar") == ("pkg:pypi/werkzeug", "1.1.1")
126+
assert split_bom_ref("pkg:pypi/werkzeug") == ("pkg:pypi/werkzeug", "")

0 commit comments

Comments
 (0)