update test suite for environments

pull/1/head
Emmanuelle Vargas-Gonzalez 2021-02-16 01:05:46 -05:00
parent 690a515f00
commit f966c64b40
2 changed files with 196 additions and 42 deletions

View File

@ -1,3 +1,4 @@
import json
import os import os
import pytest import pytest
@ -67,6 +68,11 @@ def ds2():
yield stix2.MemoryStore(stix_objs) yield stix2.MemoryStore(stix_objs)
@pytest.fixture
def fs():
yield stix2.FileSystemSource(FS_PATH)
def test_object_factory_created_by_ref_str(): def test_object_factory_created_by_ref_str():
factory = stix2.ObjectFactory(created_by_ref=IDENTITY_ID) factory = stix2.ObjectFactory(created_by_ref=IDENTITY_ID)
ind = factory.create(stix2.v20.Indicator, **INDICATOR_KWARGS) ind = factory.create(stix2.v20.Indicator, **INDICATOR_KWARGS)
@ -497,7 +503,20 @@ def test_list_semantic_check(ds, ds2):
assert round(score) == 1 assert round(score) == 1
def test_graph_equivalence_with_filesystem_source(ds): def test_graph_similarity_raises_value_error(ds):
weights = {
"_internal": {
"ignore_spec_version": False,
"versioning_checks": False,
"max_depth": -1,
},
}
with pytest.raises(ValueError):
prop_scores1 = {}
stix2.Environment().graph_similarity(ds, ds2, prop_scores1, **weights)
def test_graph_equivalence_with_filesystem_source(ds, fs):
weights = { weights = {
"_internal": { "_internal": {
"ignore_spec_version": True, "ignore_spec_version": True,
@ -505,12 +524,31 @@ def test_graph_equivalence_with_filesystem_source(ds):
"max_depth": 1, "max_depth": 1,
}, },
} }
prop_scores = {} prop_scores1 = {}
fs = stix2.FileSystemSource(FS_PATH) env1 = stix2.Environment().graph_similarity(fs, ds, prop_scores1, **weights)
env = stix2.Environment().graphically_equivalent(fs, ds, prop_scores, **weights)
assert round(env) == 28 # Switching parameters
assert round(prop_scores["matching_score"]) == 139 weights = {
assert round(prop_scores["sum_weights"]) == 500 "_internal": {
"ignore_spec_version": True,
"versioning_checks": False,
"max_depth": 1,
},
}
prop_scores2 = {}
env2 = stix2.Environment().graph_similarity(ds, fs, prop_scores2, **weights)
assert round(env1) == 25
assert round(prop_scores1["matching_score"]) == 451
assert round(prop_scores1["len_pairs"]) == 18
assert round(env2) == 25
assert round(prop_scores2["matching_score"]) == 451
assert round(prop_scores2["len_pairs"]) == 18
prop_scores1["matching_score"] = round(prop_scores1["matching_score"], 3)
prop_scores2["matching_score"] = round(prop_scores2["matching_score"], 3)
assert json.dumps(prop_scores1, sort_keys=True, indent=4) == json.dumps(prop_scores2, sort_keys=True, indent=4)
def test_graph_equivalence_with_duplicate_graph(ds): def test_graph_equivalence_with_duplicate_graph(ds):
@ -522,10 +560,10 @@ def test_graph_equivalence_with_duplicate_graph(ds):
}, },
} }
prop_scores = {} prop_scores = {}
env = stix2.Environment().graphically_equivalent(ds, ds, prop_scores, **weights) env = stix2.Environment().graph_similarity(ds, ds, prop_scores, **weights)
assert round(env) == 100 assert round(env) == 100
assert round(prop_scores["matching_score"]) == 800 assert round(prop_scores["matching_score"]) == 800
assert round(prop_scores["sum_weights"]) == 800 assert round(prop_scores["len_pairs"]) == 8
def test_graph_equivalence_with_versioning_check_on(ds2, ds): def test_graph_equivalence_with_versioning_check_on(ds2, ds):
@ -536,11 +574,31 @@ def test_graph_equivalence_with_versioning_check_on(ds2, ds):
"max_depth": 1, "max_depth": 1,
}, },
} }
prop_scores = {} prop_scores1 = {}
env = stix2.Environment().graphically_equivalent(ds, ds2, prop_scores, **weights) env1 = stix2.Environment().graph_similarity(ds, ds2, prop_scores1, **weights)
assert round(env) == 93
assert round(prop_scores["matching_score"]) == 745 # Switching parameters
assert round(prop_scores["sum_weights"]) == 800 weights = {
"_internal": {
"ignore_spec_version": False,
"versioning_checks": True,
"max_depth": 1,
},
}
prop_scores2 = {}
env2 = stix2.Environment().graph_similarity(ds2, ds, prop_scores2, **weights)
assert round(env1) == 88
assert round(prop_scores1["matching_score"]) == 789
assert round(prop_scores1["len_pairs"]) == 9
assert round(env2) == 88
assert round(prop_scores2["matching_score"]) == 789
assert round(prop_scores2["len_pairs"]) == 9
prop_scores1["matching_score"] = round(prop_scores1["matching_score"], 3)
prop_scores2["matching_score"] = round(prop_scores2["matching_score"], 3)
assert json.dumps(prop_scores1, sort_keys=True, indent=4) == json.dumps(prop_scores2, sort_keys=True, indent=4)
def test_graph_equivalence_with_versioning_check_off(ds2, ds): def test_graph_equivalence_with_versioning_check_off(ds2, ds):
@ -551,8 +609,28 @@ def test_graph_equivalence_with_versioning_check_off(ds2, ds):
"max_depth": 1, "max_depth": 1,
}, },
} }
prop_scores = {} prop_scores1 = {}
env = stix2.Environment().graphically_equivalent(ds, ds2, prop_scores, **weights) env1 = stix2.Environment().graph_similarity(ds, ds2, prop_scores1, **weights)
assert round(env) == 93
assert round(prop_scores["matching_score"]) == 745 # Switching parameters
assert round(prop_scores["sum_weights"]) == 800 weights = {
"_internal": {
"ignore_spec_version": False,
"versioning_checks": False,
"max_depth": 1,
},
}
prop_scores2 = {}
env2 = stix2.Environment().graph_similarity(ds2, ds, prop_scores2, **weights)
assert round(env1) == 88
assert round(prop_scores1["matching_score"]) == 789
assert round(prop_scores1["len_pairs"]) == 9
assert round(env2) == 88
assert round(prop_scores2["matching_score"]) == 789
assert round(prop_scores2["len_pairs"]) == 9
prop_scores1["matching_score"] = round(prop_scores1["matching_score"], 3)
prop_scores2["matching_score"] = round(prop_scores2["matching_score"], 3)
assert json.dumps(prop_scores1, sort_keys=True, indent=4) == json.dumps(prop_scores2, sort_keys=True, indent=4)

