Skip to content

Commit f6173c0

Browse files
committed
fix rdb tests, handle timestamps as text
1 parent 98d5fc4 commit f6173c0

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

stix2/datastore/relational_db/database_backends/database_backend_base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ def process_value_for_insert(self, stix_type, value):
156156
sql_type = stix_type.determine_sql_type(self)
157157
if sql_type == self.determine_sql_type_for_timestamp_property() and isinstance(value, STIXdatetime):
158158
return value.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
159-
elif sql_type == self.determine_sql_type_for_hex_property() and isinstance(stix_type, HexProperty):
159+
elif sql_type == self.determine_sql_type_for_hex_property() and isinstance(stix_type, HexProperty) and \
160+
sql_type is not Text:
161+
# make sure it isn't represented as Text
160162
return bytes.fromhex(value)
161163
else:
162164
return value

stix2/datastore/relational_db/input_creation.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ def generate_insert_for_dictionary_list(table, next_id, value):
6969

7070
@add_method(DictionaryProperty)
7171
def generate_insert_information(self, dictionary_name, stix_object, **kwargs): # noqa: F811
72-
bindings = dict()
7372
data_sink = kwargs.get("data_sink")
7473
table_name = kwargs.get("table_name")
7574
schema_name = kwargs.get("schema_name")
@@ -103,23 +102,37 @@ def generate_insert_information(self, dictionary_name, stix_object, **kwargs):
103102
]
104103
child_table_inserts = generate_insert_for_dictionary_list(table_child, next_id, value)
105104
value = next_id
105+
stix_type = IntegerProperty()
106+
else:
107+
contained_type = valid_types[0].contained
108+
stix_type = ListProperty(contained_type)
109+
value = [data_sink.db_backend.process_value_for_insert(contained_type, x) for x in value]
106110
else:
107111
value_binding = "value"
112+
stix_type = StringProperty()
108113
elif isinstance(value, int) and is_valid_type(IntegerProperty, valid_types):
109114
value_binding = "integer_value"
115+
stix_type = IntegerProperty()
110116
elif isinstance(value, str) and is_valid_type(StringProperty, valid_types):
111117
value_binding = "string_value"
118+
stix_type = StringProperty()
112119
elif isinstance(value, bool) and is_valid_type(BooleanProperty, valid_types):
113120
value_binding = "boolean_value"
121+
stix_type = BooleanProperty()
114122
elif isinstance(value, float) and is_valid_type(FloatProperty, valid_types):
115123
value_binding = "float_value"
124+
stix_type = FloatProperty()
116125
elif isinstance(value, STIXdatetime) and is_valid_type(TimestampProperty, valid_types):
117126
value_binding = "timestamp_value"
127+
stix_type = TimestampProperty()
118128
else:
119129
value_binding = "string_value"
130+
stix_type = StringProperty()
120131

121132
bindings["name"] = name
122-
bindings[value_binding] = value
133+
bindings[value_binding] = data_sink.db_backend.process_value_for_insert(stix_type, value)
134+
135+
123136

124137
insert_statements.append(insert(table).values(bindings))
125138

@@ -285,11 +298,7 @@ def generate_insert_information( # noqa: F811
285298
return insert_statements
286299
else:
287300
if db_backend.array_allowed():
288-
if isinstance(self.contained, HexProperty):
289-
return {name: [data_sink.db_backend.process_value_for_insert(self.contained, x) for x in stix_object[name]]}
290-
else:
291-
return {name: stix_object[name]}
292-
301+
return {name: [data_sink.db_backend.process_value_for_insert(self.contained, x) for x in stix_object[name]]}
293302
else:
294303
insert_statements = list()
295304
table = data_sink.tables_dictionary[

stix2/test/v21/test_datastore_relational_db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
PostgresBackend(_DB_CONNECT_URL, True),
2222
True,
2323
None,
24-
False,
24+
True,
2525
)
2626

2727
# Artifacts

0 commit comments

Comments
 (0)