Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 增加 TD test 以及 ledger 后端增加 tag 的支持 #108

Merged
merged 6 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ clean: ## Clean all the temporary files
@rm -rf ./double-entry-generator
@rm -rf ./wasm-dist

test: test-go test-alipay-beancount test-alipay-ledger test-wechat-beancount test-wechat-ledger test-huobi-beancount test-huobi-ledger test-htsec-beancount test-htsec-ledger test-icbc-beancount test-icbc-ledger ## Run all tests
test: test-go test-alipay-beancount test-alipay-ledger test-wechat-beancount test-wechat-ledger test-huobi-beancount test-huobi-ledger test-htsec-beancount test-htsec-ledger test-icbc-beancount test-icbc-ledger test-td-beancount test-td-ledger ## Run all tests

test-go: ## Run Golang tests
@go test ./...
Expand Down Expand Up @@ -122,6 +122,11 @@ test-icbc-beancount: ## Run tests for ICBC provider against beancount compiler
test-icbc-ledger: ## Run tests for ICBC provider against ledger compiler
@$(SHELL) ./test/icbc-test-ledger.sh

test-td-beancount: ## Run tests for TD provider against beancount compiler
@$(SHELL) ./test/td-test-beancount.sh
test-td-ledger: ## Run tests for TD provider against ledger compiler
@$(SHELL) ./test/td-test-ledger.sh

format: ## Format code
@gofmt -s -w pkg
@goimports -w pkg
Expand Down
2 changes: 2 additions & 0 deletions example/td/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ td:
rules:
- item: "T T"
targetAccount: Expenses:Grocery
tag: tt_tag
- item: "DOLLARAMA"
targetAccount: Expenses:Grocery
tag: grocery_tag1,cheap_tag2
- item: "DEVELOPM MSP"
targetAccount: Income:Salary
- type: 收入
Expand Down
86 changes: 44 additions & 42 deletions example/td/example-td-output.beancount
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
1970-01-01 * Open Balance
Assets:FIXME 0 CAD
Expenses:FIXME 0 CAD
Expenses:Grocery 0 CAD
Income:FIXME 0 CAD
Income:Salary 0 CAD
Equity:Opening Balances
2023-05-01 * TD - DOLLARAMA # 845 _M
Expenses:Grocery 1.96 CAD
Assets:DebitCard:TDChequing - 1.96 CAD

2023-05-01 * TD - T T SUPERMARKET _M
Expenses:Grocery 12.10 CAD
Assets:DebitCard:TDChequing - 12.10 CAD

2023-05-01 * TD - PTS TO: 02816700587
Expenses:FIXME 10.00 CAD
Assets:DebitCard:TDChequing - 10.00 CAD

2023-05-01 * TD - SFCA MSP
Assets:DebitCard:TDChequing 0.01 CAD
Assets:FIXME - 0.01 CAD

2023-05-01 * TD - ICBC AGENT #942
Expenses:FIXME 35.00 CAD
Assets:DebitCard:TDChequing - 35.00 CAD

2023-05-01 * TD - DOLLARAMA # 845 _M
Expenses:Grocery 15.68 CAD
Assets:DebitCard:TDChequing - 15.68 CAD

2023-05-01 * TD - T T SUPERMARKET _M
Expenses:Grocery 5.30 CAD
Assets:DebitCard:TDChequing - 5.30 CAD

2023-05-02 * TD - SEND E-TFR ***Y4h
Expenses:FIXME 239.13 CAD
Assets:DebitCard:TDChequing - 239.13 CAD

2023-05-03 * TD - DEVELOPM MSP
Assets:DebitCard:TDChequing 332.22 CAD
Income:Salary - 332.22 CAD
option "title" "测试"
option "operating_currency" "CAD"

1970-01-01 open Assets:FIXME
1970-01-01 open Expenses:FIXME
1970-01-01 open Expenses:Grocery
1970-01-01 open Income:FIXME
1970-01-01 open Income:Salary

2023-05-01 * "TD" "DOLLARAMA # 845 _M" #grocery_tag1 #cheap_tag2
Expenses:Grocery 1.96 CAD
Assets:DebitCard:TDChequing -1.96 CAD

2023-05-01 * "TD" "T T SUPERMARKET _M" #tt_tag
Expenses:Grocery 12.10 CAD
Assets:DebitCard:TDChequing -12.10 CAD

2023-05-01 * "TD" "PTS TO: 02816700587"
Expenses:FIXME 10.00 CAD
Assets:DebitCard:TDChequing -10.00 CAD

2023-05-01 * "TD" "SFCA MSP"
Assets:DebitCard:TDChequing 0.01 CAD
Assets:FIXME -0.01 CAD

2023-05-01 * "TD" "ICBC AGENT #942"
Expenses:FIXME 35.00 CAD
Assets:DebitCard:TDChequing -35.00 CAD

2023-05-01 * "TD" "DOLLARAMA # 845 _M" #grocery_tag1 #cheap_tag2
Expenses:Grocery 15.68 CAD
Assets:DebitCard:TDChequing -15.68 CAD

2023-05-01 * "TD" "T T SUPERMARKET _M" #tt_tag
Expenses:Grocery 5.30 CAD
Assets:DebitCard:TDChequing -5.30 CAD

2023-05-02 * "TD" "SEND E-TFR ***Y4h"
Expenses:FIXME 239.13 CAD
Assets:DebitCard:TDChequing -239.13 CAD

