Skip to content

Commit 5aa8c66

Browse files
authored
Merge pull request #97 from oasis-open/memory-tests
Test MemoryStore saving/loading to/from file
2 parents 37e9049 + 7dd222b commit 5aa8c66

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

stix2/sources/memory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def save_to_file(self, file_path, allow_custom=False):
164164
if not os.path.exists(os.path.dirname(file_path)):
165165
os.makedirs(os.path.dirname(file_path))
166166
with open(file_path, "w") as f:
167-
f.write(str(Bundle(self._data.values(), allow_custom=allow_custom)))
167+
f.write(str(Bundle(list(self._data.values()), allow_custom=allow_custom)))
168168
save_to_file.__doc__ = MemoryStore.save_to_file.__doc__
169169

170170

stix2/test/test_memory.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import os
2+
import shutil
3+
14
import pytest
25

36
from stix2 import (Bundle, Campaign, CustomObject, Filter, MemorySource,
@@ -166,6 +169,22 @@ def test_memory_store_query_multiple_filters(mem_store):
166169
assert len(resp) == 1
167170

168171

172+
def test_memory_store_save_load_file(mem_store):
173+
filename = 'memory_test/mem_store.json'
174+
mem_store.save_to_file(filename)
175+
contents = open(os.path.abspath(filename)).read()
176+
177+
assert '"id": "indicator--d81f86b9-975b-bc0b-775e-810c5ad45a4f",' in contents
178+
assert '"id": "indicator--d81f86b8-975b-bc0b-775e-810c5ad45a4f",' in contents
179+
180+
mem_store2 = MemoryStore()
181+
mem_store2.load_from_file(filename)
182+
assert mem_store2.get("indicator--d81f86b8-975b-bc0b-775e-810c5ad45a4f")
183+
assert mem_store2.get("indicator--d81f86b9-975b-bc0b-775e-810c5ad45a4f")
184+
185+
shutil.rmtree(os.path.dirname(filename))
186+
187+
169188
def test_memory_store_add_stix_object_str(mem_store):
170189
# add stix object string
171190
camp_id = "campaign--111111b6-1112-4fb0-111b-b111107ca70a"

0 commit comments

Comments
 (0)