Our SQL query is currently hard coded to fetch bulbasaur
.
sqlx::query_as!(
PokemonHp,
r#"SELECT name, hp FROM pokemon WHERE slug = "bulbasaur""#
)
We can replace "bulbasaur"
with a ?
placeholder. After we do this, we need to pass the additional argument in because sqlx will keep track of how many placeholders we're using and how many arguments we're passing in.
sqlx::query_as!(
PokemonHp,
r#"SELECT name, hp FROM pokemon WHERE slug = ?"#,
"charmander"
)
Running cargo test
with either the local database connection or the one we set up for the serverless function will show us a new test failure, which is expected because we changed the query.
DATABASE_URL=mysql://127.0.0.1:3306 cargo test -p pokemon-api
❯ DATABASE_URL=mysql://127.0.0.1:3306 cargo test -p pokemon-api
Compiling pokemon-api v0.1.0 (/rust-adventure/pokemon-api-netlify/crates/pokemon-api)
Finished test [unoptimized + debuginfo] target(s) in 1.07s
Running unittests src/main.rs (target/debug/deps/pokemon_api-b905b4a344b0a9e0)
running 1 test
test tests::accepts_apigw_request ... FAILED
failures:
---- tests::accepts_apigw_request stdout ----
thread 'tests::accepts_apigw_request' panicked at 'assertion failed: `(left == right)`
left: `Text("{\"name\":\"Charmander\",\"hp\":39}")`,
right: `Text("{\"name\":\"Bulbasaur\",\"hp\":45}")`', crates/pokemon-api/src/main.rs:61:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
tests::accepts_apigw_request
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.76s
error: test failed, to rerun pass `-p pokemon-api --bin pokemon-api`
We can update the accepts_apigw_request
test to use the new data and the tests will pass.
"{\"name\":\"Charmander\",\"hp\":39}"