Skip to content

Commit 2532211

Browse files
committed
update test
1 parent 485de42 commit 2532211

File tree

1 file changed

+72
-27
lines changed

1 file changed

+72
-27
lines changed

tests/any/any.rs

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,68 @@ async fn it_can_fail_and_recover_with_pool() -> anyhow::Result<()> {
150150
Ok(())
151151
}
152152

153+
#[sqlx_macros::test]
154+
async fn it_can_query_by_string_args() -> sqlx::Result<()> {
155+
install_default_drivers();
156+
157+
let mut conn = new::<Any>().await?;
158+
159+
let string = "Hello, world!".to_string();
160+
let ref tuple = ("Hello, world!".to_string(),);
161+
162+
#[cfg(feature = "postgres")]
163+
const SQL: &str =
164+
"SELECT 'Hello, world!' as string where 'Hello, world!' in ($1, $2, $3, $4, $5, $6, $7)";
165+
166+
#[cfg(not(feature = "postgres"))]
167+
const SQL: &str =
168+
"SELECT 'Hello, world!' as string where 'Hello, world!' in (?, ?, ?, ?, ?, ?, ?)";
169+
170+
{
171+
let query = sqlx::query(SQL)
172+
// validate flexibility of lifetimes
173+
.bind(&string)
174+
.bind(&string[..])
175+
.bind(Some(&string))
176+
.bind(Some(&string[..]))
177+
.bind(&Option::<String>::None)
178+
.bind(&string.clone())
179+
.bind(&tuple.0); // should not get "temporary value is freed at the end of this statement" here
180+
181+
let result = query.fetch_one(&mut conn).await?;
182+
183+
let column_0: String = result.try_get(0)?;
184+
185+
assert_eq!(column_0, string);
186+
}
187+
188+
{
189+
let mut query = sqlx::query(SQL);
190+
191+
let query = || -> Result<_, BoxDynError> {
192+
// validate flexibility of lifetimes
193+
query.try_bind(&string)?;
194+
query.try_bind(&string[..])?;
195+
query.try_bind(Some(&string))?;
196+
query.try_bind(Some(&string[..]))?;
197+
query.try_bind(&Option::<String>::None)?;
198+
query.try_bind(&string.clone())?;
199+
query.try_bind(&tuple.0)?;
200+
201+
Ok(query)
202+
}()
203+
.map_err(Error::Encode)?;
204+
205+
let result = query.fetch_one(&mut conn).await?;
206+
207+
let column_0: String = result.try_get(0)?;
208+
209+
assert_eq!(column_0, string);
210+
}
211+
212+
Ok(())
213+
}
214+
153215
#[cfg(feature = "json")]
154216
#[sqlx_macros::test]
155217
async fn it_encodes_decodes_json() -> anyhow::Result<()> {
@@ -165,25 +227,9 @@ async fn it_encodes_decodes_json() -> anyhow::Result<()> {
165227
"items": [1, 2, 3]
166228
});
167229

168-
// Create temp table:
169-
sqlx::query("create temporary table json_test (data TEXT)")
170-
.execute(&mut conn)
171-
.await?;
172-
173-
#[cfg(feature = "postgres")]
174-
let query = "insert into json_test (data) values ($1)";
175-
176-
#[cfg(not(feature = "postgres"))]
177-
let query = "insert into json_test (data) values (?)";
178-
179-
// Insert into the temporary table:
180-
sqlx::query(query)
181-
.bind(Json(&json_value))
182-
.execute(&mut conn)
183-
.await?;
184-
185230
// This will work by encoding JSON as text and decoding it back
186-
let result: serde_json::Value = sqlx::query_scalar("select data from json_test")
231+
let result: serde_json::Value = sqlx::query_scalar("SELECT ?")
232+
.bind(Json(&json_value))
187233
.fetch_one(&mut conn)
188234
.await?;
189235

@@ -193,25 +239,22 @@ async fn it_encodes_decodes_json() -> anyhow::Result<()> {
193239
#[derive(Serialize, Deserialize, Debug, PartialEq)]
194240
struct TestData {
195241
name: String,
196-
value: i32,
197-
items: [i32; 3],
242+
count: i32,
198243
}
199244

200245
let test_data = TestData {
201-
name: "test".to_string(),
202-
value: 42,
203-
items: [1, 2, 3],
246+
name: "example".to_string(),
247+
count: 100,
204248
};
205249

206-
let result: Json<TestData> = sqlx::query_scalar("select data from json_test")
250+
let result: Json<TestData> = sqlx::query_scalar("SELECT ?")
251+
.bind(Json(&test_data))
207252
.fetch_one(&mut conn)
208253
.await?;
209254

210255
assert_eq!(result.0, test_data);
211-
#[sqlx_macros::test]
212-
async fn it_can_query_by_string_args() -> sqlx::Result<()> {
213-
install_default_drivers();
214256

257+
<<<<<<< Updated upstream
215258
let mut conn = new::<Any>().await?;
216259

217260
let string = "Hello, world!".to_string();
@@ -372,5 +415,7 @@ async fn it_can_query_by_string_args() -> sqlx::Result<()> {
372415
assert_eq!(column_0, string);
373416
}
374417

418+
=======
419+
>>>>>>> Stashed changes
375420
Ok(())
376421
}

0 commit comments

Comments
 (0)