Skip to content

Expand test coverage. #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions test/test_bom_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@

import pytest

from custom_json_diff.lib.custom_diff import compare_dicts, perform_bom_diff, unpack_misc_data
from custom_json_diff.lib.custom_diff import (
add_short_ref_for_report,
calculate_pcts,
compare_dicts,
generate_counts,
perform_bom_diff,
unpack_misc_data
)
from custom_json_diff.lib.custom_diff_classes import (
BomComponent, BomDependency, BomDicts, FlatDicts, Options, BomVdr, BomVdrAffects
)
Expand Down Expand Up @@ -318,10 +325,32 @@ def results():
def test_bom_diff(results, options_1):
result, j1, j2 = compare_dicts(options_1)
_, result_summary = perform_bom_diff(j1, j2)
x = unpack_misc_data(result_summary, j1.options)
assert len(x.get("diff_summary", {}).get(j1.filename, {}).get("components", {}).get("frameworks", [])) == 13
assert len(x.get("diff_summary", {}).get(j2.filename, {}).get("components", {}).get("frameworks", [])) == 1
assert len(x.get("common_summary", {}).get("components", {}).get("frameworks", [])) == 5
result_summary = unpack_misc_data(result_summary, j1.options)
assert len(result_summary.get("diff_summary", {}).get(j1.filename, {}).get("components", {}).get("frameworks", [])) == 13
assert len(result_summary.get("diff_summary", {}).get(j2.filename, {}).get("components", {}).get("frameworks", [])) == 1
assert len(result_summary.get("common_summary", {}).get("components", {}).get("frameworks", [])) == 5
assert calculate_pcts(result_summary, j1, j2) == {
'breakdown': {'dependencies': ['13/17 (76.47%)', '1/5 (20.0%)'],
'frameworks': ['13/18 (72.22%)', '1/6 (16.67%)'],
'libraries': ['11/13 (84.62%)', '1/3 (33.33%)'],
'services': ['8/14 (57.14%)', '0/6 (0.0%)']},
'common_summary': [['Common libraries matched: ', '2 (25.0)%'],
['Common frameworks matched: ', '5 (41.67)%'],
['Common services matched: ', '6 (60.0)%'],
['Common dependencies matched: ', '4 (36.36)%']]
}
assert generate_counts(result_summary.get("common_summary", {})) == {'applications': 0,
'dependencies': 4, 'frameworks': 5, 'libraries': 2, 'other_components': 0, 'services': 6,
'vulnerabilities': 0}
assert generate_counts(result_summary.get("diff_summary", {}).get(j1.filename, {})) == {
'applications': 0, 'dependencies': 13, 'frameworks': 13, 'libraries': 11,
'other_components': 0, 'services': 8, 'vulnerabilities': 0}
assert add_short_ref_for_report(result_summary, j1.options).get("diff_summary", {}).get(
j2.filename, {}).get("dependencies") == [
{'ref': 'pkg:maven/javax.activation/[email protected]?type=jar',
'short_ref': 'javax.activation/[email protected]'}]



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):
# test --allow-new-data for components
Expand Down Expand Up @@ -642,3 +671,16 @@ def test_bom_dependencies(options_3):
bom_1 = BomDependency({"dependsOn": ["pkg:npm/[email protected]"], "ref": "pkg:npm/[email protected]"},options_3)
bom_1.options.allow_new_versions = True
assert bom_1 == bom_2


def test_unpack_misc_data(options_1):
data = {
"common_summary": {"misc_data": {"foo": "bar"}},
"diff_summary": {
options_1.file_1: {"misc_data": {"foo": "bar"}},
options_1.file_2: {"misc_data": {"foo": "bar"}}}
}
assert unpack_misc_data(data, options_1) == {
'common_summary': {'foo': 'bar'},
'diff_summary': {'test/sbom-java.json': {'foo': 'bar'},
'test/sbom-java2.json': {'foo': 'bar'}}}
13 changes: 0 additions & 13 deletions test/test_custom_json_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
compare_dicts, get_bom_status, get_diff, json_to_class
)
from custom_json_diff.lib.custom_diff_classes import Options
from custom_json_diff.lib.utils import sort_dict_lists


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


def test_sort_dict():
x = {
"a": 1, "b": 2, "c": [3, 2, 1],
"d": [{"name": "test 3", "value": 1}, {"value": 3}, {"name": "test 2", "value": 2}]
}

assert sort_dict_lists(x, ["url", "content", "ref", "name", "value"]) == {
"a": 1, "b": 2, "c": [1, 2, 3],
"d": [{"name": "test 3", "value": 1}, {"name": "test 2", "value": 2}, {"value": 3}]
}


def test_get_bom_status():
diff_summary_1 = {}
diff_summary_2 = {}
Expand Down
126 changes: 126 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import os.path
from datetime import datetime

import semver

from custom_json_diff.lib.utils import (
compare_bom_refs,
compare_date,
compare_generic,
compare_recommendations,
compare_versions,
filter_empty,
import_config,
json_dump,
json_load,
manual_version_compare,
sort_dict,
split_bom_ref
)


