Skip to content

Commit

Permalink
added test changes from #303
Browse files Browse the repository at this point in the history
  • Loading branch information
rashtao committed Jun 19, 2024
1 parent 9e76930 commit 14fbd3b
Show file tree
Hide file tree
Showing 20 changed files with 479 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package com.arangodb.springframework;

import com.arangodb.ArangoDatabase;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -37,6 +38,7 @@ public abstract class AbstractArangoTest {

@Autowired
protected ArangoOperations template;
protected ArangoDatabase db;
protected final Class<?>[] collections;

protected AbstractArangoTest(final Class<?>... collections) {
Expand All @@ -50,6 +52,7 @@ public void before() {
template.collection(collection).truncate();
}
AbstractArangoTest.staticTemplate = template;
db = template.driver().db(ArangoTestConfiguration.DB);
}

@AfterAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
@EnableArangoRepositories(basePackages = {
"com.arangodb.springframework.repository",
"com.arangodb.springframework.example.polymorphic.repository",
"com.arangodb.springframework.debug.repository"},
"com.arangodb.springframework.debug.repository",
"com.arangodb.springframework.testdata.chess.repo"},
namedQueriesLocation = "classpath*:arango-named-queries-test.properties")
@EnableArangoAuditing(auditorAwareRef = "auditorProvider")
public class ArangoTestConfiguration implements ArangoConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void jsonNodeToCustom() {

@Test
public void customToJsonNodeFromDriver() {
ArangoCollection col = template.driver().db(ArangoTestConfiguration.DB).collection("customJsonNodeTestEntity");
ArangoCollection col = db.collection("customJsonNodeTestEntity");
final DocumentEntity meta = col.insertDocument(new CustomJsonNodeTestEntity("abc"));
final BaseDocument doc = col.getDocument(meta.getKey(), BaseDocument.class);
assertThat(doc.getAttribute(FIELD), is("abc"));
Expand All @@ -126,7 +126,7 @@ public void customToJsonNodeFromDriver() {

@Test
public void jsonNodeToCustomFromDriver() {
ArangoCollection col = template.driver().db(ArangoTestConfiguration.DB).collection("testEntity");
ArangoCollection col = db.collection("testEntity");
final DocumentEntity meta = col.insertDocument(new TestEntity("abc"));
final CustomJsonNodeTestEntity doc = col.getDocument(meta.getKey(), CustomJsonNodeTestEntity.class);
assertThat(doc.getValue(), is("abc"));
Expand Down Expand Up @@ -192,7 +192,7 @@ public void jsonNodeToDBEntity() {

@Test
public void customToDBEntityFromDriver() {
ArangoCollection col = template.driver().db(ArangoTestConfiguration.DB).collection("customDBEntityTestEntity");
ArangoCollection col = db.collection("customDBEntityTestEntity");
final DocumentEntity meta = col.insertDocument(new CustomDBEntityTestEntity("abc"));
final BaseDocument doc = col.getDocument(meta.getKey(), BaseDocument.class);
assertThat(doc.getAttribute(FIELD), is("abc"));
Expand All @@ -201,7 +201,7 @@ public void customToDBEntityFromDriver() {

@Test
public void jsonNodeToDBEntityFromDriver() {
ArangoCollection col = template.driver().db(ArangoTestConfiguration.DB).collection("testEntity");
ArangoCollection col = db.collection("testEntity");
final DocumentEntity meta = col.insertDocument(new TestEntity("abc"));
final CustomDBEntityTestEntity doc = col.getDocument(meta.getKey(), CustomDBEntityTestEntity.class);
assertThat(doc.getValue(), is("abc"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void fieldNameAnnotation() {
entity.test = "1234";
final DocumentEntity res = template.insert(entity);
String colName = res.getId().split("/")[0];
final ObjectNode slice = template.driver().db(ArangoTestConfiguration.DB)
final ObjectNode slice = db
.collection(colName)
.getDocument(res.getKey(), ObjectNode.class);
assertThat(slice, is(notNullValue()));
Expand Down Expand Up @@ -632,7 +632,7 @@ public void auditingTest() throws InterruptedException {
assertThat(find.modifiedBy.getId(), is(createID));
}

final ObjectNode doc = template.driver().db(ArangoTestConfiguration.DB).collection("auditingTestEntity")
final ObjectNode doc = db.collection("auditingTestEntity")
.getDocument(value.id, ObjectNode.class);
assertThat(doc, is(notNullValue()));
assertThat(doc.get("createdBy").isObject(), is(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ static class SubClassWithOwnDocumentAnnotation extends BasicTestEntity {
public void overrideDocumentAnnotation() {
final SubClassWithOwnDocumentAnnotation doc = new SubClassWithOwnDocumentAnnotation();
template.insert(doc);
assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("overrideDocAn").exists(), is(true));
assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("overrideDocAn").count().getCount(),
assertThat(db.collection("overrideDocAn").exists(), is(true));
assertThat(db.collection("overrideDocAn").count().getCount(),
is(1L));
}

Expand All @@ -351,8 +351,8 @@ public void overrideEdgeAnnotation() {
edge.from = from;
edge.to = to;
template.insert(edge);
assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("overrideEdgeAn").exists(), is(true));
assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("overrideEdgeAn").count().getCount(),
assertThat(db.collection("overrideEdgeAn").exists(), is(true));
assertThat(db.collection("overrideEdgeAn").count().getCount(),
is(1L));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ public void collectionLevel() {
{
tenantProvider.setId("tenant00");
template.insert(new MultiTenancyTestEntity());
assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("tenant00_collection").exists(),
assertThat(db.collection("tenant00_collection").exists(),
is(true));
}
{
tenantProvider.setId("tenant01");
template.insert(new MultiTenancyTestEntity());
assertThat(template.driver().db(ArangoTestConfiguration.DB).collection("tenant01_collection").exists(),
assertThat(db.collection("tenant01_collection").exists(),
is(true));
}
assertThat(
template.driver().db(ArangoTestConfiguration.DB).collection("tenant00_collection").count().getCount(),
db.collection("tenant00_collection").count().getCount(),
is(1L));
assertThat(
template.driver().db(ArangoTestConfiguration.DB).collection("tenant01_collection").count().getCount(),
db.collection("tenant01_collection").count().getCount(),
is(1L));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
public class PolymorphicTemplate extends AbstractArangoTest {

private ArangoCollection col() {
return template.driver().db(ArangoTestConfiguration.DB).collection("animals");
return db.collection("animals");
}

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.arangodb.springframework.testdata.chess;

import com.arangodb.springframework.AbstractArangoTest;
import com.arangodb.springframework.core.ArangoOperations;
import com.arangodb.springframework.testdata.chess.entity.Player;
import com.arangodb.springframework.testdata.chess.entity.Score;
import com.arangodb.springframework.testdata.chess.entity.Tournament;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;

import java.time.LocalDate;
import java.util.List;

abstract class AbstractRepositoryTest extends AbstractArangoTest {

@Autowired
private ArangoOperations ops;

protected List<Player> players = List.of(
new Player("Magnus Carlsen", 2830, "Norway"),
new Player("Fabiano Caruana", 2803, "US"),
new Player("Maxime Vachier-Lagrave", 2732, "France"),
new Player("Hikaru Nakamura", 2789, "US"),
new Player("Ding Liren", 2762, "China"),
new Player("Wesley So", 2757, "US"),
new Player("Alireza Firouzja", 2760, "France"),
new Player("Anish Giri", 2745, "Netherlands"),
new Player("Ian Nepomniachtchi", 2758, "Russia")
);

protected List<Tournament> tournaments = List.of(
new Tournament(
"Tata Steel 2023",
LocalDate.of(2023, 1, 13),
"Wijk aan Zee",
new Point(4.6, 52.5)
),
new Tournament(
"World Chess Championship 2023",
LocalDate.of(2023, 4, 9),
"Astana",
new Point(71.422222, 51.147222)
),
new Tournament(
"Norway Chess 2023",
LocalDate.of(2023, 5, 30),
"Stavanger",
new Point(5.731389, 58.97)
)
);

protected List<Score> scores = List.of(
new Score(players.get(7), tournaments.get(0), 1),
new Score(players.get(0), tournaments.get(0), 3),
new Score(players.get(5), tournaments.get(0), 4),
new Score(players.get(1), tournaments.get(0), 5),
new Score(players.get(4), tournaments.get(1), 1),
new Score(players.get(8), tournaments.get(1), 2),
new Score(players.get(3), tournaments.get(1), 4),
new Score(players.get(1), tournaments.get(1), 5),
new Score(players.get(6), tournaments.get(1), 6),
new Score(players.get(3), tournaments.get(2), 1),
new Score(players.get(1), tournaments.get(2), 2),
new Score(players.get(7), tournaments.get(2), 4),
new Score(players.get(5), tournaments.get(2), 5),
new Score(players.get(0), tournaments.get(2), 6)
);

protected AbstractRepositoryTest() {
super(Player.class, Score.class, Tournament.class);
}

@BeforeEach
void importData() {
ops.insertAll(players, Player.class);
ops.insertAll(tournaments, Tournament.class);
ops.insertAll(scores, Score.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.arangodb.springframework.testdata.chess;

import com.arangodb.springframework.testdata.chess.entity.*;
import com.arangodb.springframework.testdata.chess.repo.PlayerRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import java.util.Comparator;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

abstract class PlayerRepositoryAbstract extends AbstractRepositoryTest {

@Autowired
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
PlayerRepository repo;

@Test
void findAllByCountry() {
List<Player> expected = players.stream()
.filter(it -> "US".equals(it.getCountry()))
.toList();
Iterable<Player> found = repo.findAllByCountry("US");
assertThat(found).containsExactlyInAnyOrderElementsOf(expected);
found.forEach(this::checkRefs);
}

@Test
void findAllByRatingGreaterThan() {
int rating = 2780;
List<Player> expected = players.stream()
.filter(it -> it.getRating() > rating)
.sorted(Comparator.comparingInt(Player::getRating).reversed())
.toList();

for (int i = 0; i < expected.size(); i++) {
Page<Player> page = repo.findAllByRatingGreaterThanOrderByRatingDesc(PageRequest.of(i, 1), rating);
assertThat(page.getTotalElements()).isEqualTo(expected.size());
assertThat(page.getTotalPages()).isEqualTo(expected.size());
Player current = page.iterator().next();
assertThat(current).isEqualTo(expected.get(i));
checkRefs(current);
}
}

private void checkRefs(Player p) {
List<Score> expectedScores = scores.stream()
.filter(it -> it.player().equals(p))
.toList();
assertThat(p.getScores()).containsExactlyInAnyOrderElementsOf(expectedScores);

List<Tournament> expectedTournaments = expectedScores.stream()
.map(Score::tournament)
.toList();
assertThat(p.getTournaments()).containsExactlyInAnyOrderElementsOf(expectedTournaments);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.arangodb.springframework.testdata.chess;

public class PlayerRepositoryTest extends PlayerRepositoryAbstract {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.arangodb.springframework.testdata.chess;

import com.arangodb.springframework.testdata.chess.repo.ScoreRepository;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.assertj.core.api.Assertions.assertThat;

abstract class ScoreRepositoryAbstract extends AbstractRepositoryTest {

@Autowired
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
ScoreRepository repo;

@Test
@Disabled("BTS-1859")
void findAll() {
assertThat(repo.findAll())
.hasSize(scores.size())
.containsExactlyInAnyOrderElementsOf(scores);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.arangodb.springframework.testdata.chess;

public class ScoreRepositoryTest extends ScoreRepositoryAbstract {
}
Loading

0 comments on commit 14fbd3b

Please sign in to comment.