Skip to content

Commit

Permalink
都道府県マスタの取得に失敗した場合、ApiErrorとしてエラーメッセージをParseResultに含めるように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
YuukiToriyama committed Nov 23, 2023
1 parent e314369 commit 00ff3c3
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ pub async fn parse<T: Api>(api: T, input: &str) -> ParseResult {
Some(result) => result,
};
// その都道府県の市町村名リストを取得
let prefecture = api.get_prefecture_master(prefecture_name).await.unwrap();
let prefecture = match api.get_prefecture_master(prefecture_name).await {
Err(error) => return ParseResult {
address: Address::new(prefecture_name, "", "", rest),
error: Some(error)
},
Ok(result) => result,
};
// 市町村名を特定
let (rest, city_name) = match read_city(rest, prefecture) {
None => {
Expand Down Expand Up @@ -98,6 +104,20 @@ mod parser_tests {
);
}

#[tokio::test]
async fn parse_mocked_fail_都道府県マスタの取得に失敗する() {
let api = ApiMock {should_fail: true};
let result = parse(api, "東京都新宿区西新宿二丁目8-1").await;
assert_eq!(result.address.prefecture, "東京都".to_string());
assert_eq!(result.address.city, "".to_string());
assert_eq!(result.address.town, "".to_string());
assert_eq!(result.address.rest, "新宿区西新宿二丁目8-1".to_string());
assert_eq!(
result.error.unwrap().error_type,
"ApiError".to_string()
);
}

#[tokio::test]
async fn parse_mocked_fail_市町村名が間違っている場合() {
let api = ApiMock { should_fail: false };
Expand Down

0 comments on commit 00ff3c3

Please sign in to comment.