Merge pull request #454 from chisholm/fix_make_constant

Fix AST make_constant() function to create TimestampConstants
pull/1/head
Chris Lenk 2020-09-16 09:23:15 -04:00 committed by GitHub
commit 5a34d529a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 1 deletions

View File

@ -227,7 +227,7 @@ def make_constant(value):
return value
try:
return parse_into_datetime(value)
return TimestampConstant(value)
except (ValueError, TypeError):
pass

View File

@ -1,9 +1,11 @@
import datetime
import pytest
import pytz
import stix2
from stix2.pattern_visitor import create_pattern_object
import stix2.utils
def test_create_comparison_expression():
@ -482,6 +484,44 @@ def test_invalid_startstop_qualifier():
)
@pytest.mark.parametrize(
"input_, expected_class, expected_value", [
(1, stix2.patterns.IntegerConstant, 1),
(1.5, stix2.patterns.FloatConstant, 1.5),
("abc", stix2.patterns.StringConstant, "abc"),
(True, stix2.patterns.BooleanConstant, True),
(
"2001-02-10T21:36:15Z", stix2.patterns.TimestampConstant,
stix2.utils.STIXdatetime(2001, 2, 10, 21, 36, 15, tzinfo=pytz.utc),
),
(
datetime.datetime(2001, 2, 10, 21, 36, 15, tzinfo=pytz.utc),
stix2.patterns.TimestampConstant,
stix2.utils.STIXdatetime(2001, 2, 10, 21, 36, 15, tzinfo=pytz.utc),
),
],
)
def test_make_constant_simple(input_, expected_class, expected_value):
const = stix2.patterns.make_constant(input_)
assert isinstance(const, expected_class)
assert const.value == expected_value
def test_make_constant_list():
list_const = stix2.patterns.make_constant([1, 2, 3])
assert isinstance(list_const, stix2.patterns.ListConstant)
assert all(
isinstance(elt, stix2.patterns.IntegerConstant)
for elt in list_const.value
)
assert all(
int_const.value == test_elt
for int_const, test_elt in zip(list_const.value, [1, 2, 3])
)
def test_make_constant_already_a_constant():
str_const = stix2.StringConstant('Foo')
result = stix2.patterns.make_constant(str_const)

View File

@ -1,10 +1,12 @@
import datetime
import pytest
import pytz
from stix2patterns.exceptions import ParseException
import stix2
from stix2.pattern_visitor import create_pattern_object
import stix2.utils
def test_create_comparison_expression():
@ -603,6 +605,44 @@ def test_invalid_startstop_qualifier():
)
@pytest.mark.parametrize(
"input_, expected_class, expected_value", [
(1, stix2.patterns.IntegerConstant, 1),
(1.5, stix2.patterns.FloatConstant, 1.5),
("abc", stix2.patterns.StringConstant, "abc"),
(True, stix2.patterns.BooleanConstant, True),
(
"2001-02-10T21:36:15Z", stix2.patterns.TimestampConstant,
stix2.utils.STIXdatetime(2001, 2, 10, 21, 36, 15, tzinfo=pytz.utc),
),
(
datetime.datetime(2001, 2, 10, 21, 36, 15, tzinfo=pytz.utc),
stix2.patterns.TimestampConstant,
stix2.utils.STIXdatetime(2001, 2, 10, 21, 36, 15, tzinfo=pytz.utc),
),
],
)
def test_make_constant_simple(input_, expected_class, expected_value):
const = stix2.patterns.make_constant(input_)
assert isinstance(const, expected_class)
assert const.value == expected_value
def test_make_constant_list():
list_const = stix2.patterns.make_constant([1, 2, 3])
assert isinstance(list_const, stix2.patterns.ListConstant)
assert all(
isinstance(elt, stix2.patterns.IntegerConstant)
for elt in list_const.value
)
assert all(
int_const.value == test_elt
for int_const, test_elt in zip(list_const.value, [1, 2, 3])
)
def test_make_constant_already_a_constant():
str_const = stix2.StringConstant('Foo')
result = stix2.patterns.make_constant(str_const)