-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
232 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
testdata/example.bean binary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import pytest | ||
from datetime import datetime, date | ||
from bean_utils import txs_query | ||
from conf import _load_config_from_dict | ||
from bean_utils.bean import init_bean_manager | ||
from bots import controller | ||
from bean_utils.bean_test import assert_txs_equal, mock_embedding | ||
from conf.config_data import Config | ||
from conf.i18n import gettext as _ | ||
|
||
|
||
today = str(datetime.now().astimezone().date()) | ||
|
||
|
||
@pytest.fixture | ||
def mock_env(tmp_path, monkeypatch): | ||
conf_data = { | ||
"embedding": { | ||
"enable": False, | ||
"db_store_folder": tmp_path, | ||
}, | ||
"beancount": { | ||
"filename": "testdata/example.bean", | ||
"currency": "USD", | ||
"account_distinguation_range": [2, 3], | ||
} | ||
} | ||
config = _load_config_from_dict(conf_data) | ||
manager = init_bean_manager() | ||
monkeypatch.setattr(controller, "bean_manager", manager) | ||
return config | ||
|
||
|
||
def test_fetch_expense(monkeypatch, mock_env): | ||
# Start and end is the same | ||
start, end = date(2023, 6, 29), date(2023, 6, 30) | ||
resp_table = controller.fetch_expense(start, end) | ||
assert resp_table.title == _("Expenditures on 2023-06-29") | ||
assert resp_table.headers == [_("Account"), _("Position")] | ||
assert resp_table.rows == [ | ||
["Expenses:Food", "31.59 USD"], | ||
] | ||
|
||
# Start and end is different | ||
# Test level | ||
start, end = date(2023, 6, 1), date(2023, 7, 1) | ||
resp_table = controller.fetch_expense(start, end, root_level=1) | ||
assert resp_table.title == _("Expenditures between 2023-06-01 - 2023-07-01") | ||
assert resp_table.headers == [_("Account"), _("Position")] | ||
assert resp_table.rows == [ | ||
["Expenses", "7207.08 USD, 2400.00 IRAUSD"], | ||
] | ||
|
||
|
||
def test_fetch_bill(monkeypatch, mock_env): | ||
# Start and end is the same | ||
start, end = date(2023, 6, 29), date(2023, 6, 30) | ||
resp_table = controller.fetch_bill(start, end) | ||
assert resp_table.title == _("Account changes on 2023-06-29") | ||
assert resp_table.headers == [_("Account"), _("Position")] | ||
assert resp_table.rows == [ | ||
["Expenses:Food", "31.59 USD"], | ||
["Liabilities:US", "-31.59 USD"], | ||
] | ||
|
||
# Start and end is different | ||
# Test level | ||
start, end = date(2023, 6, 1), date(2023, 7, 1) | ||
resp_table = controller.fetch_bill(start, end, root_level=1) | ||
assert resp_table.title == _("Account changes between 2023-06-01 - 2023-07-01") | ||
assert resp_table.headers == [_("Account"), _("Position")] | ||
assert resp_table.rows == [ | ||
['Assets', '3210.66768 USD, 10 VACHR, -2400.00 IRAUSD'], | ||
['Expenses', '7207.08 USD, 2400.00 IRAUSD'], | ||
['Income', '-10532.95 USD, -10 VACHR'], | ||
['Liabilities', '115.20 USD'] | ||
] | ||
|
||
|
||
def test_clone_txs(monkeypatch, mock_env): | ||
# Normal generation | ||
param = """ | ||
2023-05-23 * "Kin Soy" "Eating" #tag1 #tag2 | ||
Assets:US:BofA:Checking -23.40 USD | ||
Expenses:Food:Restaurant | ||
""" | ||
exp_trx = f""" | ||
{today} * "Kin Soy" "Eating" #tag1 #tag2 | ||
Assets:US:BofA:Checking -23.40 USD | ||
Expenses:Food:Restaurant | ||
""" | ||
response = controller.clone_txs(param) | ||
assert isinstance(response, controller.BaseMessage) | ||
assert_txs_equal(response.content, exp_trx) | ||
|
||
# Generate with error | ||
response = controller.clone_txs('') | ||
assert isinstance(response, controller.ErrorMessage) | ||
assert response.content == "No transaction found" | ||
|
||
|
||
def test_render_txs(monkeypatch, mock_env): | ||
# Normal generation | ||
responses = controller.render_txs('23.4 BofA:Checking "Kin Soy" Eating #tag1 #tag2') | ||
assert len(responses) == 1 | ||
exp_trx = f""" | ||
{today} * "Kin Soy" "Eating" #tag1 #tag2 | ||
Assets:US:BofA:Checking -23.40 USD | ||
Expenses:Food:Restaurant | ||
""" | ||
assert isinstance(responses[0], controller.BaseMessage) | ||
assert_txs_equal(responses[0].content, exp_trx) | ||
|
||
# Generate with error | ||
response = controller.render_txs('10.00 ICBC:Checking NotFound McDonalds "Big Mac"') | ||
assert isinstance(response, controller.ErrorMessage) | ||
assert response.content == 'ValueError: Account ICBC:Checking not found' | ||
|
||
|
||
def test_build_db(monkeypatch, mock_env): | ||
# Build db without embedding enabled | ||
response = controller.build_db() | ||
assert isinstance(response, controller.BaseMessage) | ||
assert response.content == _("Embedding is not enabled.") | ||
|
||
# Build db with embedding enabled | ||
monkeypatch.setattr(mock_env, "embedding", Config.from_dict({ | ||
"enable": True, | ||
"transaction_amount": 100, | ||
"candidates": 3, | ||
"output_amount": 2, | ||
})) | ||
monkeypatch.setattr(txs_query, "embedding", mock_embedding) | ||
response = controller.build_db() | ||
assert isinstance(response, controller.BaseMessage) | ||
assert response.content == f"Token usage: {mock_env.embedding.transaction_amount}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import pytest | ||
import yaml | ||
from conf.config_data import Config, ImmutableDict | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"method, args, kwargs", | ||
[ | ||
("__setitem__", ["key", "value"], {}), | ||
("__delitem__", ["key"], {}), | ||
("update", [["key", "value"]], {}), | ||
("clear", [], {}), | ||
("pop", ["key"], {}), | ||
("popitem", [], {}), | ||
("setdefault", ["key", "value"], {}), | ||
], | ||
) | ||
def test_immutable_dict(method, args, kwargs): | ||
immutable_dict = ImmutableDict({"key": "value"}) | ||
|
||
with pytest.raises(TypeError): | ||
getattr(immutable_dict, method)(*args, **kwargs) | ||
|
||
|
||
def test_config(tmp_path): | ||
conf_data = { | ||
"embedding": { | ||
"enable": False, | ||
"db_store_folder": str(tmp_path), | ||
}, | ||
"beancount": { | ||
"filename": "testdata/example.bean", | ||
"currency": "USD", | ||
"account_distinguation_range": [2, 3], | ||
} | ||
} | ||
config_path = tmp_path / "config.yaml" | ||
with open(config_path, 'w') as file: | ||
yaml.dump(conf_data, file) | ||
config = Config(str(config_path)) | ||
|
||
assert config | ||
assert config.embedding.enable is False | ||
assert config.beancount.filename == "testdata/example.bean" | ||
assert config.beancount.get("whatever") is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters