예제 #1
0
def test_initialisation():
    """Test the initialisation of the ownership_state."""
    currency_endowment = {"FET": 100}
    good_endowment = {"good_id": 2}
    ownership_state = OwnershipState()
    ownership_state.set(
        amount_by_currency_id=currency_endowment, quantities_by_good_id=good_endowment,
    )
    assert ownership_state.amount_by_currency_id is not None
    assert ownership_state.quantities_by_good_id is not None
    assert ownership_state.is_initialized
예제 #2
0
 def test_transaction_is_affordable_agent_is_seller(self):
     """Check if the agent has the goods (the agent=sender is the seller)."""
     currency_endowment = {"FET": 100}
     good_endowment = {"good_id": 20}
     ownership_state = OwnershipState()
     ownership_state.set(
         amount_by_currency_id=currency_endowment,
         quantities_by_good_id=good_endowment,
     )
     assert ownership_state.is_affordable_transaction(
         terms=self.seller_terms
     ), "We must reject the transaction."
예제 #3
0
 def test_transaction_is_affordable_malformed(self):
     """Reject the transaction when there is no wealth exchange."""
     currency_endowment = {"FET": 100}
     good_endowment = {"good_id": 20}
     ownership_state = OwnershipState()
     ownership_state.set(
         amount_by_currency_id=currency_endowment,
         quantities_by_good_id=good_endowment,
     )
     assert not ownership_state.is_affordable_transaction(
         terms=self.malformed_terms
     ), "We must reject the transaction."
예제 #4
0
 def test_transaction_is_affordable_agent_is_buyer(self):
     """Check if the agent has the money to cover the sender_amount (the agent=sender is the buyer)."""
     currency_endowment = {"FET": 100}
     good_endowment = {"good_id": 20}
     ownership_state = OwnershipState()
     ownership_state.set(
         amount_by_currency_id=currency_endowment,
         quantities_by_good_id=good_endowment,
     )
     assert ownership_state.is_affordable(
         terms=self.buyer_terms
     ), "We should have the money for the transaction!"
예제 #5
0
def test_is_affordable_for_uninitialized():
    """Test the initialisation of the ownership_state."""
    ownership_state = OwnershipState()
    buyer_terms = Terms(
        ledger_id=ETHEREUM,
        sender_address="pk1",
        counterparty_address="pk2",
        amount_by_currency_id={"FET": -1},
        is_sender_payable_tx_fee=True,
        quantities_by_good_id={"good_id": 10},
        nonce="transaction nonce",
    )
    assert ownership_state.is_affordable(
        terms=buyer_terms
    ), "Any transaction should be classed as affordable."
예제 #6
0
 def test_apply(self):
     """Test the apply function."""
     currency_endowment = {"FET": 100}
     good_endowment = {"good_id": 2}
     ownership_state = OwnershipState()
     ownership_state.set(
         amount_by_currency_id=currency_endowment,
         quantities_by_good_id=good_endowment,
     )
     list_of_terms = [self.buyer_terms]
     state = ownership_state
     new_state = ownership_state.apply_transactions(list_of_terms=list_of_terms)
     assert (
         state != new_state
     ), "after applying a list_of_terms must have a different state!"
예제 #7
0
    def setup(cls):
        """Setup the test class."""
        super().setup()
        cls.register_as = "both"
        cls.search_for = "both"
        cls.is_contract_tx = False
        cls.ledger_id = "some_ledger_id"
        cls.location = {"longitude": 0.1270, "latitude": 51.5194}
        cls.search_radius = 5.0
        cls.service_key = "tac_service"

        cls.strategy = Strategy(
            register_as=cls.register_as,
            search_for=cls.search_for,
            is_contract_tx=cls.is_contract_tx,
            ledger_id=cls.ledger_id,
            location=cls.location,
            service_key=cls.service_key,
            search_radius=cls.search_radius,
            name="strategy",
            skill_context=cls._skill.skill_context,
        )

        cls.nonce = "125"
        cls.sender = "some_sender_address"
        cls.counterparty = "some_counterparty_address"
        cls.signature = "some_signature"

        cls.mocked_currency_id = "1"
        cls.mocked_amount_by_currency_id = {cls.mocked_currency_id: 10}
        cls.mocked_quantities_by_good_id = {"2": 5, "3": 7}
        cls.mocked_ownership_state = OwnershipState()
        cls.mocked_ownership_state.set(cls.mocked_amount_by_currency_id,
                                       cls.mocked_quantities_by_good_id)
