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 add_block(self, recipient, nominator, date): self.lock.acquire() try: index = len(self.blocks) timestamp = datetime.datetime.utcnow() previous_hash = self.blocks[index - 1].hash kudo_rec = kudo.Kudo(recipient, nominator, date) self.blocks.append(block.Block(index, timestamp, previous_hash, kudo_rec)) finally: self.lock.release()
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 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 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_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 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 get_genesis_block(timestamp, initial_hash, kudo_date): return block.Block(0, timestamp, initial_hash, kudo.Kudo("", "", kudo_date))