PyTaxonomies/tests/tests.py

86 lines
2.7 KiB
Python
Raw Normal View History

2016-07-25 18:48:08 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2016-10-13 17:23:10 +02:00
import json
2016-07-25 18:48:08 +02:00
import unittest
2016-10-13 17:23:10 +02:00
from pytaxonomies import Taxonomies, EncodeTaxonomies
import pytaxonomies.api
2016-10-14 12:03:39 +02:00
import os
2016-07-25 18:48:08 +02:00
class TestPyTaxonomies(unittest.TestCase):
def setUp(self):
2017-07-21 10:07:55 +02:00
self.taxonomies_offline = Taxonomies()
2016-10-14 12:03:39 +02:00
self.loaded_tax = {}
2017-07-21 10:07:55 +02:00
for t in self.taxonomies_offline.manifest['taxonomies']:
with open('{}/{}/{}'.format(self.taxonomies_offline.url, t['name'], 'machinetag.json'), 'r') as f:
self.loaded_tax[t['name']] = json.load(f)
2016-07-25 18:48:08 +02:00
2016-07-28 11:56:02 +02:00
def test_compareOnlineOffilne(self):
2017-07-21 10:07:55 +02:00
taxonomies_online = Taxonomies(manifest_path=None)
self.assertEqual(str(taxonomies_online), str(self.taxonomies_offline))
2016-07-25 18:48:08 +02:00
2016-07-28 11:56:02 +02:00
def test_expanded_machinetags(self):
2017-07-21 10:07:55 +02:00
self.taxonomies_offline.all_machinetags(expanded=True)
2016-07-25 19:38:17 +02:00
2016-07-28 11:56:02 +02:00
def test_machinetags(self):
2017-07-21 10:07:55 +02:00
self.taxonomies_offline.all_machinetags()
2016-07-25 19:38:17 +02:00
2016-07-28 11:56:02 +02:00
def test_dict(self):
2017-07-21 10:07:55 +02:00
len(self.taxonomies_offline)
for n, t in self.taxonomies_offline.items():
2016-07-25 19:38:17 +02:00
len(t)
for p, value in t.items():
continue
2016-07-28 11:56:02 +02:00
def test_search(self):
2017-07-21 10:07:55 +02:00
self.taxonomies_offline.search('phish')
2016-07-28 11:56:02 +02:00
def test_search_expanded(self):
2017-07-21 10:07:55 +02:00
self.taxonomies_offline.search('phish', expanded=True)
2016-07-25 19:38:17 +02:00
def test_print_classes(self):
2017-07-21 10:07:55 +02:00
for taxonomy in self.taxonomies_offline.values():
2016-10-14 18:24:21 +02:00
print(taxonomy)
for predicate in taxonomy.values():
print(predicate)
for entry in predicate.values():
print(entry)
2016-07-28 12:58:20 +02:00
def test_amountEntries(self):
2017-07-21 10:07:55 +02:00
for tax in self.taxonomies_offline.values():
tax.amount_entries()
2016-07-28 12:58:20 +02:00
def test_missingDependency(self):
pytaxonomies.api.HAS_REQUESTS = False
with self.assertRaises(Exception):
2017-07-21 10:07:55 +02:00
Taxonomies(manifest_path=None)
Taxonomies()
pytaxonomies.api.HAS_REQUESTS = True
2016-10-14 12:03:39 +02:00
def test_revert_machinetags(self):
2017-07-21 10:07:55 +02:00
for tax in self.taxonomies_offline.values():
2016-10-14 18:29:10 +02:00
for p in tax.values():
2016-10-14 18:36:30 +02:00
if tax.has_entries():
for e in p.values():
mt = tax.make_machinetag(p, e)
2017-07-21 10:07:55 +02:00
self.taxonomies_offline.revert_machinetag(mt)
2016-10-14 18:36:30 +02:00
else:
mt = tax.make_machinetag(p)
2017-07-21 10:07:55 +02:00
self.taxonomies_offline.revert_machinetag(mt)
2016-07-25 19:38:17 +02:00
2016-10-13 17:23:10 +02:00
def test_json(self):
2017-07-21 10:07:55 +02:00
for key, t in self.taxonomies_offline.items():
2016-10-13 17:23:10 +02:00
json.dumps(t, cls=EncodeTaxonomies)
2016-10-14 12:03:39 +02:00
def test_recreate_dump(self):
self.maxDiff = None
2017-07-21 10:07:55 +02:00
for key, t in self.taxonomies_offline.items():
2016-10-14 12:03:39 +02:00
out = t._json()
self.assertCountEqual(out, self.loaded_tax[t.name])
2016-10-13 17:23:10 +02:00
2016-07-25 18:48:08 +02:00
if __name__ == "__main__":
unittest.main()