Skip to content

Commit

Permalink
Code cleanup for Transactions.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdlaird committed Nov 8, 2024
1 parent 7389e80 commit 77489ee
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased](https://github.com/alexdlaird/amazon-orders/compare/3.2.0...HEAD)

### Added

- Stability improvements.

## [3.2.1](https://github.com/alexdlaird/amazon-orders/compare/3.2.0...3.2.1) - 2024-11-08

### Fixed
Expand Down
22 changes: 8 additions & 14 deletions amazonorders/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@
logger = logging.getLogger(__name__)


def _get_today() -> datetime.date:
return datetime.date.today()


def _parse_transaction_form_tag(
form_tag: Tag, config: AmazonOrdersConfig
) -> Tuple[List[Transaction], Optional[str], Optional[Dict[str, str]]]:
def _parse_transaction_form_tag(form_tag: Tag,
config: AmazonOrdersConfig) \
-> Tuple[List[Transaction], Optional[str], Optional[Dict[str, str]]]:
transactions = []
date_container_tags = util.select(form_tag, config.selectors.TRANSACTION_DATE_CONTAINERS_SELECTOR)
for date_container_tag in date_container_tags:
date_tag = util.select_one(date_container_tag, config.selectors.FIELD_TRANSACTION_COMPLETED_DATE_SELECTOR)
if not date_tag:
logger.warning("Could not find date tag in transaction form.")
logger.warning("Could not find date tag in Transaction form.")
continue

date_str = date_tag.text
Expand All @@ -38,9 +34,7 @@ def _parse_transaction_form_tag(
transactions_container_tag = date_container_tag.find_next_sibling(
config.selectors.TRANSACTIONS_CONTAINER_SELECTOR)
if not isinstance(transactions_container_tag, Tag):
logger.warning(
"Could not find transactions container tag in transaction form."
)
logger.warning("Could not find transactions container tag in Transaction form.")
continue

transaction_tags = util.select(transactions_container_tag, config.selectors.TRANSACTIONS_SELECTOR)
Expand All @@ -52,7 +46,7 @@ def _parse_transaction_form_tag(
form_ie_input = util.select_one(form_tag, config.selectors.TRANSACTIONS_NEXT_PAGE_INPUT_IE_SELECTOR)
next_page_input = util.select_one(form_tag, config.selectors.TRANSACTIONS_NEXT_PAGE_INPUT_SELECTOR)
if not next_page_input or not form_state_input or not form_ie_input:
return (transactions, None, None)
return transactions, None, None

next_page_post_url = str(form_tag["action"])
next_page_post_data = {
Expand All @@ -61,7 +55,7 @@ def _parse_transaction_form_tag(
str(next_page_input["name"]): "",
}

return (transactions, next_page_post_url, next_page_post_data)
return transactions, next_page_post_url, next_page_post_data


class AmazonTransactions:
Expand Down Expand Up @@ -100,7 +94,7 @@ def get_transactions(self,
if not self.amazon_session.is_authenticated:
raise AmazonOrdersError("Call AmazonSession.login() to authenticate first.")

min_date = _get_today() - datetime.timedelta(days=days)
min_date = datetime.date.today() - datetime.timedelta(days=days)

self.amazon_session.get(self.config.constants.TRANSACTION_HISTORY_LANDING_URL)
if not self.amazon_session.last_response_parsed:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ def test_order_command(self):
self.assertIn("Order #112-2961628-4757846", response.output)

@responses.activate
@patch("amazonorders.transactions._get_today")
@patch("amazonorders.transactions.datetime", wraps=datetime)
def test_transactions_command(self, mock_get_today: Mock):
# GIVEN
mock_get_today.return_value = datetime.date(2024, 10, 11)
mock_get_today.date.today.return_value = datetime.date(2024, 10, 11)
days = 1
self.given_login_responses_success()
with open(os.path.join(self.RESOURCES_DIR, "get-transactions.html"), "r", encoding="utf-8") as f:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ def setUp(self):
self.amazon_transactions = AmazonTransactions(self.amazon_session)

@responses.activate
@patch("amazonorders.transactions._get_today")
@patch("amazonorders.transactions.datetime", wraps=datetime)
def test_transactions_command(self, mock_get_today: Mock):
# GIVEN
mock_get_today.return_value = datetime.date(2024, 10, 11)
mock_get_today.date.today.return_value = datetime.date(2024, 10, 11)
days = 1
self.amazon_session.is_authenticated = True
with open(
os.path.join(self.RESOURCES_DIR, "get-transactions.html"),
"r",
encoding="utf-8",
os.path.join(self.RESOURCES_DIR, "get-transactions.html"),
"r",
encoding="utf-8",
) as f:
responses.add(
responses.GET,
Expand Down

0 comments on commit 77489ee

Please sign in to comment.