Skip to content

Commit

Permalink
fix: aws pricing api (#160)
Browse files Browse the repository at this point in the history
* bump: version 0.2.0 → 0.3.0

* fix: aws pricing api fix

* fix: aws pricing api region fix

* fix: aws pricing api region fix

* fix: aws cost calculator test fix

* fix: aws cost calculator test fix

* fix: aws cost calculator test fix

* fix: aws cost calculator test fix
  • Loading branch information
arshiamoghimi authored Oct 28, 2023
1 parent 09e897f commit 78d8eca
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/exploration/aws/aws_cost_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _get_pricing_units(self) -> dict:
pricing information and parse it accordingly to determine the pricing units.
"""
try:
response = self.aws_session.client("pricing").get_products(
response = boto3.client("pricing", region_name="us-east-1").get_products(
ServiceCode="AWSLambda",
Filters=[
{
Expand Down
83 changes: 42 additions & 41 deletions tests/exploration/aws/test_aws_cost_calculator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from unittest import mock
from unittest.mock import patch

import pytest

Expand All @@ -20,49 +21,49 @@ def calculator_with_mock_aws_session() -> AWSCostCalculator:

class TestGetPricingUnits:
def test_get_pricing_units(self, calculator_with_mock_aws_session):
# Mock the response for get_products() from pricing client
calculator_with_mock_aws_session.aws_session.client(
"pricing"
).get_products.return_value = {
"PriceList": [
'{"product": {"attributes": {"group": "AWS-Lambda-Duration-ARM"}, "terms": {"OnDemand": {"offerTermCode": "JRTCKXETXF", "priceDimensions": {"JRTCKXETXF.6YS6EN2CT7": {"pricePerUnit": {"USD": "0.0000150000"}}}}}}}',
'{"product": {"attributes": {"group": "AWS-Lambda-Duration"}, "terms": {"OnDemand": {"offerTermCode": "JRTCKXETXF", "priceDimensions": {"JRTCKXETXF.6YS6EN2CT7": {"pricePerUnit": {"USD": "0.0000166667"}}}}}}}',
'{"product": {"attributes": {"group": "AWS-Lambda-Requests-ARM"}, "terms": {"OnDemand": {"offerTermCode": "JRTCKXETXF", "priceDimensions": {"JRTCKXETXF.6YS6EN2CT7": {"pricePerUnit": {"USD": "0.0000002000"}}}}}}}',
'{"product": {"attributes": {"group": "AWS-Lambda-Requests"}, "terms": {"OnDemand": {"offerTermCode": "JRTCKXETXF", "priceDimensions": {"JRTCKXETXF.6YS6EN2CT7": {"pricePerUnit": {"USD": "0.0000002000"}}}}}}}',
]
}

# Mock the response for get_function_configuration() from lambda client
calculator_with_mock_aws_session.aws_session.client(
"lambda"
).get_function_configuration.return_value = {"Architectures": ["x86"]}

# Call the _get_pricing_units() method
pricing_units = calculator_with_mock_aws_session._get_pricing_units()

# Perform the assertion
assert pricing_units == {"compute": 0.0000166667, "request": 0.0000002}
boto3_client_mock = mock.Mock()
with patch('boto3.client', return_value=boto3_client_mock):
# Mock the response for get_products() from pricing client
boto3_client_mock.get_products.return_value = {
"PriceList": [
'{"product": {"attributes": {"group": "AWS-Lambda-Duration-ARM"}, "terms": {"OnDemand": {"offerTermCode": "JRTCKXETXF", "priceDimensions": {"JRTCKXETXF.6YS6EN2CT7": {"pricePerUnit": {"USD": "0.0000150000"}}}}}}}',
'{"product": {"attributes": {"group": "AWS-Lambda-Duration"}, "terms": {"OnDemand": {"offerTermCode": "JRTCKXETXF", "priceDimensions": {"JRTCKXETXF.6YS6EN2CT7": {"pricePerUnit": {"USD": "0.0000166667"}}}}}}}',
'{"product": {"attributes": {"group": "AWS-Lambda-Requests-ARM"}, "terms": {"OnDemand": {"offerTermCode": "JRTCKXETXF", "priceDimensions": {"JRTCKXETXF.6YS6EN2CT7": {"pricePerUnit": {"USD": "0.0000002000"}}}}}}}',
'{"product": {"attributes": {"group": "AWS-Lambda-Requests"}, "terms": {"OnDemand": {"offerTermCode": "JRTCKXETXF", "priceDimensions": {"JRTCKXETXF.6YS6EN2CT7": {"pricePerUnit": {"USD": "0.0000002000"}}}}}}}',
]
}

# Mock the response for get_function_configuration() from lambda client
calculator_with_mock_aws_session.aws_session.client(
"lambda"
).get_function_configuration.return_value = {"Architectures": ["x86"]}

# Call the _get_pricing_units() method
pricing_units = calculator_with_mock_aws_session._get_pricing_units()

# Perform the assertion
assert pricing_units == {"compute": 0.0000166667, "request": 0.0000002}

def test_get_pricing_units_index_error(self, calculator_with_mock_aws_session):
# Mock the response for get_products() from pricing client
calculator_with_mock_aws_session.aws_session.client(
"pricing"
).get_products.return_value = {
"PriceList": [
'{"product": {"attributes": {"group": "AWS-Lambda-Duration-ARM"}, "terms": {"OnDemand": {"offerTermCode": "JRTCKXETXF", "priceDimensions": {"JRTCKXETXF.6YS6EN2CT7": {"pricePerUnit": {"USD": }}}}}}',
]
}

# Mock the response for get_function_configuration() from lambda client
calculator_with_mock_aws_session.aws_session.client(
"lambda"
).get_function_configuration.return_value = {"Architectures": ["x86"]}

with pytest.raises(CostCalculationError) as e:
# Response parsing fails.
calculator_with_mock_aws_session._get_pricing_units()

assert e.type == CostCalculationError
boto3_client_mock = mock.Mock()
with patch('boto3.client', return_value=boto3_client_mock):
# Mock the response for get_products() from pricing client
boto3_client_mock.get_products.return_value = {
"PriceList": [
'{"product": {"attributes": {"group": "AWS-Lambda-Duration-ARM"}, "terms": {"OnDemand": {"offerTermCode": "JRTCKXETXF", "priceDimensions": {"JRTCKXETXF.6YS6EN2CT7": {"pricePerUnit": {"USD": }}}}}}',
]
}

# Mock the response for get_function_configuration() from lambda client
calculator_with_mock_aws_session.aws_session.client(
"lambda"
).get_function_configuration.return_value = {"Architectures": ["x86"]}

with pytest.raises(CostCalculationError) as e:
# Response parsing fails.
calculator_with_mock_aws_session._get_pricing_units()

assert e.type == CostCalculationError


class TestCalculator:
Expand Down

0 comments on commit 78d8eca

Please sign in to comment.