def test_compare_bom_refs():
assert compare_bom_refs("", "pkg:pypi/[email protected]", "=") is False
assert compare_bom_refs("pkg:maven/org.springframework.cloud/[email protected]", "pkg:maven/org.springframework.cloud/[email protected]?type=jar", "=") is True
assert compare_bom_refs("pkg:pypi/[email protected]", "pkg:pypi/[email protected]", "<=") is False
assert compare_bom_refs("pkg:pypi/[email protected]", "pkg:pypi/[email protected]", "<=") is True
assert compare_bom_refs("pkg:pypi/[email protected]", "pkg:pypi/[email protected]", "<=") is True
assert compare_bom_refs("", "", "=") is True
assert compare_bom_refs("", "", "<") is False


def test_compare_date():
assert compare_date("", "", "=") is True
assert compare_date("", "", "==") is True
assert compare_date("", "", "<") is False
assert compare_date("2024-06-05T03:01:41.936Z", "2024-06-05", ">=") is True
assert compare_date("2024-06-05T03:01:41.936Z", "2024-06-06", "<=") is True
assert compare_date("2024-06-05T03:01:41.936Z", "2024-06-06", ">") is False
assert compare_date("", "2024-06-05", ">=") is False
assert compare_date("55-55-55", "2024-06-05", ">=") is True


def test_compare_generic():
assert compare_generic("", "", "=") is True
assert compare_generic("", "", "==") is True
assert compare_generic("", "", "<") is False
assert compare_generic("1.0", "0.9", ">=") is True
assert compare_generic(semver.Version.parse("1.0.0"), semver.Version.parse("0.9.0"), ">=") is True


def test_compare_recommendations():
assert compare_recommendations("Update to 3.1.", "Update to 3.2.", "<=") is True
assert compare_recommendations("Update to 3.9.", "Update to version 3.13.", "<=") is True
assert compare_recommendations("Update to 3.9.", "Update to v3.13.", "<=") is True
assert compare_recommendations("", "Update to version 3.13.", "<=") is True


def test_compare_versions():
assert compare_versions("", "", "=") is True
assert compare_versions("", "", "==") is True
assert compare_versions("", "", "<") is False
assert compare_versions("", "0.9.0", "=") is False
assert compare_versions("1.0.0", "0.9.0", ">=") is True


def test_filter_empty():
assert filter_empty(True, {"a": 1, "b": None}) == {"a": 1, "b": None}
assert filter_empty(False, {"a": 1, "b": None}) == {"a": 1}
assert filter_empty(False, {"a": 1, "b": {"c": 1, "d": []}}) == {"a": 1, "b": {"c": 1}}


def test_import_config():
assert import_config("test/config.toml") == {
'preset_settings': {'allow_new_data': False, 'allow_new_versions': True,
'components_only': False,
'include_extra': ['licenses', 'properties', 'hashes', 'evidence'],
'report_template': 'custom_json_diff/bom_diff_template.j2',
'type': 'bom'}, 'settings': {'excluded_fields': [],
'sort_keys': ['url', 'content', 'ref',
'name', 'value']}}


def test_json_dump():
json_dump("testfile.json", {"a": {1, 2, 3}}, sort_keys=["a"])
assert not os.path.exists("testfile.json")


def test_json_load():
assert list(json_load("test/test_data.json").keys()) == ['result_1', 'result_10', 'result_11',
'result_12', 'result_2', 'result_3',
'result_4', 'result_5', 'result_6',
'result_7', 'result_8', 'result_9',
'result_13', 'result_14']
assert json_load("notafile.json") == {}


def test_manual_version_compare():
assert manual_version_compare("1.0.0", "0.9.0", ">=") is True
assert manual_version_compare("1.0.0", "1.0.1", ">=") is False
assert manual_version_compare("2024-10", "2024-09", ">=") is True
assert manual_version_compare("1.0.0", "0.9.0", "<=") is False
assert manual_version_compare("1.0.0", "1.0.1", "<=") is True
assert manual_version_compare("2024-10", "2024-09", "<=") is False
assert manual_version_compare("1.0.0", "0.9.0", ">") is True
assert manual_version_compare("1.0.0", "1.0.1", ">") is False
assert manual_version_compare("2024-10", "2024-09", ">") is True
assert manual_version_compare("1.0.0", "0.9.0", ">=") is True
assert manual_version_compare(".", ".", ".") is True


def test_sort_dict():
x = {
"a": 1, "b": 2, "c": [3, 2, 1],
"d": [{"name": "test 3", "value": 1}, {"value": 3}, {"name": "test 2", "value": 2}]
}

assert sort_dict(x, ["url", "content", "ref", "name", "value"]) == {
"a": 1, "b": 2, "c": [1, 2, 3],
"d": [{"name": "test 3", "value": 1}, {"name": "test 2", "value": 2}, {"value": 3}]
}


def test_split_bom_ref():
assert split_bom_ref("pkg:pypi/[email protected]") == ("pkg:pypi/werkzeug", "1.1.1")
assert split_bom_ref("pkg:pypi/[email protected]?type=jar") == ("pkg:pypi/werkzeug", "1.1.1")
assert split_bom_ref("pkg:pypi/werkzeug") == ("pkg:pypi/werkzeug", "")