Skip to content

Commit af72307

Browse files
CharlesZKQschauder
authored andcommitted
Fix Flaky Tests.
Original pull request #1097
1 parent 9fc7482 commit af72307

File tree

3 files changed

+49
-23
lines changed

3 files changed

+49
-23
lines changed

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlIdentifierParameterSourceUnitTests.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.springframework.data.relational.core.sql.IdentifierProcessing;
2222
import org.springframework.data.relational.core.sql.SqlIdentifier;
2323

24+
import java.util.Arrays;
25+
2426
/**
2527
* Tests for {@link SqlIdentifierParameterSource}.
2628
*
@@ -95,10 +97,11 @@ public void addOtherDatabaseObjectIdentifierParameterSource() {
9597
parameters2.addValue(SqlIdentifier.unquoted("key3"), 222);
9698

9799
parameters.addAll(parameters2);
98-
100+
String[] allKeys = parameters.getParameterNames();
101+
Arrays.sort(allKeys);
99102
assertSoftly(softly -> {
100103

101-
softly.assertThat(parameters.getParameterNames()).isEqualTo(new String[] { "key1", "key2", "key3" });
104+
softly.assertThat(allKeys).isEqualTo(new String[] { "key1", "key2", "key3" });
102105
softly.assertThat(parameters.getValue("key1")).isEqualTo(111);
103106
softly.assertThat(parameters.hasValue("key1")).isTrue();
104107
softly.assertThat(parameters.getSqlType("key1")).isEqualTo(11);

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/PartTreeJdbcQueryUnitTests.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -591,12 +591,22 @@ public void createsQueryByEmbeddedObject() throws Exception {
591591

592592
String expectedSql = BASE_SELECT + " WHERE (" + TABLE + ".\"USER_STREET\" = :user_street AND " + TABLE
593593
+ ".\"USER_CITY\" = :user_city)";
594+
String actualSql = query.getQuery();
594595

595-
assertThat(query.getQuery()).isEqualTo(expectedSql);
596+
assertThat(compareSqlStr(expectedSql, actualSql)).isTrue();
596597
assertThat(query.getParameterSource().getValue("user_street")).isEqualTo("Hello");
597598
assertThat(query.getParameterSource().getValue("user_city")).isEqualTo("World");
598599
}
599600

601+
private boolean compareSqlStr(String expectedSql, String actualSql) {
602+
String[] expected = expectedSql.split("WHERE");
603+
String[] actual = actualSql.split("WHERE");
604+
if (!expected[0].equals(actual[0])) return false;
605+
expected[1] = expected[1].trim().substring(1, expected[1].length() - 2);
606+
String[] flakyParts = expected[1].split("AND");
607+
return actual[1].contains(flakyParts[0]) && actual[1].contains(flakyParts[1]);
608+
}
609+
600610
@Test // DATAJDBC-318
601611
public void createsQueryByEmbeddedProperty() throws Exception {
602612

spring-data-relational/src/test/java/org/springframework/data/relational/repository/query/RelationalExampleMapperTests.java

+33-20
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,23 @@
2525
import lombok.Data;
2626
import lombok.NoArgsConstructor;
2727

28+
import java.util.ArrayList;
29+
import java.util.Arrays;
30+
import java.util.HashSet;
31+
import java.util.List;
2832
import java.util.Objects;
33+
import java.util.Optional;
34+
import java.util.Set;
35+
import java.util.stream.Collector;
36+
import java.util.stream.Collectors;
2937

3038
import org.junit.jupiter.api.BeforeEach;
3139
import org.junit.jupiter.api.Test;
3240
import org.springframework.data.annotation.Id;
3341
import org.springframework.data.domain.Example;
3442
import org.springframework.data.domain.ExampleMatcher;
3543
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
44+
import org.springframework.data.relational.core.query.CriteriaDefinition;
3645
import org.springframework.data.relational.core.query.Query;
3746

3847
/**
@@ -89,10 +98,9 @@ void queryByExampleWithFirstnameAndLastname() {
8998
Example<Person> example = Example.of(person);
9099

91100
Query query = exampleMapper.getMappedExample(example);
92-
93-
assertThat(query.getCriteria()) //
94-
.map(Object::toString) //
95-
.hasValue("(firstname = 'Frodo') AND (lastname = 'Baggins')");
101+
String actual = query.getCriteria().map(Object::toString).get();
102+
String expected = "(firstname = 'Frodo') AND (lastname = 'Baggins')";
103+
assertThat(compareStrWithFlakiness(expected, actual, "AND")).isTrue();
96104
}
97105

98106
@Test // GH-929
@@ -122,10 +130,9 @@ void queryByExampleWithNullMatchingFirstnameAndLastname() {
122130
Example<Person> example = Example.of(person, matcher);
123131

124132
Query query = exampleMapper.getMappedExample(example);
125-
126-
assertThat(query.getCriteria()) //
127-
.map(Object::toString) //
128-
.hasValue("(firstname IS NULL OR firstname = 'Bilbo') AND (lastname IS NULL OR lastname = 'Baggins')");
133+
String actual = query.getCriteria().map(Object::toString).get();
134+
String expected = "(firstname IS NULL OR firstname = 'Bilbo') AND (lastname IS NULL OR lastname = 'Baggins')";
135+
assertThat(compareStrWithFlakiness(expected, actual, "AND")).isTrue();
129136
}
130137

131138
@Test // GH-929
@@ -366,10 +373,9 @@ void queryByExampleWithFirstnameOrLastname() {
366373
Example<Person> example = Example.of(person, matcher);
367374

368375
Query query = exampleMapper.getMappedExample(example);
369-
370-
assertThat(query.getCriteria()) //
371-
.map(Object::toString) //
372-
.hasValue("(firstname = 'Frodo') OR (lastname = 'Baggins')");
376+
String actual = query.getCriteria().map(Object::toString).get();
377+
String expected = "(firstname = 'Frodo') OR (lastname = 'Baggins')";
378+
assertThat(compareStrWithFlakiness(expected, actual, "OR")).isTrue();
373379
}
374380

375381
@Test // GH-929
@@ -382,10 +388,9 @@ void queryByExampleEvenHandlesInvisibleFields() {
382388
Example<Person> example = Example.of(person);
383389

384390
Query query = exampleMapper.getMappedExample(example);
385-
386-
assertThat(query.getCriteria()) //
387-
.map(Object::toString) //
388-
.hasValue("(firstname = 'Frodo') AND (secret = 'I have the ring!')");
391+
String actual = query.getCriteria().map(Object::toString).get();
392+
String expected = "(firstname = 'Frodo') AND (secret = 'I have the ring!')";
393+
assertThat(compareStrWithFlakiness(expected, actual, "AND")).isTrue();
389394
}
390395

391396
@Test // GH-929
@@ -413,11 +418,19 @@ void queryByExampleSupportsPropertyTransforms() {
413418
Example<Person> example = Example.of(person, matcher);
414419

415420
Query query = exampleMapper.getMappedExample(example);
421+
String actual = query.getCriteria().map(Object::toString).get();
422+
String expected = "(firstname = 'FRODO') AND (lastname = 'baggins') AND (secret = 'I have the ring!')";
423+
assertThat(compareStrWithFlakiness(expected, actual, "AND")).isTrue();
424+
}
416425

417-
assertThat(query.getCriteria()) //
418-
.map(Object::toString) //
419-
.hasValue("(firstname = 'FRODO') AND (lastname = 'baggins') AND (secret = 'I have the ring!')");
420-
426+
private boolean compareStrWithFlakiness(String expected, String actual, String regex) {
427+
String[] flakyParts = expected.split(regex);
428+
for (String part : flakyParts) {
429+
if (!actual.contains(part)) {
430+
return false;
431+
}
432+
}
433+
return true;
421434
}
422435

423436
@Data

0 commit comments

Comments
 (0)