Beispiel #1
0
def test_init():
    amount = UTXOAmounts(VALID_ADDR_1, "1:100")
    assert amount.address == VALID_ADDR_1
    assert len(amount.data) == 2
    assert amount.data[0] == "1"
    assert amount.data[1] == "100"

    with pytest.raises(UTXOError):
        UTXOAmounts(VALID_ADDR_1, "1:100:2")

    amount = UTXOAmounts(VALID_ADDR_1, None)
    assert len(amount.data) == 0
Beispiel #2
0
def test_as_str():
    amount = UTXOAmounts(VALID_ADDR_1, "1:100:2:332")
    str_val = amount.as_str()
    assert str_val == "1:100:2:332"

    amount = UTXOAmounts(VALID_ADDR_1, "1:100:2:332")
    amount.data = ["1", "100", "2"]
    with pytest.raises(UTXOError):
        amount.as_str()
Beispiel #3
0
def test_as_output_list():
    amount = UTXOAmounts(VALID_ADDR_1, "1:100:2:332")
    output_list = amount.as_output_list()
    assert len(output_list) == 2
    for o in output_list:
        assert isinstance(o, Output)

    amount = UTXOAmounts(VALID_ADDR_1, "1:100:2:332")
    amount.data = ["1", "100", "2"]
    with pytest.raises(UTXOError):
        amount.as_output_list()
Beispiel #4
0
def test_as_str_invalid():
    amount = UTXOAmounts(VALID_ADDR_1, "1:100:2:332")
    amount.data = [10, 11, 12, 5, 13, 6]
    with pytest.raises(TypeError):
        amount.as_str()

    amount.data = ['10', '11', '12', '5', '13', '6']
    val = amount.as_str()
    assert val == '10:11:12:5:13:6'
Beispiel #5
0
 def chk_utxo_cache(node, nodes):
     cache = {}
     utxo_data = {}
     for n in nodes:
         cache[n.name] = {}
         utxo_data[n.name] = {}
         cache_storage = n.db_manager.get_store(UTXO_CACHE_LABEL)._store
         for key, value in cache_storage.iterator(include_value=True):
             cache[n.name][key] = value
             utxo_data[n.name] = UTXOAmounts.get_amounts(key, n.db_manager.get_store(UTXO_CACHE_LABEL),
                                                         is_committed=True).as_str()
     assert all(cache[node.name] == cache[n.name] for n in nodes)
     assert all(utxo_data[node.name] == utxo_data[n.name] for n in nodes)
     print(cache)
     print(utxo_data)
Beispiel #6
0
 def chk_utxo_cache(node, nodes):
     cache = {}
     utxo_data = {}
     for n in nodes:
         cache[n.name] = {}
         utxo_data[n.name] = {}
         cache_storage = n.ledger_to_req_handler[
             TOKEN_LEDGER_ID].utxo_cache._store
         for key, value in cache_storage.iterator(include_value=True):
             cache[n.name][key] = value
             utxo_data[n.name] = UTXOAmounts.get_amounts(
                 key,
                 n.ledger_to_req_handler[TOKEN_LEDGER_ID].utxo_cache,
                 is_committed=True).as_str()
     assert all(cache[node.name] == cache[n.name] for n in nodes)
     assert all(utxo_data[node.name] == utxo_data[n.name] for n in nodes)
     print(cache)
     print(utxo_data)
Beispiel #7
0
def test_as_output_list():
    with pytest.raises(UTXOError):
        UTXOAmounts(VALID_ADDR_1, '2:20:3:30:30:40:40:30:20').as_output_list()
    with pytest.raises(UTXOError):
        UTXOAmounts(VALID_ADDR_1, '2:20:3:30:30:40:40:30:20:').as_output_list()
    with pytest.raises(UTXOError):
        UTXOAmounts(VALID_ADDR_1, ':20:3:30:30:40:40:30:20:1').as_output_list()
    with pytest.raises(UTXOError):
        UTXOAmounts(VALID_ADDR_1, '20:3:30:30:40:40:30:20:1').as_output_list()
    with pytest.raises(UTXOError):
        UTXOAmounts(VALID_ADDR_1, '2:20:3:30::40:40:30:20:1').as_output_list()
    val = '2:20:3:30:30:40:40:30:20:1'
    assert UTXOAmounts(VALID_ADDR_1, val).as_output_list() == [
        Output(VALID_ADDR_1, 2, 20),
        Output(VALID_ADDR_1, 3, 30),
        Output(VALID_ADDR_1, 30, 40),
        Output(VALID_ADDR_1, 40, 30),
        Output(VALID_ADDR_1, 20, 1),
    ]
Beispiel #8
0
def test_remove_seq_no():
    amount = UTXOAmounts(VALID_ADDR_1, "1:100:2:332")
    amount.remove_seq_no(2)
    assert len(amount.data) == 2

    amount = UTXOAmounts(VALID_ADDR_1, "1:100:2:332")
    with pytest.raises(UTXOError):
        amount.remove_seq_no(534)

    amount = UTXOAmounts(VALID_ADDR_1, "1:100:2:332")
    amount.data = ["1", "100", "2"]
    with pytest.raises(UTXOError):
        amount.remove_seq_no(2)
