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

API失敗時のテストコード: 都道府県マスタ/市区町村マスタが取得できない場合のテストコードを追加 #120

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
10 changes: 6 additions & 4 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub trait Api {
}

pub struct ApiImpl {
prefecture_master_api: PrefectureMasterApi,
city_master_api: CityMasterApi,
pub prefecture_master_api: PrefectureMasterApi,
pub city_master_api: CityMasterApi,
}

impl Api for ApiImpl {
Expand Down Expand Up @@ -72,7 +72,8 @@ impl BlockingApi for BlockingApiImpl {
fn new() -> Self {
BlockingApiImpl {
prefecture_master_api: PrefectureMasterApi {
server_url: "https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist",
server_url:
"https://yuukitoriyama.github.io/geolonia-japanese-addresses-accompanist",
},
city_master_api: CityMasterApi {
server_url: "https://geolonia.github.io/japanese-addresses/api/ja",
Expand All @@ -85,6 +86,7 @@ impl BlockingApi for BlockingApiImpl {
}

fn get_city_master(&self, prefecture_name: &str, city_name: &str) -> Result<City, Error> {
self.city_master_api.get_blocking(prefecture_name, city_name)
self.city_master_api
.get_blocking(prefecture_name, city_name)
}
}
32 changes: 32 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub async fn parse<T: Api>(api: T, input: &str) -> ParseResult {

#[cfg(test)]
mod non_blocking_tests {
use crate::api::city_master_api::CityMasterApi;
use crate::api::prefecture_master_api::PrefectureMasterApi;
use crate::api::{Api, ApiImpl};
use crate::err::ParseErrorKind;
use crate::parser::parse;
Expand All @@ -91,6 +93,21 @@ mod non_blocking_tests {
);
}

#[tokio::test]
async fn 都道府県マスタが取得できない場合() {
let mut api = ApiImpl::new();
api.prefecture_master_api = PrefectureMasterApi {
server_url: "https://example.com/invalid_url/api/",
};

let result = parse(api, "青森県青森市長島1丁目1−1").await;
assert_eq!(result.error.is_some(), true);
assert_eq!(result.address.prefecture, "青森県");
assert_eq!(result.address.city, "");
assert_eq!(result.address.town, "");
assert_eq!(result.address.rest, "青森市長島1丁目1−1");
}

#[tokio::test]
async fn 市区町村名が誤っている場合() {
let api = ApiImpl::new();
Expand All @@ -106,6 +123,21 @@ mod non_blocking_tests {
);
}

#[tokio::test]
async fn 市区町村マスタが取得できない場合() {
let mut api = ApiImpl::new();
api.city_master_api = CityMasterApi {
server_url: "https://example.com/invalid_url/api/",
};

let result = parse(api, "青森県青森市長島1丁目1−1").await;
assert_eq!(result.error.is_some(), true);
assert_eq!(result.address.prefecture, "青森県");
assert_eq!(result.address.city, "青森市");
assert_eq!(result.address.town, "");
assert_eq!(result.address.rest, "長島1丁目1−1");
}

#[tokio::test]
async fn 町名が誤っている場合() {
let api = ApiImpl::new();
Expand Down
Loading