diff --git a/pytaxonomies/api.py b/pytaxonomies/api.py index aa8d7e8..679eea3 100644 --- a/pytaxonomies/api.py +++ b/pytaxonomies/api.py @@ -126,8 +126,12 @@ class Taxonomy(collections.Mapping): p, v = self._json_predicates() if p: to_return['predicates'] = p + else: + to_return['predicates'] = None if v: to_return['values'] = v + else: + to_return['values'] = None return to_return def has_entries(self): diff --git a/tests/tests.py b/tests/tests.py index f5fb961..04c338d 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -5,13 +5,27 @@ import json import unittest from pytaxonomies import Taxonomies, EncodeTaxonomies import pytaxonomies.api +import os class TestPyTaxonomies(unittest.TestCase): def setUp(self): self.taxonomies = Taxonomies() - self.taxonomies_offline = Taxonomies(manifest_path="./misp-taxonomies/MANIFEST.json") + self.manifest_path = "./misp-taxonomies/MANIFEST.json" + self.taxonomies_offline = Taxonomies(manifest_path=self.manifest_path) + self.json_load_taxonomies() + + def __load_path(self, path): + with open(path, 'r') as f: + return json.load(f) + + def json_load_taxonomies(self): + self.manifest = self.__load_path(self.manifest_path) + self.loaded_tax = {} + for t in self.manifest['taxonomies']: + path = '{}/{}/{}'.format(os.path.dirname(os.path.realpath(self.manifest_path)), t['name'], self.manifest['path']) + self.loaded_tax[t['name']] = self.__load_path(path) def test_compareOnlineOffilne(self): self.assertEqual(str(self.taxonomies), str(self.taxonomies_offline)) @@ -54,16 +68,23 @@ class TestPyTaxonomies(unittest.TestCase): Taxonomies(manifest_path="./misp-taxonomies/MANIFEST.json") pytaxonomies.api.HAS_REQUESTS = True - def test_machinetags(self): + def test_revert_machinetags(self): tax = list(self.taxonomies.values())[0] for p in tax.values(): mt = tax.make_machinetag(p) self.taxonomies.revert_machinetag(mt) def test_json(self): - for t in self.taxonomies: + for key, t in self.taxonomies.items(): json.dumps(t, cls=EncodeTaxonomies) + def test_recreate_dump(self): + self.maxDiff = None + for key, t in self.taxonomies.items(): + out = t._json() + print(t.name) + self.assertCountEqual(out, self.loaded_tax[t.name]) + if __name__ == "__main__": unittest.main()