2023-05-03 * "TD" "DEVELOPM MSP"
Assets:DebitCard:TDChequing 332.22 CAD
Income:Salary -332.22 CAD

5 changes: 4 additions & 1 deletion example/td/example-td-output.ledger
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
Income:FIXME 0 CAD
Income:Salary 0 CAD
Equity:Opening Balances

2023-05-01 * TD - DOLLARAMA # 845 _M
; :grocery_tag1:cheap_tag2:
Expenses:Grocery 1.96 CAD
Assets:DebitCard:TDChequing - 1.96 CAD

2023-05-01 * TD - T T SUPERMARKET _M
; :tt_tag:
Expenses:Grocery 12.10 CAD
Assets:DebitCard:TDChequing - 12.10 CAD

Expand All @@ -27,10 +28,12 @@
Assets:DebitCard:TDChequing - 35.00 CAD

2023-05-01 * TD - DOLLARAMA # 845 _M
; :grocery_tag1:cheap_tag2:
Expenses:Grocery 15.68 CAD
Assets:DebitCard:TDChequing - 15.68 CAD

2023-05-01 * TD - T T SUPERMARKET _M
; :tt_tag:
Expenses:Grocery 5.30 CAD
Assets:DebitCard:TDChequing - 5.30 CAD

Expand Down
1 change: 1 addition & 0 deletions example/wechat/example-wechat-output.ledger
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
Assets:Digital:Wechat:Cash - 5505.00 CNY

2020-11-27 * 用户A - 收款方备注:二维码收款
; :income:service:
; merchantId: "/"
; method: "零钱"
; orderId: "3985734"
Expand Down
1 change: 1 addition & 0 deletions pkg/compiler/ledger/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func (ledger *Ledger) writeBill(file io.Writer, index int) error {
CommissionAccount: order.ExtraAccounts[ir.CommissionAccount],
Metadata: order.Metadata,
Currency: ledger.Config.DefaultCurrency,
Tags: order.Tags,
})
case ir.OrderTypeHuobiTrade: // Huobi trades
switch order.Type {
Expand Down
5 changes: 5 additions & 0 deletions pkg/compiler/ledger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ import (
*/

// 普通账单的模版(消费账)
// ledger的 tag语法定义: https://ledger-cli.org/doc/ledger3.html#Metadata-tags
// 单个tag定义: `; :TAG:`
// 多个tag定义: `; :TAG1:TAG2:TAG3:`
var normalOrder = `{{ .PayTime.Format "2006-01-02" }} * {{ EscapeString .Peer }} {{- if .Item }} - {{ EscapeString .Item }} {{ end }}
{{- if .Note}}; {{ .Note }}{{ end }}
{{- if .Tags}}{{printf "\n"}} ; :{{- range $index, $tag := .Tags}}{{ if $index }}:{{ end }}{{ $tag }}{{ end }}:{{ end }}
{{- range $key, $value := .Metadata }}{{ if $value }}{{ printf "\n" }} ; {{ $key }}: "{{ $value }}"{{end}}{{end}}
{{ .PlusAccount }} {{ .Money | printf "%.2f" }} {{ .Currency }}
{{ .MinusAccount }} - {{ .Money | printf "%.2f" }} {{ .Currency }}
Expand All @@ -40,6 +44,7 @@ type NormalOrderVars struct {
CommissionAccount string // 佣金账户
Metadata map[string]string // 元数据
Currency string // 货币
Tags []string // 标签
}

// 火币买入模版(手续费单位为购买单位货币)
Expand Down
30 changes: 30 additions & 0 deletions test/td-test-beancount.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
#
# E2E test for td provider.

# set -x # debug
set -eo errexit

TEST_DIR=`dirname "$(realpath $0)"`
ROOT_DIR="$TEST_DIR/.."

make -f "$ROOT_DIR/Makefile" build
mkdir -p "$ROOT_DIR/test/output"

# generate td bills output in beancount format
"$ROOT_DIR/bin/double-entry-generator" translate \
--provider td \
--config "$ROOT_DIR/example/td/config.yaml" \
--output "$ROOT_DIR/test/output/test-td-output.beancount" \
"$ROOT_DIR/example/td/example-td-records.csv"

diff -u --color \
"$ROOT_DIR/example/td/example-td-output.beancount" \
"$ROOT_DIR/test/output/test-td-output.beancount"

if [ $? -ne 0 ]; then
echo "[FAIL] td provider output is different from expected output."
exit 1
fi

echo "[PASS] All td provider tests!"
32 changes: 32 additions & 0 deletions test/td-test-ledger.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
#
# E2E test for td provider.

# set -x # debug
set -eo errexit

TEST_DIR=$(dirname "$(realpath $0)")
ROOT_DIR="$TEST_DIR/.."
OUTPUT="$ROOT_DIR/test/output/test-td-output.ledger"

make -f "$ROOT_DIR/Makefile" build
mkdir -p "$ROOT_DIR/test/output"

# generate td bills output in ledger format
"$ROOT_DIR/bin/double-entry-generator" translate \
--provider td \
--target ledger \
--config "$ROOT_DIR/example/td/config.yaml" \
--output "$OUTPUT" \
"$ROOT_DIR/example/td/example-td-records.csv"

diff -u --color \
"$ROOT_DIR/example/td/example-td-output.ledger" \
"$OUTPUT"

if [ $? -ne 0 ]; then
echo "[FAIL] td provider output is different from expected output."
exit 1
fi

echo "[PASS] All td provider for ledger target tests!"