예제 #8
0
def test_non_initialized_ownership_state_raises_exception():
    """Test that non-initialized ownership state raises exception."""
    ownership_state = OwnershipState()

    with pytest.raises(ValueError):
        ownership_state.amount_by_currency_id

    with pytest.raises(ValueError):
        ownership_state.quantities_by_good_id
예제 #9
0
def test_score_diff_from_transaction():
    """Test the difference between the scores."""
    good_holdings = {"good_id": 2}
    currency_holdings = {"FET": 100}
    utility_params = {"good_id": 20.0}
    exchange_params = {"FET": 10.0}
    ownership_state = OwnershipState()
    ownership_state.set(amount_by_currency_id=currency_holdings,
                        quantities_by_good_id=good_holdings)
    preferences = Preferences()
    preferences.set(
        utility_params_by_good_id=utility_params,
        exchange_params_by_currency_id=exchange_params,
    )
    terms = Terms(
        ledger_id=ETHEREUM,
        sender_address="agent_1",
        counterparty_address="pk",
        amount_by_currency_id={"FET": -20},
        is_sender_payable_tx_fee=True,
        quantities_by_good_id={"good_id": 10},
        nonce="transaction nonce",
    )
    cur_score = preferences.utility(quantities_by_good_id=good_holdings,
                                    amount_by_currency_id=currency_holdings)
    new_state = ownership_state.apply_transactions([terms])
    new_score = preferences.utility(
        quantities_by_good_id=new_state.quantities_by_good_id,
        amount_by_currency_id=new_state.amount_by_currency_id,
    )
    diff_scores = new_score - cur_score
    score_difference = preferences.utility_diff_from_transaction(
        ownership_state=ownership_state, terms=terms)
    assert (
        score_difference == diff_scores
    ), "The calculated difference must be equal to the return difference from the function."
    assert not preferences.is_utility_enhancing(
        ownership_state=ownership_state,
        terms=terms), "Should not enhance utility."
예제 #10
0
def test_marginal_utility():
    """Test the marginal utility."""
    currency_holdings = {"FET": 100}
    utility_params = {"good_id": 20.0}
    exchange_params = {"FET": 10.0}
    good_holdings = {"good_id": 2}
    preferences = Preferences()
    preferences.set(
        utility_params_by_good_id=utility_params,
        exchange_params_by_currency_id=exchange_params,
    )
    delta_good_holdings = {"good_id": 1}
    delta_currency_holdings = {"FET": -5}
    ownership_state = OwnershipState()
    ownership_state.set(
        amount_by_currency_id=currency_holdings,
        quantities_by_good_id=good_holdings,
    )
    marginal_utility = preferences.marginal_utility(
        ownership_state=ownership_state,
        delta_quantities_by_good_id=delta_good_holdings,
        delta_amount_by_currency_id=delta_currency_holdings,
    )
    assert marginal_utility is not None, "Marginal utility must not be none."
예제 #11
0
def test_is_utility_enhancing_uninitialized():
    """Test is_utility_enhancing when the states are uninitialized."""
    ownership_state = OwnershipState()
    preferences = Preferences()
    terms = Terms(
        ledger_id=ETHEREUM,
        sender_address="agent_1",
        counterparty_address="pk",
        amount_by_currency_id={"FET": -20},
        is_sender_payable_tx_fee=True,
        quantities_by_good_id={"good_id": 10},
        nonce="transaction nonce",
    )
    assert preferences.is_utility_enhancing(
        ownership_state=ownership_state,
        terms=terms), "Should enhance utility."
예제 #12
0
 def test_transaction_update_receive(self):
     """Test the transaction update when receiving tokens."""
     currency_endowment = {"FET": 100}
     good_endowment = {"good_id": 20}
     ownership_state = OwnershipState()
     ownership_state.set(
         amount_by_currency_id=currency_endowment,
         quantities_by_good_id=good_endowment,
     )
     assert ownership_state.amount_by_currency_id == currency_endowment
     assert ownership_state.quantities_by_good_id == good_endowment
     ownership_state.update(terms=self.seller_terms)
     expected_amount_by_currency_id = {"FET": 101}
     expected_quantities_by_good_id = {"good_id": 10}
     assert ownership_state.amount_by_currency_id == expected_amount_by_currency_id
     assert ownership_state.quantities_by_good_id == expected_quantities_by_good_id