class TransactionLogTest(unittest.TestCase): def setUp(self): log = [{ 'transactionJson': { 'contractID': 'good_contract_id', 'methodID': 'init' }, 'timestamp': '2018-01-01 00:00:00.000' }, { 'transactionJson': { 'contractID': 'last_contract_id', 'methodID': 'init' }, 'timestamp': '2018-01-03 00:00:00.000' }, { 'transactionJson': { 'contractID': 'good_contract_id', 'methodID': 'add_signature' }, 'timestamp': '2018-01-02 00:00:00.000' }] self.transaction_log = TransactionLog(log) def test_filter_by_contract_name(self): good_contract_transaction_log = self.transaction_log.filter_by_contract_name( 'good_contract_id') self.assertEquals(len(good_contract_transaction_log), 2) self.assertEquals(len(self.transaction_log), 3) def test_filter_by_method_name(self): add_signature_transaction_log = self.transaction_log.filter_by_method_name( 'add_signature') self.assertEquals(len(add_signature_transaction_log), 1) self.assertEquals(len(self.transaction_log), 3) def test_get_last_transaction(self): last_transaction = self.transaction_log.get_last_transaction() self.assertEquals(last_transaction, { 'contractID': 'last_contract_id', 'methodID': 'init' }) def test_get_last_transaction_should_throw_error_if_empty(self): empty_transaction_log = TransactionLog([]) with self.assertRaises(EmptyTransactionLogException): empty_transaction_log.get_last_transaction()
def test_get_results_returns_correct_result(self): inputs = ['new_petition_object'] no_signatures_transaction_log = loads("""[ {"transactionJson":{"inputIDs":[],"outputs":["petition_token"],"contractID":"petition_encrypted","methodID":"init"},"timestamp":"2018-04-20 10:57:09.117"}, {"transactionJson":{"inputIDs":["input_id_for_create_petition"],"outputs":["petition_token","new_petition_object"],"contractID":"petition_encrypted","methodID":"create_petition"},"timestamp":"2018-04-20 10:57:09.653"} ]""") self.chainspace_repository_mock.get_full_transaction_log.return_value = TransactionLog( no_signatures_transaction_log) self.contract_mock.contract.contract_name = 'petition_encrypted' expected_output = [3, 2] transaction_output = { "outcome": [1, 2, 0, 0, 1, 1], "type": "PetitionEncResult" } tally_petition_transaction = { 'transaction': { 'outputs': [json.dumps(transaction_output)] } } self.contract_mock.tally.return_value = tally_petition_transaction output = self.petition.get_results() self.contract_mock.tally.assert_called_with( inputs, self.petition.reference_inputs, self.petition.parameters, self.keyfile) self.chainspace_repository_mock.process_transaction.assert_called_with( tally_petition_transaction) self.assertEqual(expected_output, output)
def test_get_results_returns_an_error_when_there_is_no_petition(self): no_transactions = [] self.chainspace_repository_mock.get_full_transaction_log.return_value = TransactionLog(no_transactions) self.contract_mock.contract.contract_name = 'petition_encrypted' self.contract_mock.tally.side_effect = Exception("'scores'") with self.assertRaises(EmptyTransactionLogException): self.petition.get_results()
def get_full_transaction_log(self): response = self.client.get_transaction_log() if response.status_code != 200: return { 'error': "failed to call cs api - " + response.status_code + " : " + response.content } return TransactionLog(response.json())
def test_count_signatures_with_new_petition_returns_count_zero(self): no_signatures_transaction_log = loads("""[ {"transactionJson":{"methodID":"init","inputIDs":[],"outputs":["petition_token"],"contractID":"petition_encrypted"},"timestamp":"2018-04-20 10:57:09.117"}, {"transactionJson":{"methodID":"create_petition","inputIDs":["input_id_for_create_petition"],"outputs":["petition_token","new_petition_object"],"contractID":"petition_encrypted"},"timestamp":"2018-04-20 10:57:09.653"} ]""") self.chainspace_repository_mock.get_full_transaction_log.return_value = TransactionLog(no_signatures_transaction_log) self.contract_mock.contract.contract_name = 'petition_encrypted' output = self.petition.count_signatures() expected_output = 0 self.assertEqual(expected_output, output)
def test_get_results_should_raise_exception_if_petition_is_closed(self): transaction_log = loads("""[ {"transactionJson":{"inputIDs":[],"outputs":["petition_token"],"contractID":"petition_encrypted","methodID":"init"},"timestamp":"2018-04-20 10:57:09.117"}, {"transactionJson":{"inputIDs":["input_id_for_create_petition"],"outputs":["petition_token","new_petition_object"],"contractID":"petition_encrypted","methodID":"create_petition"},"timestamp":"2018-04-20 10:57:09.653"}, {"transactionJson":{"inputIDs":["input_id_for_tally"],"outputs":["outcome"],"contractID":"petition_encrypted","methodID":"tally"},"timestamp":"2018-04-20 10:58:09.653"} ]""") self.chainspace_repository_mock.get_full_transaction_log.return_value = TransactionLog(transaction_log) self.contract_mock.contract.contract_name = 'petition_encrypted' self.contract_mock.tally.side_effect = Exception("'scores'") with self.assertRaises(TallyClosedPetitionException): self.petition.get_results()
def setUp(self): log = [{ 'transactionJson': { 'contractID': 'good_contract_id', 'methodID': 'init' }, 'timestamp': '2018-01-01 00:00:00.000' }, { 'transactionJson': { 'contractID': 'last_contract_id', 'methodID': 'init' }, 'timestamp': '2018-01-03 00:00:00.000' }, { 'transactionJson': { 'contractID': 'good_contract_id', 'methodID': 'add_signature' }, 'timestamp': '2018-01-02 00:00:00.000' }] self.transaction_log = TransactionLog(log)
def test_count_signatures_with_closed_petition_returns_correct_count(self): closed_petition_transaction_log = loads("""[ {"transactionJson":{"methodID":"init","inputIDs":[],"outputs":["petition_token"],"contractID":"petition_encrypted"},"timestamp":"2018-04-20 10:57:09.117"}, {"transactionJson":{"methodID":"create_petition","inputIDs":["input_id_for_create_petition"],"outputs":["petition_token","new_petition_object"],"contractID":"petition_encrypted"},"timestamp":"2018-04-20 10:57:09.653"}, {"transactionJson":{"methodID":"add_signature","parameters":["[0,1,0,0,0,0]"],"inputIDs":["input_id_for_add_signature"],"outputs":[],"contractID":"petition_encrypted"},"timestamp":"2018-04-20 10:58:09.653"}, {"transactionJson":{"methodID":"add_signature","parameters":["[1,0,0,0,0,0]"],"inputIDs":["input_id_for_add_signature"],"outputs":[],"contractID":"petition_encrypted"},"timestamp":"2018-04-20 10:58:19.653"}, {"transactionJson":{"methodID":"add_signature","parameters":["[0,0,0,0,1,0]"],"inputIDs":["input_id_for_add_signature"],"outputs":[],"contractID":"petition_encrypted"},"timestamp":"2018-04-20 10:58:29.653"}, {"transactionJson":{"methodID":"tally","inputIDs":["input_id_for_tally"],"outputs":["outcome"],"contractID":"petition_encrypted"},"timestamp":"2018-04-20 10:59:09.653"} ]""") self.chainspace_repository_mock.get_full_transaction_log.return_value = TransactionLog(closed_petition_transaction_log) self.contract_mock.contract.contract_name = 'petition_encrypted' output = self.petition.count_signatures() expected_output = 3 self.assertEqual(expected_output, output)
def test_get_last_transaction_should_throw_error_if_empty(self): empty_transaction_log = TransactionLog([]) with self.assertRaises(EmptyTransactionLogException): empty_transaction_log.get_last_transaction()