def test_can_add_assets_to_composite(small_composite): asset = Asset("GBP", "pound", 102, 0.02) small_composite.add(asset) result = [x.name for x in small_composite._asset_collection] expected_result = sorted(["euro", "dollar", "pound"]) assert expected_result == result, ( f"Wrong result: expected {expected_result}, got {result}")
def test_bank_calculate_revenue_works_correctly(): assets = [ Asset("EUR", "euro", 82, 0.03), Asset("USD", "dollar", 72, 0.04), Asset("GBP", "pound", 102, 0.02), ] key_indicators = { "EUR": 83.6, "USD": 74.3, } currency_rates = { "GBP": 0.342, } currency_rates.update(key_indicators) assert 3 == len(currency_rates) period = 4 expected_result = sum( asset.calculate_revenue(period, currency_rates[asset.char_code]) for asset in assets) composite = CompositeAssets(assets) result = composite.calculate_revenue(period, currency_rates) assert expected_result == result, ( f"Wrong result: expected {expected_result}, got {result}")
def small_composite(): asset_1 = Asset("EUR", "euro", 82, 0.03) asset_2 = Asset("USD", "dollar", 73, 0.04) composite = CompositeAssets([asset_1, asset_2]) return composite
def test_can_calculate_asset_revenue_correctly(period, rate, expected_result): asset = Asset("USD", "dollar", 100, 0.04) result = asset.calculate_revenue(period, rate) assert f"{expected_result:.8f}" == f"{result:.8f}", ( f"Wrong revenue value: expected {expected_result:.8f}, got {result:.8f}" )
def medium_composite(): asset_1 = Asset("EUR", "euro", 82, 0.03) asset_2 = Asset("USD", "dollar", 73, 0.04) asset_3 = Asset("GBP", "pound", 102, 0.02) composite = CompositeAssets([asset_1, asset_2, asset_3]) return composite
def test_asset_create(): asset = Asset("USD", "dollar", 1, 0.04) assert asset.name == "dollar", "Asset create wrong!" assert asset.capital == 1, "Asset create wrong!" assert asset.interest == 0.04, "Asset create wrong!"
@pytest.mark.parametrize("period, rate, expected_result", [ pytest.param(1, 74.5, 298), pytest.param(3, 75.0, 936.48), pytest.param(7, 73.12, 2310.09316977), ]) def test_can_calculate_asset_revenue_correctly(period, rate, expected_result): asset = Asset("USD", "dollar", 100, 0.04) result = asset.calculate_revenue(period, rate) assert f"{expected_result:.8f}" == f"{result:.8f}", ( f"Wrong revenue value: expected {expected_result:.8f}, got {result:.8f}" ) @pytest.mark.parametrize("left_asset, right_asset, expected_result", [ pytest.param(Asset("USD", "dollar", 73.4, 0.045), Asset("EUR", "euro", 73.73, 0.04), True), pytest.param(Asset("EUR", "euro", 83, 0.045), Asset( "USD", "euro", 82, 0.04), False), ]) def test_can_compare_assets_correctly(left_asset, right_asset, expected_result): result = left_asset < right_asset assert result is expected_result, ( f"Wrong comparison: expected {expected_result} for assets with names \ {left_asset.name} and {right_asset.name}, got {result}") # ------------------------------------------------- TESTS FOR COMPOSITE -----------------------------------------