Skip to content

Commit

Permalink
Merge pull request #120 from YuukiToriyama/feature/unit-test-on-api-f…
Browse files Browse the repository at this point in the history
…ailed/add-test

API失敗時のテストコード: 都道府県マスタ/市区町村マスタが取得できない場合のテストコードを追加
  • Loading branch information
YuukiToriyama authored Jan 12, 2024
2 parents ac3c02c + a780a12 commit 6997063
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
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

0 comments on commit 6997063

Please sign in to comment.