def Chain_add_block_expect_index_of_well_formed_block(): #Assign theChain = chain.Chain() expected_index = 1 expected_recipient = "*****@*****.**" expected_nominator = "*****@*****.**" expected_date = datetime.date(1972, 4, 22) #Action theChain.add_block(expected_recipient, expected_nominator, expected_date) #Assert test_first.are_equal(expected_index, theChain.blocks[expected_index].index, "block index") test_first.are_equal(expected_recipient, theChain.blocks[expected_index].kudo.recipient, "recipient") test_first.are_equal(expected_nominator, theChain.blocks[expected_index].kudo.nominator, "nominator") test_first.are_equal(expected_date, theChain.blocks[expected_index].kudo.date, "date") test_first.are_equal(theChain.blocks[0].hash, theChain.blocks[expected_index].previous_hash, "previous hash matches")
def Chain_get_index_too_large_expect_None(): theChain = chain.Chain() theChain.add_block("recip1", "nom1", datetime.date.today()) #Action actual_block = theChain.get(2) #Assert test_first.are_equal(None, actual_block)
def Chain_verify_no_blocks_added_expect_True(): #Assign theChain = chain.Chain() #Action result = theChain.verify() #Assert test_first.are_equal(True, result)
def Chain_count_no_kudos_expect_0(): #Assign theChain = chain.Chain() #Action actual = theChain.count() #Assert test_first.are_equal(0, actual)
def Chain_get_list_no_blocks_expect_empty_list(): #Assign theChain = chain.Chain() #Action result = theChain.get_list() #Assert test_first.are_equal(0, len(result))
def Chain_get_index_1_expect_the_first_kudo(): #Assign theChain = chain.Chain() theChain.add_block("recip1", "nom1", datetime.date.today()) #Action actual_kudo = theChain.get(1) #Assert test_first.are_equal(theChain.blocks[1].kudo.__dict__, actual_kudo.__dict__)
def KudoJSONEncoder_default_pass_Kudo_expect_well_formed_JSON_Kudo(): #Assign kudo_rec = kudo.Kudo("recip1", "nom1", datetime.date(1972, 4, 22)) expected_json = '{"recipient": "recip1", "nominator": "nom1", "date": "1972-04-22"}' encoder = kudo_jsonencoder.KudoJSONEncoder() #Action actual_json = encoder.encode(kudo_rec) #Assert test_first.are_equal(expected_json, actual_json)
def Chain_verify_add_3_blocks_expect_True(): #Assign theChain = chain.Chain() today = datetime.date.today() theChain.add_block("recip1", "nom1", today) theChain.add_block("recip2", "nom2", today) theChain.add_block("recip3", "nom3", today) #Action result = theChain.verify() #Assert test_first.are_equal(True, result)
def Chain_get_index_2_of_3_expect_the_second_kudo(): #Assign theChain = chain.Chain() theChain.add_block("recip1", "nom1", datetime.date.today()) theChain.add_block("recip2", "nom2", datetime.date.today()) theChain.add_block("recip3", "nom3", datetime.date.today()) #Action actual_kudo = theChain.get(2) #Assert test_first.are_equal(theChain.blocks[2].kudo.__dict__, actual_kudo.__dict__)
def Chain_count_add_3_kudos_expect_3(): #Assign theChain = chain.Chain() today = datetime.date.today() theChain.add_block("recip1", "nom1", today) theChain.add_block("recip2", "nom2", today) theChain.add_block("recip3", "nom3", today) #Action actual = theChain.count() #Assert test_first.are_equal(3, actual)
def Chain_verify_remove_block_expect_False(): #Assign theChain = chain.Chain() today = datetime.date.today() theChain.add_block("recip1", "nom1", today) theChain.add_block("recip2", "nom2", today) theChain.add_block("recip3", "nom3", today) theChain.blocks.remove(theChain.blocks[2]) #Action result = theChain.verify() #Assert test_first.are_equal(False, result)
def Block_hashing_pass_required_items_expect_repeatable_hash(): #Assign index = 0 timestamp = datetime.datetime(1999, 12, 31, 0, 0, 0) previous_hash = "101" kudo_rec = kudo.Kudo("*****@*****.**", "*****@*****.**", datetime.date(2010, 3, 5)) expected_hash = "22cd5125dd3ee3d56ed6a315b8c6e121804f3dea0fcf06fb2c1cf1c54bdf1102" #Action actual_hash = block.Block.hashing(index, timestamp, previous_hash, kudo_rec) #Assert test_first.are_equal(expected_hash, actual_hash)
def Chain_get_list_add_3_blocks_expect_3_kudos(): #Assign theChain = chain.Chain() today = datetime.date.today() expected_kudos = [ kudo.Kudo("recip1", "nom1", today), kudo.Kudo("recip2", "nom2", today), kudo.Kudo("recip3", "nom3", today) ] for k in expected_kudos: theChain.add_block(k.recipient, k.nominator, k.date) #Action result = theChain.get_list() #Assert test_first.are_equal(3, len(result), "has 3 kudos") for i in range(0, len(result)): test_first.are_equal(expected_kudos[i].__dict__, result[i].__dict__, expected_kudos[i].recipient)
def Kudo_create_pass_all_items_expect_Kudo_with_all_items(): #Assign expected_recipient = "*****@*****.**" expected_nominator = "*****@*****.**" expected_date = datetime.date(1972, 4, 22) #Action actual = kudo.Kudo(expected_recipient, expected_nominator, expected_date) #Assert test_first.are_equal(expected_recipient, actual.recipient, "recipient") test_first.are_equal(expected_nominator, actual.nominator, "nominator") test_first.are_equal(expected_date, actual.date, "date")
def Block_create_pass_negative_index_expect_IndexError_exception(): #Assign index = -1 timestamp = datetime.datetime(2011, 6, 29) previous_hash = "102" kudo_rec = kudo.Kudo("*****@*****.**", "*****@*****.**", datetime.date(1972, 4, 22)) #Action and Assert try: block.Block(index, timestamp, previous_hash, kudo_rec) test_first.are_equal("exception", "no exception") except IndexError: test_first.are_equal("IndexError", "IndexError") except: test_first.are_equal("IndexError", "some other exception")
def Block_create_pass_all_items_expect_object_with_all_items_and_a_hash(): #Assign expected_index = 1 expected_timestamp = datetime.datetime(1972, 4, 22, 0, 0, 0) expected_previous_hash = 100 expected_kudo = kudo.Kudo("*****@*****.**", "*****@*****.**", datetime.date(1972, 4, 22)) #Action block_result = block.Block(expected_index, expected_timestamp, expected_previous_hash, expected_kudo) #Assert test_first.are_equal(expected_index, block_result.index, "index") test_first.are_equal(expected_timestamp, block_result.timestamp, "timestamp") test_first.are_equal(expected_previous_hash, block_result.previous_hash, "previous hash") test_first.are_equal(expected_kudo.recipient, block_result.kudo.recipient, "kudo.recipient") test_first.are_equal(expected_kudo.nominator, block_result.kudo.nominator, "kudo.nominator") test_first.are_equal(expected_kudo.date, block_result.kudo.date, "kudo.date") test_first.are_equal(True, block_result.hash is not None, "hash has a value")
def Chain_create_expect_list_with_the_genesis_block(): #Action theChain = chain.Chain() #Assert test_first.are_equal(1, len(theChain.blocks), "One block is created")
def Chain_get_genesis_block_expect_a_block_with_genesis_info(): #Assign expected_index = 0 expected_timestamp = datetime.datetime(1972, 4, 22, 0, 0, 0) expected_previous_hash = "previous_hash" expected_recipient = "" expected_nominator = "" expected_date = datetime.date(1994, 6, 13) expected_hash = "81dd14b60acc3e90ddf5a08f5704eb890616dabc922659626026db6f81546692" #Action block_result = chain.Chain.get_genesis_block(expected_timestamp, expected_previous_hash, expected_date) #Assert test_first.are_equal(expected_index, block_result.index, "index") test_first.are_equal(expected_timestamp, block_result.timestamp, "timestamp") test_first.are_equal(expected_previous_hash, block_result.previous_hash, "previous_hash") test_first.are_equal(expected_recipient, block_result.kudo.recipient, "recepient") test_first.are_equal(expected_nominator, block_result.kudo.nominator, "nominator") test_first.are_equal(expected_date, block_result.kudo.date, "date") test_first.are_equal(expected_hash, block_result.hash, "hash")