View File

@ -1,3 +1,4 @@
import json
import os import os
import pytest import pytest
@ -71,6 +72,11 @@ def ds2():
yield stix2.MemoryStore(stix_objs) yield stix2.MemoryStore(stix_objs)
@pytest.fixture
def fs():
yield stix2.FileSystemSource(FS_PATH)
def test_object_factory_created_by_ref_str(): def test_object_factory_created_by_ref_str():
factory = stix2.ObjectFactory(created_by_ref=IDENTITY_ID) factory = stix2.ObjectFactory(created_by_ref=IDENTITY_ID)
ind = factory.create(stix2.v21.Indicator, **INDICATOR_KWARGS) ind = factory.create(stix2.v21.Indicator, **INDICATOR_KWARGS)
@ -955,8 +961,30 @@ def test_list_semantic_check(ds, ds2):
) )
assert round(score) == 1 assert round(score) == 1
score = stix2.equivalence.object.list_reference_check(
object_refs2,
object_refs1,
ds2,
ds,
**weights,
)
assert round(score) == 1
def test_graph_equivalence_with_filesystem_source(ds):
def test_graph_similarity_raises_value_error(ds):
weights = {
"_internal": {
"ignore_spec_version": False,
"versioning_checks": False,
"max_depth": -1,
},
}
with pytest.raises(ValueError):
prop_scores1 = {}
stix2.Environment().graph_similarity(ds, ds2, prop_scores1, **weights)
def test_graph_equivalence_with_filesystem_source(ds, fs):
weights = { weights = {
"_internal": { "_internal": {
"ignore_spec_version": True, "ignore_spec_version": True,
@ -965,18 +993,30 @@ def test_graph_equivalence_with_filesystem_source(ds):
}, },
} }
prop_scores1 = {} prop_scores1 = {}
prop_scores2 = {} env1 = stix2.Environment().graph_similarity(fs, ds, prop_scores1, **weights)
fs = stix2.FileSystemSource(FS_PATH)
env = stix2.Environment().graph_similarity(fs, ds, prop_scores1, **weights)
assert round(env) == 26
assert round(prop_scores1["matching_score"]) == 460
assert round(prop_scores1["sum_weights"]) == 18
env = stix2.Environment().graph_similarity(ds, fs, prop_scores2, **weights) # Switching parameters
assert round(env) == 47 weights = {
assert round(prop_scores2["matching_score"]) == 852 "_internal": {
assert round(prop_scores2["sum_weights"]) == 18 "ignore_spec_version": True,
assert prop_scores1 == prop_scores2 "versioning_checks": False,
"max_depth": 1,
},
}
prop_scores2 = {}
env2 = stix2.Environment().graph_similarity(ds, fs, prop_scores2, **weights)
assert round(env1) == 23
assert round(prop_scores1["matching_score"]) == 411
assert round(prop_scores1["len_pairs"]) == 18
assert round(env2) == 23
assert round(prop_scores2["matching_score"]) == 411
assert round(prop_scores2["len_pairs"]) == 18
prop_scores1["matching_score"] = round(prop_scores1["matching_score"], 3)
prop_scores2["matching_score"] = round(prop_scores2["matching_score"], 3)
assert json.dumps(prop_scores1, sort_keys=True, indent=4) == json.dumps(prop_scores2, sort_keys=True, indent=4)
def test_graph_equivalence_with_duplicate_graph(ds): def test_graph_equivalence_with_duplicate_graph(ds):
@ -991,7 +1031,7 @@ def test_graph_equivalence_with_duplicate_graph(ds):
env = stix2.Environment().graph_similarity(ds, ds, prop_scores, **weights) env = stix2.Environment().graph_similarity(ds, ds, prop_scores, **weights)
assert round(env) == 100 assert round(env) == 100
assert round(prop_scores["matching_score"]) == 800 assert round(prop_scores["matching_score"]) == 800
assert round(prop_scores["sum_weights"]) == 8 assert round(prop_scores["len_pairs"]) == 8
def test_graph_equivalence_with_versioning_check_on(ds2, ds): def test_graph_equivalence_with_versioning_check_on(ds2, ds):
@ -1002,11 +1042,29 @@ def test_graph_equivalence_with_versioning_check_on(ds2, ds):
"max_depth": 1, "max_depth": 1,
}, },
} }
prop_scores = {} prop_scores1 = {}
env = stix2.Environment().graph_similarity(ds, ds2, prop_scores, **weights) env1 = stix2.Environment().graph_similarity(ds, ds2, prop_scores1, **weights)
assert round(env) == 93 assert round(env1) == 88
assert round(prop_scores["matching_score"]) == 745 assert round(prop_scores1["matching_score"]) == 789
assert round(prop_scores["sum_weights"]) == 8 assert round(prop_scores1["len_pairs"]) == 9
# Switching parameters
weights = {
"_internal": {
"ignore_spec_version": False,
"versioning_checks": False,
"max_depth": 1,
},
}
prop_scores2 = {}
env2 = stix2.Environment().graph_similarity(ds2, ds, prop_scores2, **weights)
assert round(env2) == 88
assert round(prop_scores2["matching_score"]) == 789
assert round(prop_scores2["len_pairs"]) == 9
prop_scores1["matching_score"] = round(prop_scores1["matching_score"], 3)
prop_scores2["matching_score"] = round(prop_scores2["matching_score"], 3)
assert json.dumps(prop_scores1, sort_keys=True, indent=4) == json.dumps(prop_scores2, sort_keys=True, indent=4)
def test_graph_equivalence_with_versioning_check_off(ds2, ds): def test_graph_equivalence_with_versioning_check_off(ds2, ds):
@ -1017,8 +1075,26 @@ def test_graph_equivalence_with_versioning_check_off(ds2, ds):
"max_depth": 1, "max_depth": 1,
}, },
} }
prop_scores = {} prop_scores1 = {}
env = stix2.Environment().graph_similarity(ds, ds2, prop_scores, **weights) env1 = stix2.Environment().graph_similarity(ds, ds2, prop_scores1, **weights)
assert round(env) == 93 assert round(env1) == 88
assert round(prop_scores["matching_score"]) == 745 assert round(prop_scores1["matching_score"]) == 789
assert round(prop_scores["sum_weights"]) == 8 assert round(prop_scores1["len_pairs"]) == 9
# Switching parameters
weights = {
"_internal": {
"ignore_spec_version": False,
"versioning_checks": False,
"max_depth": 1,
},
}
prop_scores2 = {}
env2 = stix2.Environment().graph_similarity(ds2, ds, prop_scores2, **weights)
assert round(env2) == 88
assert round(prop_scores2["matching_score"]) == 789
assert round(prop_scores2["len_pairs"]) == 9
prop_scores1["matching_score"] = round(prop_scores1["matching_score"], 3)
prop_scores2["matching_score"] = round(prop_scores2["matching_score"], 3)
assert json.dumps(prop_scores1, sort_keys=True, indent=4) == json.dumps(prop_scores2, sort_keys=True, indent=4)