Revise NamedParameterUtilsTests for consistency
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,10 +16,8 @@
|
||||
|
||||
package org.springframework.r2dbc.core;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import io.r2dbc.spi.Parameters;
|
||||
@@ -42,10 +40,13 @@ import static org.mockito.Mockito.verify;
|
||||
* @author Mark Paluch
|
||||
* @author Jens Schauder
|
||||
* @author Anton Naydenov
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
class NamedParameterUtilsTests {
|
||||
|
||||
private final BindMarkersFactory BIND_MARKERS = BindMarkersFactory.indexed("$", 1);
|
||||
private static final BindMarkersFactory INDEXED_MARKERS = BindMarkersFactory.indexed("$", 1);
|
||||
|
||||
private static final BindMarkersFactory ANONYMOUS_MARKERS = BindMarkersFactory.anonymous("?");
|
||||
|
||||
|
||||
@Test
|
||||
@@ -73,7 +74,7 @@ class NamedParameterUtilsTests {
|
||||
namedParams.addValue("a", "a").addValue("b", "b").addValue("c", "c");
|
||||
|
||||
PreparedOperation<?> operation = NamedParameterUtils.substituteNamedParameters(
|
||||
"xxx :a :b :c", BIND_MARKERS, namedParams);
|
||||
"xxx :a :b :c", INDEXED_MARKERS, namedParams);
|
||||
|
||||
assertThat(operation.toQuery()).isEqualTo("xxx $1 $2 $3");
|
||||
|
||||
@@ -87,11 +88,11 @@ class NamedParameterUtilsTests {
|
||||
void substituteObjectArray() {
|
||||
MapBindParameterSource namedParams = new MapBindParameterSource(new HashMap<>());
|
||||
namedParams.addValue("a",
|
||||
Arrays.asList(new Object[] {"Walter", "Heisenberg"},
|
||||
new Object[] {"Walt Jr.", "Flynn"}));
|
||||
List.of(new Object[] {"Walter", "Heisenberg"},
|
||||
new Object[] {"Walt Jr.", "Flynn"}));
|
||||
|
||||
PreparedOperation<?> operation = NamedParameterUtils.substituteNamedParameters(
|
||||
"xxx :a", BIND_MARKERS, namedParams);
|
||||
"xxx :a", INDEXED_MARKERS, namedParams);
|
||||
|
||||
assertThat(operation.toQuery()).isEqualTo("xxx ($1, $2), ($3, $4)");
|
||||
}
|
||||
@@ -100,13 +101,13 @@ class NamedParameterUtilsTests {
|
||||
void shouldBindObjectArray() {
|
||||
MapBindParameterSource namedParams = new MapBindParameterSource(new HashMap<>());
|
||||
namedParams.addValue("a",
|
||||
Arrays.asList(new Object[] {"Walter", "Heisenberg"},
|
||||
new Object[] {"Walt Jr.", "Flynn"}));
|
||||
List.of(new Object[] {"Walter", "Heisenberg"},
|
||||
new Object[] {"Walt Jr.", "Flynn"}));
|
||||
|
||||
BindTarget bindTarget = mock();
|
||||
|
||||
PreparedOperation<?> operation = NamedParameterUtils.substituteNamedParameters(
|
||||
"xxx :a", BIND_MARKERS, namedParams);
|
||||
"xxx :a", INDEXED_MARKERS, namedParams);
|
||||
operation.bindTo(bindTarget);
|
||||
|
||||
verify(bindTarget).bind(0, "Walter");
|
||||
@@ -141,7 +142,7 @@ class NamedParameterUtilsTests {
|
||||
|
||||
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
|
||||
PreparedOperation<?> operation = NamedParameterUtils.substituteNamedParameters(
|
||||
parsedSql, BIND_MARKERS, new MapBindParameterSource());
|
||||
parsedSql, INDEXED_MARKERS, new MapBindParameterSource());
|
||||
|
||||
assertThat(operation.toQuery()).isEqualTo(expectedSql);
|
||||
}
|
||||
@@ -312,14 +313,13 @@ class NamedParameterUtilsTests {
|
||||
void multipleEqualParameterReferencesBindsValueOnce() {
|
||||
String sql = "SELECT * FROM person where name = :id or lastname = :id";
|
||||
|
||||
BindMarkersFactory factory = BindMarkersFactory.indexed("$", 0);
|
||||
MapBindParameterSource source = new MapBindParameterSource(Map.of("id", Parameters.in("foo")));
|
||||
PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(sql, INDEXED_MARKERS, source);
|
||||
|
||||
PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(
|
||||
sql, factory, new MapBindParameterSource(
|
||||
Collections.singletonMap("id", Parameters.in("foo"))));
|
||||
assertThat(operation.toQuery())
|
||||
.isEqualTo("SELECT * FROM person where name = $1 or lastname = $1");
|
||||
|
||||
assertThat(operation.toQuery()).isEqualTo(
|
||||
"SELECT * FROM person where name = $0 or lastname = $0");
|
||||
Map<Integer, Object> bindings = new HashMap<>();
|
||||
|
||||
operation.bindTo(new BindTarget() {
|
||||
@Override
|
||||
@@ -328,8 +328,7 @@ class NamedParameterUtilsTests {
|
||||
}
|
||||
@Override
|
||||
public void bind(int index, Object value) {
|
||||
assertThat(index).isEqualTo(0);
|
||||
assertThat(value).isEqualTo(Parameters.in("foo"));
|
||||
bindings.put(index, value);
|
||||
}
|
||||
@Override
|
||||
public void bindNull(String identifier, Class<?> type) {
|
||||
@@ -340,23 +339,25 @@ class NamedParameterUtilsTests {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
});
|
||||
|
||||
assertThat(bindings)
|
||||
.hasSize(1)
|
||||
.containsEntry(0, Parameters.in("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void multipleEqualCollectionParameterReferencesBindsValueOnce() {
|
||||
void multipleEqualCollectionParameterReferencesForIndexedMarkersBindsValueOnce() {
|
||||
String sql = "SELECT * FROM person where name IN (:ids) or lastname IN (:ids)";
|
||||
|
||||
BindMarkersFactory factory = BindMarkersFactory.indexed("$", 0);
|
||||
MapBindParameterSource source = new MapBindParameterSource(Map.of("ids",
|
||||
Parameters.in(List.of("foo", "bar", "baz"))));
|
||||
PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(sql, INDEXED_MARKERS, source);
|
||||
|
||||
assertThat(operation.toQuery())
|
||||
.isEqualTo("SELECT * FROM person where name IN ($1, $2, $3) or lastname IN ($1, $2, $3)");
|
||||
|
||||
MultiValueMap<Integer, Object> bindings = new LinkedMultiValueMap<>();
|
||||
|
||||
PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(
|
||||
sql, factory, new MapBindParameterSource(Collections.singletonMap("ids",
|
||||
Parameters.in(Arrays.asList("foo", "bar", "baz")))));
|
||||
|
||||
assertThat(operation.toQuery()).isEqualTo(
|
||||
"SELECT * FROM person where name IN ($0, $1, $2) or lastname IN ($0, $1, $2)");
|
||||
|
||||
operation.bindTo(new BindTarget() {
|
||||
@Override
|
||||
public void bind(String identifier, Object value) {
|
||||
@@ -364,8 +365,6 @@ class NamedParameterUtilsTests {
|
||||
}
|
||||
@Override
|
||||
public void bind(int index, Object value) {
|
||||
assertThat(index).isIn(0, 1, 2);
|
||||
assertThat(value).isIn("foo", "bar", "baz");
|
||||
bindings.add(index, value);
|
||||
}
|
||||
@Override
|
||||
@@ -378,25 +377,24 @@ class NamedParameterUtilsTests {
|
||||
}
|
||||
});
|
||||
|
||||
assertThat(bindings).containsEntry(0, Collections.singletonList("foo")) //
|
||||
.containsEntry(1, Collections.singletonList("bar")) //
|
||||
.containsEntry(2, Collections.singletonList("baz"));
|
||||
assertThat(bindings)
|
||||
.hasSize(3)
|
||||
.containsEntry(0, List.of("foo"))
|
||||
.containsEntry(1, List.of("bar"))
|
||||
.containsEntry(2, List.of("baz"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void multipleEqualParameterReferencesForAnonymousMarkersBindsValueMultipleTimes() {
|
||||
void multipleEqualParameterReferencesForAnonymousMarkersBindsValueTwice() {
|
||||
String sql = "SELECT * FROM person where name = :id or lastname = :id";
|
||||
|
||||
BindMarkersFactory factory = BindMarkersFactory.anonymous("?");
|
||||
MapBindParameterSource source = new MapBindParameterSource(Map.of("id", Parameters.in("foo")));
|
||||
PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(sql, ANONYMOUS_MARKERS, source);
|
||||
|
||||
PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(
|
||||
sql, factory, new MapBindParameterSource(
|
||||
Collections.singletonMap("id", Parameters.in("foo"))));
|
||||
assertThat(operation.toQuery())
|
||||
.isEqualTo("SELECT * FROM person where name = ? or lastname = ?");
|
||||
|
||||
assertThat(operation.toQuery()).isEqualTo(
|
||||
"SELECT * FROM person where name = ? or lastname = ?");
|
||||
|
||||
Map<Integer, Object> bindValues = new LinkedHashMap<>();
|
||||
Map<Integer, Object> bindings = new HashMap<>();
|
||||
|
||||
operation.bindTo(new BindTarget() {
|
||||
@Override
|
||||
@@ -405,7 +403,7 @@ class NamedParameterUtilsTests {
|
||||
}
|
||||
@Override
|
||||
public void bind(int index, Object value) {
|
||||
bindValues.put(index, value);
|
||||
bindings.put(index, value);
|
||||
}
|
||||
@Override
|
||||
public void bindNull(String identifier, Class<?> type) {
|
||||
@@ -417,21 +415,23 @@ class NamedParameterUtilsTests {
|
||||
}
|
||||
});
|
||||
|
||||
assertThat(bindValues).hasSize(2).containsEntry(0, Parameters.in("foo")).containsEntry(1, Parameters.in("foo"));
|
||||
assertThat(bindings)
|
||||
.hasSize(2)
|
||||
.containsEntry(0, Parameters.in("foo"))
|
||||
.containsEntry(1, Parameters.in("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void multipleEqualParameterReferencesBindsNullOnce() {
|
||||
String sql = "SELECT * FROM person where name = :id or lastname = :id";
|
||||
|
||||
BindMarkersFactory factory = BindMarkersFactory.indexed("$", 0);
|
||||
MapBindParameterSource source = new MapBindParameterSource(Map.of("id", Parameters.in(String.class)));
|
||||
PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(sql, INDEXED_MARKERS, source);
|
||||
|
||||
PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(
|
||||
sql, factory, new MapBindParameterSource(
|
||||
Collections.singletonMap("id", Parameters.in(String.class))));
|
||||
assertThat(operation.toQuery())
|
||||
.isEqualTo("SELECT * FROM person where name = $1 or lastname = $1");
|
||||
|
||||
assertThat(operation.toQuery()).isEqualTo(
|
||||
"SELECT * FROM person where name = $0 or lastname = $0");
|
||||
Map<Integer, Object> bindings = new HashMap<>();
|
||||
|
||||
operation.bindTo(new BindTarget() {
|
||||
@Override
|
||||
@@ -440,8 +440,7 @@ class NamedParameterUtilsTests {
|
||||
}
|
||||
@Override
|
||||
public void bind(int index, Object value) {
|
||||
assertThat(index).isEqualTo(0);
|
||||
assertThat(value).isEqualTo(Parameters.in(String.class));
|
||||
bindings.put(index, value);
|
||||
}
|
||||
@Override
|
||||
public void bindNull(String identifier, Class<?> type) {
|
||||
@@ -452,16 +451,20 @@ class NamedParameterUtilsTests {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
});
|
||||
|
||||
assertThat(bindings)
|
||||
.hasSize(1)
|
||||
.containsEntry(0, Parameters.in(String.class));
|
||||
}
|
||||
|
||||
|
||||
private String expand(ParsedSql sql) {
|
||||
return NamedParameterUtils.substituteNamedParameters(sql, BIND_MARKERS,
|
||||
private static String expand(ParsedSql sql) {
|
||||
return NamedParameterUtils.substituteNamedParameters(sql, INDEXED_MARKERS,
|
||||
new MapBindParameterSource()).toQuery();
|
||||
}
|
||||
|
||||
private String expand(String sql) {
|
||||
return NamedParameterUtils.substituteNamedParameters(sql, BIND_MARKERS,
|
||||
private static String expand(String sql) {
|
||||
return NamedParameterUtils.substituteNamedParameters(sql, INDEXED_MARKERS,
|
||||
new MapBindParameterSource()).toQuery();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user