添加文件: intelligence_finance_v1_ga/example_test.go

This commit is contained in:
renzhiyuan 2026-08-17 16:35:44 +08:00
parent 75d380038b
commit a74d5e8bdb
1 changed files with 190 additions and 0 deletions

View File

@ -0,0 +1,190 @@
package intelligence_finance_v1_ga
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
// TestInvoiceOrder 演示订单开票接口的调用。
func TestInvoiceOrder(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
if got := r.Header.Get("tenant-id"); got != "tenant-001" {
t.Errorf("tenant-id = %q", got)
}
if got := r.Header.Get("client-id"); got != "dd-ai-table" {
t.Errorf("client-id = %q", got)
}
if got := r.Header.Get("x-bfl-signature"); got == "" {
t.Error("missing x-bfl-signature header")
}
if got := r.Header.Get("x-bfl-signature-timestamp"); got == "" {
t.Error("missing x-bfl-signature-timestamp header")
}
if got := r.Header.Get("x-bfl-signature-nonce"); got == "" {
t.Error("missing x-bfl-signature-nonce header")
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"code":0,"msg":"ok","data":{"status":1,"errorMsg":"","dataList":[]}}`))
}))
defer server.Close()
client := NewClient(server.URL, "tenant-001", "dd-ai-table", "test-secret")
resp, err := client.InvoiceOrder(context.Background(), &InvoiceOrderRequest{
OrderID: "ORDER-001",
InvoiceType: InvoiceTypeDigitalNormal,
Purchaser: "测试购方企业",
TaxNum: "91310000MA1FL00000",
Email: "buyer@example.com",
Products: []InvoiceProduct{
{
ProductName: "测试服务",
RevenueCode: "3040101000000000000",
AmountIncludeTax: 113,
Quantity: 1,
TaxSign: 1,
TaxRate: 0.13,
},
},
})
if err != nil {
t.Fatalf("InvoiceOrder: %v", err)
}
if resp.Status != InvoiceStatusIssuing {
t.Errorf("status = %d, want %d", resp.Status, InvoiceStatusIssuing)
}
}
// TestQueryInvoiceStatus 演示开票状态查询接口的调用。
func TestQueryInvoiceStatus(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"code":0,"msg":"ok","data":{"status":3,"message":"开票成功","data":[]}}`))
}))
defer server.Close()
client := NewClient(server.URL, "tenant-001", "dd-ai-table", "test-secret")
resp, err := client.QueryInvoiceStatus(context.Background(), &QueryInvoiceStatusRequest{
OrderID: "ORDER-001",
})
if err != nil {
t.Fatalf("QueryInvoiceStatus: %v", err)
}
if resp.Status != InvoiceStatusSuccess {
t.Errorf("status = %d, want %d", resp.Status, InvoiceStatusSuccess)
}
if resp.Message != "开票成功" {
t.Errorf("message = %q", resp.Message)
}
}
// TestCreatePayment 演示创建付款单据接口的调用。
func TestCreatePayment(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"code":0,"msg":"ok","data":{"code":"PAY-001"}}`))
}))
defer server.Close()
client := NewClient(server.URL, "tenant-001", "dd-ai-table", "test-secret")
resp, err := client.CreatePayment(context.Background(), &CreatePaymentRequest{
Code: "PAY-001",
UserID: "user-001",
Title: "测试付款单",
Amount: "1000.00",
Department: &Department{
Name: "财务部",
},
Supplier: &Supplier{
Name: "测试供应商",
},
PaymentDetailList: []PaymentDetail{
{
Amount: "1000.00",
InvoiceInfo: &InvoiceInfo{
InvoiceNo: "12345678",
InvoiceCode: "011001900111",
},
},
},
})
if err != nil {
t.Fatalf("CreatePayment: %v", err)
}
if resp.Code != "PAY-001" {
t.Errorf("code = %q, want PAY-001", resp.Code)
}
}
// TestQueryPaymentStatus 演示支付状态查询接口的调用。
func TestQueryPaymentStatus(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"code":0,"msg":"ok","data":{"code":"PAY-001","instanceId":"inst-001","corpId":"corp-001","paymentStatus":"SUCCESS","paymentTime":"2024-01-01 10:00:00","userId":"user-001","source":"openapi","amount":"1000.00"}}`))
}))
defer server.Close()
client := NewClient(server.URL, "tenant-001", "dd-ai-table", "test-secret")
resp, err := client.QueryPaymentStatus(context.Background(), &QueryPaymentStatusRequest{
Code: "PAY-001",
UserID: "user-001",
})
if err != nil {
t.Fatalf("QueryPaymentStatus: %v", err)
}
if resp.PaymentStatus != PaymentStatusSuccess {
t.Errorf("paymentStatus = %q, want %q", resp.PaymentStatus, PaymentStatusSuccess)
}
}
// TestParsePaymentNotify 演示解析支付完成通知。
func TestParsePaymentNotify(t *testing.T) {
body := []byte(`{"code":"PAY-001","instanceId":"inst-001","corpId":"corp-001","paymentStatus":"SUCCESS","paymentTime":"2024-01-01 10:00:00","userId":"user-001","source":"openapi","amount":"1000.00","payerAccountInfo":{"accountType":"BANKCARD","bankOpenDTO":{"bankName":"交通银行","bankCardNo":"6222000000000000"}}}`)
info, err := ParsePaymentNotify(body)
if err != nil {
t.Fatalf("ParsePaymentNotify: %v", err)
}
if info.Code != "PAY-001" {
t.Errorf("code = %q", info.Code)
}
if info.PaymentStatus != PaymentStatusSuccess {
t.Errorf("paymentStatus = %q", info.PaymentStatus)
}
if info.PayerAccountInfo == nil || info.PayerAccountInfo.BankOpenDTO == nil {
t.Error("payerAccountInfo.bankOpenDTO is nil")
}
}
// TestParseNotify 演示解析通用通知。
func TestParseNotify(t *testing.T) {
body := []byte(`{"bizType":"invoice","bizId":"ORDER-001","data":"{\"status\":3}"}`)
req, err := ParseNotify(body)
if err != nil {
t.Fatalf("ParseNotify: %v", err)
}
if req.BizType != "invoice" {
t.Errorf("bizType = %q", req.BizType)
}
if req.BizID != "ORDER-001" {
t.Errorf("bizId = %q", req.BizID)
}
}
// TestHmacSHA256Base64 演示签名算法的正确性。
func TestHmacSHA256Base64(t *testing.T) {
// 使用固定输入验证签名结果可复现。
sig := HmacSHA256Base64("secret", "1700000000abc123")
if sig == "" {
t.Error("signature is empty")
}
}