Skip to content

Commit

Permalink
Add tests for threshold sha256 type
Browse files Browse the repository at this point in the history
  • Loading branch information
sbellem committed Jun 20, 2017
1 parent 88bc72d commit 9fcb7d5
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cryptoconditions/types/threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pyasn1.codec.native.decoder import decode as nat_decode

from cryptoconditions.condition import Condition
from cryptoconditions.exceptions import MissingDataError
from cryptoconditions.exceptions import MissingDataError, ValidationError
from cryptoconditions.fulfillment import Fulfillment
from cryptoconditions.types.ed25519 import Ed25519Sha256
from cryptoconditions.types.base_sha256 import BaseSha256
Expand Down Expand Up @@ -55,8 +55,12 @@ def __init__(self, threshold=None):
Args:
threshold (int): Integer threshold
"""
if threshold and (not isinstance(threshold, int) or threshold < 1):
raise ValueError('Threshold must be a integer greater than zero, was: {}'.format(threshold))
if threshold is not None and (
not isinstance(threshold, int) or threshold < 1):
raise ValueError(
'Threshold must be a integer greater than zero, was: {}'.
format(threshold)
)
self.threshold = threshold
self.subconditions = []

Expand Down Expand Up @@ -141,7 +145,7 @@ def asn1_dict_payload(self):
key=lambda x: x['body'].cost,
)
if len(subfulfillments) < self.threshold:
raise Exception('Not enough fulfillments')
raise ValidationError('Not enough fulfillments')

minimal_fulfillments = subfulfillments[:self.threshold]
remaining_conditions = chain(
Expand Down
86 changes: 86 additions & 0 deletions tests/types/test_threshold.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from pytest import mark, raises


class TestThresholdSha256:

@mark.parametrize('threshold', (0, 0.1, 'a'))
def test_init_with_invalid_threshold(self, threshold):
from cryptoconditions.types.threshold import ThresholdSha256
with raises(ValueError) as exc:
ThresholdSha256(threshold=threshold)
assert exc.value.args == (
'Threshold must be a integer greater than zero, was: {}'.
format(threshold),
)

def test_add_subcondition_type_error(self):
from cryptoconditions.types.threshold import ThresholdSha256
threshold_obj = ThresholdSha256()
with raises(TypeError) as exc:
threshold_obj.add_subcondition(123)
assert exc.value.args == (
'Subconditions must be URIs or objects of type Condition',)

def test_add_subcondition_as_uri(self, minimal_threshold):
from cryptoconditions.types.threshold import ThresholdSha256
from cryptoconditions.fulfillment import Fulfillment
threshold_obj = ThresholdSha256(
threshold=minimal_threshold.json['threshold'])
subfulfillment = Fulfillment.from_json(
minimal_threshold.json['subfulfillments'][0])
subcondition_uri = subfulfillment.condition_uri
threshold_obj.add_subcondition(subcondition_uri)
threshold_obj.serialize_uri == minimal_threshold.fulfillment

def test_add_subcondition_as_object(self, minimal_threshold):
from cryptoconditions.types.threshold import ThresholdSha256
from cryptoconditions.fulfillment import Fulfillment
threshold_obj = ThresholdSha256(
threshold=minimal_threshold.json['threshold'])
subfulfillment = Fulfillment.from_json(
minimal_threshold.json['subfulfillments'][0])
subcondition_object = subfulfillment.condition
threshold_obj.add_subcondition(subcondition_object)
threshold_obj.serialize_uri == minimal_threshold.fulfillment

def test_add_subfulfillment_type_error(self):
from cryptoconditions.types.threshold import ThresholdSha256
threshold_obj = ThresholdSha256()
with raises(TypeError) as exc:
threshold_obj.add_subfulfillment(123)
assert exc.value.args == (
'Subfulfillments must be URIs or objects of type Fulfillment',)

def test_add_subfulfillment_as_uri(self, minimal_threshold):
from cryptoconditions.types.threshold import ThresholdSha256
from cryptoconditions.fulfillment import Fulfillment
threshold_obj = ThresholdSha256(
threshold=minimal_threshold.json['threshold'])
subfulfillment = Fulfillment.from_json(
minimal_threshold.json['subfulfillments'][0])
subfulfillment_uri = subfulfillment.serialize_uri()
threshold_obj.add_subfulfillment(subfulfillment_uri)
threshold_obj.serialize_uri == minimal_threshold.fulfillment

def test_add_subfulfillment_as_object(self, minimal_threshold):
from cryptoconditions.types.threshold import ThresholdSha256
from cryptoconditions.fulfillment import Fulfillment
threshold_obj = ThresholdSha256(
threshold=minimal_threshold.json['threshold'])
subfulfillment_object = Fulfillment.from_json(
minimal_threshold.json['subfulfillments'][0])
threshold_obj.add_subfulfillment(subfulfillment_object)
threshold_obj.serialize_uri == minimal_threshold.fulfillment

def test_asn1_dict_payload(self):
from cryptoconditions.exceptions import ValidationError
from cryptoconditions.types.threshold import ThresholdSha256
threshold_obj = ThresholdSha256(threshold=1)
with raises(ValidationError) as exc:
threshold_obj.asn1_dict_payload
assert exc.value.args == ('Not enough fulfillments',)

def test_calculate_worst_case_length(self):
from cryptoconditions.types.threshold import ThresholdSha256
cost = ThresholdSha256.calculate_worst_case_length(1, ())
assert cost == float('-inf')

0 comments on commit 9fcb7d5

Please sign in to comment.