Skip to content

Commit

Permalink
Refine test code.
Browse files Browse the repository at this point in the history
Closes: #114
  • Loading branch information
junghoon-vans authored Nov 18, 2023
1 parent 68b842b commit a8b86b1
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,17 @@

package io.vanslog.spring.data.meilisearch.client;

import static org.assertj.core.api.AssertionsForClassTypes.*;
import static org.assertj.core.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.meilisearch.sdk.json.GsonJsonHandler;

/**
* Tests for {@link ClientConfiguration}.
*
* @author Junghoon Ban
*/
class ClientConfigurationTest {
class ClientConfigurationUnitTest {

ClientConfiguration clientConfiguration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package io.vanslog.spring.data.meilisearch.config;

import static org.assertj.core.api.AssertionsForClassTypes.*;
import static org.assertj.core.api.Assertions.*;

import io.vanslog.spring.data.meilisearch.client.ClientConfiguration;
import io.vanslog.spring.data.meilisearch.client.MeilisearchClient;
Expand All @@ -42,7 +42,7 @@
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration
class MeilisearchConfigurationTest {
class MeilisearchConfigurationUnitTests {

@Autowired private MeilisearchClient meilisearchClient;
@Autowired private MeilisearchOperations meilisearchTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,14 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import com.meilisearch.sdk.Client;

/**
* Namespace based configuration test.
*
* @author Junghoon Ban
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration("namespace.xml")
class MeilisearchNamespaceHandlerTest {
class MeilisearchNamespaceHandlerUnitTests {

@Autowired private ApplicationContext context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

package io.vanslog.spring.data.meilisearch.core;

import static org.assertj.core.api.AssertionsForClassTypes.*;
import static org.assertj.core.api.Assertions.*;

import io.vanslog.spring.data.meilisearch.annotations.Document;
import io.vanslog.spring.data.meilisearch.client.MeilisearchClient;
import io.vanslog.spring.data.meilisearch.entities.Movie;
import io.vanslog.spring.data.meilisearch.junit.jupiter.MeilisearchTest;
import io.vanslog.spring.data.meilisearch.junit.jupiter.MeilisearchTestConfiguration;
Expand All @@ -31,29 +32,52 @@
import org.springframework.data.annotation.Id;
import org.springframework.test.context.ContextConfiguration;

import com.meilisearch.sdk.exceptions.MeilisearchException;

/**
* Integration tests for {@link MeilisearchTemplate}.
*
* @author Junghoon Ban
*/
@MeilisearchTest
@ContextConfiguration(classes = { MeilisearchTestConfiguration.class })
class MeilisearchTemplateTest {
class MeilisearchTemplateIntegrationTests {

@Autowired MeilisearchClient meilisearchClient;
@Autowired MeilisearchOperations meilisearchTemplate;

Movie movie1 = new Movie(1, "Carol", "A love story", new String[] { "Romance", "Drama" });
Movie movie2 = new Movie(2, "Wonder Woman", "A superhero film", new String[] { "Action", "Adventure" });
Movie movie3 = new Movie(3, "Life of Pi", "A survival film", new String[] { "Adventure", "Drama" });

@BeforeEach
void setUp() {
meilisearchTemplate.deleteAll(Movie.class);
void setUp() throws MeilisearchException {
meilisearchClient.index("movies").deleteAllDocuments();
}

@Test
void shouldSaveEntity() {

Movie saved = meilisearchTemplate.save(movie1);

assertThat(saved).isEqualTo(movie1);
}

@Test
void shouldSaveDocument() {
void shouldSaveEntities() {

List<Movie> movies = List.of(movie1, movie2);

List<Movie> saved = meilisearchTemplate.save(movies);

assertThat(saved).isEqualTo(movies);
}

@Test
void shouldGetEntity() {

meilisearchTemplate.save(movie1);

Movie saved = meilisearchTemplate.get("1", Movie.class);

assertThat(saved.getId()).isEqualTo(movie1.getId());
Expand All @@ -63,76 +87,91 @@ void shouldSaveDocument() {
}

@Test
void shouldSaveDocuments() {
List<Movie> movies = List.of(movie1, movie2);
void shouldGetEntities() {

List<Movie> movies = List.of(movie1, movie2, movie3);
meilisearchTemplate.save(movies);
List<Movie> saved = meilisearchTemplate.multiGet(Movie.class);

assertThat(saved.size()).isEqualTo(movies.size());
List<Movie> savedMovies = meilisearchTemplate.multiGet(Movie.class);

assertThat(savedMovies).containsExactlyInAnyOrder(movie1, movie2, movie3);
}

@Test
void shouldDeleteDocument() {
meilisearchTemplate.save(movie1);
meilisearchTemplate.delete(movie1);
void shouldGetCertainEntities() {

Movie saved = meilisearchTemplate.get("1", Movie.class);
assertThat(saved).isNull();
List<Movie> movies = List.of(movie1, movie2, movie3);
meilisearchTemplate.save(movies);

List<Movie> savedMovies = meilisearchTemplate.multiGet(Movie.class, List.of("1", "3"));

assertThat(savedMovies).containsExactlyInAnyOrder(movie1, movie3);
}

@Test
void shouldDeleteDocuments() {
meilisearchTemplate.save(List.of(movie1, movie2, movie3));
meilisearchTemplate.delete(Movie.class, List.of("1", "2"));
void returnTrueWhenDocumentExists() {

meilisearchTemplate.save(movie1);

List<Movie> saved = meilisearchTemplate.multiGet(Movie.class);
boolean exists = meilisearchTemplate.exists("1", Movie.class);

assertThat(saved.size()).isEqualTo(1);
assertThat(exists).isTrue();
}

@Test
void shouldDeleteAllDocuments() {
meilisearchTemplate.save(List.of(movie1, movie2));
meilisearchTemplate.deleteAll(Movie.class);
void returnFalseWhenDocumentDoesNotExist() {

List<Movie> saved = meilisearchTemplate.multiGet(Movie.class);
meilisearchTemplate.save(movie1);
meilisearchTemplate.delete(movie1);

boolean exists = meilisearchTemplate.exists("1", Movie.class);

assertThat(saved.size()).isZero();
assertThat(exists).isFalse();
}

@Test
void shouldCountDocuments() {

meilisearchTemplate.save(List.of(movie1, movie2));

long count = meilisearchTemplate.count(Movie.class);

assertThat(count).isEqualTo(2);
}

@Test
void shouldExistsDocument() {
void shouldDeleteDocument() {

meilisearchTemplate.save(movie1);

boolean exists = meilisearchTemplate.exists("1", Movie.class);
boolean nonExists = meilisearchTemplate.exists("2", Movie.class);
meilisearchTemplate.delete(movie1);

assertThat(exists).isTrue();
assertThat(nonExists).isFalse();
assertThat(meilisearchTemplate.get("1", Movie.class)).isNull();
}

@Test
void shouldGetCertainDocuments() {
List<Movie> movies = List.of(movie1, movie2, movie3);
void shouldDeleteDocuments() {

meilisearchTemplate.save(movies);
List<Movie> saved = meilisearchTemplate.multiGet(Movie.class, List.of("1", "3", "4"));
meilisearchTemplate.save(List.of(movie1, movie2, movie3));

boolean result = meilisearchTemplate.delete(Movie.class, List.of("1", "2"));

assertThat(saved.size()).isEqualTo(2);
assertThat(saved.get(0).getTitle()).isEqualTo(movie1.getTitle());
assertThat(saved.get(1).getTitle()).isEqualTo(movie3.getTitle());
assertThat(result).isTrue();
}

@Test
void shouldSaveDocumentWithAnnotatedIdField() {
void shouldDeleteAllDocuments() {

meilisearchTemplate.save(List.of(movie1, movie2));

boolean result = meilisearchTemplate.deleteAll(Movie.class);

assertThat(result).isTrue();
}

@Test
void shouldSaveEntityWithAnnotatedIdField() {

AnnotatedIdField annotatedIdField = new AnnotatedIdField();
String documentId = "idField";
annotatedIdField.setName(documentId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* @author Junghoon Ban
*/
class MappingMeilisearchConverterTest {
class MappingMeilisearchConverterUnitTests {

private MappingMeilisearchConverter converter;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
/*
* Copyright 2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.vanslog.spring.data.meilisearch.core.convert;

import static org.assertj.core.api.AssertionsForClassTypes.*;
import static org.assertj.core.api.Assertions.*;

import io.vanslog.spring.data.meilisearch.config.MeilisearchConfigurationSupport;

Expand All @@ -18,7 +33,7 @@

@ExtendWith(SpringExtension.class)
@ContextConfiguration
public class MeilisearchCustomConversionsTests {
class MeilisearchCustomConversionsUnitTest {

@Autowired MeilisearchConverter meilisearchConverter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*
* @author Junghoon Ban
*/
class SimpleMeilisearchPersistentEntityTests {
class SimpleMeilisearchPersistentEntityUnitTests {

@Test
void shouldReturnIndexUid() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* @author Junghoon Ban
*/
class SimpleMeilisearchPersistentPropertyTest {
class SimpleMeilisearchPersistentPropertyUnitTests {
private final SimpleMeilisearchMappingContext context = new SimpleMeilisearchMappingContext();

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import io.vanslog.spring.data.meilisearch.annotations.Document;

import java.util.Arrays;
import java.util.Objects;

import org.springframework.data.annotation.Id;

/**
Expand Down Expand Up @@ -74,4 +77,28 @@ public String[] getGenres() {
public void setGenres(String[] genres) {
this.genres = genres;
}

@Override
public boolean equals(Object object) {

if (this == object) {
return true;
}

if (object == null || getClass() != object.getClass()) {
return false;
}

Movie movie = (Movie) object;
return id == movie.id && Objects.equals(title, movie.title) && Objects.equals(description, movie.description)
&& Arrays.equals(genres, movie.genres);
}

@Override
public int hashCode() {

int result = Objects.hash(id, title, description);
result = 31 * result + Arrays.hashCode(genres);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package io.vanslog.spring.data.meilisearch.repository;

import static org.assertj.core.api.AssertionsForClassTypes.*;
import static org.assertj.core.api.Assertions.*;

import io.vanslog.spring.data.meilisearch.entities.Movie;
import io.vanslog.spring.data.meilisearch.junit.jupiter.MeilisearchTest;
Expand All @@ -39,8 +39,8 @@
* @author Junghoon Ban
*/
@MeilisearchTest
@ContextConfiguration(classes = MeilisearchRepositoryIntegrationTest.Config.class)
class MeilisearchRepositoryIntegrationTest {
@ContextConfiguration(classes = MeilisearchRepositoryIntegrationTests.Config.class)
class MeilisearchRepositoryIntegrationTests {

@Autowired private MovieRepository movieRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package io.vanslog.spring.data.meilisearch.repository.support;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.assertj.core.api.Assertions.*;

import io.vanslog.spring.data.meilisearch.annotations.Document;
import io.vanslog.spring.data.meilisearch.core.mapping.SimpleMeilisearchMappingContext;
Expand All @@ -32,7 +31,7 @@
*
* @author Junghoon Ban
*/
class MeilisearchEntityInformationCreatorImplTest {
class MeilisearchEntityInformationCreatorImplUnitTests {

MeilisearchEntityInformationCreatorImpl entityInformationCreator;

Expand Down Expand Up @@ -60,7 +59,7 @@ void shouldThrowIllegalArgumentExceptionOnMissingIdAnnotation() {
}

@Document(indexUid = "entity-no-id")
class EntityNoId {
static class EntityNoId {

}
}

0 comments on commit a8b86b1

Please sign in to comment.