def test_append_hash(self): """ Tests append_hash functionality. Makes sure all vertices have a hash value. """ data, edges = _gen_simple_dag() build_block_dag(data, edges, _hashfunc, append_hashes=True) for _, val in data.items(): self.assertIsNotNone(val["hash"])
def test_no_hashfunction(self): """ Tests invocation with no hash function supplied (uses default). """ data, edges = _gen_simple_dag() sig_1 = build_block_dag(data, edges) sig_2 = build_block_dag(data, edges, _hashfunc) self.assertEqual(sig_1["signature"], sig_2["signature"])
def test_append_hash_preexisting(self): """ Tests append_hash functionality and that append_hashes overwrites pre-existing values. """ data, edges = _gen_simple_dag() sig = build_block_dag(data, edges, _hashfunc, append_hashes=True) data["a"]["data"] = [5, 6, 7, 8] sig_2 = build_block_dag(data, edges, _hashfunc, append_hashes=True) self.assertNotEqual(sig["signature"], sig_2["signature"])
def test_changed_names(self): """ Tests two DAGs with the same structured but different key names. Should produce identical signatures. """ data_1, edges_1 = _gen_simple_dag() data_2, _ = _gen_simple_dag() data_2["ab"] = data_2.pop("b") data_2["ba"] = data_2.pop("a") edges_2 = [("ba", "ab"), ("ba", "c"), ("ab", "d"), ("c", "d")] sig_1 = build_block_dag(data_1, edges_1, _hashfunc) sig_2 = build_block_dag(data_2, edges_2, _hashfunc) self.assertEqual(sig_1["signature"], sig_2["signature"])
def test_empty(self): """ Tests a completely empty DAG """ data = {} edges = [] sig = build_block_dag(data, edges, _hashfunc) self.assertEqual({"signature": None}, sig)
def test_filter_to_empty(self): """ Tests a DAG that is filtered to nothing. """ data, edges = _gen_simple_dag() sig = build_block_dag(data, edges, _hashfunc, ["non_field"]) sig.pop("signature") for _, sig_val in sig.items(): self.assertIsNone(sig_val["data_hash"])
def test_simple_empty(self): """ Tests a dag that is filtered with no fields. Should return None as signature. """ data, edges = _gen_simple_dag() sig = build_block_dag(data, edges, _hashfunc, []) sig.pop("signature") for _, sig_val in sig.items(): self.assertIsNone(sig_val["data_hash"])
def test_simple_filtered(self): """ Makes sure that inclusive filtering works """ data, edges = _gen_simple_dag() sig = build_block_dag(data, edges, _hashfunc, ["data"]) self.assertIsNone(sig["d"]["data_hash"]) for key in ["a", "b", "c"]: self.assertIsNotNone(sig[key]["data_hash"])
def test_simple_unfiltered(self): """ A simple test accepting all data in the generated DAG. """ data, edges = _gen_simple_dag() sig = build_block_dag(data, edges, _hashfunc) sig.pop("signature") for _, sig_val in sig.items(): self.assertIsNotNone(sig_val["data_hash"])
def test_multiple_parents(self): """ Tests a DAG with multiple identical parents. Parents hashes should append and not be a set. """ data, edges = _gen_simple_dag() data["e"] = data["a"].copy() edges.append(("e", "b")) sig = build_block_dag(data, edges, _hashfunc, ["data"]) self.assertEqual(sig["a"]["hash"], sig["e"]["hash"]) self.assertEqual(2, len(sig["b"]["parent_hashes"]))
def _main(): data = { "a": {"data": 3, "important": [1, 2], "not_data": "help"}, "b": {"data": "secrets"}, "c": {"data": [1, "two", 3], "important": [4, 5]}, "d": {"not_data": "help"}, } edges = [("a", "c"), ("b", "c"), ("b", "d"), ("c", "d")] sig = build_block_dag(data, edges, _hashfunc, ["data", "important"]) print(pretty_prints(data, edges, sig))
def _main(): data_1 = { "a": { "data": 4 }, "b": { "data": "potato" }, "c": { "data": [1, 2, "three", [4]] }, "d": { "not_data": 4 }, } edges_1 = [("a", "b"), ("a", "c"), ("b", "d"), ("c", "d")] data_2 = { "a": { "data": 4 }, "b": { "data": "potato" }, "c": { "data": [1, 23, "three", [4]] }, "d": { "not_data": 5 }, } edges_2 = [("a", "b"), ("a", "c"), ("b", "d"), ("c", "d")] sig_1 = build_block_dag(data_1, edges_1, _hashfunc, ["data"]) sig_2 = build_block_dag(data_2, edges_2, _hashfunc, ["data"]) dags_same, diff_1, diff_2 = compare_dags(sig_1, sig_2) if not dags_same: print(diff_1) print(diff_2)
def _main(): data = { "a": { "data": 3 }, "b": { "data": "stringy_string" }, "c": { "data": [1, 2, "three", [4, 5]] }, "d": { "not_data": None }, } edges = [("a", "b"), ("a", "c"), ("b", "d"), ("c", "d")] sig = build_block_dag(data, edges, data_fields=["data"], append_hashes=True) print(pretty_prints(data, edges, sig))
def _main(): data, edges = _binomial_tree(15) print(f"{len(data)} vertices\n{len(edges)} edges") sig = build_block_dag(data, edges, _hashfunc, ["data", "important"]) print(pretty_prints(data, edges, sig))