Skip to content

Commit

Permalink
Update mockito and use async functions
Browse files Browse the repository at this point in the history
Otherwise tests will fail to run due to it creating another tokio runtime within an existing one.
  • Loading branch information
CosminLazar committed Apr 16, 2024
1 parent b660a70 commit 9e94c3e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ url = { version = "2.4.1", features = ["serde"] }

[dev-dependencies]
fluent-asserter = "0.1.9"
mockito = "1.2.0"
mockito = "1.4.0"
testcontainers = "0.15.0"
assert-json-diff = "2.0.2"
insta = { version = "1.38.0", features = ["yaml", "filters"] }
26 changes: 15 additions & 11 deletions src/mitaffald/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ mod tests {

#[tokio::test]
async fn can_extract_data_using_address_id() {
let mut remote = mockito::Server::new();
let mut remote = mockito::Server::new_async().await;
let address_id = "123".to_string();
let config = AffaldVarmeConfig {
address: Address::Id(AddressId {
Expand All @@ -160,18 +160,19 @@ mod tests {
)
.with_status(200)
.with_body_from_file("src/mitaffald/remote_responses/container_information.json")
.create();
.create_async()
.await;

let actual = get_containers(config).await;

remote.assert();
remote.assert_async().await;
assert_that!(actual.is_ok()).is_true();
insta::assert_debug_snapshot!(actual.unwrap());
}

#[tokio::test]
async fn can_extract_data_using_traditional_address() {
let mut remote = mockito::Server::new();
let mut remote = mockito::Server::new_async().await;
let config = AffaldVarmeConfig {
address: Address::FullySpecified(TraditionalAddress {
street_name: "Kongevejen".to_string(),
Expand All @@ -192,25 +193,27 @@ mod tests {
]))
.with_status(200)
.with_body_from_file("src/mitaffald/remote_responses/address_lookup.json")
.create();
.create_async()
.await;

let container_info_mock = remote
.mock("GET", "/api/calendar/address/07514448_100_______")
.with_status(200)
.with_body_from_file("src/mitaffald/remote_responses/container_information.json")
.create();
.create_async()
.await;

let actual = get_containers(config).await;

address_lookup_mock.assert();
container_info_mock.assert();
address_lookup_mock.assert_async().await;
container_info_mock.assert_async().await;
assert_that!(actual.is_ok()).is_true();
insta::assert_debug_snapshot!(actual.unwrap());
}

#[tokio::test]
async fn can_handle_server_error() {
let mut remote = mockito::Server::new();
let mut remote = mockito::Server::new_async().await;
let config = AffaldVarmeConfig {
address: Address::Id(AddressId { id: "123".into() }),
base_url: Url::parse(remote.url().as_str()).unwrap(),
Expand All @@ -219,11 +222,12 @@ mod tests {
let remote = remote
.mock("GET", mockito::Matcher::Regex(".*".to_string()))
.with_status(500)
.create();
.create_async()
.await;

let actual = get_containers(config).await;

remote.assert();
remote.assert_async().await;
assert_that!(actual.is_err()).is_true();
assert_that!(actual.unwrap_err()).contains("Unexpected status code");
}
Expand Down
7 changes: 4 additions & 3 deletions tests/full_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn smoke_test() {
let mqtt_server = docker.run(HiveMQContainer::default());
let mqtt_server_port = mqtt_server.get_host_port_ipv4(1883);

let mut mit_affald_server = mockito::Server::new();
let mut mit_affald_server = mockito::Server::new_async().await;
let mit_affald_server_url = Url::parse(&mit_affald_server.url()).unwrap();
let address_id = "123".to_string();
let mit_affald_server = mit_affald_server
Expand All @@ -34,7 +34,8 @@ async fn smoke_test() {
)
.with_status(200)
.with_body_from_file("src/mitaffald/remote_responses/container_information.json")
.create();
.create_async()
.await;

let settings = Settings {
affaldvarme: AffaldVarmeConfig {
Expand Down Expand Up @@ -70,7 +71,7 @@ async fn smoke_test() {
collect_result.unwrap_err()
);

mit_affald_server.assert();
mit_affald_server.assert_async().await;

let actual = actual(collect_result.unwrap());
let expected = expectations();
Expand Down
7 changes: 4 additions & 3 deletions tests/full_flow_insta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn smoke_test_insta() {
let mqtt_server = docker.run(HiveMQContainer::default());
let mqtt_server_port = mqtt_server.get_host_port_ipv4(1883);

let mut mit_affald_server = mockito::Server::new();
let mut mit_affald_server = mockito::Server::new_async().await;
let mit_affald_server_url = Url::parse(&mit_affald_server.url()).unwrap();
let address_id = "123".to_string();
let mit_affald_server = mit_affald_server
Expand All @@ -32,7 +32,8 @@ async fn smoke_test_insta() {
)
.with_status(200)
.with_body_from_file("src/mitaffald/remote_responses/container_information.json")
.create();
.create_async()
.await;

let settings = Settings {
affaldvarme: AffaldVarmeConfig {
Expand Down Expand Up @@ -68,7 +69,7 @@ async fn smoke_test_insta() {
collect_result.unwrap_err()
);

mit_affald_server.assert();
mit_affald_server.assert_async().await;

let actual = actual(collect_result.unwrap());

Expand Down

0 comments on commit 9e94c3e

Please sign in to comment.