Beispiel #9
0
def test_add_amount():
    amount = UTXOAmounts(VALID_ADDR_1, "1:100")
    amount.add_amount(3, 23)
    assert len(amount.data) == 4
    assert amount.data[2] == "3"
    assert amount.data[3] == "23"
    assert amount.as_str() == "1:100:3:23"

    amount = UTXOAmounts(VALID_ADDR_1, "1:100:2:332")
    amount.add_amount(3, 23)
    assert len(amount.data) == 6
    assert amount.data[4] == "3"
    assert amount.data[5] == "23"
    assert amount.as_str() == "1:100:2:332:3:23"

    with pytest.raises(UTXOError):
        amount = UTXOAmounts(VALID_ADDR_1, "1:100")
        amount.add_amount(None, 32)
        amount.add_amount(32, None)
Beispiel #10
0
def test_parse_val_empty():
    amount = UTXOAmounts(VALID_ADDR_1, "")
    assert amount.data == []
Beispiel #11
0
def test_parse_val_invalid_wrong_split():
    val = '10,11,12,5,13,6'

    with pytest.raises(UTXOError):
        UTXOAmounts(VALID_ADDR_1, val)
Beispiel #12
0
def test_init_invalid():
    val = [10, 11, 12, 5, 13, 6]
    with pytest.raises(UTXOError):
        UTXOAmounts(VALID_ADDR_1, val)
Beispiel #13
0
def test_init_bytearray():
    val = bytearray(b'10:10')
    amount = UTXOAmounts(VALID_ADDR_1, val)
    assert amount.data == ['10', '10']
Beispiel #14
0
def test_init_more_complex():
    val = '10:11:12:5:13:6'
    amount = UTXOAmounts(VALID_ADDR_1, val)
    assert amount.data == ['10', '11', '12', '5', '13', '6']
Beispiel #15
0
def test_sum_amounts_from_seq_nos_amounts():
    seq_nos_amounts = ":".join(
        ['2', '20', '3', '30', '30', '40', '40', '30', '20', '1'])
    amounts = UTXOAmounts(VALID_ADDR_1, seq_nos_amounts)

    with pytest.raises(UTXONotFound):
        amounts.sum_amounts({9})
    with pytest.raises(UTXONotFound):
        amounts.sum_amounts({2, 9})
    assert amounts.sum_amounts(set()) == 0
    assert amounts.sum_amounts({2}) == 20
    assert amounts.sum_amounts({2, 3}) == 50
    assert amounts.sum_amounts({30, 40}) == 70
    assert amounts.sum_amounts({20}) == 1
    assert amounts.sum_amounts({2, 30, 40, 3, 20}) == 121
Beispiel #16
0
def test_get_amounts():
    mock = MockUtxoCache({VALID_ADDR_1: "1:300"})
    amounts = UTXOAmounts.get_amounts(VALID_ADDR_1, mock)
    assert amounts.address == VALID_ADDR_1
    assert isinstance(amounts.data, list)
Beispiel #17
0
def test_remove_seq_no():
    seq_nos_amounts = ":".join(
        ['2', '20', '3', '30', '30', '40', '40', '30', '20', '1'])
    amounts = UTXOAmounts(VALID_ADDR_1, seq_nos_amounts)
    original_length = len(amounts.data)

    with pytest.raises(UTXONotFound):
        amounts.remove_seq_no(35)
        # UTXOCache.remove_seq_no('35', seq_nos_amounts)
    assert original_length == len(amounts.data)
    assert amounts.data == [
        '2', '20', '3', '30', '30', '40', '40', '30', '20', '1'
    ]

    amounts.remove_seq_no('2')
    assert original_length == len(amounts.data) + 2
    assert amounts.data == ['3', '30', '30', '40', '40', '30', '20', '1']

    amounts.remove_seq_no('40', )
    assert original_length == len(amounts.data) + 4
    assert amounts.data == ['3', '30', '30', '40', '20', '1']

    amounts.remove_seq_no('20')
    assert original_length == len(amounts.data) + 6
    assert amounts.data == ['3', '30', '30', '40']

    with pytest.raises(UTXONotFound):
        amounts.remove_seq_no('19')
    assert original_length == len(amounts.data) + 6
    assert amounts.data == ['3', '30', '30', '40']

    with pytest.raises(UTXONotFound):
        amounts.remove_seq_no('40')
    assert original_length == len(amounts.data) + 6
    assert amounts.data == ['3', '30', '30', '40']

    amounts.remove_seq_no('30')
    assert original_length == len(amounts.data) + 8
    assert amounts.data == ['3', '30']

    amounts.remove_seq_no('3')
    assert original_length == len(amounts.data) + 10
    assert amounts.data == []