|
| 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