1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.talentcard.server.business.merchant;
 
import static org.assertj.core.api.Assertions.assertThat;
 
import org.junit.jupiter.api.Test;
 
/**
 * 关键词解析是纯函数,这里覆盖的是「用户输入的写法与目录里的写法不一致」这一类真实的搜索失败:
 * 目录是「中国农业银行股份有限公司邻水丰禾支行」,用户输入「农行邻水丰禾支行」搜不到。
 */
class BankBranchKeywordTests {
 
    @Test
    void splitsTheUsersHabitualSpellingIntoBankAndBranchParts() {
        BankBranchKeyword parsed = BankBranchKeyword.parse("农行邻水丰禾支行");
 
        assertThat(parsed.bankName()).isEqualTo("农业银行");
        assertThat(parsed.branchName()).isEqualTo("邻水丰禾支行");
        assertThat(parsed.clearingCode()).isEmpty();
    }
 
    @Test
    void normalizesCommonBankAbbreviations() {
        assertThat(BankBranchKeyword.parse("工行").bankName()).isEqualTo("工商银行");
        assertThat(BankBranchKeyword.parse("建行").bankName()).isEqualTo("建设银行");
        assertThat(BankBranchKeyword.parse("中行").bankName()).isEqualTo("中国银行");
        assertThat(BankBranchKeyword.parse("邮储").bankName()).isEqualTo("邮政储蓄银行");
        assertThat(BankBranchKeyword.parse("农商行").bankName()).isEqualTo("农村商业银行");
        assertThat(BankBranchKeyword.parse("浦发").bankName()).isEqualTo("浦东发展银行");
    }
 
    @Test
    void prefersTheLongestBankNameSoNoStrayPrefixIsLeft() {
        assertThat(BankBranchKeyword.parse("中国农业银行").bankName()).isEqualTo("农业银行");
        assertThat(BankBranchKeyword.parse("中国农业银行").branchName()).isEmpty();
        assertThat(BankBranchKeyword.parse("中国银行").bankName()).isEqualTo("中国银行");
        assertThat(BankBranchKeyword.parse("中国银行").branchName()).isEmpty();
        // 农业发展银行的名称里也含「农业」,不能和农业银行混为一谈。
        assertThat(BankBranchKeyword.parse("中国农业发展银行").bankName()).isEqualTo("农业发展银行");
    }
 
    @Test
    void keepsTheBranchFragmentWhenTheUserTypesTheOfficialFullName() {
        BankBranchKeyword parsed = BankBranchKeyword.parse("中国农业银行股份有限公司邻水丰禾支行");
 
        assertThat(parsed.bankName()).isEqualTo("农业银行");
        assertThat(parsed.branchName()).isEqualTo("股份有限公司邻水丰禾支行");
    }
 
    @Test
    void findsTheBankSpellingWhereverItAppears() {
        BankBranchKeyword parsed = BankBranchKeyword.parse("攀枝花农行");
 
        assertThat(parsed.bankName()).isEqualTo("农业银行");
        assertThat(parsed.branchName()).isEqualTo("攀枝花");
    }
 
    @Test
    void dropsSpacesSoTheBranchFragmentsBecomeContiguous() {
        BankBranchKeyword parsed = BankBranchKeyword.parse("农行 邻水 丰禾");
 
        assertThat(parsed.bankName()).isEqualTo("农业银行");
        assertThat(parsed.branchName()).isEqualTo("邻水丰禾");
    }
 
    @Test
    void treatsDigitsAsAClearingCodeFragment() {
        assertThat(BankBranchKeyword.parse("103676368390").clearingCode()).isEqualTo("103676368390");
        assertThat(BankBranchKeyword.parse("103676368390").branchName()).isEmpty();
        // 只记得后几位也要能搜到,所以按片段匹配而不是补齐前导零。
        assertThat(BankBranchKeyword.parse(" 676368390 ").clearingCode()).isEqualTo("676368390");
    }
 
    @Test
    void fallsBackToTheWholeKeywordWhenNoBankNameIsRecognized() {
        BankBranchKeyword parsed = BankBranchKeyword.parse("邻水丰禾");
 
        assertThat(parsed.bankName()).isEmpty();
        assertThat(parsed.branchName()).isEqualTo("邻水丰禾");
        // 表里没有的银行(如浙江泰隆商业银行)走这条路径,由 bank_name 那一侧命中。
        assertThat(BankBranchKeyword.parse("泰隆").branchName()).isEqualTo("泰隆");
    }
 
    @Test
    void blankKeywordsProduceNoPredicates() {
        assertThat(BankBranchKeyword.parse(null)).isEqualTo(new BankBranchKeyword("", "", ""));
        assertThat(BankBranchKeyword.parse("   ")).isEqualTo(new BankBranchKeyword("", "", ""));
    }
}