Skip to content

Commit

Permalink
Update and add tests for explicit AssertionErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
titusz committed Jan 30, 2022
1 parent cfeb94a commit 9a7e720
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 23 deletions.
5 changes: 5 additions & 0 deletions tests/test_code_content_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ def test_gen_image_code_schema_conformance():
assert iscc_obj.iscc == "ISCC:EEA4GQZQTY6J5DTH"


def test_soft_hash_image_v0_larger_256_raises():
with pytest.raises(AssertionError):
ic.soft_hash_image_v0(IMG_SAMPLE_PIXELS, bits=288)


IMG_WHITE_PIXELS = [255] * 1024
IMG_BLACK_PIXELS = [0] * 1024
IMG_SAMPLE_PIXELS = [
Expand Down
24 changes: 24 additions & 0 deletions tests/test_iscc_id.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-
import io

import pytest

import iscc_core as ic


Expand Down Expand Up @@ -62,3 +65,24 @@ def test_incr_iscc_id_v0():
assert ic.iscc_id.incr_iscc_id_v0("MAADB7WD7TC5XELQ") == "MAADB7WD7TC5XELQAE"
assert ic.Code("MAADB7WD7TC5XELQAE").explain == "ID-PRIVATE-V0-64-30fec3fcc5db9170-1"
assert ic.iscc_id.incr_iscc_id_v0("MAADB7WD7TC5XELQAE") == "MAADB7WD7TC5XELQAI"


def test_incr_iscc_id_v0_raises_wrong_mt():
mc = ic.Code.rnd(ic.MT.META).code
with pytest.raises(AssertionError):
ic.incr_iscc_id_v0(mc)


def test_incr_iscc_id_v0_raises_wrong_vs():
mc = ic.Code.rnd(ic.MT.ID)
head = list(mc._head)
head[2] = 1
mc._head = tuple(head)
with pytest.raises(AssertionError):
ic.incr_iscc_id_v0(mc.code)


def test_gen_iscc_id_v0_raises_chain_id():
code = ic.Code.rnd(ic.MT.ISCC, bits=256)
with pytest.raises(AssertionError):
ic.gen_iscc_id_v0(5, code.code)
10 changes: 5 additions & 5 deletions tests/test_minhash.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ def test_minhash_empty():


def test_minhash_single_feature():
mh = ic.minhash([2 ** 16])
mh = ic.minhash([2**16])
assert isinstance(mh, list)
assert len(mh) == 64
assert mh[0] == 1968499307
assert mh[-1] == 2739100501


def test_minhash_32bit_features():
i32 = 2 ** 32 - 1
mh = ic.minhash([2 ** 64 - 1])
i32 = 2**32 - 1
mh = ic.minhash([2**64 - 1])
for n in mh:
assert n <= i32


def test_minhash_compress():
mh = ic.minhash([2 ** 16])
mh = ic.minhash([2**16])
digest = ic.compress(mh)
assert len(digest) == 32
assert digest.hex() == "a18e2fb2bd663d21db9c7dcc9ae78380253cae5bf089766d87a6b51fcb3f8f8e"
Expand All @@ -35,5 +35,5 @@ def test_minhash_compress():


def test_minhash_64():
mh = ic.minhash_64([2 ** 16])
mh = ic.minhash_64([2**16])
assert mh.hex() == "a18e2fb2bd663d21"
36 changes: 18 additions & 18 deletions tests/test_simhash.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@


def test_similarity_hash():
all_zero = 0b0 .to_bytes(8, "big")
all_zero = 0b0.to_bytes(8, "big")
assert iscc_core.simhash.similarity_hash([all_zero, all_zero]) == all_zero

all_ones = 0b11111111 .to_bytes(1, "big")
all_ones = 0b11111111.to_bytes(1, "big")
assert iscc_core.simhash.similarity_hash([all_ones, all_ones]) == all_ones

a = 0b0110 .to_bytes(1, "big")
b = 0b1100 .to_bytes(1, "big")
r = 0b1110 .to_bytes(1, "big")
a = 0b0110.to_bytes(1, "big")
b = 0b1100.to_bytes(1, "big")
r = 0b1110.to_bytes(1, "big")
assert iscc_core.simhash.similarity_hash([a, b]) == r

a = 0b01101001 .to_bytes(1, "big")
b = 0b00111000 .to_bytes(1, "big")
c = 0b11100100 .to_bytes(1, "big")
r = 0b01101000 .to_bytes(1, "big")
a = 0b01101001.to_bytes(1, "big")
b = 0b00111000.to_bytes(1, "big")
c = 0b11100100.to_bytes(1, "big")
r = 0b01101000.to_bytes(1, "big")
assert iscc_core.simhash.similarity_hash([a, b, c]) == r

a = 0b01100101 .to_bytes(1, "big")
b = 0b01011001 .to_bytes(1, "big")
c = 0b10010101 .to_bytes(1, "big")
d = 0b10101001 .to_bytes(1, "big")
r = 0b11111101 .to_bytes(1, "big")
a = 0b01100101.to_bytes(1, "big")
b = 0b01011001.to_bytes(1, "big")
c = 0b10010101.to_bytes(1, "big")
d = 0b10101001.to_bytes(1, "big")
r = 0b11111101.to_bytes(1, "big")
assert iscc_core.simhash.similarity_hash([a, b, c, d]) == r

a = 0b0110100101101001 .to_bytes(2, "big")
b = 0b0011100000111000 .to_bytes(2, "big")
c = 0b1110010011100100 .to_bytes(2, "big")
r = 0b0110100001101000 .to_bytes(2, "big")
a = 0b0110100101101001.to_bytes(2, "big")
b = 0b0011100000111000.to_bytes(2, "big")
c = 0b1110010011100100.to_bytes(2, "big")
r = 0b0110100001101000.to_bytes(2, "big")
assert iscc_core.simhash.similarity_hash([a, b, c]) == r


Expand Down
12 changes: 12 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import io
import os

import pytest
import iscc_core as ic
Expand Down Expand Up @@ -110,3 +111,14 @@ def test_ipfs_hash_raises(static_bytes):

def test_canonicalize():
assert ic.canonicalize({"hello": "wörld"}) == b'{"hello":"w\xc3\xb6rld"}'


def test_sliding_window_raises():
with pytest.raises(AssertionError):
ic.sliding_window([1, 2, 3, 4, 5], 1)


def test_hamming_distance_raises():
a, b = os.urandom(8), os.urandom(9)
with pytest.raises(AssertionError):
ic.hamming_distance(a, b)

0 comments on commit 9a7e720

Please sign in to comment.