Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from hivemind.hivemind import HivemindIssue, HivemindOption, HivemindOpinion, HivemindState
from helpers.ipfshelpers import IPFS_API
from helpers.hotwallethelpers import get_address_from_wallet
from helpers.hotwallethelpers import get_private_key_from_wallet
from helpers.messagehelpers import sign_message
print('Starting Spellbook integration test: hivemind with multiple questions')
print('----------------------------------------------\n')
questions = ['Which number is bigger?', 'Which number is smaller?']
description = 'Rank the numbers in ascending or descending order depending on the question.'
option_type = 'String'
hivemind_issue = HivemindIssue()
assert isinstance(hivemind_issue, HivemindIssue)
for question_index, question in enumerate(questions):
print('question %s: %s' % (question_index, question))
hivemind_issue.add_question(question=question)
assert hivemind_issue.questions[question_index] == question
print('description:', description)
hivemind_issue.set_description(description=description)
assert hivemind_issue.description == description
print('option_type:', option_type)
hivemind_issue.set_answer_type(answer_type=option_type)
assert hivemind_issue.answer_type == option_type
print(hivemind_option.get())
STRING_OPTION1_HASH = hivemind_option.save()
print(STRING_OPTION1_HASH)
hivemind_option.set('fortytwo')
STRING_OPTION2_HASH = hivemind_option.save()
hivemind_state = HivemindState()
hivemind_state.set_hivemind_issue(issue_hash=STRING_ISSUE_HASH)
hivemind_state.add_option(STRING_OPTION1_HASH)
hivemind_state.add_option(STRING_OPTION2_HASH)
STRING_STATE_HASH = hivemind_state.save()
hivemind_issue = HivemindIssue()
hivemind_issue.add_question(question='Choose a number')
hivemind_issue.set_description(description='Choose a number')
hivemind_issue.answer_type = 'Integer'
hivemind_issue.set_constraints({'min_value': 0, 'max_value': 10})
INTEGER_QUESTION_HASH = hivemind_issue.save()
hivemind_option = HivemindOption()
hivemind_option.set_hivemind_issue(INTEGER_QUESTION_HASH)
hivemind_option.set(8)
INTEGER_OPTION1_HASH = hivemind_option.save()
hivemind_option.set(5)
INTEGER_OPTION2_HASH = hivemind_option.save()
hivemind_option.set(6)
def test_initialization(self):
assert isinstance(HivemindIssue(), HivemindIssue)
from helpers.hotwallethelpers import get_private_key_from_wallet
from helpers.messagehelpers import sign_message
print('Starting Spellbook integration test: hivemind with restrictions')
print('----------------------------------------------\n')
question = 'Which number is bigger?'
description = 'Rank the numbers from high to low'
option_type = 'String'
print('question:', question)
print('description:', description)
print('option_type:', option_type)
hivemind_issue = HivemindIssue()
assert isinstance(hivemind_issue, HivemindIssue)
hivemind_issue.add_question(question=question)
assert hivemind_issue.questions[0] == question
hivemind_issue.set_description(description=description)
assert hivemind_issue.description == description
hivemind_issue.set_answer_type(answer_type=option_type)
assert hivemind_issue.answer_type == option_type
hivemind_issue.set_tags(tags='mycompanyhash')
assert hivemind_issue.tags == 'mycompanyhash'
restrictions = {'addresses': [get_address_from_wallet(account=0, index=0), get_address_from_wallet(account=0, index=1)],
'options_per_address': 10}
print('\n\n###############################')
print('#Hivemind issue #')
print('###############################')
question0 = 'Which number is bigger?'
question1 = 'Which number is smaller?'
description = 'Rank the numbers from high to low'
answer_type = 'String'
print('question0:', question0)
print('question1:', question1)
print('description:', description)
print('answer_type:', answer_type)
print('Test initialization')
hivemind_issue = HivemindIssue()
assert isinstance(hivemind_issue, HivemindIssue)
print('Test adding main question')
hivemind_issue.add_question(question=question0)
assert hivemind_issue.questions[0] == question0
print('Test adding second question')
hivemind_issue.add_question(question=question1)
assert hivemind_issue.questions[1] == question1
print('Test if an existing question can not be added twice')
hivemind_issue.add_question(question=question0)
assert len(hivemind_issue.questions) == 2
print('Test only a string or unicode can be added as a question')
hivemind_issue.add_question(question=1)
self.questions = []
self.description = ''
self.tags = None
self.answer_type = u'String'
self.consensus_type = u'Single' # Single or Ranked: Is the expected result of this question a single answer or a ranked list?
self.constraints = None
self.restrictions = None
# What happens when an option is selected: valid values are None, Finalize, Exclude, Reset
# None : nothing happens
# Finalize : Hivemind is finalized, no new options or opinions can be added anymore
# Exclude : The selected option is excluded from the results
# Reset : All opinions are reset
self.on_selection = None
super(HivemindIssue, self).__init__(multihash=multihash)
def set_hivemind_issue(self, hivemind_issue_hash):
self.hivemind_issue_hash = hivemind_issue_hash
self._hivemind_issue = HivemindIssue(multihash=self.hivemind_issue_hash)
self.answer_type = self._hivemind_issue.answer_type
def is_valid_hivemind_option(self):
try:
isinstance(HivemindIssue(multihash=self.value), HivemindIssue)
except Exception as ex:
LOG.error('IPFS hash %s is not a valid hivemind: %s' % (self.value, ex))
return False
return True