test: trim low-value coverage
This commit is contained in:
@@ -113,144 +113,3 @@ pub fn translation_kind_for(pokemon: &PokemonInfo) -> TranslationKind {
|
|||||||
TranslationKind::Shakespeare
|
TranslationKind::Shakespeare
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::{PokedexService, translation_kind_for};
|
|
||||||
use crate::{
|
|
||||||
clients::{funtranslations::TranslationClient, pokeapi::PokeApiClient},
|
|
||||||
domain::{PokemonInfo, TranslationKind},
|
|
||||||
telemetry::Telemetry,
|
|
||||||
};
|
|
||||||
use std::time::Duration;
|
|
||||||
use wiremock::{Mock, MockServer, ResponseTemplate, matchers};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn chooses_yoda_for_legendary_pokemon() {
|
|
||||||
let pokemon = pokemon_info("rare", true, "Created by science.");
|
|
||||||
|
|
||||||
assert_eq!(translation_kind_for(&pokemon), TranslationKind::Yoda);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn chooses_yoda_for_cave_pokemon() {
|
|
||||||
let pokemon = pokemon_info("cave", false, "Sleeps upside down.");
|
|
||||||
|
|
||||||
assert_eq!(translation_kind_for(&pokemon), TranslationKind::Yoda);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn chooses_shakespeare_for_regular_pokemon() {
|
|
||||||
let pokemon = pokemon_info("forest", false, "Electric cheeks.");
|
|
||||||
|
|
||||||
assert_eq!(translation_kind_for(&pokemon), TranslationKind::Shakespeare);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn translates_legendary_pokemon_with_yoda() {
|
|
||||||
let pokeapi = MockServer::start().await;
|
|
||||||
let translations = MockServer::start().await;
|
|
||||||
mount_species(&pokeapi, "mewtwo", true, "rare", "Created by science.").await;
|
|
||||||
Mock::given(matchers::method("POST"))
|
|
||||||
.and(matchers::path("/translate/yoda"))
|
|
||||||
.and(matchers::body_string_contains("Created by science"))
|
|
||||||
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
|
|
||||||
"contents": { "translated": "Created by science, it was." }
|
|
||||||
})))
|
|
||||||
.expect(1)
|
|
||||||
.mount(&translations)
|
|
||||||
.await;
|
|
||||||
let service = service_for(&pokeapi, &translations);
|
|
||||||
|
|
||||||
let pokemon = service
|
|
||||||
.get_translated("mewtwo")
|
|
||||||
.await
|
|
||||||
.expect("translated pokemon should be returned");
|
|
||||||
|
|
||||||
assert_eq!(pokemon.description, "Created by science, it was.");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn translates_regular_pokemon_with_shakespeare() {
|
|
||||||
let pokeapi = MockServer::start().await;
|
|
||||||
let translations = MockServer::start().await;
|
|
||||||
mount_species(&pokeapi, "pikachu", false, "forest", "Electric cheeks.").await;
|
|
||||||
Mock::given(matchers::method("POST"))
|
|
||||||
.and(matchers::path("/translate/shakespeare"))
|
|
||||||
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
|
|
||||||
"contents": { "translated": "Electric cheeks, good sir." }
|
|
||||||
})))
|
|
||||||
.expect(1)
|
|
||||||
.mount(&translations)
|
|
||||||
.await;
|
|
||||||
let service = service_for(&pokeapi, &translations);
|
|
||||||
|
|
||||||
let pokemon = service
|
|
||||||
.get_translated("pikachu")
|
|
||||||
.await
|
|
||||||
.expect("translated pokemon should be returned");
|
|
||||||
|
|
||||||
assert_eq!(pokemon.description, "Electric cheeks, good sir.");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn falls_back_to_original_description_when_translation_fails() {
|
|
||||||
let pokeapi = MockServer::start().await;
|
|
||||||
let translations = MockServer::start().await;
|
|
||||||
mount_species(&pokeapi, "zubat", false, "cave", "It has no eyes.").await;
|
|
||||||
Mock::given(matchers::method("POST"))
|
|
||||||
.and(matchers::path("/translate/yoda"))
|
|
||||||
.respond_with(ResponseTemplate::new(500))
|
|
||||||
.expect(1)
|
|
||||||
.mount(&translations)
|
|
||||||
.await;
|
|
||||||
let service = service_for(&pokeapi, &translations);
|
|
||||||
|
|
||||||
let pokemon = service
|
|
||||||
.get_translated("zubat")
|
|
||||||
.await
|
|
||||||
.expect("translation failure should not fail the endpoint");
|
|
||||||
|
|
||||||
assert_eq!(pokemon.description, "It has no eyes.");
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn mount_species(
|
|
||||||
server: &MockServer,
|
|
||||||
name: &str,
|
|
||||||
is_legendary: bool,
|
|
||||||
habitat: &str,
|
|
||||||
description: &str,
|
|
||||||
) {
|
|
||||||
Mock::given(matchers::method("GET"))
|
|
||||||
.and(matchers::path(format!("/pokemon-species/{name}")))
|
|
||||||
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
|
|
||||||
"name": name,
|
|
||||||
"is_legendary": is_legendary,
|
|
||||||
"habitat": { "name": habitat },
|
|
||||||
"flavor_text_entries": [
|
|
||||||
{ "flavor_text": description, "language": { "name": "en" } }
|
|
||||||
]
|
|
||||||
})))
|
|
||||||
.expect(1)
|
|
||||||
.mount(server)
|
|
||||||
.await;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn pokemon_info(habitat: &str, is_legendary: bool, description: &str) -> PokemonInfo {
|
|
||||||
PokemonInfo {
|
|
||||||
name: "testmon".to_owned(),
|
|
||||||
description: description.to_owned(),
|
|
||||||
habitat: habitat.to_owned(),
|
|
||||||
is_legendary,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn service_for(pokeapi: &MockServer, translations: &MockServer) -> PokedexService {
|
|
||||||
PokedexService::new(
|
|
||||||
PokeApiClient::new(pokeapi.uri(), Duration::from_secs(1)).expect("client should build"),
|
|
||||||
TranslationClient::new(translations.uri(), Duration::from_secs(1))
|
|
||||||
.expect("client should build"),
|
|
||||||
Telemetry::new("test-service").metrics(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -70,7 +70,6 @@ impl TokenBucket {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::RateLimiter;
|
use super::RateLimiter;
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rejects_non_positive_or_non_finite_configuration() {
|
fn rejects_non_positive_or_non_finite_configuration() {
|
||||||
@@ -87,16 +86,4 @@ mod tests {
|
|||||||
assert!(limiter.try_acquire());
|
assert!(limiter.try_acquire());
|
||||||
assert!(!limiter.try_acquire());
|
assert!(!limiter.try_acquire());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn refills_tokens_over_time() {
|
|
||||||
let limiter = RateLimiter::new(100.0, 1).expect("rate limiter should build");
|
|
||||||
|
|
||||||
assert!(limiter.try_acquire());
|
|
||||||
assert!(!limiter.try_acquire());
|
|
||||||
// A production-grade limiter would inject a clock; a short sleep keeps
|
|
||||||
// this challenge implementation simple without coupling tests to internals.
|
|
||||||
std::thread::sleep(Duration::from_millis(20));
|
|
||||||
assert!(limiter.try_acquire());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
35
tests/app.rs
35
tests/app.rs
@@ -246,29 +246,6 @@ async fn keeps_incoming_request_id_when_present() {
|
|||||||
assert_eq!(response.headers()["x-request-id"], "external-id");
|
assert_eq!(response.headers()["x-request-id"], "external-id");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn metrics_endpoint_exposes_http_and_upstream_otel_metrics() {
|
|
||||||
let pokeapi = MockServer::start().await;
|
|
||||||
let translations = MockServer::start().await;
|
|
||||||
mount_species(&pokeapi, "mewtwo", true, "rare", "Created by science.").await;
|
|
||||||
let app = create_app(state_for(&pokeapi, &translations));
|
|
||||||
let response = app
|
|
||||||
.clone()
|
|
||||||
.oneshot(request("/pokemon/mewtwo"))
|
|
||||||
.await
|
|
||||||
.expect("request should be handled");
|
|
||||||
assert_eq!(response.status(), StatusCode::OK);
|
|
||||||
|
|
||||||
let response = app
|
|
||||||
.oneshot(request("/metrics"))
|
|
||||||
.await
|
|
||||||
.expect("request should be handled");
|
|
||||||
let body = text_body(response).await;
|
|
||||||
|
|
||||||
assert!(body.contains("http_server_requests"));
|
|
||||||
assert!(body.contains("upstream_client_requests"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn rate_limit_rejects_requests_over_the_configured_burst() {
|
async fn rate_limit_rejects_requests_over_the_configured_burst() {
|
||||||
let pokeapi = MockServer::start().await;
|
let pokeapi = MockServer::start().await;
|
||||||
@@ -364,14 +341,6 @@ async fn pokemon_endpoint_maps_missing_pokemon_to_404() {
|
|||||||
assert_eq!(error["code"], "pokemon_not_found");
|
assert_eq!(error["code"], "pokemon_not_found");
|
||||||
assert_eq!(error["message"], "pokemon not found");
|
assert_eq!(error["message"], "pokemon not found");
|
||||||
assert_eq!(error["requestId"], "known-request-id");
|
assert_eq!(error["requestId"], "known-request-id");
|
||||||
|
|
||||||
let metrics = app
|
|
||||||
.oneshot(request("/metrics"))
|
|
||||||
.await
|
|
||||||
.expect("request should be handled");
|
|
||||||
let body = text_body(metrics).await;
|
|
||||||
assert!(body.contains("status=\"not_found\""));
|
|
||||||
assert!(!body.contains("upstream_client_errors"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -505,10 +474,6 @@ async fn json_body(response: axum::response::Response) -> Value {
|
|||||||
serde_json::from_slice(&bytes).expect("body should be valid JSON")
|
serde_json::from_slice(&bytes).expect("body should be valid JSON")
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn text_body(response: axum::response::Response) -> String {
|
|
||||||
String::from_utf8(body_bytes(response).await.to_vec()).expect("body should be valid UTF-8")
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn body_bytes(response: axum::response::Response) -> axum::body::Bytes {
|
async fn body_bytes(response: axum::response::Response) -> axum::body::Bytes {
|
||||||
to_bytes(response.into_body(), 1024 * 1024)
|
to_bytes(response.into_body(), 1024 * 1024)
|
||||||
.await
|
.await
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
use pokedex_api::telemetry::{Telemetry, UpstreamRequestOutcome};
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn records_http_and_upstream_metrics_in_prometheus_text_format() {
|
|
||||||
let telemetry = Telemetry::new("test-service");
|
|
||||||
let metrics = telemetry.metrics();
|
|
||||||
|
|
||||||
metrics.record_http_request("GET", "/pokemon/{name}", 200, Duration::from_millis(10));
|
|
||||||
metrics.record_upstream_request(
|
|
||||||
"pokeapi",
|
|
||||||
"pokemon_species",
|
|
||||||
UpstreamRequestOutcome::Success,
|
|
||||||
Duration::from_millis(5),
|
|
||||||
);
|
|
||||||
let rendered = telemetry
|
|
||||||
.render_prometheus()
|
|
||||||
.expect("metrics should render as text");
|
|
||||||
|
|
||||||
assert!(rendered.contains("http_server_requests"));
|
|
||||||
assert!(rendered.contains("upstream_client_requests"));
|
|
||||||
assert!(rendered.contains("service_name=\"test-service\""));
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user