In the test_properties.py test suites, I thought code like

assert prop.clean(...)

doesn't test well enough since clean() methods on this branch
produce 2-tuples, and you should test what's in the tuple, not
just that it returned something non-empty.  So I fixed it in
several places to test the tuple contents.
pull/1/head
Michael Chisholm 2020-06-24 19:39:47 -04:00
parent 03c265c3a3
commit 986404b7b7
3 changed files with 16 additions and 24 deletions

View File

@ -71,7 +71,9 @@ def test_property_fixed_and_required():
def test_list_property_property_type(): def test_list_property_property_type():
p = ListProperty(StringProperty) p = ListProperty(StringProperty)
assert p.clean(['abc', 'xyz'], False) result = p.clean(['abc', 'xyz'], False)
assert result == (['abc', 'xyz'], False)
with pytest.raises(ValueError): with pytest.raises(ValueError):
p.clean([], False) p.clean([], False)
@ -89,7 +91,8 @@ def test_list_property_property_type_custom():
TestObj(foo="xyz"), TestObj(foo="xyz"),
] ]
assert p.clean(objs_custom, True) result = p.clean(objs_custom, True)
assert result == (objs_custom, True)
with pytest.raises(CustomContentError): with pytest.raises(CustomContentError):
p.clean(objs_custom, False) p.clean(objs_custom, False)
@ -99,7 +102,8 @@ def test_list_property_property_type_custom():
{"foo": "xyz"}, {"foo": "xyz"},
] ]
assert p.clean(dicts_custom, True) result = p.clean(dicts_custom, True)
assert result == (objs_custom, True)
with pytest.raises(ExtraPropertiesError): with pytest.raises(ExtraPropertiesError):
p.clean(dicts_custom, False) p.clean(dicts_custom, False)
@ -114,10 +118,12 @@ def test_list_property_object_type():
p = ListProperty(TestObj) p = ListProperty(TestObj)
objs = [TestObj(foo="abc"), TestObj(foo="xyz")] objs = [TestObj(foo="abc"), TestObj(foo="xyz")]
assert p.clean(objs, False) result = p.clean(objs, False)
assert result == (objs, False)
dicts = [{"foo": "abc"}, {"foo": "xyz"}] dicts = [{"foo": "abc"}, {"foo": "xyz"}]
assert p.clean(dicts, False) result = p.clean(dicts, False)
assert result == (objs, False)
def test_list_property_object_type_custom(): def test_list_property_object_type_custom():
@ -133,7 +139,8 @@ def test_list_property_object_type_custom():
TestObj(foo="xyz"), TestObj(foo="xyz"),
] ]
assert p.clean(objs_custom, True) result = p.clean(objs_custom, True)
assert result == (objs_custom, True)
with pytest.raises(CustomContentError): with pytest.raises(CustomContentError):
p.clean(objs_custom, False) p.clean(objs_custom, False)
@ -143,7 +150,8 @@ def test_list_property_object_type_custom():
{"foo": "xyz"}, {"foo": "xyz"},
] ]
assert p.clean(dicts_custom, True) result = p.clean(dicts_custom, True)
assert result == (objs_custom, True)
with pytest.raises(ExtraPropertiesError): with pytest.raises(ExtraPropertiesError):
p.clean(dicts_custom, False) p.clean(dicts_custom, False)

View File

@ -16,6 +16,7 @@ from stix2.v20.common import MarkingProperty
from . import constants from . import constants
ID_PROP = IDProperty('my-type', spec_version="2.0") ID_PROP = IDProperty('my-type', spec_version="2.0")
MY_ID = 'my-type--232c9d3f-49fc-4440-bb01-607f638778e7' MY_ID = 'my-type--232c9d3f-49fc-4440-bb01-607f638778e7'

View File

@ -23,23 +23,6 @@ def test_dictionary_property():
p.clean({}) p.clean({})
def test_string_property():
prop = StringProperty()
assert prop.clean('foobar')
assert prop.clean(1)
assert prop.clean([1, 2, 3])
def test_type_property():
prop = TypeProperty('my-type')
assert prop.clean('my-type')
with pytest.raises(ValueError):
prop.clean('not-my-type')
assert prop.clean(prop.default())
ID_PROP = IDProperty('my-type', spec_version="2.1") ID_PROP = IDProperty('my-type', spec_version="2.1")
MY_ID = 'my-type--232c9d3f-49fc-4440-bb01-607f638778e7' MY_ID = 'my-type--232c9d3f-49fc-4440-bb01-607f638778e7'