store cookies in xdg-data-dir, clean up typing, fix some tests

This commit is contained in:
2025-12-07 08:15:59 -05:00
parent 5af4b5c4eb
commit 163b4a525c
7 changed files with 122 additions and 157 deletions

View File

@@ -9,9 +9,8 @@ def test_get_accounts():
res = amazon.get_accounts(env)
assert len(res) == 1
email, pwd = res[0]
assert email == 'test@example.com'
assert pwd == 'password123'
assert res[0].email == 'test@example.com'
assert res[0].password == 'password123'
def test_get_accounts_numbered():
@@ -22,9 +21,8 @@ def test_get_accounts_numbered():
res = amazon.get_accounts(env)
assert len(res) == 1
email, pwd = res[0]
assert email == 'test@example.com'
assert pwd == 'password123'
assert res[0].email == 'test@example.com'
assert res[0].password == 'password123'
def test_get_accounts_numbered_multi():
@@ -38,13 +36,11 @@ def test_get_accounts_numbered_multi():
assert len(res) == 2
email1, pwd1 = res[0]
assert email1 == 'test@example.com'
assert pwd1 == 'password123'
assert res[0].email == 'test@example.com'
assert res[0].password == 'password123'
email2, pwd2 = res[1]
assert email2 == 'test2@example.com'
assert pwd2 == 'password456'
assert res[1].email == 'test2@example.com'
assert res[1].password == 'password456'
def test_get_accounts_both():
@@ -57,10 +53,9 @@ def test_get_accounts_both():
res = amazon.get_accounts(env)
assert len(res) == 2
email1, pwd1 = res[0]
assert email1 == 'test@example.com'
assert pwd1 == 'password123'
email2, pwd2 = res[1]
assert email2 == 'test2@example.com'
assert pwd2 == 'password456'
assert res[0].email == 'test@example.com'
assert res[0].password == 'password123'
assert res[1].email == 'test2@example.com'
assert res[1].password == 'password456'

View File

@@ -1,75 +1,16 @@
import datetime
import json
import pickle
from types import SimpleNamespace
from unittest.mock import MagicMock
from amazonorders.entity.order import Order
from amazonorders.entity.item import Item
from amazonorders.entity.shipment import Shipment
from amazonorders.entity.transaction import Transaction
from amazonorders.entity.recipient import Recipient
import pytest
from ai import ItemClassification
import main
KEYS = {
Item: [
'title', 'link', 'price', 'seller', 'condition',
'return_eligible_date', 'image_link', 'quantity'
],
Order: [
'full_details', 'index', 'shipments', 'items', 'order_number', 'order_details_link',
'grand_total', 'order_placed_date', 'recipient', 'payment_method', 'payment_method_last_4',
'subtotal', 'shipping_total', 'free_shipping', 'promotion_applied', 'coupon_savings',
'reward_points', 'subscription_discount', 'total_before_tax', 'estimated_tax',
'refund_total', 'multibuy_discount', 'amazon_discount', 'gift_card', 'gift_wrap',
],
Transaction: [
'completed_date', 'payment_method', 'grand_total', 'is_refund',
'order_number', 'order_details_link', 'seller',
],
Shipment: [
'items', 'delivery_status', 'tracking_link',
],
Recipient: [
'name', 'address',
],
}
def to_dict(entity):
entity_keys = KEYS[type(entity)]
result = {}
for k, v in entity.__dict__.items():
if k in entity_keys:
if type(v) in KEYS:
result[k] = to_dict(v)
elif type(v) == list and len(v) > 0 and type(v[0]) in KEYS:
result[k] = [to_dict(i) for i in v]
elif type(v) in (datetime.date, datetime.datetime):
result[k] = v.isoformat()
else:
result[k] = v
return result
# @pytest.fixture
def mock_amz_hist():
with open('output/transactions_2025.pkl', 'rb') as f:
return pickle.load(f)
# @pytest.fixture
def mock_ynab_hist():
with open('output/ynab_history.json') as f:
return json.load(f)
from main import TxCategory
def test_get_tx_categories(monkeypatch):
order = SimpleNamespace(
order = MagicMock(
items=[
SimpleNamespace(
MagicMock(
title='An example item',
price=17.49
)
@@ -77,7 +18,8 @@ def test_get_tx_categories(monkeypatch):
)
item_details={
'An example item': SimpleNamespace(
'An example item': ItemClassification(
name='Example item',
category='Example category',
description='example',
)
@@ -95,13 +37,13 @@ def test_get_tx_categories(monkeypatch):
def test_get_categories_multi_items(monkeypatch):
order = SimpleNamespace(
order = MagicMock(
items=[
SimpleNamespace(
MagicMock(
title='An example item',
price=17.49
),
SimpleNamespace(
MagicMock(
title='Another example',
price=12.34,
)
@@ -109,11 +51,13 @@ def test_get_categories_multi_items(monkeypatch):
)
item_details={
'An example item': SimpleNamespace(
'An example item': ItemClassification(
name='Example item 1',
category='Example category',
description='example',
),
'Another example': SimpleNamespace(
'Another example': ItemClassification(
name='Example item 2',
category='Example category',
description='second example',
)
@@ -131,13 +75,13 @@ def test_get_categories_multi_items(monkeypatch):
def test_get_categories_multi_categories(monkeypatch):
order = SimpleNamespace(
order = MagicMock(
items=[
SimpleNamespace(
MagicMock(
title='An example item',
price=17.49
),
SimpleNamespace(
MagicMock(
title='Another example',
price=12.34,
)
@@ -145,11 +89,13 @@ def test_get_categories_multi_categories(monkeypatch):
)
item_details={
'An example item': SimpleNamespace(
'An example item': ItemClassification(
name='Example item 1',
category='Example category',
description='example',
),
'Another example': SimpleNamespace(
'Another example': ItemClassification(
name='Example item 2',
category='Other category',
description='second example',
)
@@ -172,12 +118,12 @@ def test_get_categories_multi_categories(monkeypatch):
def test_build_tx_update_multi():
tx_categories = [
SimpleNamespace(
TxCategory(
category_name='Example category',
description='example',
ratio=0.68,
),
SimpleNamespace(
TxCategory(
category_name='Other category',
description='second example',
ratio=0.32,
@@ -189,10 +135,13 @@ def test_build_tx_update_multi():
'Other category': '456',
}
# ynab uses tenths of a cent as its unit
tx_total = 23760
tx = MagicMock(
id='958e13e6-7bfd-465e-956a-887f15c8c456',
# ynab uses tenths of a cent as its unit
amount=23760,
)
res = main.build_tx_update(tx_categories, category_ids, tx_total)
res = main.build_tx_update(tx_categories, category_ids, tx)
assert len(res.subtransactions) == 2
sub0 = res.subtransactions[0]