Skip to content

Commit 4ae9e81

Browse files
committed
repositories tx tests
1 parent a2b947e commit 4ae9e81

19 files changed

+513
-2
lines changed

src/main/java/com/arangodb/springframework/repository/query/ArangoResultConverter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ private GeoResult<T> buildGeoResult(final ArangoJsonNode data) {
160160
private GeoResults<T> buildGeoResults(final ArangoCursor<ArangoJsonNode> cursor) {
161161
final List<GeoResult<T>> list = new LinkedList<>();
162162
cursor.forEachRemaining(o -> list.add(buildGeoResult(o)));
163+
// FIXME: DE-803
164+
// convert geoResults to Metrics.NEUTRAL before
165+
// invoking GeoResults.GeoResults(java.util.List<? extends org.springframework.data.geo.GeoResult<T>>)
163166
return new GeoResults<>(list);
164167
}
165168

src/test/java/com/arangodb/springframework/AbstractTxTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public abstract class AbstractTxTest extends AbstractArangoTest {
1313
protected String tx;
1414
protected final DocumentCreateOptions insertOpts = new DocumentCreateOptions();
1515
protected final DocumentReadOptions findOpts = new DocumentReadOptions();
16-
protected final AqlQueryOptions queryOpts = new AqlQueryOptions();
16+
protected final AqlQueryOptions queryOpts = new AqlQueryOptions().batchSize(1);
1717
private final boolean withinTx;
1818

1919
protected AbstractTxTest(boolean withinTx, Class<?>... collections) {

src/test/java/com/arangodb/springframework/ArangoTestConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
@EnableArangoRepositories(basePackages = {
5050
"com.arangodb.springframework.repository",
5151
"com.arangodb.springframework.example.polymorphic.repository",
52-
"com.arangodb.springframework.debug.repository"},
52+
"com.arangodb.springframework.debug.repository",
53+
"com.arangodb.springframework.testdata.chess.repo"},
5354
namedQueriesLocation = "classpath*:arango-named-queries-test.properties")
5455
@EnableArangoAuditing(auditorAwareRef = "auditorProvider")
5556
public class ArangoTestConfiguration implements ArangoConfiguration {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.arangodb.springframework.testdata.chess;
2+
3+
import com.arangodb.springframework.AbstractTxTest;
4+
import com.arangodb.springframework.core.ArangoOperations;
5+
import com.arangodb.springframework.testdata.chess.entity.Player;
6+
import com.arangodb.springframework.testdata.chess.entity.Score;
7+
import com.arangodb.springframework.testdata.chess.entity.Tournament;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.data.geo.Point;
11+
12+
import java.time.LocalDate;
13+
import java.util.List;
14+
15+
abstract class AbstractRepositoryTest extends AbstractTxTest {
16+
17+
@Autowired
18+
private ArangoOperations ops;
19+
20+
protected List<Player> players = List.of(
21+
new Player("Magnus Carlsen", 2830, "Norway"),
22+
new Player("Fabiano Caruana", 2803, "US"),
23+
new Player("Maxime Vachier-Lagrave", 2732, "France"),
24+
new Player("Hikaru Nakamura", 2789, "US"),
25+
new Player("Ding Liren", 2762, "China"),
26+
new Player("Wesley So", 2757, "US"),
27+
new Player("Alireza Firouzja", 2760, "France"),
28+
new Player("Anish Giri", 2745, "Netherlands"),
29+
new Player("Ian Nepomniachtchi", 2758, "Russia")
30+
);
31+
32+
protected List<Tournament> tournaments = List.of(
33+
new Tournament(
34+
"Tata Steel 2023",
35+
LocalDate.of(2023, 1, 13),
36+
"Wijk aan Zee",
37+
new Point(4.6, 52.5)
38+
),
39+
new Tournament(
40+
"World Chess Championship 2023",
41+
LocalDate.of(2023, 4, 9),
42+
"Astana",
43+
new Point(71.422222, 51.147222)
44+
),
45+
new Tournament(
46+
"Norway Chess 2023",
47+
LocalDate.of(2023, 5, 30),
48+
"Stavanger",
49+
new Point(5.731389, 58.97)
50+
)
51+
);
52+
53+
protected List<Score> scores = List.of(
54+
new Score(players.get(7), tournaments.get(0), 1),
55+
new Score(players.get(0), tournaments.get(0), 3),
56+
new Score(players.get(5), tournaments.get(0), 4),
57+
new Score(players.get(1), tournaments.get(0), 5),
58+
new Score(players.get(4), tournaments.get(1), 1),
59+
new Score(players.get(8), tournaments.get(1), 2),
60+
new Score(players.get(3), tournaments.get(1), 4),
61+
new Score(players.get(1), tournaments.get(1), 5),
62+
new Score(players.get(6), tournaments.get(1), 6),
63+
new Score(players.get(3), tournaments.get(2), 1),
64+
new Score(players.get(1), tournaments.get(2), 2),
65+
new Score(players.get(7), tournaments.get(2), 4),
66+
new Score(players.get(5), tournaments.get(2), 5),
67+
new Score(players.get(0), tournaments.get(2), 6)
68+
);
69+
70+
protected AbstractRepositoryTest(boolean withinTx) {
71+
super(withinTx, Player.class, Score.class, Tournament.class);
72+
}
73+
74+
@BeforeEach
75+
void importData() {
76+
ops.insertAll(players, insertOpts, Player.class);
77+
ops.insertAll(tournaments, insertOpts, Tournament.class);
78+
ops.insertAll(scores, insertOpts, Score.class);
79+
}
80+
81+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.arangodb.springframework.testdata.chess;
2+
3+
import com.arangodb.springframework.testdata.chess.entity.*;
4+
import com.arangodb.springframework.testdata.chess.repo.PlayerRepository;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.data.domain.Page;
8+
import org.springframework.data.domain.PageRequest;
9+
10+
import java.util.Comparator;
11+
import java.util.List;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
abstract class PlayerRepositoryAbstract extends AbstractRepositoryTest {
16+
17+
@Autowired
18+
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
19+
PlayerRepository repo;
20+
21+
PlayerRepositoryAbstract(boolean withinTx) {
22+
super(withinTx);
23+
}
24+
25+
@Test
26+
void findAllByCountry() {
27+
List<Player> expected = players.stream()
28+
.filter(it -> "US".equals(it.getCountry()))
29+
.toList();
30+
Iterable<Player> found = repo.findAllByCountry("US", queryOpts);
31+
assertThat(found).containsExactlyInAnyOrderElementsOf(expected);
32+
found.forEach(this::checkRefs);
33+
}
34+
35+
@Test
36+
void findAllByRatingGreaterThan() {
37+
int rating = 2780;
38+
List<Player> expected = players.stream()
39+
.filter(it -> it.getRating() > rating)
40+
.sorted(Comparator.comparingInt(Player::getRating).reversed())
41+
.toList();
42+
43+
for (int i = 0; i < expected.size(); i++) {
44+
Page<Player> page = repo.findAllByRatingGreaterThanOrderByRatingDesc(PageRequest.of(i, 1), rating, queryOpts);
45+
assertThat(page.getTotalElements()).isEqualTo(expected.size());
46+
assertThat(page.getTotalPages()).isEqualTo(expected.size());
47+
Player current = page.iterator().next();
48+
assertThat(current).isEqualTo(expected.get(i));
49+
checkRefs(current);
50+
}
51+
}
52+
53+
private void checkRefs(Player p) {
54+
List<Score> expectedScores = scores.stream()
55+
.filter(it -> it.player().equals(p))
56+
.toList();
57+
assertThat(p.getScores()).containsExactlyInAnyOrderElementsOf(expectedScores);
58+
59+
List<Tournament> expectedTournaments = expectedScores.stream()
60+
.map(Score::tournament)
61+
.toList();
62+
assertThat(p.getTournaments()).containsExactlyInAnyOrderElementsOf(expectedTournaments);
63+
}
64+
65+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.arangodb.springframework.testdata.chess;
2+
3+
public class PlayerRepositoryTest extends PlayerRepositoryAbstract {
4+
PlayerRepositoryTest() {
5+
super(false);
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.arangodb.springframework.testdata.chess;
2+
3+
public class PlayerRepositoryTxTest extends PlayerRepositoryAbstract {
4+
PlayerRepositoryTxTest() {
5+
super(true);
6+
}
7+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.arangodb.springframework.testdata.chess;
2+
3+
import com.arangodb.springframework.testdata.chess.repo.ScoreRepository;
4+
import org.junit.jupiter.api.Disabled;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
abstract class ScoreRepositoryAbstract extends AbstractRepositoryTest {
11+
12+
@Autowired
13+
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
14+
ScoreRepository repo;
15+
16+
ScoreRepositoryAbstract(boolean withinTx) {
17+
super(withinTx);
18+
}
19+
20+
@Test
21+
@Disabled("BTS-1859")
22+
void findAll() {
23+
assertThat(repo.findAll(queryOpts))
24+
.hasSize(scores.size())
25+
.containsExactlyInAnyOrderElementsOf(scores);
26+
}
27+
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.arangodb.springframework.testdata.chess;
2+
3+
public class ScoreRepositoryTest extends ScoreRepositoryAbstract {
4+
ScoreRepositoryTest() {
5+
super(false);
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.arangodb.springframework.testdata.chess;
2+
3+
public class ScoreRepositoryTxTest extends ScoreRepositoryAbstract {
4+
ScoreRepositoryTxTest() {
5+
super(true);
6+
}
7+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.arangodb.springframework.testdata.chess;
2+
3+
import com.arangodb.springframework.testdata.chess.entity.Player;
4+
import com.arangodb.springframework.testdata.chess.entity.Score;
5+
import com.arangodb.springframework.testdata.chess.entity.Tournament;
6+
import com.arangodb.springframework.testdata.chess.repo.TournamentRepository;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.data.domain.PageRequest;
10+
import org.springframework.data.geo.*;
11+
12+
import java.time.LocalDate;
13+
import java.util.List;
14+
import java.util.Map;
15+
import java.util.function.Function;
16+
17+
import static java.util.stream.Collectors.toMap;
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.withinPercentage;
20+
21+
abstract class TournamentRepositoryAbstract extends AbstractRepositoryTest {
22+
23+
@Autowired
24+
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
25+
TournamentRepository repo;
26+
27+
TournamentRepositoryAbstract(boolean withinTx) {
28+
super(withinTx);
29+
}
30+
31+
// equirectangular approximation
32+
static Distance calculateDistance(Point p1, Point p2) {
33+
double lat1Rad = Math.toRadians(p1.getY());
34+
double lat2Rad = Math.toRadians(p2.getY());
35+
double lon1Rad = Math.toRadians(p1.getX());
36+
double lon2Rad = Math.toRadians(p2.getX());
37+
38+
double x = (lon2Rad - lon1Rad) * Math.cos((lat1Rad + lat2Rad) / 2);
39+
double y = (lat2Rad - lat1Rad);
40+
double distance = Math.sqrt(x * x + y * y) * 6371;
41+
42+
return new Distance(distance, Metrics.KILOMETERS);
43+
}
44+
45+
@Test
46+
void findAllByDateBetween() {
47+
var start = LocalDate.of(2023, 1, 1);
48+
var end = LocalDate.of(2023, 5, 1);
49+
List<Tournament> expected = tournaments.stream()
50+
.filter(it -> it.getDate().isAfter(start))
51+
.filter(it -> it.getDate().isBefore(end))
52+
.toList();
53+
List<Tournament> found = repo.findAllByDateBetween(start, end, queryOpts);
54+
assertThat(found)
55+
.hasSize(expected.size())
56+
.containsExactlyInAnyOrderElementsOf(expected);
57+
found.forEach(this::checkRefs);
58+
}
59+
60+
@Test
61+
void findByNameContainingIgnoreCase() {
62+
String match = "2023";
63+
List<Tournament> expected = tournaments.stream()
64+
.filter(it -> it.getName().contains(match))
65+
.toList();
66+
Iterable<Tournament> found = repo.findByNameContainingIgnoreCase(match, queryOpts);
67+
assertThat(found)
68+
.hasSize(expected.size())
69+
.containsExactlyInAnyOrderElementsOf(expected);
70+
found.forEach(this::checkRefs);
71+
}
72+
73+
@Test
74+
public void findAllByLocationWithin() {
75+
Point p = new Point(4.893611, 52.372778); // Amsterdam
76+
Distance d = new Distance(1_000, Metrics.KILOMETERS);
77+
78+
List<Tournament> expected = tournaments.stream()
79+
.filter(it -> calculateDistance(p, it.getLocation()).compareTo(d) < 0)
80+
.toList();
81+
82+
List<Tournament> found = repo.findAllByLocationWithin(p, d, queryOpts);
83+
assertThat(found)
84+
.hasSize(expected.size())
85+
.containsExactlyInAnyOrderElementsOf(expected);
86+
found.forEach(this::checkRefs);
87+
}
88+
89+
@Test
90+
public void findAllByLocationWithinPageable() {
91+
Point p = new Point(4.893611, 52.372778); // Amsterdam
92+
Distance d = new Distance(1_000, Metrics.KILOMETERS);
93+
94+
Map<Tournament, Distance> distances = tournaments.stream()
95+
.collect(toMap(Function.identity(), t -> calculateDistance(p, t.getLocation())));
96+
List<Tournament> expected = tournaments.stream()
97+
.filter(it -> distances.get(it).compareTo(d) < 0)
98+
.toList();
99+
100+
for (int i = 0; i < expected.size(); i++) {
101+
GeoPage<Tournament> page = repo.findAllByLocationWithin(PageRequest.of(i, 1), p, d, queryOpts);
102+
assertThat(page.getTotalElements()).isEqualTo(expected.size());
103+
assertThat(page.getTotalPages()).isEqualTo(expected.size());
104+
GeoResult<Tournament> current = page.iterator().next();
105+
double expectedDistKm = distances.get(current.getContent()).in(Metrics.KILOMETERS).getValue();
106+
assertThat(current.getContent()).isEqualTo(expected.get(i));
107+
assertThat(current.getDistance().in(Metrics.KILOMETERS).getValue()).isCloseTo(expectedDistKm, withinPercentage(.01));
108+
// FIXME: DE-803
109+
// assertThat(page.getAverageDistance().in(Metrics.KILOMETERS).getValue()).isCloseTo(expectedDistKm, withinPercentage(.01));
110+
checkRefs(current.getContent());
111+
}
112+
}
113+
114+
private void checkRefs(Tournament t) {
115+
List<Score> expectedScores = scores.stream()
116+
.filter(it -> it.tournament().equals(t))
117+
.toList();
118+
assertThat(t.getStandings()).containsExactlyInAnyOrderElementsOf(expectedScores);
119+
120+
List<Player> expectedPlayers = expectedScores.stream()
121+
.map(Score::player)
122+
.toList();
123+
assertThat(t.getPlayers()).containsExactlyInAnyOrderElementsOf(expectedPlayers);
124+
}
125+
126+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.arangodb.springframework.testdata.chess;
2+
3+
public class TournamentRepositoryTest extends TournamentRepositoryAbstract {
4+
TournamentRepositoryTest() {
5+
super(false);
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.arangodb.springframework.testdata.chess;
2+
3+
public class TournamentRepositoryTxTest extends TournamentRepositoryAbstract {
4+
TournamentRepositoryTxTest() {
5+
super(true);
6+
}
7+
}

0 commit comments

Comments
 (0)